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 <lib/vserver.h>
36 #define ENSC_WRAPPERS_DIRENT 1
37 #define ENSC_WRAPPERS_FCNTL 1
38 #define ENSC_WRAPPERS_UNISTD 1
41 struct Arguments const * global_args = 0;
43 int wrapper_exit_code = 1;
46 checkForRace(int fd, char const * name, struct stat const *exp_st)
50 if (fstat(fd, &st)==-1) {
55 if (st.st_dev != exp_st->st_dev ||
56 st.st_ino != exp_st->st_ino ||
57 st.st_mode != exp_st->st_mode) {
59 WRITE_MSG(2, "race while visiting '");
69 isSpecialDir(char const *d)
71 return ( (d[0]=='.' && !global_args->do_display_dot) ||
72 (d[0]=='.' && (d[1]=='\0' || (d[1]=='.' && d[2]=='\0'))) );
75 #define CONCAT_PATHS(LHS, LHS_LEN, RHS) \
76 size_t l_rhs = strlen(RHS); \
77 char new_path[(LHS_LEN) + l_rhs + sizeof("/")]; \
78 memcpy(new_path, LHS, (LHS_LEN)); \
79 memcpy(new_path+(LHS_LEN), "/", 1); \
80 memcpy(new_path+(LHS_LEN)+1, RHS, l_rhs); \
81 new_path[(LHS_LEN)+1+l_rhs] = '\0';
84 iterateFilesystem(char const *path)
86 bool do_again = false;
87 size_t path_len = strlen(path);
90 DIR * dir = opendir(".");
97 // show current directory entry first
98 if (lstat(".", &cur_st)==-1) perror("lstat()");
99 else err += handleFile(".", path) ? 0 : 1;
101 // strip trailing '/'
102 while (path_len>0 && path[path_len-1]=='/') --path_len;
104 // process regular files before directories
106 struct dirent *ent = Ereaddir(dir);
110 if (isSpecialDir(ent->d_name)) continue;
112 if (lstat(ent->d_name, &st)==-1) {
118 if (S_ISDIR(st.st_mode) && global_args->do_recurse) {
124 CONCAT_PATHS(path, path_len, ent->d_name);
125 err += handleFile(ent->d_name, new_path) ? 0 : 1;
130 int cur_dir = Eopen(".", O_RDONLY, 0);
134 struct dirent *ent = Ereaddir(dir);
138 if (isSpecialDir(ent->d_name)) continue;
140 if (lstat(ent->d_name, &st)==-1) {
146 if (!S_ISDIR(st.st_mode) ||
147 (global_args->local_fs && st.st_dev!=cur_st.st_dev))
150 if (safeChdir(ent->d_name, &st)==-1) {
157 CONCAT_PATHS(path, path_len, ent->d_name);
158 err += iterateFilesystem(new_path);
172 processFile(char const *path)
176 if (lstat(path, &st)==-1) {
181 if (S_ISDIR(st.st_mode) && !global_args->do_display_dir) {
183 return iterateFilesystem(path);
186 return handleFile(path, path) ? 0 : 1;
189 int main(int argc, char *argv[])
191 uint64_t err_cnt = 0;
193 struct Arguments args = {
195 .do_display_dot = false,
196 .do_display_dir = false,
209 int c = getopt_long(argc, argv, CMDLINE_OPTIONS_SHORT,
214 case CMD_HELP : showHelp(1, argv[0], 0);
215 case CMD_VERSION : showVersion();
216 case CMD_IMMU : args.set_mask |= VC_IATTR_IMMUTABLE; /*@fallthrough@*/
217 case CMD_IMMUX : args.set_mask |= VC_IATTR_IUNLINK; break;
218 case CMD_ADMIN : args.set_mask |= VC_IATTR_ADMIN; break;
219 case CMD_WATCH : args.set_mask |= VC_IATTR_WATCH; break;
220 case CMD_HIDE : args.set_mask |= VC_IATTR_HIDE; break;
221 case CMD_BARRIER : args.set_mask |= VC_IATTR_BARRIER; break;
222 case CMD_UNSET_IMMU : args.del_mask |= VC_IATTR_IMMUTABLE; /*@fallthrough@*/
223 case CMD_UNSET_IMMUX : args.del_mask |= VC_IATTR_IUNLINK; break;
224 case CMD_UNSET_ADMIN : args.del_mask |= VC_IATTR_ADMIN; break;
225 case CMD_UNSET_WATCH : args.del_mask |= VC_IATTR_WATCH; break;
226 case CMD_UNSET_HIDE : args.del_mask |= VC_IATTR_HIDE; break;
227 case CMD_UNSET_BARRIER : args.del_mask |= VC_IATTR_BARRIER; break;
228 case 'R' : args.do_recurse = true; break;
229 case 'a' : args.do_display_dot = true; break;
230 case 'd' : args.do_display_dir = true; break;
231 case 'n' : args.do_mapping = false; break;
232 case 's' : args.do_set = true; break;
233 case 'u' : args.do_unset = true; break;
234 case 'c' : args.ctx_str = optarg; break;
235 case 'x' : args.local_fs = true; break;
237 WRITE_MSG(2, "Try '");
238 WRITE_STR(2, argv[0]);
239 WRITE_MSG(2, " --help\" for more information.\n");
245 fixupParams(&args, argc);
248 err_cnt = processFile(".");
249 else for (i=optind; i<argc; ++i)
250 err_cnt += processFile(argv[i]);
252 return err_cnt>0 ? EXIT_FAILURE : EXIT_SUCCESS;