Add afs.cmd.
[paraslash.git] / afs_common.c
1 /*
2  * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7
8 /** \file afs_common.c Functions common to all audio file selectors. */
9
10 #include "para.h"
11 #include "server.cmdline.h"
12 #include "afh.h"
13 #include "server.h"
14 #include "vss.h"
15 #include <dirent.h> /* readdir() */
16 #include <sys/stat.h> /* stat */
17 #include <sys/types.h> /* mode_t */
18 #include "error.h"
19 #include "string.h"
20
21 /**
22  * Traverse the given directory recursively.
23  *
24  * \param dirname The directory to traverse.
25  * \param f The function to call for each entry.
26  *
27  * For each regular file whose filename ends in .yyy, where yyy is a supported
28  * audio format, the supplied function \a f is called.  The directory and
29  * filename component of the regular file are passed to \a f.
30  *
31  * \return On success, 1 is returned. Otherwise, this function returns a
32  * negative value which indicates the kind of the error.
33  */
34 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
35 {
36         DIR *dir = NULL;
37         struct dirent *entry;
38         /*
39          * Opening the current directory (".") and calling fchdir() to return
40          * is usually faster and more reliable than saving cwd in some buffer
41          * and calling chdir() afterwards (see man 3 getcwd).
42          */
43         int cwd_fd = open(".", O_RDONLY);
44         struct stat s;
45         int ret;
46
47         if (cwd_fd < 0)
48                 return -E_GETCWD;
49         ret = -E_CHDIR;
50         if (chdir(dirname) < 0)
51                 goto out;
52         ret = -E_OPENDIR;
53         dir = opendir(".");
54         if (!dir)
55                 goto out;
56         /* scan cwd recursively */
57         while ((entry = readdir(dir))) {
58                 mode_t m;
59                 char *tmp;
60
61                 if (!strcmp(entry->d_name, "."))
62                         continue;
63                 if (!strcmp(entry->d_name, ".."))
64                         continue;
65                 ret = -E_LSTAT;
66                 if (lstat(entry->d_name, &s) == -1)
67                         continue;
68                 m = s.st_mode;
69                 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
70                         continue;
71                 if (S_ISREG(m)) { /* regular file */
72                         if (guess_audio_format(entry->d_name) < 0)
73                                 continue;
74                         ret = f(dirname, entry->d_name);
75                         if (ret < 0)
76                                 goto out;
77                         continue;
78                 }
79                 /* directory */
80                 tmp = make_message("%s/%s", dirname, entry->d_name);
81                 ret = find_audio_files(tmp, f);
82                 free(tmp);
83                 if (ret < 0)
84                         goto out;
85         }
86         ret = 1;
87 out:
88         if (dir)
89                 closedir(dir);
90         if (fchdir(cwd_fd) < 0)
91                 ret = -E_CHDIR;
92         close(cwd_fd);
93         if (ret < 0)
94                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
95         return ret;
96 }