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]=='.' && !global_args->do_display_dot) ||
66 (d[0]=='.' && (d[1]=='\0' || (d[1]=='.' && d[2]=='\0'))) );
69 #define CONCAT_PATHS(LHS, LHS_LEN, RHS) \
70 size_t l_rhs = strlen(RHS); \
71 char new_path[(LHS_LEN) + l_rhs + sizeof("/")]; \
72 memcpy(new_path, LHS, (LHS_LEN)); \
73 memcpy(new_path+(LHS_LEN), "/", 1); \
74 memcpy(new_path+(LHS_LEN)+1, RHS, l_rhs); \
75 new_path[(LHS_LEN)+1+l_rhs] = '\0';
78 iterateFilesystem(char const *path)
80 bool do_again = false;
81 size_t path_len = strlen(path);
84 DIR * dir = opendir(".");
91 // show current directory entry first
92 if (lstat(".", &cur_st)==-1) perror("lstat()");
93 else err += handleFile(".", path, &cur_st) ? 0 : 1;
96 while (path_len>0 && path[path_len-1]=='/') --path_len;
98 // process regular files before directories
100 struct dirent *ent = Ereaddir(dir);
104 if (isSpecialDir(ent->d_name)) continue;
106 if (lstat(ent->d_name, &st)==-1) {
112 if (S_ISDIR(st.st_mode) && global_args->do_recurse) {
118 CONCAT_PATHS(path, path_len, ent->d_name);
119 err += handleFile(ent->d_name, new_path, &st) ? 0 : 1;
124 int cur_dir = Eopen(".", O_RDONLY, 0);
128 struct dirent *ent = Ereaddir(dir);
132 if (isSpecialDir(ent->d_name)) continue;
134 if (lstat(ent->d_name, &st)==-1) {
140 if (!S_ISDIR(st.st_mode) ||
141 (global_args->local_fs && st.st_dev!=cur_st.st_dev))
144 if (safeChdir(ent->d_name, &st)==-1) {
151 CONCAT_PATHS(path, path_len, ent->d_name);
152 err += iterateFilesystem(new_path);
166 processFile(char const *path)
170 if (lstat(path, &st)==-1) {
175 if (S_ISDIR(st.st_mode) && !global_args->do_display_dir) {
177 return iterateFilesystem(path);
180 return handleFile(path, path, &st);
183 int main(int argc, char *argv[])
185 uint64_t err_cnt = 0;
187 struct Arguments args = {
189 .do_display_dot = false,
190 .do_display_dir = false,
203 int c = getopt_long(argc, argv, CMDLINE_OPTIONS_SHORT,
208 case CMD_HELP : showHelp(1, argv[0], 0);
209 case CMD_VERSION : showVersion();
210 case CMD_IMMUTABLE : args.immutable = true; break;
211 case CMD_IMMULINK : args.immulink = true; break;
212 case CMD_LEGACY : args.is_legacy = true; break;
213 case 'R' : args.do_recurse = true; break;
214 case 'a' : args.do_display_dot = true; break;
215 case 'd' : args.do_display_dir = true; break;
216 case 'n' : args.do_mapping = false; break;
217 case 's' : args.do_set = true; break;
218 case 'u' : args.do_unset = true; break;
219 case 'c' : args.ctx_str = optarg; break;
220 case 'x' : args.local_fs = true; break;
222 WRITE_MSG(2, "Try '");
223 WRITE_STR(2, argv[0]);
224 WRITE_MSG(2, " --help\" for more information.\n");
230 fixupParams(&args, argc);
233 err_cnt = processFile(".");
234 else for (i=optind; i<argc; ++i)
235 err_cnt += processFile(argv[i]);
237 return err_cnt>0 ? EXIT_FAILURE : EXIT_SUCCESS;