2 * Copyright (C) 2005-2007 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 afs.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 * traverse the given directory recursively
34 * @param dirname the directory to traverse
35 * @param f: the function to call for each entry.
37 * for each regular file whose filename ends in .yyy, where yyy is a supported
38 * audio format, the supplied function \a f is called. The directory and
39 * filename component of the regular file are passed to \a f.
41 * \return On success, 1 is returned. Otherwise, this function returns a
42 * negative value which indicates the kind of the error.
44 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
49 * Opening the current directory (".") and calling fchdir() to return
50 * is usually faster and more reliable than saving cwd in some buffer
51 * and calling chdir() afterwards (see man 3 getcwd).
53 int cwd_fd = open(".", O_RDONLY);
57 // PARA_DEBUG_LOG("dirname: %s\n", dirname);
61 if (chdir(dirname) < 0)
67 /* scan cwd recursively */
68 while ((entry = readdir(dir))) {
72 if (!strcmp(entry->d_name, "."))
74 if (!strcmp(entry->d_name, ".."))
77 if (lstat(entry->d_name, &s) == -1)
80 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
82 if (S_ISREG(m)) { /* regular file */
83 if (guess_audio_format(entry->d_name) < 0)
85 ret = f(dirname, entry->d_name);
91 tmp = make_message("%s/%s", dirname, entry->d_name);
92 ret = find_audio_files(tmp, f);
101 if (fchdir(cwd_fd) < 0)
105 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));