From 42be3989bf2b58a972680c506db1d08e0d34aa04 Mon Sep 17 00:00:00 2001 From: Justin Maggard Date: Tue, 10 Feb 2009 03:10:09 +0000 Subject: [PATCH] * Allow users to specify the file names and order that the Album Art scanner will search for. * Lots of changes for inotify support. --- Makefile | 2 +- albumart.c | 16 +- inotify.c | 532 +++++++++++++++++++++++++++++++++++++++++++++++ inotify.h | 3 + metadata.c | 29 +-- metadata.h | 2 +- minidlna.c | 101 ++++----- minidlna.conf | 10 +- minidlnatypes.h | 9 +- options.c | 4 +- options.h | 5 +- scanner.c | 259 +++++++++++++---------- scanner.h | 9 + upnpglobalvars.c | 17 +- upnpglobalvars.h | 10 +- upnpsoap.c | 16 +- 16 files changed, 800 insertions(+), 224 deletions(-) create mode 100644 inotify.c create mode 100644 inotify.h diff --git a/Makefile b/Makefile index d03ba45..8257dbe 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ BASEOBJS = minidlna.o upnphttp.o upnpdescgen.o upnpsoap.o \ upnpreplyparse.o minixml.o \ getifaddr.o daemonize.o upnpglobalvars.o \ options.o minissdp.o upnpevents.o \ - sql.o utils.o metadata.o albumart.o scanner.o + sql.o utils.o metadata.o albumart.o scanner.o inotify.o ALLOBJS = $(BASEOBJS) $(LNXOBJS) diff --git a/albumart.c b/albumart.c index e7bd5cb..3197387 100644 --- a/albumart.c +++ b/albumart.c @@ -174,25 +174,15 @@ char * check_for_album_file(char * dir, char * dlna_pn) { char * file = malloc(PATH_MAX); - char * album_art_names[] = - { - "AlbumArtSmall.jpg", "albumartsmall.jpg", - "Cover.jpg", "cover.jpg", - "AlbumArt.jpg", "albumart.jpg", - "Album.jpg", "album.jpg", - "Folder.jpg", "folder.jpg", - "Thumb.jpg", "thumb.jpg", - 0 - }; + struct album_art_name_s * album_art_name; struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; FILE *infile; int width=0, height=0; - int i; - for( i=0; album_art_names[i]; i++ ) + for( album_art_name = album_art_names; album_art_name; album_art_name = album_art_name->next ) { - sprintf(file, "%s/%s", dir, album_art_names[i]); + sprintf(file, "%s/%s", dir, album_art_name->name); if( access(file, R_OK) == 0 ) { infile = fopen(file, "r"); diff --git a/inotify.c b/inotify.c new file mode 100644 index 0000000..93d38f2 --- /dev/null +++ b/inotify.c @@ -0,0 +1,532 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "upnpglobalvars.h" +#include "utils.h" +#include "sql.h" +#include "scanner.h" + +#define EVENT_SIZE ( sizeof (struct inotify_event) ) +#define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) +#define DESIRED_WATCH_LIMIT 65536 + +#define PATH_BUF_SIZE PATH_MAX + +struct watch +{ + int wd; /* watch descriptor */ + char *path; /* watched path */ + struct watch *next; + struct watch *prev; +}; + +static struct watch *watches; +static struct watch *lastwatch = NULL; + +char *get_path_from_wd(int wd) +{ + struct watch *w = watches; + + while( w != NULL ) + { + if( w->wd == wd ) + return w->path; + w = w->next; + } + + return NULL; +} + +int +add_watch(int fd, const char * path) +{ + struct watch *nw; + int wd; + + wd = inotify_add_watch(fd, path, IN_CREATE|IN_CLOSE_WRITE|IN_DELETE); + if( wd < 0 ) + { + perror("inotify_add_watch()"); + return -1; + } + + nw = malloc(sizeof(struct watch)); + if( nw == NULL ) + { + perror("malloc()"); + return -1; + } + nw->wd = wd; + nw->prev = lastwatch; + nw->next = NULL; + nw->path = strdup(path); + + if( watches == NULL ) + { + watches = nw; + } + + if( lastwatch != NULL ) + { + lastwatch->next = nw; + } + lastwatch = nw; + + return wd; +} + +int +remove_watch(int fd, const char * path) +{ + struct watch *w; + + for( w = watches; w; w = w->next ) + { + if( strcmp(path, w->path) == 0 ) + return(inotify_rm_watch(fd, w->wd)); + } + + return 1; +} + +unsigned int +next_highest(unsigned int num) +{ + num |= num >> 1; + num |= num >> 2; + num |= num >> 4; + num |= num >> 8; + num |= num >> 16; + return(++num); +} + +int +inotify_create_watches(int fd) +{ + FILE * max_watches; + unsigned int num_watches = 0, watch_limit = 8192; + char **result; + int i, rows = 0; + + if( sql_get_table(db, "SELECT count(ID) from DETAILS where SIZE is NULL and PATH is not NULL", &result, &rows, NULL) == SQLITE_OK ) + { + if( rows ) + { + num_watches = strtoul(result[1], NULL, 10); + } + sqlite3_free_table(result); + } + + max_watches = fopen("/proc/sys/fs/inotify/max_user_watches", "r"); + if( max_watches ) + { + fscanf(max_watches, "%u", &watch_limit); + fclose(max_watches); + if( (watch_limit < DESIRED_WATCH_LIMIT) || (watch_limit < (num_watches*3/4)) ) + { + max_watches = fopen("/proc/sys/fs/inotify/max_user_watches", "w"); + if( max_watches ) + { + if( DESIRED_WATCH_LIMIT >= (num_watches*3/4) ) + { + fprintf(max_watches, "%u", DESIRED_WATCH_LIMIT); + } + else if( next_highest(num_watches) >= (num_watches*3/4) ) + { + fprintf(max_watches, "%u", next_highest(num_watches)); + } + else + { + fprintf(max_watches, "%u", next_highest(next_highest(num_watches))); + } + fclose(max_watches); + } + else + { + printf("WARNING: Inotify max_user_watches [%u] is low or close to the number of used watches [%u] " + "and I do not have permission to increase this limit. Please do so manually by " + "writing a higher value into /proc/sys/fs/inotify/max_user_watches.\n", watch_limit, num_watches); + } + } + } + else + { + printf("WARNING: Could not read inotify max_user_watches! " + "Hopefully it is enough to cover %u current directories plus any new ones added.\n", num_watches); + } + + sql_get_table(db, "SELECT PATH from DETAILS where SIZE is NULL and PATH is not NULL", &result, &rows, NULL); + for( i=1; i <= rows; i++ ) + { + //DEBUG printf("Add watch to %s\n", result[i]); + add_watch(fd, result[i]); + } + sqlite3_free_table(result); + + return rows; +} + +int +inotify_remove_watches(int fd) +{ + struct watch *w = watches; + int rm_watches = 0; + + while( w ) + { + inotify_rm_watch(fd, w->wd); + rm_watches++; + w = w->next; + } + + return rm_watches; +} + +int add_dir_watch(int fd, char * path, char * filename) +{ + DIR *ds; + struct dirent *e; + char *buf; + int wd; + int i = 0; + + if( filename ) + asprintf(&buf, "%s/%s", path, filename); + else + buf = strdup(path); + + wd = add_watch(fd, buf); + if( wd == -1 ) + { + perror("add_watch()"); + } + else + { + printf("Added watch to %s [%d]\n", buf, wd); + } + + ds = opendir(buf); + if( ds != NULL ) + { + while( (e = readdir(ds)) ) + { + if( strcmp(e->d_name, ".") == 0 || + strcmp(e->d_name, "..") == 0 ) + continue; + if( e->d_type == DT_DIR ) + i += add_dir_watch(fd, buf, e->d_name); + } + } + else + { + perror(strerror(errno)); + } + closedir(ds); + i++; + free(buf); + + return(i); +} + +int +inotify_insert_file(char * name, const char * path) +{ + char * sql; + char **result; + int rows; + char * last_dir = strdup(path); + char * path_buf = strdup(path); + char * base_name = malloc(strlen(path)); + char * base_copy = base_name; + char * parent_buf = NULL; + char * id; + int depth = 1; + + /* If it's already in the database, just skip it for now. + * TODO: compare modify timestamps */ + sql = sqlite3_mprintf("SELECT ID from DETAILS where PATH = '%q'", path); + if( sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK ) + { + if( rows ) + { + free(last_dir); + free(path_buf); + free(base_name); + sqlite3_free(sql); + sqlite3_free_table(result); + return -1; + } + sqlite3_free_table(result); + } + sqlite3_free(sql); + /* Find the parentID. If it's not found, create all necessary parents. */ + while( depth ) + { + depth = 0; + strcpy(path_buf, path); + parent_buf = dirname(path_buf); + strcpy(last_dir, path_buf); + + do + { + printf("Checking %s\n", parent_buf); + sql = sqlite3_mprintf("SELECT OBJECT_ID from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)" + " where d.PATH = '%q' and REF_ID is NULL", parent_buf); + if( (sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK) && rows ) + { + id = strdup(result[1]); + sqlite3_free_table(result); + sqlite3_free(sql); + if( !depth ) + break; + printf("Found first known parentID: %s\n", id); + /* Insert newly-found directory */ + strcpy(base_name, last_dir); + base_copy = basename(base_name); + insert_directory(base_copy, last_dir, BROWSEDIR_ID, id+2, get_next_available_id("OBJECTS", id)); + free(id); + break; + } + depth++; + strcpy(last_dir, path_buf); + parent_buf = dirname(parent_buf); + sqlite3_free_table(result); + sqlite3_free(sql); + } + while( strcmp(parent_buf, "/") != 0 ); + + if( strcmp(parent_buf, "/") == 0 ) + { + printf("No parents found!\n"); + break; + } + strcpy(path_buf, path); + } + free(last_dir); + free(path_buf); + free(base_name); + + if( !depth ) + { + insert_file(name, path, id+2, get_next_available_id("OBJECTS", id)); + free(id); + } + return depth; +} + +int +inotify_insert_directory(int fd, char *name, const char * path) +{ + DIR * ds; + struct dirent * e; + char * sql; + char **result; + char *id, *path_buf, *parent_buf, *esc_name; + int wd; + int rows, i = 0; + + parent_buf = dirname(strdup(path)); + sql = sqlite3_mprintf("SELECT OBJECT_ID from OBJECTS o left join DETAILS d on (d.ID = o.DETAIL_ID)" + " where d.PATH = '%q' and REF_ID is NULL", parent_buf); + if( sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK ) + { + if( rows ) + { + id = strdup(result[1]); + sqlite3_free_table(result); + insert_directory(name, path, BROWSEDIR_ID, id+2, get_next_available_id("OBJECTS", id)); + free(id); + } + else + { + sqlite3_free_table(result); + } + } + free(parent_buf); + sqlite3_free(sql); + + wd = add_watch(fd, path); + if( wd == -1 ) + { + perror("add_watch()"); + } + else + { + printf("Added watch to %s [%d]\n", path, wd); + } + + ds = opendir(path); + if( ds != NULL ) + { + while( (e = readdir(ds)) ) + { + if( strcmp(e->d_name, ".") == 0 || + strcmp(e->d_name, "..") == 0 ) + continue; + esc_name = modifyString(strdup(e->d_name), "&", "&amp;", 0); + asprintf(&path_buf, "%s/%s", path, e->d_name); + if( e->d_type == DT_DIR ) + { + inotify_insert_directory(fd, esc_name, path_buf); + } + else if( e->d_type == DT_REG ) + { + inotify_insert_file(esc_name, path_buf); + } + free(esc_name); + free(path_buf); + } + } + else + { + perror(strerror(errno)); + } + closedir(ds); + i++; + + return(i); +} + +int +inotify_remove_file(const char * path) +{ + char * sql; + char **result; + sqlite_int64 detailID = 0; + int rows, ret = 1; + + sql = sqlite3_mprintf("SELECT ID from DETAILS where PATH = '%q'", path); + if( (sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK) ) + { + if( rows ) + { + detailID = strtoll(result[1], NULL, 10); + ret = 0; + } + sqlite3_free_table(result); + } + sqlite3_free(sql); + if( detailID ) + { + asprintf(&sql, "DELETE from DETAILS where ID = %lld", detailID); + sql_exec(db, sql); + free(sql); + asprintf(&sql, "DELETE from OBJECTS where DETAIL_ID = %lld", detailID); + sql_exec(db, sql); + free(sql); + } + + return ret; +} + +int +inotify_remove_directory(int fd, const char * path) +{ + char * sql; + char * sql2; + char **result; + sqlite_int64 detailID = 0; + int rows, i, ret = 1; + + remove_watch(fd, path); + sql = sqlite3_mprintf("SELECT ID from DETAILS where PATH glob '%q/*'" + " UNION ALL SELECT ID from DETAILS where PATH = '%q'", path, path); + if( (sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK) ) + { + if( rows ) + { + for( i=1; i <= rows; i++ ) + { + detailID = strtoll(result[i], NULL, 10); + asprintf(&sql2, "DELETE from DETAILS where ID = %lld", detailID); + sql_exec(db, sql2); + free(sql2); + asprintf(&sql2, "DELETE from OBJECTS where DETAIL_ID = %lld", detailID); + sql_exec(db, sql2); + free(sql2); + } + ret = 0; + } + sqlite3_free_table(result); + } + sqlite3_free(sql); + /* Clean up any album art entries in the deleted directory */ + sql = sqlite3_mprintf("DELETE from ALBUM_ART where PATH glob '%q/*'", path); + sql_exec(db, sql); + sqlite3_free(sql); + + return ret; +} + +void * +start_inotify() +{ + int fd; + char buffer[BUF_LEN]; + int length, i = 0; + char * esc_name = NULL; + char * path_buf = NULL; + + fd = inotify_init(); + + if ( fd < 0 ) { + perror( "inotify_init" ); + } + + inotify_create_watches(fd); + while( 1 ) + { + length = read(fd, buffer, BUF_LEN); + + if ( length < 0 ) { + perror( "read" ); + } + + i = 0; + while( i < length ) + { + struct inotify_event * event = ( struct inotify_event * ) &buffer[ i ]; + if ( event->len ) + { + esc_name = modifyString(strdup(event->name), "&", "&amp;", 0); + asprintf(&path_buf, "%s/%s", get_path_from_wd(event->wd), event->name); + if ( event->mask & IN_CREATE && event->mask & IN_ISDIR ) + { + //DEBUG printf( "The directory %s was created.\n", path_buf ); + inotify_insert_directory(fd, esc_name, path_buf); + } + else if ( event->mask & IN_CLOSE_WRITE ) + { + //DEBUG printf( "The file %s was changed.\n", path_buf ); + inotify_insert_file(esc_name, path_buf); + } + else if ( event->mask & IN_DELETE ) + { + if ( event->mask & IN_ISDIR ) { + //DEBUG printf("The directory %s was deleted.\n", path_buf); + inotify_remove_directory(fd, path_buf); + } + else { + //DEBUG printf( "The file %s was deleted.\n", path_buf); + inotify_remove_file(path_buf); + } + } + free(esc_name); + free(path_buf); + } + i += EVENT_SIZE + event->len; + } + } + inotify_remove_watches(fd); + close(fd); + + exit( 0 ); +} diff --git a/inotify.h b/inotify.h new file mode 100644 index 0000000..c739439 --- /dev/null +++ b/inotify.h @@ -0,0 +1,3 @@ + +void * +start_inotify(); diff --git a/metadata.c b/metadata.c index e8a9406..1f516c1 100644 --- a/metadata.c +++ b/metadata.c @@ -100,16 +100,16 @@ get_fourcc(const char *s) } sqlite_int64 -GetFolderMetadata(const char * name, const char * artist, const char * genre, const char * album_art, const char * art_dlna_pn) +GetFolderMetadata(const char * name, const char * path, const char * artist, const char * genre, const char * album_art, const char * art_dlna_pn) { char * sql; int ret; sql = sqlite3_mprintf( "INSERT into DETAILS" - " (TITLE, CREATOR, ARTIST, GENRE, ALBUM_ART, ART_DLNA_PN) " + " (TITLE, PATH, CREATOR, ARTIST, GENRE, ALBUM_ART, ART_DLNA_PN) " "VALUES" - " ('%q', %Q, %Q, %Q, %lld, %Q);", - name, artist, artist, genre, + " ('%q', %Q, %Q, %Q, %Q, %lld, %Q);", + name, path, artist, artist, genre, album_art ? strtoll(album_art, NULL, 10) : 0, art_dlna_pn); @@ -134,7 +134,6 @@ GetAudioMetadata(const char * path, char * name) TagLib_Tag *tag; const TagLib_AudioProperties *properties; char *sql; - char *zErrMsg = NULL; char *title, *artist, *album, *genre, *comment; int free_flags = 0; sqlite_int64 album_art; @@ -285,11 +284,9 @@ GetAudioMetadata(const char * path, char * name) free(comment); //DEBUG printf("SQL: %s\n", sql); - if( sqlite3_exec(db, sql, 0, 0, &zErrMsg) != SQLITE_OK ) + if( sql_exec(db, sql) != SQLITE_OK ) { - fprintf(stderr, "Error inserting details for '%s'! [%s]\n", path, zErrMsg); - if (zErrMsg) - sqlite3_free(zErrMsg); + fprintf(stderr, "Error inserting details for '%s'!\n", path); ret = 0; } else @@ -327,7 +324,6 @@ GetImageMetadata(const char * path, char * name) struct stat file; sqlite_int64 ret; char *sql; - char *zErrMsg = NULL; metadata_t m; memset(&m, '\0', sizeof(metadata_t)); @@ -442,11 +438,9 @@ GetImageMetadata(const char * path, char * name) " (%Q, '%q', %llu, '%s', %Q, %d, '%q', %Q, %Q);", path, name, size, date, m.resolution, thumb, model, m.dlna_pn, m.mime); //DEBUG printf("SQL: %s\n", sql); - if( sqlite3_exec(db, sql, 0, 0, &zErrMsg) != SQLITE_OK ) + if( sql_exec(db, sql) != SQLITE_OK ) { - fprintf(stderr, "Error inserting details for '%s'! [%s]\n", path, zErrMsg); - if (zErrMsg) - sqlite3_free(zErrMsg); + fprintf(stderr, "Error inserting details for '%s'!\n", path); ret = 0; } else @@ -499,7 +493,6 @@ GetVideoMetadata(const char * path, char * name) off_t size = 0; struct stat file; char *sql; - char *zErrMsg = NULL; int ret, i; struct tm *modtime; char date[20]; @@ -876,11 +869,9 @@ GetVideoMetadata(const char * path, char * name) m.resolution, name, m.dlna_pn, m.mime); //DEBUG printf("SQL: %s\n", sql); - if( sqlite3_exec(db, sql, 0, 0, &zErrMsg) != SQLITE_OK ) + if( sql_exec(db, sql) != SQLITE_OK ) { - fprintf(stderr, "Error inserting details for '%s'! [%s]\n", path, zErrMsg); - if (zErrMsg) - sqlite3_free(zErrMsg); + fprintf(stderr, "Error inserting details for '%s'!\n", path); ret = 0; } else diff --git a/metadata.h b/metadata.h index 3d7bd81..e196f9e 100644 --- a/metadata.h +++ b/metadata.h @@ -44,7 +44,7 @@ char * modifyString(char * string, const char * before, const char * after, short like); sqlite_int64 -GetFolderMetadata(const char * name, const char * artist, const char * genre, const char * album_art, const char * art_dlna_pn); +GetFolderMetadata(const char * name, const char * path, const char * artist, const char * genre, const char * album_art, const char * art_dlna_pn); sqlite_int64 GetAudioMetadata(const char * path, char * name); diff --git a/minidlna.c b/minidlna.c index a77d769..2b34e12 100644 --- a/minidlna.c +++ b/minidlna.c @@ -24,19 +24,13 @@ #include #include #include -#if defined(sun) -#include -#else -/* for BSD's sysctl */ -#include -#endif - -#include +#include /* unix sockets */ #include "config.h" #include "upnpglobalvars.h" +#include "sql.h" #include "upnphttp.h" #include "upnpdescgen.h" #include "minidlnapath.h" @@ -48,6 +42,7 @@ #include "daemonize.h" #include "upnpevents.h" #include "scanner.h" +#include "inotify.h" #include "commonrdr.h" /* MAX_LAN_ADDR : maximum number of interfaces @@ -303,10 +298,6 @@ init(int argc, char * * argv) if(strcmp(ary_options[i].value, "yes") == 0) SETFLAG(SYSUPTIMEMASK); /*sysuptime = 1;*/ break; - case UPNPPACKET_LOG: - if(strcmp(ary_options[i].value, "yes") == 0) - SETFLAG(LOGPACKETSMASK); /*logpackets = 1;*/ - break; case UPNPSERIAL: strncpy(serialnumber, ary_options[i].value, SERIALNUMBER_MAX_LEN); serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0'; @@ -315,10 +306,6 @@ init(int argc, char * * argv) strncpy(modelnumber, ary_options[i].value, MODELNUMBER_MAX_LEN); modelnumber[MODELNUMBER_MAX_LEN-1] = '\0'; break; - case UPNPSECUREMODE: - if(strcmp(ary_options[i].value, "yes") == 0) - SETFLAG(SECUREMODEMASK); - break; case UPNPFRIENDLYNAME: strncpy(friendly_name, ary_options[i].value, FRIENDLYNAME_MAX_LEN); friendly_name[FRIENDLYNAME_MAX_LEN-1] = '\0'; @@ -373,6 +360,29 @@ init(int argc, char * * argv) break; } break; + case UPNPALBUMART_NAMES: + usleep(1); + char *string, *word; + for( string = ary_options[i].value; (word = strtok(string, "/")); string = NULL ) { + struct album_art_name_s * this_name = calloc(1, sizeof(struct album_art_name_s)); + this_name->name = strdup(word); + if( !album_art_names ) + { + album_art_names = this_name; + } + else + { + struct album_art_name_s * all_names = album_art_names; + while( all_names->next ) + all_names = all_names->next; + all_names->next = this_name; + } + } + break; + case UPNPINOTIFY: + if( (strcmp(ary_options[i].value, "yes") != 0) && !atoi(ary_options[i].value) ) + CLEARFLAG(INOTIFYMASK); + break; default: fprintf(stderr, "Unknown option in file %s\n", optionsfile); @@ -416,13 +426,6 @@ init(int argc, char * * argv) /*case 'l': logfilename = argv[++i]; break;*/ - case 'L': - /*logpackets = 1;*/ - SETFLAG(LOGPACKETSMASK); - break; - case 'S': - SETFLAG(SECUREMODEMASK); - break; case 'p': if(i+1 < argc) runtime_vars.port = atoi(argv[++i]); @@ -594,43 +597,26 @@ main(int argc, char * * argv) #endif struct timeval timeout, timeofday, lasttimeofday = {0, 0}; int max_fd = -1; + int last_changecnt = 0; + char * sql; + pthread_t thread; if(init(argc, argv) != 0) return 1; LIST_INIT(&upnphttphead); - if( access(DB_PATH, F_OK) ) + if( sqlite3_open(DB_PATH, &db) != SQLITE_OK ) { - struct media_dir_s * media_path = media_dirs; - sqlite3_open(DB_PATH, &db); - freopen("/dev/null", "a", stderr); - if( CreateDatabase() != 0 ) - { - fprintf(stderr, "Error creating database!\n"); - return -1; - } - #if USE_FORK - pid_t newpid = fork(); - if( newpid ) - goto fork_done; - #endif - while( media_path ) - { - ScanDirectory(media_path->path, NULL, media_path->type); - media_path = media_path->next; - } - freopen("/proc/self/fd/2", "a", stderr); - #if USE_FORK - _exit(0); - #endif + fprintf(stderr, "ERROR: Failed to open sqlite database! Exiting...\n"); + exit(-1); } else { char **result; int rows; - sqlite3_open(DB_PATH, &db); - if( sqlite3_get_table(db, "pragma user_version", &result, &rows, 0, 0) == SQLITE_OK ) + sqlite3_busy_timeout(db, 2000); + if( sql_get_table(db, "pragma user_version", &result, &rows, 0) == SQLITE_OK ) { if( atoi(result[1]) != DB_VERSION ) { struct media_dir_s * media_path = media_dirs; @@ -662,6 +648,11 @@ main(int argc, char * * argv) } sqlite3_free_table(result); } + if( GETFLAG(INOTIFYMASK) && pthread_create(&thread, NULL, start_inotify, NULL) ) + { + printf("ERROR: pthread_create() failed\n"); + exit(-1); + } } #if USE_FORK fork_done: @@ -697,6 +688,8 @@ main(int argc, char * * argv) { /* Check if we need to send SSDP NOTIFY messages and do it if * needed */ + /* Also check if we need to increment our SystemUpdateID + * at most once every 2 seconds */ if(gettimeofday(&timeofday, 0) < 0) { syslog(LOG_ERR, "gettimeofday(): %m"); @@ -730,6 +723,15 @@ main(int argc, char * * argv) timeout.tv_usec = lasttimeofday.tv_usec - timeofday.tv_usec; } } + if(timeofday.tv_sec >= (lasttimeofday.tv_sec + 2)) + { + if( sqlite3_total_changes(db) != last_changecnt ) + { + updateID++; + last_changecnt = sqlite3_total_changes(db); + upnp_event_var_change_notify(EContentDirectory); + } + } } /* select open sockets (SSDP, HTTP listen, and all HTTP soap sockets) */ @@ -868,6 +870,9 @@ shutdown: for(i=0; i>32); + last_date.objectID = (int)container; + strcpy(last_date.name, date_taken); + //DEBUG printf("Creating cached date item: %s/%s/%X\n", last_date.name, last_date.parentID, last_date.objectID); } - container = insert_container("DATES", date_taken, "3$12", NULL, "album.photoAlbum", NULL, NULL, NULL, NULL); - parentID = container>>32; - objectID = container; sql = sqlite3_mprintf( "INSERT into OBJECTS" " (OBJECT_ID, PARENT_ID, REF_ID, CLASS, DETAIL_ID, NAME) " "VALUES" - " ('3$12$%X$%X', '3$12$%X', '%s', '%s', %lu, %Q)", - parentID, objectID, parentID, refID, class, detailID, name); - sql_exec(db, sql); - sqlite3_free(sql); - } - if( cam && date ) - { - container = insert_container("CAMS", cam, "3$13", NULL, "storageFolder", NULL, NULL, NULL, NULL); - parentID = container>>32; - //objectID = container; - char parent[64]; - sprintf(parent, "3$13$%X", parentID); - long long int subcontainer = insert_container("CAMDATE", date_taken, parent, cam, "storageFolder", NULL, NULL, NULL, NULL); - int subParentID = subcontainer>>32; - int subObjectID = subcontainer; - sql = sqlite3_mprintf( "INSERT into OBJECTS" - " (OBJECT_ID, PARENT_ID, REF_ID, CLASS, DETAIL_ID, NAME) " - "VALUES" - " ('3$13$%X$%X$%X', '3$13$%X$%X', '%s', '%s', %lu, %Q)", - parentID, subParentID, subObjectID, parentID, subParentID, refID, class, detailID, name); + " ('%s$%X', '%s', '%s', '%s', %lu, %Q)", + last_date.parentID, last_date.objectID, last_date.parentID, refID, class, detailID, name); sql_exec(db, sql); sqlite3_free(sql); + + if( cam ) + { + if( strcmp(cam, last_cam.name) != 0 ) + { + container = insert_container(cam, "3$13", NULL, "storageFolder", NULL, NULL, NULL, NULL); + sprintf(last_cam.parentID, "3$13$%llX", container>>32); + strcpy(last_cam.name, cam); + last_camdate.name[0] = '\0'; + } + if( strcmp(last_camdate.name, date_taken) == 0 ) + { + last_camdate.objectID++; + //DEBUG printf("Using last camdate item: %s/%s/%s/%X\n", cam, last_camdate.name, last_camdate.parentID, last_camdate.objectID); + } + else + { + container = insert_container(date_taken, last_cam.parentID, NULL, "album.photoAlbum", NULL, NULL, NULL, NULL); + sprintf(last_camdate.parentID, "%s$%llX", last_cam.parentID, container>>32); + last_camdate.objectID = (int)container; + strcpy(last_camdate.name, date_taken); + //DEBUG printf("Creating cached camdate item: %s/%s/%s/%X\n", cam, last_camdate.name, last_camdate.parentID, last_camdate.objectID); + } + sql = sqlite3_mprintf( "INSERT into OBJECTS" + " (OBJECT_ID, PARENT_ID, REF_ID, CLASS, DETAIL_ID, NAME) " + "VALUES" + " ('%s$%X', '%s', '%s', '%s', %lu, %Q)", + last_camdate.parentID, last_camdate.objectID, last_camdate.parentID, refID, class, detailID, name); + sql_exec(db, sql); + sqlite3_free(sql); + } } /* All Images */ + if( !last_all_objectID ) + { + last_all_objectID = get_next_available_id("OBJECTS", "1$4"); + } sql = sqlite3_mprintf( "INSERT into OBJECTS" " (OBJECT_ID, PARENT_ID, REF_ID, CLASS, DETAIL_ID, NAME) " "VALUES" - " ('3$11$%X', '3$11', '%s', '%s', %lu, %Q)", + " ('3$11$%llX', '3$11', '%s', '%s', %lu, %Q)", last_all_objectID++, refID, class, detailID, name); sql_exec(db, sql); sqlite3_free(sql); @@ -189,104 +221,114 @@ insert_containers(const char * name, const char *path, const char * refID, const { char *artist = cols ? result[7+cols]:NULL, *album = cols ? result[8+cols]:NULL, *genre = cols ? result[9+cols]:NULL; char *album_art = cols ? result[19+cols]:NULL, *art_dlna_pn = cols ? result[20+cols]:NULL; - static char last_artist[1024] = "0"; - static int last_artist_parentID, last_artist_objectID; - static char last_album[1024]; - static int last_album_parentID, last_album_objectID; - static char last_genre[1024]; - static int last_genre_parentID, last_genre_objectID; - static int last_all_objectID = 0; + static struct virtual_item last_album; + static struct virtual_item last_artist; + static struct virtual_item last_artistalbum; + static struct virtual_item last_genre; + static sqlite_int64 last_all_objectID = 0; - if( artist ) + if( album ) { - if( strcmp(artist, last_artist) == 0 ) + if( strcmp(album, last_album.name) == 0 ) { - objectID = ++last_artist_objectID; - parentID = last_artist_parentID; + last_album.objectID++; + //DEBUG printf("Using last album item: %s/%s/%X\n", last_album.name, last_album.parentID, last_album.objectID); } else { - strcpy(last_artist, artist); - container = insert_container("ARTISTS", artist, "1$6", NULL, "person.musicArtist", NULL, genre, NULL, NULL); - parentID = container>>32; - objectID = container; - last_artist_objectID = objectID; - last_artist_parentID = parentID; + strcpy(last_album.name, album); + container = insert_container(album, "1$7", NULL, "album.musicAlbum", artist, genre, album_art, art_dlna_pn); + sprintf(last_album.parentID, "1$7$%llX", container>>32); + last_album.objectID = (int)container; + //DEBUG printf("Creating cached album item: %s/%s/%X\n", last_album.name, last_album.parentID, last_album.objectID); } sql = sqlite3_mprintf( "INSERT into OBJECTS" " (OBJECT_ID, PARENT_ID, REF_ID, CLASS, DETAIL_ID, NAME) " "VALUES" - " ('1$6$%X$%X', '1$6$%X', '%s', '%s', %lu, %Q)", - parentID, objectID, parentID, refID, class, detailID, name); + " ('%s$%X', '%s', '%s', '%s', %lu, %Q)", + last_album.parentID, last_album.objectID, last_album.parentID, refID, class, detailID, name); sql_exec(db, sql); sqlite3_free(sql); } - if( album ) + if( artist ) { - if( strcmp(album, last_album) == 0 ) + if( strcmp(artist, last_artist.name) != 0 ) { - objectID = ++last_album_objectID; - parentID = last_album_parentID; + container = insert_container(artist, "1$6", NULL, "person.musicArtist", NULL, genre, NULL, NULL); + sprintf(last_artist.parentID, "1$6$%llX", container>>32); + strcpy(last_artist.name, artist); + last_artistalbum.name[0] = '\0'; + } + if( strcmp(album?album:"Unknown", last_artistalbum.name) == 0 ) + { + last_artistalbum.objectID++; + //DEBUG printf("Using last artist/album item: %s/%s/%X\n", last_artist.name, last_artist.parentID, last_artist.objectID); } else { - strcpy(last_album, album); - container = insert_container("ALBUMS", album, "1$7", NULL, "album.musicAlbum", artist, genre, album_art, art_dlna_pn); - parentID = container>>32; - objectID = container; - last_album_objectID = objectID; - last_album_parentID = parentID; + container = insert_container(album?album:"Unknown", last_artist.parentID, album?last_album.parentID:NULL, "album.musicAlbum", artist, genre, album_art, art_dlna_pn); + sprintf(last_artistalbum.parentID, "%s$%llX", last_artist.parentID, container>>32); + last_artistalbum.objectID = (int)container; + strcpy(last_artistalbum.name, album?album:"Unknown"); + //DEBUG printf("Creating cached artist/album item: %s/%s/%X\n", last_artist.name, last_artist.parentID, last_artist.objectID); } sql = sqlite3_mprintf( "INSERT into OBJECTS" " (OBJECT_ID, PARENT_ID, REF_ID, CLASS, DETAIL_ID, NAME) " "VALUES" - " ('1$7$%X$%X', '1$7$%X', '%s', '%s', %lu, %Q)", - parentID, objectID, parentID, refID, class, detailID, name); + " ('%s$%X', '%s', '%s', '%s', %lu, %Q)", + last_artistalbum.parentID, last_artistalbum.objectID, last_artistalbum.parentID, refID, class, detailID, name); sql_exec(db, sql); sqlite3_free(sql); } if( genre ) { - if( strcmp(genre, last_genre) == 0 ) + if( strcmp(genre, last_genre.name) == 0 ) { - objectID = ++last_genre_objectID; - parentID = last_genre_parentID; + last_genre.objectID++; + //DEBUG printf("Using last genre item: %s/%s/%X\n", last_genre.name, last_genre.parentID, last_genre.objectID); } else { - strcpy(last_genre, genre); - container = insert_container("GENRES", genre, "1$5", NULL, "genre.musicGenre", NULL, NULL, NULL, NULL); - parentID = container>>32; - objectID = container; - last_genre_objectID = objectID; - last_genre_parentID = parentID; + strcpy(last_genre.name, genre); + container = insert_container(genre, "1$5", NULL, "genre.musicGenre", NULL, NULL, NULL, NULL); + sprintf(last_genre.parentID, "1$5$%llX", container>>32); + last_genre.objectID = (int)container; + //DEBUG printf("Creating cached genre item: %s/%s/%X\n", last_genre.name, last_genre.parentID, last_genre.objectID); } sql = sqlite3_mprintf( "INSERT into OBJECTS" " (OBJECT_ID, PARENT_ID, REF_ID, CLASS, DETAIL_ID, NAME) " "VALUES" - " ('1$5$%X$%X', '1$5$%X', '%s', '%s', %lu, %Q)", - parentID, objectID, parentID, refID, class, detailID, name); + " ('%s$%X', '%s', '%s', '%s', %lu, %Q)", + last_genre.parentID, last_genre.objectID, last_genre.parentID, refID, class, detailID, name); sql_exec(db, sql); sqlite3_free(sql); } /* All Music */ + if( !last_all_objectID ) + { + last_all_objectID = get_next_available_id("OBJECTS", "1$4"); + } sql = sqlite3_mprintf( "INSERT into OBJECTS" " (OBJECT_ID, PARENT_ID, REF_ID, CLASS, DETAIL_ID, NAME) " "VALUES" - " ('1$4$%X', '1$4', '%s', '%s', %lu, %Q)", + " ('1$4$%llX', '1$4', '%s', '%s', %lu, %Q)", last_all_objectID++, refID, class, detailID, name); sql_exec(db, sql); sqlite3_free(sql); } else if( strstr(class, "videoItem") ) { - static int last_all_objectID = 0; + static sqlite_int64 last_all_objectID = 0; - /* All Music */ + /* All Videos */ + if( !last_all_objectID ) + { + last_all_objectID = get_next_available_id("OBJECTS", "1$4"); + } sql = sqlite3_mprintf( "INSERT into OBJECTS" " (OBJECT_ID, PARENT_ID, REF_ID, CLASS, DETAIL_ID, NAME) " "VALUES" - " ('2$8$%X', '2$8', '%s', '%s', %lu, %Q)", + " ('2$8$%llX', '2$8', '%s', '%s', %lu, %Q)", last_all_objectID++, refID, class, detailID, name); sql_exec(db, sql); sqlite3_free(sql); @@ -298,7 +340,7 @@ int insert_directory(const char * name, const char * path, const char * base, const char * parentID, int objectID) { char * sql; - int ret, found = 0; + int ret, rows, found = 0; sqlite_int64 detailID; char * refID = NULL; char class[] = "container.storageFolder"; @@ -333,7 +375,7 @@ insert_directory(const char * name, const char * path, const char * base, const sqlite3_free(sql); /* Does not exist. Need to create, and may need to create parents also */ sql = sqlite3_mprintf("SELECT DETAIL_ID from OBJECTS where OBJECT_ID = '%s'", refID); - if( (sql_get_table(db, sql, &result, NULL, NULL) == SQLITE_OK) && atoi(result[1]) ) + if( (sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK) && rows ) { detailID = atoi(result[1]); } @@ -361,7 +403,7 @@ insert_directory(const char * name, const char * path, const char * base, const return 1; } - detailID = GetFolderMetadata(name, NULL, NULL, NULL, NULL); + detailID = GetFolderMetadata(name, path, NULL, NULL, NULL, NULL); sql = sqlite3_mprintf( "INSERT into OBJECTS" " (OBJECT_ID, PARENT_ID, REF_ID, DETAIL_ID, CLASS, NAME) " "VALUES" @@ -475,6 +517,7 @@ CreateDatabase(void) 0 }; sql_exec(db, "pragma temp_store = MEMORY"); + sql_exec(db, "pragma journal_mode = OFF"); sql_exec(db, "pragma synchronous = OFF;"); sql_exec(db, "pragma cache_size = 8192;"); @@ -523,6 +566,14 @@ CreateDatabase(void) "PATH TEXT NOT NULL, " "EMBEDDED BOOL DEFAULT 0" ");"); + if( ret != SQLITE_OK ) + goto sql_failed; + ret = sql_exec(db, "CREATE TABLE SETTINGS (" + "UPDATE_ID INTEGER PRIMARY KEY" + ");"); + if( ret != SQLITE_OK ) + goto sql_failed; + ret = sql_exec(db, "INSERT into SETTINGS values (0)"); if( ret != SQLITE_OK ) goto sql_failed; for( i=0; containers[i]; i=i+3 ) @@ -530,18 +581,11 @@ CreateDatabase(void) sprintf(sql_buf, "INSERT into OBJECTS (OBJECT_ID, PARENT_ID, DETAIL_ID, CLASS, NAME)" " values " "('%s', '%s', %lld, 'container.storageFolder', '%s')", - containers[i], containers[i+1], GetFolderMetadata(containers[i+2], NULL, NULL, NULL, NULL), containers[i+2]); + containers[i], containers[i+1], GetFolderMetadata(containers[i+2], NULL, NULL, NULL, NULL, NULL), containers[i+2]); ret = sql_exec(db, sql_buf); if( ret != SQLITE_OK ) goto sql_failed; } - sql_exec(db, "create TEMP TABLE ARTISTS (ITEM TEXT, OBJECT_ID TEXT, SUBITEM TEXT DEFAULT NULL);"); - sql_exec(db, "create TEMP TABLE ALBUMS (ITEM TEXT, OBJECT_ID TEXT, SUBITEM TEXT DEFAULT NULL);"); - sql_exec(db, "create TEMP TABLE GENRES (ITEM TEXT, OBJECT_ID TEXT, SUBITEM TEXT DEFAULT NULL);"); - sql_exec(db, "create TEMP TABLE DATES (ITEM TEXT, OBJECT_ID TEXT, SUBITEM TEXT DEFAULT NULL);"); - sql_exec(db, "create TEMP TABLE CAMS (ITEM TEXT, OBJECT_ID TEXT, SUBITEM TEXT DEFAULT NULL);"); - sql_exec(db, "create TEMP TABLE CAMDATE (ITEM TEXT, OBJECT_ID TEXT, SUBITEM TEXT DEFAULT NULL);"); - sql_exec(db, "create INDEX IDX_OBJECTS_OBJECT_ID ON OBJECTS(OBJECT_ID);"); sql_exec(db, "create INDEX IDX_OBJECTS_PARENT_ID ON OBJECTS(PARENT_ID);"); sql_exec(db, "create INDEX IDX_OBJECTS_DETAIL_ID ON OBJECTS(DETAIL_ID);"); @@ -636,13 +680,6 @@ ScanDirectory(const char * dir, const char * parent, enum media_types type) return; } -/* sql = sqlite3_mprintf("SELECT OBJECT_ID, max(ID) from OBJECTS where PARENT_ID = '%s$%X'", rootParent, parentID); - ret = sql_get_table(db, sql, &result, 0, &cols); - if( result[2] && (sscanf(rindex(result[2], '$')+1, "%X", &objectID) == 1) ) - { - objectID++; - } -*/ if( !parent ) startID = get_next_available_id("OBJECTS", BROWSEDIR_ID); diff --git a/scanner.h b/scanner.h index 77f3dcb..c4ca4e4 100644 --- a/scanner.h +++ b/scanner.h @@ -15,6 +15,15 @@ #define VIDEO_DIR_ID "2$21" #define IMAGE_DIR_ID "3$22" +sqlite_int64 +get_next_available_id(const char * table, const char * parentID); + +int +insert_directory(const char * name, const char * path, const char * base, const char * parentID, int objectID); + +int +insert_file(char * name, const char * path, const char * parentID, int object); + int CreateDatabase(void); diff --git a/upnpglobalvars.c b/upnpglobalvars.c index 32053c1..1288770 100644 --- a/upnpglobalvars.c +++ b/upnpglobalvars.c @@ -17,17 +17,8 @@ /* startup time */ time_t startup_time = 0; -#if 0 -/* use system uptime */ -int sysuptime = 0; - -/* log packets flag */ -int logpackets = 0; - -#endif - struct runtime_vars_s runtime_vars; -int runtime_flags = 0; +int runtime_flags = INOTIFYMASK; const char * pidfilename = "/var/run/minidlna.pid"; @@ -44,6 +35,8 @@ int n_lan_addr = 0; struct lan_addr_s lan_addr[MAX_LAN_ADDR]; /* UPnP-A/V [DLNA] */ -sqlite3 *db; -struct media_dir_s * media_dirs = NULL; +sqlite3 * db; char friendly_name[FRIENDLYNAME_MAX_LEN]; +struct media_dir_s * media_dirs = NULL; +struct album_art_name_s * album_art_names = NULL; +__u32 updateID = 0; diff --git a/upnpglobalvars.h b/upnpglobalvars.h index c41dc4d..4c9e32f 100644 --- a/upnpglobalvars.h +++ b/upnpglobalvars.h @@ -8,6 +8,8 @@ #define __UPNPGLOBALVARS_H__ #include +#include // Defines __u32 + #include "minidlnatypes.h" #include "config.h" @@ -50,10 +52,9 @@ extern time_t startup_time; extern struct runtime_vars_s runtime_vars; /* runtime boolean flags */ extern int runtime_flags; -#define LOGPACKETSMASK 0x0001 +#define INOTIFYMASK 0x0001 #define SYSUPTIMEMASK 0x0002 #define CHECKCLIENTIPMASK 0x0008 -#define SECUREMODEMASK 0x0010 #define SETFLAG(mask) runtime_flags |= mask #define GETFLAG(mask) runtime_flags & mask @@ -81,9 +82,10 @@ extern struct lan_addr_s lan_addr[]; /* UPnP-A/V [DLNA] */ extern sqlite3 *db; -#define MEDIADIR_MAX_LEN (256) -extern struct media_dir_s * media_dirs; #define FRIENDLYNAME_MAX_LEN (64) extern char friendly_name[]; +extern struct media_dir_s * media_dirs; +extern struct album_art_name_s * album_art_names; +extern __u32 updateID; #endif diff --git a/upnpsoap.c b/upnpsoap.c index c6ceb8c..4dea89b 100644 --- a/upnpsoap.c +++ b/upnpsoap.c @@ -78,7 +78,7 @@ GetSystemUpdateID(struct upnphttp * h, const char * action) bodylen = snprintf(body, sizeof(body), resp, action, "urn:schemas-upnp-org:service:ContentDirectory:1", - 1, action); + updateID, action); BuildSendAndCloseSoapResp(h, body, bodylen); } @@ -397,7 +397,7 @@ BrowseContentDirectory(struct upnphttp * h, const char * action) "<DIDL-Lite" CONTENT_DIRECTORY_SCHEMAS; static const char resp1[] = "</DIDL-Lite>"; - static const char resp2[] = "0"; + static const char resp2[] = ""; char *resp = calloc(1, 1048576); char str_buf[4096]; @@ -466,7 +466,10 @@ BrowseContentDirectory(struct upnphttp * h, const char * action) sqlite3_free(zErrMsg); } strcat(resp, resp1); - sprintf(str_buf, "\n%u\n%u\n", args.returned, args.total); + sprintf(str_buf, "\n%u\n" + "%u\n" + "%u", + args.returned, args.total, updateID); strcat(resp, str_buf); strcat(resp, resp2); BuildSendAndCloseSoapResp(h, resp, strlen(resp)); @@ -484,7 +487,7 @@ SearchContentDirectory(struct upnphttp * h, const char * action) "<DIDL-Lite" CONTENT_DIRECTORY_SCHEMAS; static const char resp1[] = "</DIDL-Lite>"; - static const char resp2[] = "0"; + static const char resp2[] = ""; char *resp = calloc(1, 1048576); char *zErrMsg = 0; @@ -581,7 +584,10 @@ SearchContentDirectory(struct upnphttp * h, const char * action) } sqlite3_free(sql); strcat(resp, resp1); - sprintf(str_buf, "\n%u\n%u\n", args.returned, args.total); + sprintf(str_buf, "\n%u\n" + "%u\n" + "%u", + args.returned, args.total, updateID); strcat(resp, str_buf); strcat(resp, resp2); BuildSendAndCloseSoapResp(h, resp, strlen(resp));