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>
16 #include "server.cmdline.h"
20 #include <dirent.h> /* readdir() */
21 #include <sys/stat.h> /* stat */
22 #include <sys/types.h> /* mode_t */
27 * Traverse the given directory recursively.
29 * \param dirname The directory to traverse.
30 * \param f The function to call for each entry.
32 * For each regular file whose filename ends in .yyy, where yyy is a supported
33 * audio format, the supplied function \a f is called. The directory and
34 * filename component of the regular file are passed to \a f.
36 * \return On success, 1 is returned. Otherwise, this function returns a
37 * negative value which indicates the kind of the error.
39 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
42 int ret, ret2, cwd_fd;
45 ret = para_opendir(dirname, &dir, &cwd_fd);
48 /* scan cwd recursively */
49 while ((entry = readdir(dir))) {
54 if (!strcmp(entry->d_name, "."))
56 if (!strcmp(entry->d_name, ".."))
59 if (lstat(entry->d_name, &s) == -1)
62 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
64 if (S_ISREG(m)) { /* regular file */
65 if (guess_audio_format(entry->d_name) < 0)
67 ret = f(dirname, entry->d_name);
73 tmp = make_message("%s/%s", dirname, entry->d_name);
74 ret = find_audio_files(tmp, f);
83 ret2 = fchdir(cwd_fd);
84 if (ret2 < 0 && ret >= 0)
88 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));