struct audio_format_handler: use proper pointer type for the init function
[paraslash.git] / db.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19
20 /** \file db.c functions common to all audio file selectors */
21
22 #include "server.cmdline.h"
23 #include "server.h"
24 #include "afs.h"
25 #include <dirent.h> /* readdir() */
26 #include <sys/stat.h> /* stat */
27 #include <sys/types.h> /* mode_t */
28 #include "error.h"
29 #include "string.h"
30
31 /*
32 * return 1 if name matches any supported audio format
33 */
34 static int match_audio_file_name(char *name)
35 {
36 int i,j, len = strlen(name);
37
38 FOR_EACH_AUDIO_FORMAT(i) {
39 for (j = 0; afl[i].suffixes[j]; j++) {
40 const char *p = afl[i].suffixes[j];
41 int plen = strlen(p);
42 if (len < plen + 1)
43 continue;
44 if (name[len - plen - 1] != '.')
45 continue;
46 if (strcasecmp(name + len - plen, p))
47 continue;
48 return 1;
49 }
50 }
51 return 0;
52 }
53
54 /**
55 * traverse the given directory recursively
56 *
57 * @param dirname the directory to traverse
58 * @param f: the function to call for each entry.
59 *
60 * for each regular file whose filename ends in .yyy, where yyy is a supported
61 * audio format, the supplied function \a f is called. The directory and
62 * filename component of the regular file are passed to \a f.
63 *
64 * \return On success, 1 is returned. Otherwise, this function returns a
65 * negative value which indicates the kind of the error.
66 */
67 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
68 {
69 DIR *dir = NULL;
70 struct dirent *entry;
71 /*
72 * Opening the current directory (".") and calling fchdir() to return
73 * is usually faster and more reliable than saving cwd in some buffer
74 * and calling chdir() afterwards (see man 3 getcwd).
75 */
76 char cwd_fd = open(".", O_RDONLY);
77 struct stat s;
78 int ret = -1;
79
80 // PARA_DEBUG_LOG("dirname: %s\n", dirname);
81 if (cwd_fd < 0)
82 return -E_GETCWD;
83 ret = -E_CHDIR;
84 if (chdir(dirname) < 0)
85 goto out;
86 ret = -E_OPENDIR;
87 dir = opendir(".");
88 if (!dir)
89 goto out;
90 /* scan cwd recursively */
91 while ((entry = readdir(dir))) {
92 mode_t m;
93 char *tmp;
94
95 if (!strcmp(entry->d_name, "."))
96 continue;
97 if (!strcmp(entry->d_name, ".."))
98 continue;
99 ret = -E_LSTAT;
100 if (lstat(entry->d_name, &s) == -1)
101 goto out;
102 m = s.st_mode;
103 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
104 continue;
105 if (S_ISREG(m)) { /* regular file */
106 if (!match_audio_file_name(entry->d_name))
107 continue;
108 if (f(dirname, entry->d_name) < 0)
109 goto out;
110 continue;
111 }
112 /* directory */
113 tmp = make_message("%s/%s", dirname, entry->d_name);
114 ret = find_audio_files(tmp, f);
115 free(tmp);
116 if (ret < 0)
117 goto out;
118 }
119 ret = 1;
120 out:
121 if (dir)
122 closedir(dir);
123 if (fchdir(cwd_fd) < 0)
124 ret = -E_CHDIR;
125 close(cwd_fd);
126 if (ret < 0)
127 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
128 return ret;
129 }