]> git.tuebingen.mpg.de Git - paraslash.git/blob - afs_common.c
Fix off-by-one bug in chunktable saving.
[paraslash.git] / afs_common.c
1 /*
2  * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7
8 /** \file afs_common.c Functions common to all audio file selectors. */
9
10 #include <sys/types.h>
11 #include <dirent.h>
12
13
14 #include "para.h"
15 #include "error.h"
16 #include "string.h"
17 #include "fd.h"
18 #include "server.cmdline.h"
19 #include "afh.h"
20 #include "afs.h"
21 #include "server.h"
22 #include "vss.h"
23 #include <dirent.h> /* readdir() */
24 #include <sys/stat.h> /* stat */
25 #include <sys/types.h> /* mode_t */
26
27 /**
28  * Traverse the given directory recursively.
29  *
30  * \param dirname The directory to traverse.
31  * \param f The function to call for each entry.
32  *
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.
36  *
37  * \return On success, 1 is returned. Otherwise, this function returns a
38  * negative value which indicates the kind of the error.
39  */
40 int find_audio_files(const char *dirname, int (*f)(const char *, const char *))
41 {
42         DIR *dir = NULL;
43         int ret, ret2, cwd_fd;
44         struct dirent *entry;
45
46         ret = para_opendir(dirname, &dir, &cwd_fd);
47         if (ret < 0)
48                 return ret;
49         /* scan cwd recursively */
50         while ((entry = readdir(dir))) {
51                 mode_t m;
52                 char *tmp;
53                 struct stat s;
54
55                 if (!strcmp(entry->d_name, "."))
56                         continue;
57                 if (!strcmp(entry->d_name, ".."))
58                         continue;
59                 ret = -E_LSTAT;
60                 if (lstat(entry->d_name, &s) == -1)
61                         continue;
62                 m = s.st_mode;
63                 if (!S_ISREG(m) && !S_ISDIR(m)) /* skip links, sockets, ... */
64                         continue;
65                 if (S_ISREG(m)) { /* regular file */
66                         if (guess_audio_format(entry->d_name) < 0)
67                                 continue;
68                         ret = f(dirname, entry->d_name);
69                         if (ret < 0)
70                                 goto out;
71                         continue;
72                 }
73                 /* directory */
74                 tmp = make_message("%s/%s", dirname, entry->d_name);
75                 ret = find_audio_files(tmp, f);
76                 free(tmp);
77                 if (ret < 0)
78                         goto out;
79         }
80         ret = 1;
81 out:
82         if (dir)
83                 closedir(dir);
84         ret2 = fchdir(cwd_fd);
85         if (ret2 < 0 && ret >= 0)
86                 ret = ret2;
87         close(cwd_fd);
88         if (ret < 0)
89                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
90         return ret;
91 }