From 14547e226636f9bbc47964be415b651dd5ea6b76 Mon Sep 17 00:00:00 2001 From: Justin Maggard Date: Tue, 17 Feb 2009 08:25:15 +0000 Subject: [PATCH] * Fix inotify detection in the root media directory. * Hold off creating inotify watches until the initial file scan is complete. * Use a thread instead of fork when scanning. --- inotify.c | 15 ++++++++++++++- metadata.c | 2 ++ minidlna.c | 26 ++++++-------------------- scanner.c | 18 ++++++++++++++++++ scanner.h | 4 ++-- upnpglobalvars.c | 1 + upnpglobalvars.h | 1 + 7 files changed, 44 insertions(+), 23 deletions(-) diff --git a/inotify.c b/inotify.c index 41c26ab..d2098e3 100644 --- a/inotify.c +++ b/inotify.c @@ -120,6 +120,7 @@ inotify_create_watches(int fd) unsigned int num_watches = 0, watch_limit = 8192; char **result; int i, rows = 0; + struct media_dir_s * media_path; if( sql_get_table(db, "SELECT count(ID) from DETAILS where SIZE is NULL and PATH is not NULL", &result, &rows, NULL) == SQLITE_OK ) { @@ -168,6 +169,12 @@ inotify_create_watches(int fd) "Hopefully it is enough to cover %u current directories plus any new ones added.\n", num_watches); } + media_path = media_dirs; + while( media_path ) + { + add_watch(fd, media_path->path); + media_path = media_path->next; + } 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++ ) { @@ -310,7 +317,8 @@ inotify_insert_file(char * name, const char * path) if( strcmp(parent_buf, "/") == 0 ) { - printf("No parents found!\n"); + id = strdup(""); + depth = 0; break; } strcpy(path_buf, path); @@ -353,6 +361,7 @@ inotify_insert_directory(int fd, char *name, const char * path) else { sqlite3_free_table(result); + insert_directory(name, path, BROWSEDIR_ID, "", get_next_available_id("OBJECTS", id)); } } free(parent_buf); @@ -493,6 +502,10 @@ start_inotify() perror( "inotify_init" ); } + while( scanning ) + { + sleep(1); + } inotify_create_watches(fd); while( 1 ) { diff --git a/metadata.c b/metadata.c index 1f516c1..8cd9f6f 100644 --- a/metadata.c +++ b/metadata.c @@ -153,6 +153,8 @@ GetAudioMetadata(const char * path, char * name) tag = taglib_file_tag(audio_file); properties = taglib_file_audioproperties(audio_file); + if( !properties ) + return 0; seconds = taglib_audioproperties_length(properties) % 60; minutes = (taglib_audioproperties_length(properties) - seconds) / 60; diff --git a/minidlna.c b/minidlna.c index 4ce84c4..7fc929e 100644 --- a/minidlna.c +++ b/minidlna.c @@ -600,7 +600,7 @@ main(int argc, char * * argv) int max_fd = -1; int last_changecnt = 0; char * sql; - pthread_t thread; + pthread_t thread[2]; if(init(argc, argv) != 0) return 1; @@ -622,48 +622,34 @@ main(int argc, char * * argv) { char **result; int rows; - sqlite3_busy_timeout(db, 2000); + sqlite3_busy_timeout(db, 2500); 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; printf("Database version mismatch; need to recreate...\n"); sqlite3_close(db); unlink(DB_PATH "/files.db"); system("rm -rf " DB_PATH "/art_cache"); sqlite3_open(DB_PATH "/files.db", &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 ) + if( pthread_create(&thread[0], NULL, start_scanner, NULL) ) { - ScanDirectory(media_path->path, NULL, media_path->type); - media_path = media_path->next; + printf("ERROR: pthread_create() failed\n"); + exit(-1); } - freopen("/proc/self/fd/2", "a", stderr); - #if USE_FORK - _exit(0); - #endif } sqlite3_free_table(result); } - if( GETFLAG(INOTIFYMASK) && pthread_create(&thread, NULL, start_inotify, NULL) ) + if( GETFLAG(INOTIFYMASK) && pthread_create(&thread[1], NULL, start_inotify, NULL) ) { printf("ERROR: pthread_create() failed\n"); exit(-1); } } - #if USE_FORK - fork_done: - #endif sudp = OpenAndConfSSDPReceiveSocket(n_lan_addr, lan_addr); if(sudp < 0) diff --git a/scanner.c b/scanner.c index eac6f0b..c1ae3c2 100644 --- a/scanner.c +++ b/scanner.c @@ -716,3 +716,21 @@ ScanDirectory(const char * dir, const char * parent, enum media_types type) printf("Scanning %s finished!\n", dir); } } + +void * +start_scanner() +{ + struct media_dir_s * media_path = media_dirs; + + scanning = 1; + freopen("/dev/null", "a", stderr); + while( media_path ) + { + ScanDirectory(media_path->path, NULL, media_path->type); + media_path = media_path->next; + } + freopen("/proc/self/fd/2", "a", stderr); + scanning = 0; + + return 0; +} diff --git a/scanner.h b/scanner.h index f6f3dba..daec516 100644 --- a/scanner.h +++ b/scanner.h @@ -27,7 +27,7 @@ insert_file(char * name, const char * path, const char * parentID, int object); int CreateDatabase(void); -void -ScanDirectory(const char * dir, const char * parent, enum media_types type); +void * +start_scanner(); #endif diff --git a/upnpglobalvars.c b/upnpglobalvars.c index 1288770..b21fadc 100644 --- a/upnpglobalvars.c +++ b/upnpglobalvars.c @@ -39,4 +39,5 @@ sqlite3 * db; char friendly_name[FRIENDLYNAME_MAX_LEN]; struct media_dir_s * media_dirs = NULL; struct album_art_name_s * album_art_names = NULL; +short int scanning = 0; __u32 updateID = 0; diff --git a/upnpglobalvars.h b/upnpglobalvars.h index bdc026d..c27ec10 100644 --- a/upnpglobalvars.h +++ b/upnpglobalvars.h @@ -85,6 +85,7 @@ extern sqlite3 *db; extern char friendly_name[]; extern struct media_dir_s * media_dirs; extern struct album_art_name_s * album_art_names; +extern short int scanning; extern __u32 updateID; #endif