1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/* MiniDLNA media server
* Copyright (C) 2009-2010 Justin Maggard
*
* This file is part of MiniDLNA.
*
* MiniDLNA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* MiniDLNA 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 MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <libgen.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "tagutils/tagutils.h"
#include "upnpglobalvars.h"
#include "scanner.h"
#include "metadata.h"
#include "utils.h"
#include "sql.h"
#include "log.h"
int
insert_playlist(const char * path, char * name)
{
struct song_metadata plist;
struct stat file;
int items = 0, matches, ret;
char type[4];
strncpy(type, strrchr(name, '.')+1, 4);
if( start_plist(path, NULL, &file, NULL, type) != 0 )
{
DPRINTF(E_WARN, L_SCANNER, "Bad playlist [%s]\n", path);
return -1;
}
while( (ret = next_plist_track(&plist, &file, NULL, type)) == 0 )
{
items++;
freetags(&plist);
}
if( ret == 2 ) // Bad playlist -- contains binary characters
{
DPRINTF(E_WARN, L_SCANNER, "Bad playlist [%s]\n", path);
return -1;
}
strip_ext(name);
DPRINTF(E_DEBUG, L_SCANNER, "Playlist %s contains %d items\n", name, items);
matches = sql_get_int_field(db, "SELECT count(*) from PLAYLISTS where NAME = '%q'", name);
if( matches > 0 )
{
sql_exec(db, "INSERT into PLAYLISTS"
" (NAME, PATH, ITEMS) "
"VALUES"
" ('%q(%d)', '%q', %d)",
name, matches, path, items);
}
else
{
sql_exec(db, "INSERT into PLAYLISTS"
" (NAME, PATH, ITEMS) "
"VALUES"
" ('%q', '%q', %d)",
name, path, items);
}
return 0;
}
static unsigned int
gen_dir_hash(const char *path)
{
char dir[PATH_MAX], *base;
int len;
strncpy(dir, path, sizeof(dir));
dir[sizeof(dir)-1] = '\0';
base = strrchr(dir, '/');
if( !base )
base = strrchr(dir, '\\');
if( base )
{
*base = '\0';
len = base - dir;
}
else
return 0;
return DJBHash(dir, len);
}
int
fill_playlists()
{
int rows, i, found, len;
char **result;
char *plpath, *plname, *fname, *last_dir;
unsigned int hash, last_hash = 0;
char class[] = "playlistContainer";
struct song_metadata plist;
struct stat file;
char type[4];
int64_t plID, detailID;
char sql_buf[] = "SELECT ID, NAME, PATH from PLAYLISTS where ITEMS > FOUND";
DPRINTF(E_WARN, L_SCANNER, "Parsing playlists...\n");
if( sql_get_table(db, sql_buf, &result, &rows, NULL) != SQLITE_OK )
return -1;
if( !rows )
goto done;
rows++;
for( i=3; i<rows*3; i++ )
{
plID = strtoll(result[i], NULL, 10);
plname = result[++i];
plpath = result[++i];
last_dir = NULL;
last_hash = 0;
strncpy(type, strrchr(plpath, '.')+1, 4);
if( start_plist(plpath, NULL, &file, NULL, type) != 0 )
continue;
DPRINTF(E_DEBUG, L_SCANNER, "Scanning playlist \"%s\" [%s]\n", plname, plpath);
if( sql_get_int_field(db, "SELECT ID from OBJECTS where PARENT_ID = '"MUSIC_PLIST_ID"'"
" and NAME = '%q'", plname) <= 0 )
{
detailID = GetFolderMetadata(plname, NULL, NULL, NULL, 0);
sql_exec(db, "INSERT into OBJECTS"
" (OBJECT_ID, PARENT_ID, DETAIL_ID, CLASS, NAME) "
"VALUES"
" ('%s$%llX', '%s', %lld, 'container.%s', '%q')",
MUSIC_PLIST_ID, plID, MUSIC_PLIST_ID, detailID, class, plname);
}
plpath = dirname(plpath);
found = 0;
while( next_plist_track(&plist, &file, NULL, type) == 0 )
{
hash = gen_dir_hash(plist.path);
if( sql_get_int_field(db, "SELECT 1 from OBJECTS where OBJECT_ID = '%s$%llX$%d'",
MUSIC_PLIST_ID, plID, plist.track) == 1 )
{
//DEBUG DPRINTF(E_DEBUG, L_SCANNER, "%d: already in database\n", plist.track);
found++;
freetags(&plist);
continue;
}
if( last_dir )
{
if( hash == last_hash )
{
fname = basename(plist.path);
detailID = sql_get_int_field(db, "SELECT ID from DETAILS where PATH = '%q/%q'", last_dir, fname);
}
else
detailID = -1;
if( detailID <= 0 )
{
sqlite3_free(last_dir);
last_dir = NULL;
}
else
goto found;
}
fname = plist.path;
DPRINTF(E_DEBUG, L_SCANNER, "%d: checking database for %s\n", plist.track, plist.path);
if( !strpbrk(fname, "\\/") )
{
len = strlen(fname) + strlen(plpath) + 2;
plist.path = malloc(len);
snprintf(plist.path, len, "%s/%s", plpath, fname);
free(fname);
fname = plist.path;
}
else
{
while( *fname == '\\' )
{
fname++;
}
}
retry:
//DEBUG DPRINTF(E_DEBUG, L_SCANNER, "* Searching for %s in db\n", fname);
detailID = sql_get_int_field(db, "SELECT ID from DETAILS where PATH like '%%%q'", fname);
if( detailID > 0 )
{
found:
DPRINTF(E_DEBUG, L_SCANNER, "+ %s found in db\n", fname);
sql_exec(db, "INSERT into OBJECTS"
" (OBJECT_ID, PARENT_ID, CLASS, DETAIL_ID, NAME, REF_ID) "
"SELECT"
" '%s$%llX$%d', '%s$%llX', CLASS, DETAIL_ID, NAME, OBJECT_ID from OBJECTS"
" where DETAIL_ID = %lld and OBJECT_ID glob '" BROWSEDIR_ID "$*'",
MUSIC_PLIST_ID, plID, plist.track,
MUSIC_PLIST_ID, plID,
detailID);
if( !last_dir )
{
last_dir = sql_get_text_field(db, "SELECT PATH from DETAILS where ID = %lld", detailID);
fname = strrchr(last_dir, '/');
if( fname )
*fname = '\0';
last_hash = hash;
}
found++;
}
else
{
DPRINTF(E_DEBUG, L_SCANNER, "- %s not found in db\n", fname);
if( strchr(fname, '\\') )
{
fname = modifyString(fname, "\\", "/");
goto retry;
}
else if( (fname = strchr(fname, '/')) )
{
fname++;
goto retry;
}
}
freetags(&plist);
}
if( last_dir )
{
sqlite3_free(last_dir);
last_dir = NULL;
}
sql_exec(db, "UPDATE PLAYLISTS set FOUND = %d where ID = %lld", found, plID);
}
done:
sqlite3_free_table(result);
DPRINTF(E_WARN, L_SCANNER, "Finished parsing playlists.\n");
return 0;
}
|