Merge /fml/ag-raetsch/home/maan/scm/paraslash_meins/paraslash
[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 = -1;
44
45 // PARA_DEBUG_LOG("dirname: %s\n", dirname);
46 if (cwd_fd < 0)
47 return -E_GETCWD;
48 ret = -E_CHDIR;
49 if (chdir(dirname) < 0)
50 goto out;
51 ret = -E_OPENDIR;
52 dir = opendir(".");
53 if (!dir)
54 goto out;
55 /* scan cwd recursively */
56 while ((entry = readdir(dir))) {
57 mode_t m;
58 char *tmp;
59
60 if (!strcmp(entry->d_name, "."))
61 continue;
62 if (!strcmp(entry->d_name, ".."))
63 continue;
64 ret = -E_LSTAT;
65 if (lstat(entry->d_name, &s) == -1)
66 continue;
67 m = s.st_mode;
68 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
69 continue;
70 if (S_ISREG(m)) { /* regular file */
71 if (guess_audio_format(entry->d_name) < 0)
72 continue;
73 ret = f(dirname, entry->d_name);
74 if (ret < 0)
75 goto out;
76 continue;
77 }
78 /* directory */
79 tmp = make_message("%s/%s", dirname, entry->d_name);
80 ret = find_audio_files(tmp, f);
81 free(tmp);
82 if (ret < 0)
83 goto out;
84 }
85 ret = 1;
86 out:
87 if (dir)
88 closedir(dir);
89 if (fchdir(cwd_fd) < 0)
90 ret = -E_CHDIR;
91 close(cwd_fd);
92 if (ret < 0)
93 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
94 return ret;
95 }