460b9539 |
1 | /* |
2 | * This file is part of DisOrder. |
3 | * Copyright (C) 2004 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 lstat %s", path); |
43 | return; |
44 | } |
45 | /* skip files that aren't world-readable */ |
46 | if(!(sb.st_mode & 0004)) |
47 | return; |
48 | if(S_ISDIR(sb.st_mode)) { |
49 | if(!(dp = opendir(path))) { |
50 | disorder_error(errno, "cannot open directory %s", path); |
51 | return; |
52 | } |
53 | while((errno = 0), |
54 | (de = readdir(dp))) { |
55 | if(de->d_name[0] != '.') { |
56 | disorder_asprintf(&np, "%s/%s", path, de->d_name); |
57 | disorder_scan(np); |
58 | } |
59 | } |
60 | if(errno) |
61 | disorder_error(errno, "error reading directory %s", path); |
62 | closedir(dp); |
63 | } else if(S_ISREG(sb.st_mode)) |
64 | if(printf("%s%c", path, 0) < 0) |
65 | disorder_fatal(errno, "error writing to scanner output pipe"); |
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 | */ |