* 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.
This commit is contained in:
Justin Maggard 2009-02-17 08:25:15 +00:00
parent 35ce036f3c
commit 14547e2266
7 changed files with 44 additions and 23 deletions

View File

@ -120,6 +120,7 @@ inotify_create_watches(int fd)
unsigned int num_watches = 0, watch_limit = 8192; unsigned int num_watches = 0, watch_limit = 8192;
char **result; char **result;
int i, rows = 0; 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 ) 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); "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); 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++ ) for( i=1; i <= rows; i++ )
{ {
@ -310,7 +317,8 @@ inotify_insert_file(char * name, const char * path)
if( strcmp(parent_buf, "/") == 0 ) if( strcmp(parent_buf, "/") == 0 )
{ {
printf("No parents found!\n"); id = strdup("");
depth = 0;
break; break;
} }
strcpy(path_buf, path); strcpy(path_buf, path);
@ -353,6 +361,7 @@ inotify_insert_directory(int fd, char *name, const char * path)
else else
{ {
sqlite3_free_table(result); sqlite3_free_table(result);
insert_directory(name, path, BROWSEDIR_ID, "", get_next_available_id("OBJECTS", id));
} }
} }
free(parent_buf); free(parent_buf);
@ -493,6 +502,10 @@ start_inotify()
perror( "inotify_init" ); perror( "inotify_init" );
} }
while( scanning )
{
sleep(1);
}
inotify_create_watches(fd); inotify_create_watches(fd);
while( 1 ) while( 1 )
{ {

View File

@ -153,6 +153,8 @@ GetAudioMetadata(const char * path, char * name)
tag = taglib_file_tag(audio_file); tag = taglib_file_tag(audio_file);
properties = taglib_file_audioproperties(audio_file); properties = taglib_file_audioproperties(audio_file);
if( !properties )
return 0;
seconds = taglib_audioproperties_length(properties) % 60; seconds = taglib_audioproperties_length(properties) % 60;
minutes = (taglib_audioproperties_length(properties) - seconds) / 60; minutes = (taglib_audioproperties_length(properties) - seconds) / 60;

View File

@ -600,7 +600,7 @@ main(int argc, char * * argv)
int max_fd = -1; int max_fd = -1;
int last_changecnt = 0; int last_changecnt = 0;
char * sql; char * sql;
pthread_t thread; pthread_t thread[2];
if(init(argc, argv) != 0) if(init(argc, argv) != 0)
return 1; return 1;
@ -622,48 +622,34 @@ main(int argc, char * * argv)
{ {
char **result; char **result;
int rows; 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( sql_get_table(db, "pragma user_version", &result, &rows, 0) == SQLITE_OK )
{ {
if( atoi(result[1]) != DB_VERSION ) { if( atoi(result[1]) != DB_VERSION ) {
struct media_dir_s * media_path = media_dirs;
printf("Database version mismatch; need to recreate...\n"); printf("Database version mismatch; need to recreate...\n");
sqlite3_close(db); sqlite3_close(db);
unlink(DB_PATH "/files.db"); unlink(DB_PATH "/files.db");
system("rm -rf " DB_PATH "/art_cache"); system("rm -rf " DB_PATH "/art_cache");
sqlite3_open(DB_PATH "/files.db", &db); sqlite3_open(DB_PATH "/files.db", &db);
freopen("/dev/null", "a", stderr);
if( CreateDatabase() != 0 ) if( CreateDatabase() != 0 )
{ {
fprintf(stderr, "Error creating database!\n"); fprintf(stderr, "Error creating database!\n");
return -1; return -1;
} }
#if USE_FORK if( pthread_create(&thread[0], NULL, start_scanner, NULL) )
pid_t newpid = fork();
if( newpid )
goto fork_done;
#endif
while( media_path )
{ {
ScanDirectory(media_path->path, NULL, media_path->type); printf("ERROR: pthread_create() failed\n");
media_path = media_path->next; exit(-1);
} }
freopen("/proc/self/fd/2", "a", stderr);
#if USE_FORK
_exit(0);
#endif
} }
sqlite3_free_table(result); 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"); printf("ERROR: pthread_create() failed\n");
exit(-1); exit(-1);
} }
} }
#if USE_FORK
fork_done:
#endif
sudp = OpenAndConfSSDPReceiveSocket(n_lan_addr, lan_addr); sudp = OpenAndConfSSDPReceiveSocket(n_lan_addr, lan_addr);
if(sudp < 0) if(sudp < 0)

View File

@ -716,3 +716,21 @@ ScanDirectory(const char * dir, const char * parent, enum media_types type)
printf("Scanning %s finished!\n", dir); 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;
}

View File

@ -27,7 +27,7 @@ insert_file(char * name, const char * path, const char * parentID, int object);
int int
CreateDatabase(void); CreateDatabase(void);
void void *
ScanDirectory(const char * dir, const char * parent, enum media_types type); start_scanner();
#endif #endif

View File

@ -39,4 +39,5 @@ sqlite3 * db;
char friendly_name[FRIENDLYNAME_MAX_LEN]; char friendly_name[FRIENDLYNAME_MAX_LEN];
struct media_dir_s * media_dirs = NULL; struct media_dir_s * media_dirs = NULL;
struct album_art_name_s * album_art_names = NULL; struct album_art_name_s * album_art_names = NULL;
short int scanning = 0;
__u32 updateID = 0; __u32 updateID = 0;

View File

@ -85,6 +85,7 @@ extern sqlite3 *db;
extern char friendly_name[]; extern char friendly_name[];
extern struct media_dir_s * media_dirs; extern struct media_dir_s * media_dirs;
extern struct album_art_name_s * album_art_names; extern struct album_art_name_s * album_art_names;
extern short int scanning;
extern __u32 updateID; extern __u32 updateID;
#endif #endif