chiark / gitweb /
mention gpl3 in README.developers
[disorder] / plugins / fs.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2007, 2008 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <config.h>
20
21 #include <string.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <syslog.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <stdio.h>
29 #include <unistd.h>
30
31 #include <disorder.h>
32
33 void disorder_scan(const char *path) {
34   struct stat sb;
35   DIR *dp;
36   struct dirent *de;
37   char *np;
38
39   if(stat(path, &sb) < 0) {
40     disorder_error(errno, "cannot stat %s", path);
41     return;
42   }
43   if(S_ISDIR(sb.st_mode)) {
44     if(!(dp = opendir(path))) {
45       disorder_error(errno, "cannot open directory %s", path);
46       return;
47     }
48     while((errno = 0),
49           (de = readdir(dp))) {
50       if(de->d_name[0] != '.') {
51         disorder_asprintf(&np, "%s/%s", path, de->d_name);
52         disorder_scan(np);
53       }
54     }
55     if(errno)
56       disorder_error(errno, "error reading directory %s", path);
57     closedir(dp);
58   } else if(S_ISREG(sb.st_mode)) {
59     if(access(path, R_OK) < 0) {
60       disorder_error(errno, "cannot access file %s", path);
61       return;
62     }
63     if(printf("%s%c", path, 0) < 0)
64       disorder_fatal(errno, "error writing to scanner output pipe");
65   }
66 }
67
68 int disorder_check(const char attribute((unused)) *root, const char *path) {
69   if(access(path, R_OK) == 0)
70     return 1;
71   else if(errno == ENOENT)
72     return 0;
73   else {
74     disorder_error(errno, "cannot access %s", path);
75     return -1;
76   }
77 }
78
79 /*
80 Local Variables:
81 c-basic-offset:2
82 comment-column:40
83 End:
84 */