2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
20 /** \file db.c functions common to all audio file selectors */
22 #include "server.cmdline.h"
25 #include <dirent.h> /* readdir() */
26 #include <sys/stat.h> /* stat */
27 #include <sys/types.h> /* mode_t */
32 * return 1 if name matches any supported audio format
34 static int match_audio_file_name(char *name)
36 int i, len = strlen(name);
37 const char *pattern[] = {SUPPORTED_AUDIO_FORMATS_ARRAY};
39 for (i = 0; pattern[i]; i++) {
40 const char *p = pattern[i];
44 if (name[len - plen - 1] != '.')
46 if (strcasecmp(name + len - plen, p))
54 * traverse the given directory recursively
56 * @param dirname the directory to traverse
57 * @param f: the function to call for each entry.
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.
63 * \return On success, 1 is returned. Otherwise, this function returns a
64 * negative value which indicates the kind of the error.
66 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
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).
75 char cwd_fd = open(".", O_RDONLY);
79 // PARA_DEBUG_LOG("dirname: %s\n", dirname);
83 if (chdir(dirname) < 0)
89 /* scan cwd recursively */
90 while ((entry = readdir(dir))) {
94 if (!strcmp(entry->d_name, "."))
96 if (!strcmp(entry->d_name, ".."))
99 if (lstat(entry->d_name, &s) == -1)
102 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
104 if (S_ISREG(m)) { /* regular file */
105 if (!match_audio_file_name(entry->d_name))
107 if (f(dirname, entry->d_name) < 0)
112 tmp = make_message("%s/%s", dirname, entry->d_name);
113 ret = find_audio_files(tmp, f);
122 if (fchdir(cwd_fd) < 0)
126 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));