2b7b9ee9edc54f4b90a9ff85483eb573145df924
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
8 /** \file afs.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
);
45 // PARA_DEBUG_LOG("dirname: %s\n", dirname);
49 if (chdir(dirname
) < 0)
55 /* scan cwd recursively */
56 while ((entry
= readdir(dir
))) {
60 if (!strcmp(entry
->d_name
, "."))
62 if (!strcmp(entry
->d_name
, ".."))
65 if (lstat(entry
->d_name
, &s
) == -1)
68 if (!S_ISREG(m
) && !S_ISDIR(m
)) /* skip links, sockets, ... */
70 if (S_ISREG(m
)) { /* regular file */
71 if (guess_audio_format(entry
->d_name
) < 0)
73 ret
= f(dirname
, entry
->d_name
);
79 tmp
= make_message("%s/%s", dirname
, entry
->d_name
);
80 ret
= find_audio_files(tmp
, f
);
89 if (fchdir(cwd_fd
) < 0)
93 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));