2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
8 /** \file afs_common.c Functions common to all audio file selectors. */
10 #include "server.cmdline.h"
13 #include <dirent.h> /* readdir() */
14 #include <sys/stat.h> /* stat */
15 #include <sys/types.h> /* mode_t */
20 * Traverse the given directory recursively.
22 * \param dirname The directory to traverse.
23 * \param f The function to call for each entry.
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.
29 * \return On success, 1 is returned. Otherwise, this function returns a
30 * negative value which indicates the kind of the error.
32 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
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).
41 int cwd_fd = open(".", O_RDONLY);
48 if (chdir(dirname) < 0)
54 /* scan cwd recursively */
55 while ((entry = readdir(dir))) {
59 if (!strcmp(entry->d_name, "."))
61 if (!strcmp(entry->d_name, ".."))
64 if (lstat(entry->d_name, &s) == -1)
67 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
69 if (S_ISREG(m)) { /* regular file */
70 if (guess_audio_format(entry->d_name) < 0)
72 ret = f(dirname, entry->d_name);
78 tmp = make_message("%s/%s", dirname, entry->d_name);
79 ret = find_audio_files(tmp, f);
88 if (fchdir(cwd_fd) < 0)
92 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));