/** \file afs_common.c Functions common to all audio file selectors. */
+#include <sys/types.h>
+#include <dirent.h>
+
+
#include "para.h"
+#include "fd.h"
#include "server.cmdline.h"
#include "afh.h"
#include "server.h"
int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
{
DIR *dir = NULL;
+ int ret, ret2, cwd_fd;
struct dirent *entry;
- /*
- * Opening the current directory (".") and calling fchdir() to return
- * is usually faster and more reliable than saving cwd in some buffer
- * and calling chdir() afterwards (see man 3 getcwd).
- */
- int cwd_fd = open(".", O_RDONLY);
- struct stat s;
- int ret;
- if (cwd_fd < 0)
- return -E_GETCWD;
- ret = -E_CHDIR;
- if (chdir(dirname) < 0)
- goto out;
- ret = -E_OPENDIR;
- dir = opendir(".");
- if (!dir)
- goto out;
+ ret = para_opendir(dirname, &dir, &cwd_fd);
+ if (ret < 0)
+ return ret;
/* scan cwd recursively */
while ((entry = readdir(dir))) {
mode_t m;
char *tmp;
+ struct stat s;
if (!strcmp(entry->d_name, "."))
continue;
out:
if (dir)
closedir(dir);
- if (fchdir(cwd_fd) < 0)
- ret = -E_CHDIR;
+ ret2 = fchdir(cwd_fd);
+ if (ret2 < 0 && ret >= 0)
+ ret = ret2;
close(cwd_fd);
if (ret < 0)
PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));