From 104957bf2ff4a76ae65f7ae6a2f978ef3cf9a698 Mon Sep 17 00:00:00 2001 From: Ron Pedde Date: Thu, 21 Apr 2005 03:16:19 +0000 Subject: [PATCH] add db_delete_playlist, fix m3u scanning to use new database routines --- src/db-generic.c | 39 ++++++++++++++++++++++++++++++++++++++- src/db-generic.h | 4 ++++ src/dbs-sqlite.c | 36 ++++++++++++++++++++++++++++++++++-- src/dbs-sqlite.h | 4 ++-- src/mp3-scanner.c | 40 +++++++++++++++++++++++++--------------- 5 files changed, 103 insertions(+), 20 deletions(-) diff --git a/src/db-generic.c b/src/db-generic.c index 70bd65d5..ed88d70f 100644 --- a/src/db-generic.c +++ b/src/db-generic.c @@ -48,6 +48,7 @@ typedef struct tag_db_functions { int(*dbs_add)(MP3FILE*); int(*dbs_add_playlist)(char *, int, char *,char *, int *); int(*dbs_add_playlist_item)(int, int); + int(*dbs_delete_playlist)(int); int(*dbs_enum_start)(DBQUERYINFO *); int(*dbs_enum_size)(DBQUERYINFO *, int *); int(*dbs_enum_fetch)(DBQUERYINFO *, unsigned char **); @@ -58,7 +59,9 @@ typedef struct tag_db_functions { int(*dbs_get_count)(CountType_t); MP3FILE*(*dbs_fetch_item)(int); MP3FILE*(*dbs_fetch_path)(char *); + M3UFILE*(*dbs_fetch_playlist)(char *, int); void(*dbs_dispose_item)(MP3FILE*); + void(*dbs_dispose_playlist)(M3UFILE*); }DB_FUNCTIONS; /** All supported backend databases, and pointers to the db specific implementations */ @@ -72,6 +75,7 @@ DB_FUNCTIONS db_functions[] = { db_sqlite_add, db_sqlite_add_playlist, db_sqlite_add_playlist_item, + db_sqlite_delete_playlist, db_sqlite_enum_start, db_sqlite_enum_size, db_sqlite_enum_fetch, @@ -82,7 +86,9 @@ DB_FUNCTIONS db_functions[] = { db_sqlite_get_count, db_sqlite_fetch_item, db_sqlite_fetch_path, - db_sqlite_dispose_item + db_sqlite_fetch_playlist, + db_sqlite_dispose_item, + db_sqlite_dispose_playlist }, #endif { NULL,NULL } @@ -499,6 +505,23 @@ int db_add_playlist_item(int playlistid, int songid) { return retval; } +/** + * delete a playlist + * + * \param playlistid id of the playlist to delete + * \returns 0 on success, error code otherwise + */ +int db_delete_playlist(int playlistid) { + int retval; + + db_writelock(); + retval=db_current->dbs_delete_playlist(playlistid); + if(retval == DB_E_SUCCESS) + db_revision_no++; + db_unlock(); + + return retval; +} /** * start a db enumeration, based info in the DBQUERYINFO struct @@ -588,6 +611,16 @@ MP3FILE *db_fetch_path(char *path) { return retval; } +M3UFILE *db_fetch_playlist(char *path, int index) { + M3UFILE *retval; + + db_readlock(); + retval=db_current->dbs_fetch_playlist(path,index); + db_unlock(); + + return retval; +} + int db_start_scan(void) { int retval; @@ -614,6 +647,10 @@ void db_dispose_item(MP3FILE *pmp3) { return db_current->dbs_dispose_item(pmp3); } +void db_dispose_playlist(M3UFILE *pm3u) { + return db_current->dbs_dispose_playlist(pm3u); +} + int db_get_count(CountType_t type) { int retval; diff --git a/src/db-generic.h b/src/db-generic.h index 70f69958..1d0bedfa 100644 --- a/src/db-generic.h +++ b/src/db-generic.h @@ -157,9 +157,12 @@ extern int db_scanning(void); extern int db_add_playlist(char *name, int type, char *clause, char *path,int *playlistid); extern int db_add_playlist_item(int playlistid, int songid); +extern int db_delete_playlist(int playlistid); +extern int db_delete_playlist_item(int playlistid, int songid); extern MP3FILE *db_fetch_item(int id); extern MP3FILE *db_fetch_path(char *path); +extern M3UFILE *db_fetch_playlist(char *path, int index); /* metatag parsing */ @@ -180,6 +183,7 @@ extern int db_dmap_add_container(char *where, char *tag, int size); extern int db_get_song_count(void); extern int db_get_playlist_count(void); extern void db_dispose_item(MP3FILE *pmp3); +extern void db_dispose_playlist(M3UFILE *pm3u); #define DB_E_SUCCESS 0 diff --git a/src/dbs-sqlite.c b/src/dbs-sqlite.c index 41bf5d2c..0a5ef795 100644 --- a/src/dbs-sqlite.c +++ b/src/dbs-sqlite.c @@ -308,6 +308,28 @@ int db_sqlite_end_scan(void) { return 0; } +/** + * delete a playlist + * + * \param playlistid playlist to delete + */ +int db_sqlite_delete_playlist(int playlistid) { + int type; + int result; + + result=db_sqlite_get_int(E_DBG,&type,"select type from playlists where id=%d",playlistid); + if(result != DB_E_SUCCESS) { + if(result == DB_E_NOROWS) + return DB_E_INVALID_PLAYLIST; + return result; + } + + /* got a good playlist, now do what we need to do */ + db_sqlite_exec(E_FATAL,"delete from playlists where id=%d",playlistid); + db_sqlite_exec(E_FATAL,"delete from playlistitems where id=%d",playlistid); + + return DB_E_SUCCESS; +} /** * add a playlist @@ -1254,11 +1276,11 @@ void db_sqlite_build_mp3file(char **valarray, MP3FILE *pmp3) { * * \param path path to fetch */ -M3UFILE *db_sqlite_fetch_playlist(char *path) { +M3UFILE *db_sqlite_fetch_playlist(char *path, int index) { int rows,cols; char **resarray; int result; - M3UFILE *pm3u; + M3UFILE *pm3u=NULL; result = db_sqlite_get_table(E_DBG,&resarray,&rows,&cols, "select * from playlists where path='%q'",path); @@ -1369,6 +1391,16 @@ void db_sqlite_dispose_item(MP3FILE *pmp3) { free(pmp3); } +void db_sqlite_dispose_playlist(M3UFILE *pm3u) { + if(!pm3u) + return; + + MAYBEFREE(pm3u->title); + MAYBEFREE(pm3u->query); + MAYBEFREE(pm3u->path); + free(pm3u); +} + /** * count either the number of playlists, or the number of * songs diff --git a/src/dbs-sqlite.h b/src/dbs-sqlite.h index 89628658..857624f0 100644 --- a/src/dbs-sqlite.h +++ b/src/dbs-sqlite.h @@ -36,12 +36,12 @@ extern int db_sqlite_end_scan(void); extern int db_sqlite_get_count(CountType_t type); extern MP3FILE *db_sqlite_fetch_item(int id); extern MP3FILE *db_sqlite_fetch_path(char *path); -extern M3UFILE *db_sqlite_fetch_playlist(char *path); +extern M3UFILE *db_sqlite_fetch_playlist(char *path, int index); extern void db_sqlite_dispose_item(MP3FILE *pmp3); extern void db_sqlite_dispose_playlist(M3UFILE *pm3u); extern int db_sqlite_add_playlist(char *name, int type, char *clause, char *path, int *playlistid); extern int db_sqlite_add_playlist_item(int playlistid, int songid); - +extern int db_sqlite_delete_playlist(int playlistid); typedef enum { songID, diff --git a/src/mp3-scanner.c b/src/mp3-scanner.c index e1dab27e..a41fc099 100644 --- a/src/mp3-scanner.c +++ b/src/mp3-scanner.c @@ -295,7 +295,7 @@ static int scan_get_wavfileinfo(char *file, MP3FILE *pmp3); static int scan_get_urlfileinfo(char *file, MP3FILE *pmp3); static int scan_freetags(MP3FILE *pmp3); -//static void scan_static_playlist(char *path, struct dirent *pde, struct stat *psb); +static void scan_static_playlist(char *path, struct dirent *pde, struct stat *psb); static void scan_music_file(char *path, struct dirent *pde, struct stat *psb); static int scan_decode_mp3_frame(unsigned char *frame, SCAN_FRAMEINFO *pfi); @@ -489,30 +489,39 @@ int scan_path(char *path) { * * Scan a file as a static playlist */ -/* + void scan_static_playlist(char *path, struct dirent *pde, struct stat *psb) { char playlist_path[PATH_MAX]; char m3u_path[PATH_MAX]; char linebuffer[PATH_MAX]; int fd; int playlistid; - struct stat sb; + M3UFILE *pm3u; + MP3FILE *pmp3; DPRINTF(E_WARN,L_SCAN|L_PL,"Processing static playlist: %s\n",pde->d_name); - - - if(db_playlist_last_modified(psb->st_ino) == psb->st_mtime) - return; + snprintf(playlist_path,sizeof(playlist_path),"%s/%s",path,pde->d_name); - db_delete_playlist(psb->st_ino); + pm3u = db_fetch_playlist(playlist_path,0); + if(pm3u && (pm3u->db_timestamp > psb->st_mtime)) { + /* already up-to-date */ + db_dispose_playlist(pm3u); + return; + } strcpy(m3u_path,pde->d_name); - snprintf(playlist_path,sizeof(playlist_path),"%s/%s",path,pde->d_name); m3u_path[strlen(pde->d_name) - 4] = '\0'; - playlistid=psb->st_ino; + + if(pm3u) + db_delete_playlist(pm3u->id); + fd=open(playlist_path,O_RDONLY); if(fd != -1) { - db_add_playlist(playlistid,m3u_path,psb->st_mtime,0); + if(db_add_playlist(m3u_path,2,NULL,playlist_path,&playlistid) != DB_E_SUCCESS) { + DPRINTF(E_LOG,L_SCAN,"Error adding m3u playlist %s\n",playlist_path); + db_dispose_playlist(pm3u); + return; + } DPRINTF(E_INF,L_SCAN|L_PL,"Added playlist as id %d\n",playlistid); memset(linebuffer,0x00,sizeof(linebuffer)); @@ -536,9 +545,9 @@ void scan_static_playlist(char *path, struct dirent *pde, struct stat *psb) { DPRINTF(E_DBG,L_SCAN|L_PL,"Checking %s\n",m3u_path); // might be valid, might not... - if(!stat(m3u_path,&sb)) { - // FIXME: check to see if valid inode! - db_add_playlist_song(playlistid,sb.st_ino); + if((pmp3=db_fetch_path(m3u_path))) { + db_add_playlist_item(playlistid,pmp3->id); + db_dispose_item(pmp3); } else { DPRINTF(E_WARN,L_SCAN|L_PL,"Playlist entry %s bad: %s\n", m3u_path,strerror(errno)); @@ -547,9 +556,10 @@ void scan_static_playlist(char *path, struct dirent *pde, struct stat *psb) { close(fd); } + db_dispose_playlist(pm3u); DPRINTF(E_WARN,L_SCAN|L_PL,"Done processing playlist\n"); } -*/ + /* * scan_music_file