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. */
11 #include "server.cmdline.h"
15 #include <dirent.h> /* readdir() */
16 #include <sys/stat.h> /* stat */
17 #include <sys/types.h> /* mode_t */
22 * Traverse the given directory recursively.
24 * \param dirname The directory to traverse.
25 * \param f The function to call for each entry.
27 * For each regular file whose filename ends in .yyy, where yyy is a supported
28 * audio format, the supplied function \a f is called. The directory and
29 * filename component of the regular file are passed to \a f.
31 * \return On success, 1 is returned. Otherwise, this function returns a
32 * negative value which indicates the kind of the error.
34 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
39 * Opening the current directory (".") and calling fchdir() to return
40 * is usually faster and more reliable than saving cwd in some buffer
41 * and calling chdir() afterwards (see man 3 getcwd).
43 int cwd_fd = open(".", O_RDONLY);
50 if (chdir(dirname) < 0)
56 /* scan cwd recursively */
57 while ((entry = readdir(dir))) {
61 if (!strcmp(entry->d_name, "."))
63 if (!strcmp(entry->d_name, ".."))
66 if (lstat(entry->d_name, &s) == -1)
69 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
71 if (S_ISREG(m)) { /* regular file */
72 if (guess_audio_format(entry->d_name) < 0)
74 ret = f(dirname, entry->d_name);
80 tmp = make_message("%s/%s", dirname, entry->d_name);
81 ret = find_audio_files(tmp, f);
90 if (fchdir(cwd_fd) < 0)
94 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));