2009-04-20 10:27:56 -04:00
|
|
|
/*
|
2010-01-05 13:34:00 -05:00
|
|
|
* Copyright (C) 2009-2010 Julien BLACHE <jb@jblache.org>
|
2009-04-20 10:27:56 -04:00
|
|
|
*
|
|
|
|
* Rewritten from mt-daapd code:
|
|
|
|
* Copyright (C) 2003 Ron Pedde (ron@pedde.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
2010-01-09 07:42:23 -05:00
|
|
|
#include <sys/param.h>
|
2009-04-20 10:27:56 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2009-05-08 11:46:32 -04:00
|
|
|
#include "logger.h"
|
2009-06-07 12:58:02 -04:00
|
|
|
#include "db.h"
|
2009-04-20 11:34:39 -04:00
|
|
|
#include "filescanner.h"
|
2010-01-09 07:44:10 -05:00
|
|
|
#include "misc.h"
|
2009-04-20 10:27:56 -04:00
|
|
|
|
|
|
|
|
2013-10-16 05:09:24 -04:00
|
|
|
/* Get metadata from the EXTINF tag */
|
2013-10-15 07:36:11 -04:00
|
|
|
static int
|
2014-03-11 18:20:29 -04:00
|
|
|
extinf_get(char *string, struct media_file_info *mfi, int *extinf)
|
2013-10-15 07:36:11 -04:00
|
|
|
{
|
2013-10-16 05:09:24 -04:00
|
|
|
char *ptr;
|
2013-10-15 07:36:11 -04:00
|
|
|
|
2014-04-18 07:08:31 -04:00
|
|
|
if (strncmp(string, "#EXTINF:", strlen("#EXTINF:")) != 0)
|
2013-10-16 05:09:24 -04:00
|
|
|
return 0;
|
2013-10-15 07:36:11 -04:00
|
|
|
|
2013-10-16 05:09:24 -04:00
|
|
|
ptr = strchr(string, ',');
|
|
|
|
if (!ptr || strlen(ptr) < 2)
|
|
|
|
return 0;
|
2013-10-15 07:36:11 -04:00
|
|
|
|
2013-10-16 05:09:24 -04:00
|
|
|
/* New extinf found, so clear old data */
|
2014-04-18 07:08:31 -04:00
|
|
|
free_mfi(mfi, 1);
|
2013-10-15 07:36:11 -04:00
|
|
|
|
2014-03-11 18:20:29 -04:00
|
|
|
*extinf = 1;
|
|
|
|
mfi->artist = strdup(ptr + 1);
|
2013-10-15 07:36:11 -04:00
|
|
|
|
2014-03-11 18:20:29 -04:00
|
|
|
ptr = strstr(mfi->artist, " -");
|
2013-10-16 05:09:24 -04:00
|
|
|
if (ptr && strlen(ptr) > 3)
|
2014-03-11 18:20:29 -04:00
|
|
|
mfi->title = strdup(ptr + 3);
|
2013-10-16 05:09:24 -04:00
|
|
|
else
|
2014-03-11 18:20:29 -04:00
|
|
|
mfi->title = strdup("");
|
2013-10-16 05:09:24 -04:00
|
|
|
if (ptr)
|
|
|
|
*ptr = '\0';
|
2013-10-15 07:36:11 -04:00
|
|
|
|
2013-10-16 05:09:24 -04:00
|
|
|
return 1;
|
2013-10-15 07:36:11 -04:00
|
|
|
}
|
|
|
|
|
2009-04-20 11:34:39 -04:00
|
|
|
void
|
2013-08-14 17:40:55 -04:00
|
|
|
scan_m3u_playlist(char *file, time_t mtime)
|
2009-04-20 10:27:56 -04:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2014-03-11 18:20:29 -04:00
|
|
|
struct media_file_info mfi;
|
2009-04-20 10:27:56 -04:00
|
|
|
struct playlist_info *pli;
|
|
|
|
struct stat sb;
|
|
|
|
char buf[PATH_MAX];
|
|
|
|
char *entry;
|
|
|
|
char *filename;
|
|
|
|
char *ptr;
|
|
|
|
size_t len;
|
2014-03-11 18:20:29 -04:00
|
|
|
int extinf;
|
2009-04-20 10:27:56 -04:00
|
|
|
int pl_id;
|
2013-08-20 15:58:35 -04:00
|
|
|
int mfi_id;
|
2009-04-20 10:27:56 -04:00
|
|
|
int ret;
|
2013-08-20 15:58:35 -04:00
|
|
|
int i;
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2014-06-22 19:51:32 -04:00
|
|
|
DPRINTF(E_LOG, L_SCAN, "Processing static playlist: %s\n", file);
|
2009-04-20 10:27:56 -04:00
|
|
|
|
|
|
|
ret = stat(file, &sb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_SCAN, "Could not stat() '%s': %s\n", file, strerror(errno));
|
|
|
|
|
2009-04-20 11:34:39 -04:00
|
|
|
return;
|
2009-04-20 10:27:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
filename = strrchr(file, '/');
|
|
|
|
if (!filename)
|
|
|
|
filename = file;
|
|
|
|
else
|
|
|
|
filename++;
|
|
|
|
|
2009-06-07 12:58:02 -04:00
|
|
|
pli = db_pl_fetch_bypath(file);
|
2009-04-20 10:27:56 -04:00
|
|
|
|
|
|
|
if (pli)
|
|
|
|
{
|
2009-06-11 13:04:21 -04:00
|
|
|
DPRINTF(E_DBG, L_SCAN, "Playlist found, updating\n");
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2009-06-11 13:04:21 -04:00
|
|
|
pl_id = pli->id;
|
2009-06-07 12:58:02 -04:00
|
|
|
|
2009-06-11 13:04:21 -04:00
|
|
|
free_pli(pli, 0);
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2009-06-11 13:04:21 -04:00
|
|
|
db_pl_ping(pl_id);
|
|
|
|
db_pl_clear_items(pl_id);
|
2009-04-20 10:27:56 -04:00
|
|
|
}
|
2009-06-11 13:04:21 -04:00
|
|
|
else
|
|
|
|
pl_id = 0;
|
2009-04-20 10:27:56 -04:00
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
|
|
|
if (!fp)
|
|
|
|
{
|
2014-06-22 19:51:32 -04:00
|
|
|
DPRINTF(E_LOG, L_SCAN, "Could not open playlist '%s': %s\n", file, strerror(errno));
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2009-04-20 11:34:39 -04:00
|
|
|
return;
|
2009-04-20 10:27:56 -04:00
|
|
|
}
|
|
|
|
|
2009-06-11 13:04:21 -04:00
|
|
|
if (pl_id == 0)
|
|
|
|
{
|
|
|
|
/* Get only the basename, to be used as the playlist name */
|
|
|
|
ptr = strrchr(filename, '.');
|
|
|
|
if (ptr)
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
/* Safe: filename is a subset of file which is <= PATH_MAX already */
|
|
|
|
strncpy(buf, filename, sizeof(buf));
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2009-06-11 13:04:21 -04:00
|
|
|
/* Restore the full filename */
|
|
|
|
if (ptr)
|
|
|
|
*ptr = '.';
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2009-06-11 13:04:21 -04:00
|
|
|
ret = db_pl_add(buf, file, &pl_id);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_SCAN, "Error adding m3u playlist '%s'\n", file);
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2009-06-11 13:04:21 -04:00
|
|
|
return;
|
|
|
|
}
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2009-06-11 13:04:21 -04:00
|
|
|
DPRINTF(E_INFO, L_SCAN, "Added playlist as id %d\n", pl_id);
|
2009-04-20 10:27:56 -04:00
|
|
|
}
|
|
|
|
|
2014-03-11 18:20:29 -04:00
|
|
|
extinf = 0;
|
2014-04-18 07:08:31 -04:00
|
|
|
memset(&mfi, 0, sizeof(struct media_file_info));
|
2013-10-15 17:40:06 -04:00
|
|
|
|
2009-04-20 10:27:56 -04:00
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL)
|
|
|
|
{
|
|
|
|
len = strlen(buf);
|
2013-10-15 07:36:11 -04:00
|
|
|
if (len >= (sizeof(buf) - 1))
|
2009-04-20 10:27:56 -04:00
|
|
|
{
|
2013-10-15 07:36:11 -04:00
|
|
|
DPRINTF(E_LOG, L_SCAN, "Playlist entry exceeds PATH_MAX, discarding\n");
|
2009-04-20 10:27:56 -04:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-10-15 07:36:11 -04:00
|
|
|
/* rtrim and check that length is sane (ignore blank lines) */
|
|
|
|
while ((len > 0) && isspace(buf[len - 1]))
|
2009-04-20 10:27:56 -04:00
|
|
|
{
|
|
|
|
len--;
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
2013-10-15 07:36:11 -04:00
|
|
|
if (len < 1)
|
|
|
|
continue;
|
|
|
|
|
2014-04-18 07:08:31 -04:00
|
|
|
/* Saves metadata in mfi if EXTINF metadata line */
|
2014-03-11 18:20:29 -04:00
|
|
|
if (extinf_get(buf, &mfi, &extinf))
|
2013-10-15 17:40:06 -04:00
|
|
|
continue;
|
2013-10-15 07:36:11 -04:00
|
|
|
|
|
|
|
/* Check that first char is sane for a path */
|
|
|
|
if ((!isalnum(buf[0])) && (buf[0] != '/') && (buf[0] != '.'))
|
|
|
|
continue;
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2014-04-18 07:08:31 -04:00
|
|
|
/* Check if line is an URL, will be added to library */
|
2013-08-14 17:40:55 -04:00
|
|
|
if (strcmp(buf, "http://") > 0)
|
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_SCAN, "Playlist contains URL entry\n");
|
|
|
|
|
|
|
|
filename = strdup(buf);
|
2013-08-20 15:58:35 -04:00
|
|
|
if (!filename)
|
|
|
|
{
|
2013-10-15 17:40:06 -04:00
|
|
|
DPRINTF(E_LOG, L_SCAN, "Out of memory for playlist filename\n");
|
2013-08-14 17:40:55 -04:00
|
|
|
|
2013-08-20 15:58:35 -04:00
|
|
|
continue;
|
|
|
|
}
|
2013-08-14 17:40:55 -04:00
|
|
|
|
2014-03-11 18:20:29 -04:00
|
|
|
if (extinf)
|
|
|
|
DPRINTF(E_INFO, L_SCAN, "Playlist has EXTINF metadata, artist is '%s', title is '%s'\n", mfi.artist, mfi.title);
|
2013-10-15 17:40:06 -04:00
|
|
|
|
2014-03-11 18:20:29 -04:00
|
|
|
filescanner_process_media(filename, mtime, 0, F_SCAN_TYPE_URL, &mfi);
|
2009-04-20 10:27:56 -04:00
|
|
|
}
|
2014-04-18 07:08:31 -04:00
|
|
|
/* Regular file, should already be in library */
|
2009-04-20 10:27:56 -04:00
|
|
|
else
|
|
|
|
{
|
2013-10-15 07:36:11 -04:00
|
|
|
/* m3u might be from Windows so we change backslash to forward slash */
|
|
|
|
for (i = 0; i < strlen(buf); i++)
|
2009-04-20 10:27:56 -04:00
|
|
|
{
|
2013-08-20 15:58:35 -04:00
|
|
|
if (buf[i] == '\\')
|
|
|
|
buf[i] = '/';
|
2009-04-20 10:27:56 -04:00
|
|
|
}
|
|
|
|
|
2013-08-21 17:16:25 -04:00
|
|
|
/* Now search for the library item where the path has closest match to playlist item */
|
|
|
|
/* Succes is when we find an unambiguous match, or when we no longer can expand the */
|
|
|
|
/* the path to refine our search. */
|
|
|
|
entry = NULL;
|
|
|
|
do
|
2013-08-20 15:58:35 -04:00
|
|
|
{
|
2013-08-21 17:16:25 -04:00
|
|
|
ptr = strrchr(buf, '/');
|
|
|
|
if (entry)
|
|
|
|
*(entry - 1) = '/';
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
*ptr = '\0';
|
|
|
|
entry = ptr + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
entry = buf;
|
|
|
|
|
|
|
|
DPRINTF(E_SPAM, L_SCAN, "Playlist entry is now %s\n", entry);
|
2013-11-30 17:12:09 -05:00
|
|
|
ret = db_files_get_count_bymatch(entry);
|
2013-08-21 17:16:25 -04:00
|
|
|
|
|
|
|
} while (ptr && (ret > 1));
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2013-08-21 17:16:25 -04:00
|
|
|
if (ret > 0)
|
2013-08-20 15:58:35 -04:00
|
|
|
{
|
2013-11-30 17:12:09 -05:00
|
|
|
mfi_id = db_file_id_bymatch(entry);
|
2013-08-20 15:58:35 -04:00
|
|
|
DPRINTF(E_DBG, L_SCAN, "Found playlist entry match, id is %d, entry is %s\n", mfi_id, entry);
|
2013-08-21 17:16:25 -04:00
|
|
|
|
|
|
|
filename = db_file_path_byid(mfi_id);
|
2013-08-20 15:58:35 -04:00
|
|
|
if (!filename)
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_SCAN, "Playlist entry %s matches file id %d, but file path is missing.\n", entry, mfi_id);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-08-21 17:16:25 -04:00
|
|
|
{
|
|
|
|
DPRINTF(E_DBG, L_SCAN, "No match for playlist entry %s\n", entry);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-14 14:32:46 -04:00
|
|
|
}
|
2009-04-20 10:27:56 -04:00
|
|
|
|
2013-08-14 14:32:46 -04:00
|
|
|
ret = db_pl_add_item_bypath(pl_id, filename);
|
|
|
|
if (ret < 0)
|
|
|
|
DPRINTF(E_WARN, L_SCAN, "Could not add %s to playlist\n", filename);
|
2009-06-11 14:27:31 -04:00
|
|
|
|
2014-04-18 07:08:31 -04:00
|
|
|
/* Clean up in preparation for next item */
|
|
|
|
extinf = 0;
|
|
|
|
free_mfi(&mfi, 1);
|
2013-08-14 14:32:46 -04:00
|
|
|
free(filename);
|
2009-04-20 10:27:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!feof(fp))
|
|
|
|
{
|
|
|
|
DPRINTF(E_LOG, L_SCAN, "Error reading playlist '%s': %s\n", file, strerror(errno));
|
|
|
|
|
|
|
|
fclose(fp);
|
2009-04-20 11:34:39 -04:00
|
|
|
return;
|
2009-04-20 10:27:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
2009-05-08 11:52:56 -04:00
|
|
|
DPRINTF(E_INFO, L_SCAN, "Done processing playlist\n");
|
2009-04-20 10:27:56 -04:00
|
|
|
}
|