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