Get rid of E_SIGNAL_PIPE.
[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 <sys/types.h>
11 #include <dirent.h>
12
13
14 #include "para.h"
15 #include "fd.h"
16 #include "server.cmdline.h"
17 #include "afh.h"
18 #include "server.h"
19 #include "vss.h"
20 #include <dirent.h> /* readdir() */
21 #include <sys/stat.h> /* stat */
22 #include <sys/types.h> /* mode_t */
23 #include "error.h"
24 #include "string.h"
25
26 /**
27  * Traverse the given directory recursively.
28  *
29  * \param dirname The directory to traverse.
30  * \param f The function to call for each entry.
31  *
32  * For each regular file whose filename ends in .yyy, where yyy is a supported
33  * audio format, the supplied function \a f is called.  The directory and
34  * filename component of the regular file are passed to \a f.
35  *
36  * \return On success, 1 is returned. Otherwise, this function returns a
37  * negative value which indicates the kind of the error.
38  */
39 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
40 {
41         DIR *dir = NULL;
42         int ret, ret2, cwd_fd;
43         struct dirent *entry;
44
45         ret = para_opendir(dirname, &dir, &cwd_fd);
46         if (ret < 0)
47                 return ret;
48         /* scan cwd recursively */
49         while ((entry = readdir(dir))) {
50                 mode_t m;
51                 char *tmp;
52                 struct stat s;
53
54                 if (!strcmp(entry->d_name, "."))
55                         continue;
56                 if (!strcmp(entry->d_name, ".."))
57                         continue;
58                 ret = -E_LSTAT;
59                 if (lstat(entry->d_name, &s) == -1)
60                         continue;
61                 m = s.st_mode;
62                 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
63                         continue;
64                 if (S_ISREG(m)) { /* regular file */
65                         if (guess_audio_format(entry->d_name) < 0)
66                                 continue;
67                         ret = f(dirname, entry->d_name);
68                         if (ret < 0)
69                                 goto out;
70                         continue;
71                 }
72                 /* directory */
73                 tmp = make_message("%s/%s", dirname, entry->d_name);
74                 ret = find_audio_files(tmp, f);
75                 free(tmp);
76                 if (ret < 0)
77                         goto out;
78         }
79         ret = 1;
80 out:
81         if (dir)
82                 closedir(dir);
83         ret2 = fchdir(cwd_fd);
84         if (ret2 < 0 && ret >= 0)
85                 ret = ret2;
86         close(cwd_fd);
87         if (ret < 0)
88                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
89         return ret;
90 }