* Handle inotify move events.

* Enable minimal logging to a file by default.
This commit is contained in:
Justin Maggard 2009-03-03 00:13:05 +00:00
parent 2664fe0d91
commit e3340c0839
2 changed files with 25 additions and 22 deletions

View File

@ -57,17 +57,17 @@ add_watch(int fd, const char * path)
struct watch *nw; struct watch *nw;
int wd; int wd;
wd = inotify_add_watch(fd, path, IN_CREATE|IN_CLOSE_WRITE|IN_DELETE); wd = inotify_add_watch(fd, path, IN_CREATE|IN_CLOSE_WRITE|IN_DELETE|IN_MOVE);
if( wd < 0 ) if( wd < 0 )
{ {
perror("inotify_add_watch()"); DPRINTF(E_ERROR, L_INOTIFY, "inotify_add_watch() [%s]\n", strerror(errno));
return -1; return -1;
} }
nw = malloc(sizeof(struct watch)); nw = malloc(sizeof(struct watch));
if( nw == NULL ) if( nw == NULL )
{ {
perror("malloc()"); DPRINTF(E_ERROR, L_INOTIFY, "malloc()\n");
return -1; return -1;
} }
nw->wd = wd; nw->wd = wd;
@ -219,7 +219,7 @@ int add_dir_watch(int fd, char * path, char * filename)
wd = add_watch(fd, buf); wd = add_watch(fd, buf);
if( wd == -1 ) if( wd == -1 )
{ {
perror("add_watch()"); DPRINTF(E_ERROR, L_INOTIFY, "add_watch() [%s]\n", strerror(errno));
} }
else else
{ {
@ -240,7 +240,7 @@ int add_dir_watch(int fd, char * path, char * filename)
} }
else else
{ {
perror(strerror(errno)); DPRINTF(E_ERROR, L_INOTIFY, "Opendir error! [%s]\n", strerror(errno));
} }
closedir(ds); closedir(ds);
i++; i++;
@ -296,6 +296,7 @@ inotify_insert_file(char * name, const char * path)
if( (sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK) && rows ) if( (sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK) && rows )
{ {
id = strdup(result[1]); id = strdup(result[1]);
printf("depth: %d, id: %s\n", depth, id);
sqlite3_free_table(result); sqlite3_free_table(result);
sqlite3_free(sql); sqlite3_free(sql);
if( !depth ) if( !depth )
@ -318,7 +319,7 @@ inotify_insert_file(char * name, const char * path)
if( strcmp(parent_buf, "/") == 0 ) if( strcmp(parent_buf, "/") == 0 )
{ {
id = strdup(""); id = calloc(1, 3);
depth = 0; depth = 0;
break; break;
} }
@ -371,7 +372,7 @@ inotify_insert_directory(int fd, char *name, const char * path)
wd = add_watch(fd, path); wd = add_watch(fd, path);
if( wd == -1 ) if( wd == -1 )
{ {
perror("add_watch()"); DPRINTF(E_ERROR, L_INOTIFY, "add_watch() failed");
} }
else else
{ {
@ -402,7 +403,7 @@ inotify_insert_directory(int fd, char *name, const char * path)
} }
else else
{ {
perror(strerror(errno)); DPRINTF(E_ERROR, L_INOTIFY, "opendir failed! [%s]\n", strerror(errno));
} }
closedir(ds); closedir(ds);
i++; i++;
@ -538,7 +539,7 @@ start_inotify()
fd = inotify_init(); fd = inotify_init();
if ( fd < 0 ) { if ( fd < 0 ) {
perror( "inotify_init" ); DPRINTF(E_ERROR, L_INOTIFY, "inotify_init() failed!\n");
} }
while( scanning ) while( scanning )
@ -551,7 +552,7 @@ start_inotify()
length = read(fd, buffer, BUF_LEN); length = read(fd, buffer, BUF_LEN);
if ( length < 0 ) { if ( length < 0 ) {
perror( "read" ); DPRINTF(E_ERROR, L_INOTIFY, "read failed!\n");
} }
i = 0; i = 0;
@ -562,24 +563,27 @@ start_inotify()
{ {
esc_name = modifyString(strdup(event->name), "&", "&amp;amp;", 0); esc_name = modifyString(strdup(event->name), "&", "&amp;amp;", 0);
asprintf(&path_buf, "%s/%s", get_path_from_wd(event->wd), event->name); asprintf(&path_buf, "%s/%s", get_path_from_wd(event->wd), event->name);
if ( event->mask & IN_CREATE && event->mask & IN_ISDIR ) if ( (event->mask & IN_CREATE && event->mask & IN_ISDIR) ||
(event->mask & IN_MOVED_TO && event->mask & IN_ISDIR) )
{ {
DPRINTF(E_DEBUG, L_INOTIFY, "The directory %s was created.\n", path_buf ); DPRINTF(E_DEBUG, L_INOTIFY, "The directory %s was %s.\n", path_buf, (event->mask & IN_MOVED_TO ? "moved here" : "created"));
inotify_insert_directory(fd, esc_name, path_buf); inotify_insert_directory(fd, esc_name, path_buf);
} }
else if ( event->mask & IN_CLOSE_WRITE ) else if ( event->mask & IN_CLOSE_WRITE || event->mask & IN_MOVED_TO )
{ {
DPRINTF(E_DEBUG, L_INOTIFY, "The file %s was changed.\n", path_buf ); DPRINTF(E_DEBUG, L_INOTIFY, "The file %s was %s.\n", path_buf, (event->mask & IN_MOVED_TO ? "moved here" : "changed"));
inotify_insert_file(esc_name, path_buf); inotify_insert_file(esc_name, path_buf);
} }
else if ( event->mask & IN_DELETE ) else if ( event->mask & IN_DELETE || event->mask & IN_MOVED_FROM )
{ {
if ( event->mask & IN_ISDIR ) { if ( event->mask & IN_ISDIR )
DPRINTF(E_DEBUG, L_INOTIFY, "The directory %s was deleted.\n", path_buf); {
DPRINTF(E_DEBUG, L_INOTIFY, "The directory %s was %s.\n", path_buf, (event->mask & IN_MOVED_FROM ? "moved away" : "deleted"));
inotify_remove_directory(fd, path_buf); inotify_remove_directory(fd, path_buf);
} }
else { else
DPRINTF(E_DEBUG, L_INOTIFY, "The file %s was deleted.\n", path_buf); {
DPRINTF(E_DEBUG, L_INOTIFY, "The file %s was %s.\n", path_buf, (event->mask & IN_MOVED_FROM ? "moved away" : "deleted"));
inotify_remove_file(path_buf); inotify_remove_file(path_buf);
} }
} }

View File

@ -524,8 +524,7 @@ init(int argc, char * * argv)
#else #else
pid = daemonize(); pid = daemonize();
#endif #endif
log_init(DB_PATH "/minidlna.log", NULL); log_init(DB_PATH "/minidlna.log", "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn");
} }
if(checkforrunning(pidfilename) < 0) if(checkforrunning(pidfilename) < 0)
@ -592,7 +591,7 @@ main(int argc, char * * argv)
if(init(argc, argv) != 0) if(init(argc, argv) != 0)
return 1; return 1;
DPRINTF(E_ERROR, L_GENERAL, "Starting MiniDLNA...\n"); DPRINTF(E_WARN, L_GENERAL, "Starting MiniDLNA...\n");
LIST_INIT(&upnphttphead); LIST_INIT(&upnphttphead);
if( access(DB_PATH, F_OK) != 0 ) if( access(DB_PATH, F_OK) != 0 )