Makefile.in: add tow missing headers for the tarball
[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, len = strlen(name);
37         const char *pattern[] = {SUPPORTED_AUDIO_FORMATS_ARRAY};
38
39         for (i = 0; pattern[i]; i++) {
40                 const char *p = pattern[i];
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         return 0;
51 }
52
53 /**
54  * traverse the given directory recursively
55  *
56  * @param dirname the directory to traverse
57  * @param f: the function to call for each entry.
58  *
59  * for each regular file whose filename ends in .yyy, where yyy is a supported
60  * audio format, the supplied function \a f is called.  The directory and
61  * filename component of the regular file are passed to \a f.
62  *
63  * \return On success, 1 is returned. Otherwise, this function returns a
64  * negative value which indicates the kind of the error.
65  */
66 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
67 {
68         DIR *dir = NULL;
69         struct dirent *entry;
70         /*
71          * Opening the current directory (".") and calling fchdir() to return
72          * is usually faster and more reliable than saving cwd in some buffer
73          * and calling chdir() afterwards (see man 3 getcwd).
74          */
75         char cwd_fd = open(".", O_RDONLY);
76         struct stat s;
77         int ret = -1;
78
79 //      PARA_DEBUG_LOG("dirname: %s\n", dirname);
80         if (cwd_fd < 0)
81                 return -E_GETCWD;
82         ret = -E_CHDIR;
83         if (chdir(dirname) < 0)
84                 goto out;
85         ret = -E_OPENDIR;
86         dir = opendir(".");
87         if (!dir)
88                 goto out;
89         /* scan cwd recursively */
90         while ((entry = readdir(dir))) {
91                 mode_t m;
92                 char *tmp;
93
94                 if (!strcmp(entry->d_name, "."))
95                         continue;
96                 if (!strcmp(entry->d_name, ".."))
97                         continue;
98                 ret = -E_LSTAT;
99                 if (lstat(entry->d_name, &s) == -1)
100                         goto out;
101                 m = s.st_mode;
102                 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
103                         continue;
104                 if (S_ISREG(m)) { /* regular file */
105                         if (!match_audio_file_name(entry->d_name))
106                                 continue;
107                         if (f(dirname, entry->d_name) < 0)
108                                 goto out;
109                         continue;
110                 }
111                 /* directory */
112                 tmp = make_message("%s/%s", dirname, entry->d_name);
113                 ret = find_audio_files(tmp, f);
114                 free(tmp);
115                 if (ret < 0)
116                         goto out;
117         }
118         ret = 1;
119 out:
120         if (dir)
121                 closedir(dir);
122         if (fchdir(cwd_fd) < 0)
123                 ret = -E_CHDIR;
124         close(cwd_fd);
125         if (ret < 0)
126                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
127         return ret;
128 }