]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - afs_common.c
make get_playlist_data() generic.
[paraslash.git] / afs_common.c
index 72be6a9da47223b2849b28c97c1427a7cb22df59..17b4e16ff8f43c0b075d0f949bb3a0c7f35a1809 100644 (file)
@@ -7,7 +7,12 @@
 
 /** \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;
@@ -87,8 +80,9 @@ int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
 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));