ok, finished incremental update!

now just need to polish up a few things with update

git-svn-id: https://svn.musicpd.org/mpd/trunk@745 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Warren Dukes 2004-04-14 05:26:32 +00:00
parent ba1d3302e2
commit 2a24a94ba7
4 changed files with 135 additions and 28 deletions

3
TODO
View File

@ -4,9 +4,6 @@
quit in the middle of an update quit in the middle of an update
k) when bg-update, have parent print out new old stuff to log on k) when bg-update, have parent print out new old stuff to log on
reading db, not the child reading db, not the child
m) MOST IMPORTANT: update needs to deal better with directories/files
that don't exist in the db, but do exit in the fs (i.e.
calling updated on a newly created file).
n) mpd command for rereading db n) mpd command for rereading db
o) rewrite update functions to indicate if something was updated, then o) rewrite update functions to indicate if something was updated, then
when update process returns, indicate wheater db was updated when update process returns, indicate wheater db was updated

View File

@ -94,7 +94,7 @@ void deleteEmptyDirectoriesInDirectory(Directory * directory);
void removeSongFromDirectory(Directory * directory, char * shortname); void removeSongFromDirectory(Directory * directory, char * shortname);
int addSubDirectoryToDirectory(Directory * directory, char * shortname, Directory * addSubDirectoryToDirectory(Directory * directory, char * shortname,
char * name); char * name);
Directory * getDirectoryDetails(char * name, char ** shortname, Directory * getDirectoryDetails(char * name, char ** shortname,
@ -107,6 +107,8 @@ Song * getSongDetails(char * file, char ** shortnameRet,
void updatePath(char * utf8path); void updatePath(char * utf8path);
void sortDirectory(Directory * directory);
void clearUpdatePid() { void clearUpdatePid() {
directory_updatePid = 0; directory_updatePid = 0;
} }
@ -312,15 +314,12 @@ int removeDeletedFromDirectory(Directory * directory) {
if(!utf8) continue; if(!utf8) continue;
utf8 = strdup(utf8);
if(directory->utf8name) { if(directory->utf8name) {
s = malloc(strlen(directory->utf8name)+strlen(utf8)+2); s = malloc(strlen(directory->utf8name)+strlen(utf8)+2);
sprintf(s,"%s/%s",directory->utf8name,utf8); sprintf(s,"%s/%s",directory->utf8name,utf8);
} }
else s= strdup(utf8); else s= strdup(utf8);
insertInList(entList,utf8,s); insertInList(entList,utf8,s);
free(utf8);
} }
closedir(dir); closedir(dir);
@ -331,11 +330,14 @@ int removeDeletedFromDirectory(Directory * directory) {
if(findInList(entList,node->key,&name)) { if(findInList(entList,node->key,&name)) {
if(!isDir((char *)name,NULL)) { if(!isDir((char *)name,NULL)) {
LOG("removing directory: %s\n",(char*)name); LOG("removing directory: %s\n",(char*)name);
deleteFromList(directory->subDirectories,node->key); deleteFromList(directory->subDirectories,
node->key);
} }
} }
else { else {
LOG("removing directory: %s\n",(char*)name); LOG("removing directory: ");
if(directory->utf8name) LOG("%s/",directory->utf8name);
LOG("%s\n",node->key);
deleteFromList(directory->subDirectories,node->key); deleteFromList(directory->subDirectories,node->key);
} }
node = tmpNode; node = tmpNode;
@ -360,39 +362,104 @@ int removeDeletedFromDirectory(Directory * directory) {
return 0; return 0;
} }
Directory * addDirectoryPathToDB(char * utf8path, char ** shortname) {
char * parent;
Directory * parentDirectory;
void * directory;
parent = strdup(parentPath(utf8path));
if(strlen(parent)==0) parentDirectory = (void *)mp3rootDirectory;
else parentDirectory = addDirectoryPathToDB(parent,shortname);
*shortname = utf8path+strlen(parent);
while(*(*shortname) && *(*shortname)=='/') (*shortname)++;
if(!findInList(parentDirectory->subDirectories,*shortname, &directory))
{
directory = (void *)addSubDirectoryToDirectory(parentDirectory,
*shortname,utf8path);
}
/* if we're adding directory paths, make sure to delete filenames
with potentially the same name*/
removeSongFromDirectory(parentDirectory,*shortname);
free(parent);
return (Directory *)parentDirectory;
}
Directory * addParentPathToDB(char * utf8path, char ** shortname) {
char * parent;
Directory * parentDirectory;
parent = strdup(parentPath(utf8path));
if(strlen(parent)==0) parentDirectory = (void *)mp3rootDirectory;
else parentDirectory = addDirectoryPathToDB(parent,shortname);
*shortname = utf8path+strlen(parent);
while(*(*shortname) && *(*shortname)=='/') (*shortname)++;
free(parent);
return (Directory *)parentDirectory;
}
void updatePath(char * utf8path) { void updatePath(char * utf8path) {
Directory * directory; Directory * directory;
Directory * parentDirectory; Directory * parentDirectory;
Song * song; Song * song;
char * shortname; char * shortname;
DIR * dir; char * path = sanitizePathDup(utf8path);
if(NULL==path) return;
/* if path is in the DB try to update it, or else delete it */ /* if path is in the DB try to update it, or else delete it */
if((directory = getDirectoryDetails(utf8path,&shortname, if((directory = getDirectoryDetails(path,&shortname,
&parentDirectory))) &parentDirectory)))
{ {
/* if updateDirectory fials, means we should delete it */ /* if this update directory is successfull, we are done */
if(updateDirectory(directory)<0 && directory!=mp3rootDirectory) if(updateDirectory(directory)==0)
{ {
free(path);
sortDirectory(directory);
return;
}
/* we don't want to delete the root directory */
else if(directory == mp3rootDirectory) {
free(path);
return;
}
/* if updateDirectory fials, means we should delete it */
else {
LOG("removing directory: %s\n",path);
deleteFromList(parentDirectory->subDirectories, deleteFromList(parentDirectory->subDirectories,
shortname); shortname);
} }
} }
else if((song = getSongDetails(utf8path,&shortname,&parentDirectory))) { else if((song = getSongDetails(path,&shortname,&parentDirectory))) {
if(song && updateSongInfo(song)<0) { /* if this song update is successfull, we are done */
removeSongFromDirectory(parentDirectory,shortname); if(song && updateSongInfo(song)==0) {
free(path);
return;
} }
/* if updateDirectory fials, means we should delete it */
else removeSongFromDirectory(parentDirectory,shortname);
} }
/* apth not found in the db, see if it actually exists on the fs */
else if(isDir(utf8path,NULL)) { /* path not found in the db, see if it actually exists on the fs.
/* create parent/get parent directory */ * Also, if by chance a directory was replaced by a file of the same
/* create new directory and add to parent */ * name or vice versa, we need to add it to the db
/* explore direcotry */ */
} if(isDir(path,NULL) || isMusic(path,NULL)) {
else if((isMusic(utf8path,NULL))) { parentDirectory = addParentPathToDB(path,&shortname);
/* create parent/get parent directory */ addToDirectory(parentDirectory,shortname,path);
/* add song to directory */ sortDirectory(parentDirectory);
} }
free(path);
} }
int updateDirectory(Directory * directory) { int updateDirectory(Directory * directory) {
@ -479,7 +546,7 @@ int exploreDirectory(Directory * directory) {
return 0; return 0;
} }
int addSubDirectoryToDirectory(Directory * directory, char * shortname, Directory * addSubDirectoryToDirectory(Directory * directory, char * shortname,
char * name) char * name)
{ {
Directory * subDirectory = newDirectory(name,-1); Directory * subDirectory = newDirectory(name,-1);
@ -487,12 +554,13 @@ int addSubDirectoryToDirectory(Directory * directory, char * shortname,
insertInList(directory->subDirectories,shortname,subDirectory); insertInList(directory->subDirectories,shortname,subDirectory);
exploreDirectory(subDirectory); exploreDirectory(subDirectory);
return 0; return subDirectory;
} }
int addToDirectory(Directory * directory, char * shortname, char * name) { int addToDirectory(Directory * directory, char * shortname, char * name) {
if(isDir(name,NULL)) { if(isDir(name,NULL)) {
return addSubDirectoryToDirectory(directory,shortname,name); addSubDirectoryToDirectory(directory,shortname,name);
return 0;
} }
else if(isMusic(name,NULL)) { else if(isMusic(name,NULL)) {
Song * song; Song * song;

View File

@ -20,6 +20,7 @@
#include "log.h" #include "log.h"
#include "charConv.h" #include "charConv.h"
#include "conf.h" #include "conf.h"
#include "utf8.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -202,3 +203,41 @@ char * parentPath(char * path) {
return parentPath; return parentPath;
} }
char * sanitizePathDup(char * path) {
int len = strlen(path)+1;
char * ret = malloc(len);
char * cp = ret;
memset(ret,0,len);
len = 0;
/* illeminate more than one '/' in a row, like "///" */
while(*path) {
while(*path=='/') path++;
if(*path=='.') {
/* we dont want to have hidden directoires, or '.' or
".." in our path */
free(ret);
return NULL;
}
while(*path && *path!='/') {
*(cp++) = *(path++);
len++;
}
if(*path=='/') {
*(cp++) = *(path++);
len++;
}
}
if(len && ret[len-1]=='/') {
len--;
ret[len] = '\0';
}
printf("sanitized: %s\n", ret);
return realloc(ret,len+1);
}

View File

@ -49,4 +49,7 @@ char * rpp2app(char * file);
/* static char * returned */ /* static char * returned */
char * parentPath(char * path); char * parentPath(char * path);
/* strips extra "///" and leading "/" and trailing "/" */
char * sanitizePathDup(char * path);
#endif #endif