* Update database entries if inotify catches a file CLOSE_WRITE and the modify time is newer than the last recorded time in the database.

This commit is contained in:
Justin Maggard 2009-10-23 02:38:42 +00:00
parent 590f0761f4
commit 0f0d22c2f5
4 changed files with 55 additions and 39 deletions

View File

@ -20,6 +20,7 @@
#endif #endif
#include "upnpglobalvars.h" #include "upnpglobalvars.h"
#include "inotify.h"
#include "utils.h" #include "utils.h"
#include "sql.h" #include "sql.h"
#include "scanner.h" #include "scanner.h"
@ -275,6 +276,7 @@ inotify_insert_file(char * name, const char * path)
int depth = 1; int depth = 1;
enum media_types type = ALL_MEDIA; enum media_types type = ALL_MEDIA;
struct media_dir_s * media_path = media_dirs; struct media_dir_s * media_path = media_dirs;
struct stat st;
/* Is it cover art for another file? */ /* Is it cover art for another file? */
if( is_image(path) ) if( is_image(path) )
@ -317,19 +319,29 @@ inotify_insert_file(char * name, const char * path)
break; break;
} }
/* If it's already in the database, just skip it for now. /* If it's already in the database and hasn't been modified, skip it. */
* TODO: compare modify timestamps */ if( stat(path, &st) != 0 )
sql = sqlite3_mprintf("SELECT ID from DETAILS where PATH = '%q'", path); return -1;
sql = sqlite3_mprintf("SELECT TIMESTAMP from DETAILS where PATH = '%q'", path);
if( sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK ) if( sql_get_table(db, sql, &result, &rows, NULL) == SQLITE_OK )
{ {
if( rows ) if( rows )
{ {
free(last_dir); if( atoi(result[1]) < st.st_mtime )
free(path_buf); {
free(base_name); DPRINTF(E_DEBUG, L_INOTIFY, "%s is newer than the last db entry.\n", path);
sqlite3_free(sql); inotify_remove_file(path_buf);
sqlite3_free_table(result); }
return -1; else
{
free(last_dir);
free(path_buf);
free(base_name);
sqlite3_free(sql);
sqlite3_free_table(result);
return -1;
}
} }
sqlite3_free_table(result); sqlite3_free_table(result);
} }
@ -518,22 +530,27 @@ inotify_remove_file(const char * path)
if( sql_get_table(db, sql, &result2, NULL, NULL) == SQLITE_OK ) if( sql_get_table(db, sql, &result2, NULL, NULL) == SQLITE_OK )
{ {
children = atoi(result2[1]); children = atoi(result2[1]);
if( children < 2 )
{
free(sql);
asprintf(&sql, "DELETE from OBJECTS where OBJECT_ID = '%s'", result[i]);
sql_exec(db, sql);
}
sqlite3_free_table(result2); sqlite3_free_table(result2);
if( children < 2 ) if( children < 2 )
{ {
*rindex(result[i], '$') = '\0';
free(sql); free(sql);
asprintf(&sql, "DELETE from DETAILS where ID ="
" (SELECT DETAIL_ID from OBJECTS where OBJECT_ID = '%s')", result[i]);
sql_exec(db, sql);
free(sql);
asprintf(&sql, "DELETE from OBJECTS where OBJECT_ID = '%s'", result[i]);
sql_exec(db, sql);
free(sql);
*rindex(result[i], '$') = '\0';
asprintf(&sql, "SELECT count(*) from OBJECTS where PARENT_ID = '%s'", result[i]); asprintf(&sql, "SELECT count(*) from OBJECTS where PARENT_ID = '%s'", result[i]);
if( sql_get_table(db, sql, &result2, NULL, NULL) == SQLITE_OK ) if( sql_get_table(db, sql, &result2, NULL, NULL) == SQLITE_OK )
{ {
if( atoi(result2[1]) == 0 ) if( atoi(result2[1]) == 0 )
{ {
free(sql);
asprintf(&sql, "DELETE from DETAILS where ID ="
" (SELECT DETAIL_ID from OBJECTS where OBJECT_ID = '%s')", result[i]);
sql_exec(db, sql);
free(sql); free(sql);
asprintf(&sql, "DELETE from OBJECTS where OBJECT_ID = '%s'", result[i]); asprintf(&sql, "DELETE from OBJECTS where OBJECT_ID = '%s'", result[i]);
sql_exec(db, sql); sql_exec(db, sql);

View File

@ -1,3 +1,6 @@
int
inotify_remove_file(const char * path);
void * void *
start_inotify(); start_inotify();

View File

@ -357,11 +357,11 @@ GetAudioMetadata(const char * path, char * name)
album_art = find_album_art(path, song.image, song.image_size); album_art = find_album_art(path, song.image, song.image_size);
sql = sqlite3_mprintf( "INSERT into DETAILS" sql = sqlite3_mprintf( "INSERT into DETAILS"
" (PATH, SIZE, DURATION, CHANNELS, BITRATE, SAMPLERATE, DATE," " (PATH, SIZE, TIMESTAMP, DURATION, CHANNELS, BITRATE, SAMPLERATE, DATE,"
" TITLE, CREATOR, ARTIST, ALBUM, GENRE, COMMENT, DISC, TRACK, DLNA_PN, MIME, ALBUM_ART) " " TITLE, CREATOR, ARTIST, ALBUM, GENRE, COMMENT, DISC, TRACK, DLNA_PN, MIME, ALBUM_ART) "
"VALUES" "VALUES"
" (%Q, %d, '%s', %d, %d, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d, %d, %Q, '%s', %lld);", " (%Q, %lld, %ld, '%s', %d, %d, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d, %d, %Q, '%s', %lld);",
path, song.file_size, duration, song.channels, song.bitrate, song.samplerate, date, path, file.st_size, file.st_mtime, duration, song.channels, song.bitrate, song.samplerate, date,
title, band, artist, album, genre, comment, song.disc, song.track, title, band, artist, album, genre, comment, song.disc, song.track,
dlna_pn, song.mime?song.mime:mime, album_art); dlna_pn, song.mime?song.mime:mime, album_art);
freetags(&song); freetags(&song);
@ -418,7 +418,6 @@ GetImageMetadata(const char * path, char * name)
struct jpeg_error_mgr jerr; struct jpeg_error_mgr jerr;
FILE *infile; FILE *infile;
int width=0, height=0, thumb=0; int width=0, height=0, thumb=0;
off_t size;
char *date = NULL, *cam = NULL; char *date = NULL, *cam = NULL;
char make[32], model[64] = {'\0'}; char make[32], model[64] = {'\0'};
char b[1024]; char b[1024];
@ -430,12 +429,10 @@ GetImageMetadata(const char * path, char * name)
memset(&m, '\0', sizeof(metadata_t)); memset(&m, '\0', sizeof(metadata_t));
//DEBUG DPRINTF(E_DEBUG, L_METADATA, "Parsing %s...\n", path); //DEBUG DPRINTF(E_DEBUG, L_METADATA, "Parsing %s...\n", path);
if ( stat(path, &file) == 0 ) if ( stat(path, &file) != 0 )
size = file.st_size;
else
return 0; return 0;
strip_ext(name); strip_ext(name);
//DEBUG DPRINTF(E_DEBUG, L_METADATA, " * size: %jd\n", size); //DEBUG DPRINTF(E_DEBUG, L_METADATA, " * size: %jd\n", file.st_size);
/* MIME hard-coded to JPEG for now, until we add PNG support */ /* MIME hard-coded to JPEG for now, until we add PNG support */
asprintf(&m.mime, "image/jpeg"); asprintf(&m.mime, "image/jpeg");
@ -541,10 +538,10 @@ no_exifdata:
asprintf(&m.resolution, "%dx%d", width, height); asprintf(&m.resolution, "%dx%d", width, height);
sql = sqlite3_mprintf( "INSERT into DETAILS" sql = sqlite3_mprintf( "INSERT into DETAILS"
" (PATH, TITLE, SIZE, DATE, RESOLUTION, THUMBNAIL, CREATOR, DLNA_PN, MIME) " " (PATH, TITLE, SIZE, TIMESTAMP, DATE, RESOLUTION, THUMBNAIL, CREATOR, DLNA_PN, MIME) "
"VALUES" "VALUES"
" (%Q, '%q', %lld, %Q, %Q, %d, %Q, %Q, %Q);", " (%Q, '%q', %lld, %ld, %Q, %Q, %d, %Q, %Q, %Q);",
path, name, size, date, m.resolution, thumb, cam, m.dlna_pn, m.mime); path, name, file.st_size, file.st_mtime, date, m.resolution, thumb, cam, m.dlna_pn, m.mime);
//DEBUG DPRINTF(E_DEBUG, L_METADATA, "SQL: %s\n", sql); //DEBUG DPRINTF(E_DEBUG, L_METADATA, "SQL: %s\n", sql);
if( sql_exec(db, sql) != SQLITE_OK ) if( sql_exec(db, sql) != SQLITE_OK )
{ {
@ -572,7 +569,6 @@ no_exifdata:
sqlite_int64 sqlite_int64
GetVideoMetadata(const char * path, char * name) GetVideoMetadata(const char * path, char * name)
{ {
off_t size = 0;
struct stat file; struct stat file;
char *sql; char *sql;
int ret, i; int ret, i;
@ -590,14 +586,13 @@ GetVideoMetadata(const char * path, char * name)
date[0] = '\0'; date[0] = '\0';
DPRINTF(E_DEBUG, L_METADATA, "Parsing video %s...\n", name); DPRINTF(E_DEBUG, L_METADATA, "Parsing video %s...\n", name);
if ( stat(path, &file) == 0 ) if ( stat(path, &file) != 0 )
{ return 0;
modtime = localtime(&file.st_mtime);
strftime(date, sizeof(date), "%FT%T", modtime);
size = file.st_size;
}
strip_ext(name); strip_ext(name);
//DEBUG DPRINTF(E_DEBUG, L_METADATA, " * size: %jd\n", size); //DEBUG DPRINTF(E_DEBUG, L_METADATA, " * size: %jd\n", file.st_size);
modtime = localtime(&file.st_mtime);
strftime(date, sizeof(date), "%FT%T", modtime);
av_register_all(); av_register_all();
if( av_open_input_file(&ctx, path, NULL, 0, NULL) != 0 ) if( av_open_input_file(&ctx, path, NULL, 0, NULL) != 0 )
@ -997,11 +992,11 @@ GetVideoMetadata(const char * path, char * name)
album_art = find_album_art(path, NULL, 0); album_art = find_album_art(path, NULL, 0);
sql = sqlite3_mprintf( "INSERT into DETAILS" sql = sqlite3_mprintf( "INSERT into DETAILS"
" (PATH, SIZE, DURATION, DATE, CHANNELS, BITRATE, SAMPLERATE, RESOLUTION," " (PATH, SIZE, TIMESTAMP, DURATION, DATE, CHANNELS, BITRATE, SAMPLERATE, RESOLUTION,"
" CREATOR, TITLE, DLNA_PN, MIME, ALBUM_ART) " " CREATOR, TITLE, DLNA_PN, MIME, ALBUM_ART) "
"VALUES" "VALUES"
" (%Q, %lld, %Q, %Q, %Q, %Q, %Q, %Q, %Q, '%q', %Q, '%q', %lld);", " (%Q, %lld, %ld, %Q, %Q, %Q, %Q, %Q, %Q, %Q, '%q', %Q, '%q', %lld);",
path, size, m.duration, path, file.st_size, file.st_mtime, m.duration,
strlen(date) ? date : NULL, strlen(date) ? date : NULL,
m.channels, m.bitrate, m.frequency, m.resolution, m.channels, m.bitrate, m.frequency, m.resolution,
m.artist, name, m.dlna_pn, m.mime, m.artist, name, m.dlna_pn, m.mime,

View File

@ -628,7 +628,8 @@ CreateDatabase(void)
"DLNA_PN TEXT, " "DLNA_PN TEXT, "
"MIME TEXT, " "MIME TEXT, "
"ALBUM_ART INTEGER DEFAULT 0, " "ALBUM_ART INTEGER DEFAULT 0, "
"DISC INTEGER" "DISC INTEGER, "
"TIMESTAMP INTEGER"
")"); ")");
if( ret != SQLITE_OK ) if( ret != SQLITE_OK )
goto sql_failed; goto sql_failed;