From 996e8ce072838c424dd6a54dc6eb1be06a939345 Mon Sep 17 00:00:00 2001 From: Warren Dukes Date: Sat, 10 Apr 2004 17:56:01 +0000 Subject: [PATCH] work in prep of using msells/sbh's non-blocking update method Here, i've made readDirectoryDB detect when stuff is deleted, added and updated. So after a update, and we call redDirectoryDB, we update the db instead of just adding stuff w/o "updating" and "deleting" stuff as neccessary. git-svn-id: https://svn.musicpd.org/mpd/trunk@659 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/directory.c | 45 +++++++++++++++++++++++++++++++++---- src/song.c | 59 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 96 insertions(+), 8 deletions(-) diff --git a/src/directory.c b/src/directory.c index 3da1a0517..4ef4af10b 100644 --- a/src/directory.c +++ b/src/directory.c @@ -59,7 +59,7 @@ typedef struct _Directory { time_t mtime; /* modification time */ } Directory; -Directory * mp3rootDirectory; +Directory * mp3rootDirectory = NULL; char directorydb[MAXPATHLEN+1]; @@ -438,6 +438,9 @@ void readDirectoryInfo(FILE * fp,Directory * directory) { Directory * subDirectory; char * name; time_t mtime; + int strcmpRet; + ListNode * nextDirNode = directory->subDirectories->firstNode; + ListNode * nodeTemp; while(myFgets(buffer,bufferSize,fp) && 0!=strncmp(DIRECTORY_END,buffer,strlen(DIRECTORY_END))) { if(0==strncmp(DIRECTORY_DIR,buffer,strlen(DIRECTORY_DIR))) { @@ -461,8 +464,36 @@ void readDirectoryInfo(FILE * fp,Directory * directory) { exit(EXIT_FAILURE); } name = strdup(&(buffer[strlen(DIRECTORY_BEGIN)])); - subDirectory = newDirectory(directory,name,mtime); - insertInList(directory->subDirectories,key,(void *)subDirectory); + + while(nextDirNode && (strcmpRet = + strcmp(key,nextDirNode->key)) > 0) { + nodeTemp = nextDirNode->nextNode; + deleteNodeFromList(directory->subDirectories, + nextDirNode); + nextDirNode = nodeTemp; + } + + if(!nextDirNode) { + subDirectory = newDirectory(directory,name, + mtime); + insertInList(directory->subDirectories,key, + (void *)subDirectory); + } + else if(strcmpRet == 0) { + subDirectory = (Directory *)nextDirNode->data; + subDirectory->mtime = mtime; + nextDirNode = nextDirNode->nextNode; + } + else { + subDirectory = newDirectory(directory,name, + mtime); + insertInListBeforeNode( + directory->subDirectories, + nextDirNode, + key, + (void *)subDirectory); + } + free(key); free(name); readDirectoryInfo(fp,subDirectory); @@ -475,6 +506,12 @@ void readDirectoryInfo(FILE * fp,Directory * directory) { exit(EXIT_FAILURE); } } + + while(nextDirNode) { + nodeTemp = nextDirNode->nextNode; + deleteNodeFromList(directory->subDirectories,nextDirNode); + nextDirNode = nodeTemp; + } } void sortDirectory(Directory * directory) { @@ -516,7 +553,7 @@ int writeDirectoryDB() { int readDirectoryDB() { FILE * fp; - mp3rootDirectory = newDirectory(NULL,NULL,0); + if(!mp3rootDirectory) mp3rootDirectory = newDirectory(NULL,NULL,0); while(!(fp=fopen(directorydb,"r")) && errno==EINTR); if(!fp) return -1; diff --git a/src/song.c b/src/song.c index 826fbceb8..7cf327080 100644 --- a/src/song.c +++ b/src/song.c @@ -104,6 +104,12 @@ void freeSong(Song * song) { free(song); } +void freeJustSong(Song * song) { + free(song->utf8file); + if(song->tag) freeMpdTag(song->tag); + free(song); +} + SongList * newSongList() { return makeList((ListFreeDataFunc *)freeSong); } @@ -160,19 +166,58 @@ void writeSongInfoFromList(FILE * fp, SongList * list) { myfprintf(fp,"%s\n",SONG_END); } +void insertSongIntoList(SongList * list, ListNode ** nextSongNode, char * key, + Song * song) +{ + ListNode * nodeTemp; + int cmpRet= 0; + + while(*nextSongNode && (cmpRet = strcmp(key,(*nextSongNode)->key)) > 0) + { + nodeTemp = (*nextSongNode)->nextNode; + deleteNodeFromList(list,*nextSongNode); + *nextSongNode = nodeTemp; + } + + if(!(*nextSongNode)) { + insertInList(list,key,(void *)song); + addSongToTables(song); + } + else if(cmpRet == 0) { + Song * tempSong = (Song *)((*nextSongNode)->data); + if(tempSong->mtime != song->mtime) { + removeASongFromTables(tempSong); + freeMpdTag(tempSong->tag); + tempSong->tag = song->tag; + tempSong->mtime = song->mtime; + song->tag = NULL; + freeJustSong(song); + addSongToTables(tempSong); + } + *nextSongNode = (*nextSongNode)->nextNode; + } + else { + addSongToTables(song); + insertInListBeforeNode(list,*nextSongNode,key,(void *)song); + } +} + void readSongInfoIntoList(FILE * fp, SongList * list) { char buffer[MAXPATHLEN+1024]; int bufferSize = MAXPATHLEN+1024; Song * song = NULL; char * key = NULL; + ListNode * nextSongNode = list->firstNode; + ListNode * nodeTemp; while(myFgets(buffer,bufferSize,fp) && 0!=strcmp(SONG_END,buffer)) { if(0==strncmp(SONG_KEY,buffer,strlen(SONG_KEY))) { if(song) { - insertInList(list,key,(void *)song); - addSongToTables(song); + insertSongIntoList(list,&nextSongNode,key,song); + song = NULL; free(key); } + key = strdup(&(buffer[strlen(SONG_KEY)])); song = newNullSong(); } @@ -213,10 +258,16 @@ void readSongInfoIntoList(FILE * fp, SongList * list) { } if(song) { - insertInList(list,key,(void *)song); - addSongToTables(song); + insertSongIntoList(list,&nextSongNode,key,song); + song = NULL; free(key); } + + while(nextSongNode) { + nodeTemp = nextSongNode->nextNode; + deleteNodeFromList(list,nextSongNode); + nextSongNode = nodeTemp; + } } int updateSongInfo(Song * song) {