chiark / gitweb /
Merge from disorder.4.0
[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 2 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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <syslog.h>
28 #include <errno.h>
29 #include <dirent.h>
30 #include <stdio.h>
31 #include <unistd.h>
32
33 #include <disorder.h>
34
35 void disorder_scan(const char *path) {
36   struct stat sb;
37   DIR *dp;
38   struct dirent *de;
39   char *np;
40
41   if(stat(path, &sb) < 0) {
42     disorder_error(errno, "cannot stat %s", path);
43     return;
44   }
45   if(S_ISDIR(sb.st_mode)) {
46     if(!(dp = opendir(path))) {
47       disorder_error(errno, "cannot open directory %s", path);
48       return;
49     }
50     while((errno = 0),
51           (de = readdir(dp))) {
52       if(de->d_name[0] != '.') {
53         disorder_asprintf(&np, "%s/%s", path, de->d_name);
54         disorder_scan(np);
55       }
56     }
57     if(errno)
58       disorder_error(errno, "error reading directory %s", path);
59     closedir(dp);
60   } else if(S_ISREG(sb.st_mode)) {
61     if(access(path, R_OK) < 0) {
62       disorder_error(errno, "cannot access file %s", path);
63       return;
64     }
65     if(printf("%s%c", path, 0) < 0)
66       disorder_fatal(errno, "error writing to scanner output pipe");
67   }
68 }
69
70 int disorder_check(const char attribute((unused)) *root, const char *path) {
71   if(access(path, R_OK) == 0)
72     return 1;
73   else if(errno == ENOENT)
74     return 0;
75   else {
76     disorder_error(errno, "cannot access %s", path);
77     return -1;
78   }
79 }
80
81 /*
82 Local Variables:
83 c-basic-offset:2
84 comment-column:40
85 End:
86 */