directory: don't pass fd to traverseAllIn()

This patch continues the work of the previous patch: don't pass a file
descriptor at all to traverseAllIn().  Since this fd was only used to
report "directory not found" errors, we can easily move that check to
the caller.  This is a great relief, since it removes the dependency
on a client connection from a lot of enumeration functions.
This commit is contained in:
Max Kellermann 2008-09-07 13:48:37 +02:00
parent 528be8a0a9
commit f320c9fa1d
6 changed files with 69 additions and 32 deletions

View File

@ -442,7 +442,13 @@ static int handleAdd(int fd, mpd_unused int *permission,
if (isRemoteUrl(path))
return addToPlaylist(path, NULL);
result = addAllIn(fd, path);
result = addAllIn(path);
if (result == (enum playlist_result)-1) {
commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found");
return -1;
}
return print_playlist_result(fd, result);
}
@ -658,6 +664,9 @@ static int handleFind(int fd, mpd_unused int *permission,
}
ret = findSongsIn(fd, NULL, numItems, items);
if (ret == -1)
commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found");
freeLocateTagItemArray(numItems, items);
@ -680,6 +689,9 @@ static int handleSearch(int fd, mpd_unused int *permission,
}
ret = searchForSongsIn(fd, NULL, numItems, items);
if (ret == -1)
commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found");
freeLocateTagItemArray(numItems, items);
@ -702,6 +714,9 @@ static int handleCount(int fd, mpd_unused int *permission,
}
ret = searchStatsForSongsIn(fd, NULL, numItems, items);
if (ret == -1)
commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found");
freeLocateTagItemArray(numItems, items);
@ -840,10 +855,17 @@ static int handleListAll(int fd, mpd_unused int *permission,
mpd_unused int argc, char *argv[])
{
char *directory = NULL;
int ret;
if (argc == 2)
directory = argv[1];
return printAllIn(fd, directory);
ret = printAllIn(fd, directory);
if (ret == -1)
commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found");
return ret;
}
static int handleVolume(int fd, mpd_unused int *permission,
@ -962,6 +984,10 @@ static int handleList(int fd, mpd_unused int *permission,
if (conditionals)
freeLocateTagItemArray(numConditionals, conditionals);
if (ret == -1)
commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found");
return ret;
}
@ -1055,10 +1081,16 @@ static int handleListAllInfo(int fd, mpd_unused int *permission,
mpd_unused int argc, char *argv[])
{
char *directory = NULL;
int ret;
if (argc == 2)
directory = argv[1];
return printInfoForAllIn(fd, directory);
ret = printInfoForAllIn(fd, directory);
if (ret == -1)
commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found");
return ret;
}
static int handlePing(mpd_unused int fd, mpd_unused int *permission,
@ -1175,7 +1207,14 @@ static int handlePlaylistAdd(int fd, mpd_unused int *permission,
if (isRemoteUrl(path))
result = addToStoredPlaylist(path, playlist);
else
result = addAllInToStoredPlaylist(fd, path, playlist);
result = addAllInToStoredPlaylist(path, playlist);
if (result == (enum playlist_result)-1) {
commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found");
return -1;
}
return print_playlist_result(fd, result);
}

View File

@ -107,7 +107,7 @@ int searchForSongsIn(int fd, const char *name, int numItems,
data.array.numItems = numItems;
data.array.items = items;
ret = traverseAllIn(fd, name, searchInDirectory, NULL, &data);
ret = traverseAllIn(name, searchInDirectory, NULL, &data);
for (i = 0; i < numItems; i++) {
free(items[i].needle);
@ -139,7 +139,7 @@ int findSongsIn(int fd, const char *name, int numItems, LocateTagItem * items)
data.array.numItems = numItems;
data.array.items = items;
return traverseAllIn(fd, name, findInDirectory, NULL, &data);
return traverseAllIn(name, findInDirectory, NULL, &data);
}
static void printSearchStats(int fd, SearchStats *stats)
@ -173,7 +173,7 @@ int searchStatsForSongsIn(int fd, const char *name, int numItems,
stats.numberOfSongs = 0;
stats.playTime = 0;
ret = traverseAllIn(fd, name, searchStatsInDirectory, NULL, &stats);
ret = traverseAllIn(name, searchStatsInDirectory, NULL, &stats);
if (ret == 0)
printSearchStats(fd, &stats);
@ -182,7 +182,7 @@ int searchStatsForSongsIn(int fd, const char *name, int numItems,
int printAllIn(int fd, const char *name)
{
return traverseAllIn(fd, name, printSongInDirectory,
return traverseAllIn(name, printSongInDirectory,
printDirectoryInDirectory, (void*)(size_t)fd);
}
@ -204,18 +204,18 @@ static int directoryAddSongToStoredPlaylist(Song *song, void *_data)
return 0;
}
int addAllIn(int fd, const char *name)
int addAllIn(const char *name)
{
return traverseAllIn(fd, name, directoryAddSongToPlaylist, NULL, NULL);
return traverseAllIn(name, directoryAddSongToPlaylist, NULL, NULL);
}
int addAllInToStoredPlaylist(int fd, const char *name, const char *utf8file)
int addAllInToStoredPlaylist(const char *name, const char *utf8file)
{
struct add_data data = {
.path = utf8file,
};
return traverseAllIn(fd, name, directoryAddSongToStoredPlaylist, NULL,
return traverseAllIn(name, directoryAddSongToStoredPlaylist, NULL,
&data);
}
@ -238,26 +238,26 @@ static int sumSongTime(Song * song, void *data)
int printInfoForAllIn(int fd, const char *name)
{
return traverseAllIn(fd, name, directoryPrintSongInfo,
return traverseAllIn(name, directoryPrintSongInfo,
printDirectoryInDirectory, (void*)(size_t)fd);
}
int countSongsIn(int fd, const char *name)
int countSongsIn(const char *name)
{
int count = 0;
void *ptr = (void *)&count;
traverseAllIn(fd, name, NULL, countSongsInDirectory, ptr);
traverseAllIn(name, NULL, countSongsInDirectory, ptr);
return count;
}
unsigned long sumSongTimesIn(int fd, const char *name)
unsigned long sumSongTimesIn(const char *name)
{
unsigned long dbPlayTime = 0;
void *ptr = (void *)&dbPlayTime;
traverseAllIn(fd, name, sumSongTime, NULL, ptr);
traverseAllIn(name, sumSongTime, NULL, ptr);
return dbPlayTime;
}
@ -332,7 +332,7 @@ int listAllUniqueTags(int fd, int type, int numConditionals,
resetVisitedFlagsInTagTracker(type);
}
ret = traverseAllIn(fd, NULL, listUniqueTagsInDirectory, NULL,
ret = traverseAllIn(NULL, listUniqueTagsInDirectory, NULL,
&data);
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
@ -370,7 +370,7 @@ void printSavedMemoryFromFilenames(void)
{
int sum = 0;
traverseAllIn(STDERR_FILENO, NULL, sumSavedFilenameMemoryInSong,
traverseAllIn(NULL, sumSavedFilenameMemoryInSong,
sumSavedFilenameMemoryInDirectory, (void *)&sum);
DEBUG("saved memory from filenames: %i\n", sum);

View File

@ -23,9 +23,9 @@
int printAllIn(int fd, const char *name);
int addAllIn(int fd, const char *name);
int addAllIn(const char *name);
int addAllInToStoredPlaylist(int fd, const char *name, const char *utf8file);
int addAllInToStoredPlaylist(const char *name, const char *utf8file);
int printInfoForAllIn(int fd, const char *name);
@ -37,9 +37,9 @@ int findSongsIn(int fd, const char *name, int numItems, LocateTagItem * items);
int searchStatsForSongsIn(int fd, const char *name, int numItems,
LocateTagItem * items);
int countSongsIn(int fd, const char *name);
int countSongsIn(const char *name);
unsigned long sumSongTimesIn(int fd, const char *name);
unsigned long sumSongTimesIn(const char *name);
int listAllUniqueTags(int fd, int type, int numConditiionals,
LocateTagItem * conditionals);

View File

@ -1155,8 +1155,8 @@ int readDirectoryDB(void)
readDirectoryInfo(fp, mp3rootDirectory);
while (fclose(fp) && errno == EINTR) ;
stats.numberOfSongs = countSongsIn(STDERR_FILENO, NULL);
stats.dbPlayTime = sumSongTimesIn(STDERR_FILENO, NULL);
stats.numberOfSongs = countSongsIn(NULL);
stats.dbPlayTime = sumSongTimesIn(NULL);
if (stat(dbFile, &st) == 0)
directory_dbModTime = st.st_mtime;
@ -1220,7 +1220,7 @@ static int traverseAllInSubDirectory(Directory * directory,
return errFlag;
}
int traverseAllIn(int fd, const char *name,
int traverseAllIn(const char *name,
int (*forEachSong) (Song *, void *),
int (*forEachDir) (Directory *, void *), void *data)
{
@ -1231,8 +1231,6 @@ int traverseAllIn(int fd, const char *name,
if ((song = getSongFromDB(name)) && forEachSong) {
return forEachSong(song, data);
}
commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found");
return -1;
}
@ -1257,8 +1255,8 @@ void initMp3Directory(void)
mp3rootDirectory = newDirectory(NULL, NULL);
exploreDirectory(mp3rootDirectory);
freeAllDirectoryStats(mp3rootDirectory);
stats.numberOfSongs = countSongsIn(STDERR_FILENO, NULL);
stats.dbPlayTime = sumSongTimesIn(STDERR_FILENO, NULL);
stats.numberOfSongs = countSongsIn(NULL);
stats.dbPlayTime = sumSongTimesIn(NULL);
}
static Song *getSongDetails(const char *file, const char **shortnameRet,

View File

@ -64,7 +64,7 @@ Song *getSongFromDB(const char *file);
time_t getDbModTime(void);
int traverseAllIn(int fd, const char *name,
int traverseAllIn(const char *name,
int (*forEachSong) (Song *, void *),
int (*forEachDir) (Directory *, void *), void *data);

View File

@ -60,7 +60,7 @@ int getNumberOfTagItems(int type)
resetVisitedFlagsInTagTracker(type);
traverseAllIn(-1, NULL, visit_tag_items, NULL, (void*)(size_t)type);
traverseAllIn(NULL, visit_tag_items, NULL, (void*)(size_t)type);
ret = (int)num_visited[type];
resetVisitedFlagsInTagTracker(type);