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 <sys/types.h>
18 #include "server.cmdline.h"
23 #include <dirent.h> /* readdir() */
24 #include <sys/stat.h> /* stat */
25 #include <sys/types.h> /* mode_t */
28 * Traverse the given directory recursively.
30 * \param dirname The directory to traverse.
31 * \param f The function to call for each entry.
33 * For each regular file whose filename ends in .yyy, where yyy is a supported
34 * audio format, the supplied function \a f is called. The directory and
35 * filename component of the regular file are passed to \a f.
37 * \return On success, 1 is returned. Otherwise, this function returns a
38 * negative value which indicates the kind of the error.
40 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
43 int ret, ret2, cwd_fd;
46 ret = para_opendir(dirname, &dir, &cwd_fd);
49 /* scan cwd recursively */
50 while ((entry = readdir(dir))) {
55 if (!strcmp(entry->d_name, "."))
57 if (!strcmp(entry->d_name, ".."))
60 if (lstat(entry->d_name, &s) == -1)
63 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
65 if (S_ISREG(m)) { /* regular file */
66 if (guess_audio_format(entry->d_name) < 0)
68 ret = f(dirname, entry->d_name);
74 tmp = make_message("%s/%s", dirname, entry->d_name);
75 ret = find_audio_files(tmp, f);
84 ret2 = fchdir(cwd_fd);
85 if (ret2 < 0 && ret >= 0)
89 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));