3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "wrappers-dirent.h"
28 #include <lib/vserver.h>
35 struct Arguments const * global_args = 0;
37 int wrapper_exit_code = 1;
40 checkForRace(int fd, char const * name, struct stat const *exp_st)
44 if (fstat(fd, &st)==-1) {
49 if (st.st_dev != exp_st->st_dev ||
50 st.st_ino != exp_st->st_ino ||
51 st.st_mode != exp_st->st_mode) {
53 WRITE_MSG(2, "race while visiting '");
63 isSpecialDir(char const *d)
65 return (d[0]=='.' && (d[1]=='\0' || (d[1]=='.' && d[2]=='\0')));
68 #define CONCAT_PATHS(LHS, LHS_LEN, RHS) \
69 size_t l_rhs = strlen(RHS); \
70 char new_path[(LHS_LEN) + l_rhs + sizeof("/")]; \
71 memcpy(new_path, LHS, (LHS_LEN)); \
72 memcpy(new_path+(LHS_LEN), "/", 1); \
73 memcpy(new_path+(LHS_LEN)+1, RHS, l_rhs); \
74 new_path[(LHS_LEN)+1+l_rhs] = '\0';
77 iterateFilesystem(char const *path)
79 bool do_again = false;
80 size_t path_len = strlen(path);
81 DIR * dir = Eopendir(".");
86 if (lstat(".", &st)==-1) perror("lstat()");
87 else err += handleFile(".", path, &st) ? 0 : 1;
91 struct dirent *ent = Ereaddir(dir);
95 if (isSpecialDir(ent->d_name)) continue;
97 if (lstat(ent->d_name, &st)==-1) {
103 if (S_ISDIR(st.st_mode) && global_args->do_recurse) {
109 CONCAT_PATHS(path, path_len, ent->d_name);
110 err += handleFile(ent->d_name, new_path, &st) ? 0 : 1;
115 int cur_dir = Eopen(".", O_RDONLY, 0);
119 struct dirent *ent = Ereaddir(dir);
123 if (isSpecialDir(ent->d_name)) continue;
125 if (lstat(ent->d_name, &st)==-1) {
131 if (!S_ISDIR(st.st_mode)) continue;
132 safeChdir(ent->d_name, &st);
134 CONCAT_PATHS(path, path_len, ent->d_name);
135 err += iterateFilesystem(new_path);
149 processFile(char const *path)
153 if (lstat(path, &st)==-1) {
158 if (S_ISDIR(st.st_mode) && !global_args->do_display_dir)
159 return iterateFilesystem(path);
161 return handleFile(path, path, &st);
164 int main(int argc, char *argv[])
166 uint64_t err_cnt = 0;
168 struct Arguments args = {
170 .do_display_dot = false,
171 .do_display_dir = false,
183 int c = getopt_long(argc, argv, CMDLINE_OPTIONS_SHORT,
188 case CMD_HELP : showHelp(1, argv[0], 0);
189 case CMD_VERSION : showVersion();
190 case CMD_IMMUTABLE : args.immutable = true; break;
191 case CMD_IMMULINK : args.immulink = true; break;
192 case CMD_LEGACY : args.is_legacy = true; break;
193 case 'R' : args.do_recurse = true; break;
194 case 'a' : args.do_display_dot = true; break;
195 case 'd' : args.do_display_dir = true; break;
196 case 'n' : args.do_mapping = false; break;
197 case 's' : args.do_set = true; break;
198 case 'u' : args.do_unset = true; break;
199 case 'c' : args.ctx_str = optarg; break;
201 WRITE_MSG(2, "Try '");
202 WRITE_STR(2, argv[0]);
203 WRITE_MSG(2, " --help\" for more information.\n");
209 fixupParams(&args, argc);
212 err_cnt = processFile(".");
213 else for (i=optind; i<argc; ++i)
214 err_cnt += processFile(argv[i]);
216 return err_cnt>0 ? EXIT_FAILURE : EXIT_SUCCESS;