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 isSpecialDir(char const *d)
48 return ( (d[0]=='.' && !global_args->do_display_dot) ||
49 (d[0]=='.' && (d[1]=='\0' || (d[1]=='.' && d[2]=='\0'))) );
52 #define CONCAT_PATHS(LHS, LHS_LEN, RHS) \
53 size_t l_rhs = strlen(RHS); \
54 char new_path[(LHS_LEN) + l_rhs + sizeof("/")]; \
55 memcpy(new_path, LHS, (LHS_LEN)); \
56 memcpy(new_path+(LHS_LEN), "/", 1); \
57 memcpy(new_path+(LHS_LEN)+1, RHS, l_rhs); \
58 new_path[(LHS_LEN)+1+l_rhs] = '\0';
61 iterateFilesystem(char const *path)
63 bool do_again = false;
64 size_t path_len = strlen(path);
67 DIR * dir = opendir(".");
74 // show current directory entry first
75 if (lstat(".", &cur_st)==-1) perror("lstat()");
76 else err += handleFile(".", path) ? 0 : 1;
79 while (path_len>0 && path[path_len-1]=='/') --path_len;
81 // process regular files before directories
83 struct dirent *ent = Ereaddir(dir);
87 if (isSpecialDir(ent->d_name)) continue;
89 if (lstat(ent->d_name, &st)==-1) {
95 if (S_ISDIR(st.st_mode) && global_args->do_recurse) {
101 CONCAT_PATHS(path, path_len, ent->d_name);
102 err += handleFile(ent->d_name, new_path) ? 0 : 1;
107 int cur_dir = Eopen(".", O_RDONLY, 0);
111 struct dirent *ent = Ereaddir(dir);
115 if (isSpecialDir(ent->d_name)) continue;
117 if (lstat(ent->d_name, &st)==-1) {
123 if (!S_ISDIR(st.st_mode) ||
124 (global_args->local_fs && st.st_dev!=cur_st.st_dev))
127 if (safeChdir(ent->d_name, &st)==-1) {
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 int cur_dir = Eopen(".", O_RDONLY, 0);
162 ret = iterateFilesystem(path);
168 return handleFile(path, path) ? 0 : 1;
171 int main(int argc, char *argv[])
173 uint64_t err_cnt = 0;
175 struct Arguments args = {
177 .do_display_dot = false,
178 .do_display_dir = false,
192 int c = getopt_long(argc, argv, CMDLINE_OPTIONS_SHORT,
197 case CMD_HELP : showHelp(1, argv[0], 0);
198 case CMD_VERSION : showVersion();
199 case CMD_IMMU : args.set_mask |= VC_IATTR_IMMUTABLE; /*@fallthrough@*/
200 case CMD_IMMUX : args.set_mask |= VC_IATTR_IUNLINK; break;
201 case CMD_IMMUTABLE : args.set_mask |= VC_IATTR_IMMUTABLE; break;
202 case CMD_ADMIN : args.set_mask |= VC_IATTR_ADMIN; break;
203 case CMD_WATCH : args.set_mask |= VC_IATTR_WATCH; break;
204 case CMD_HIDE : args.set_mask |= VC_IATTR_HIDE; break;
205 case CMD_BARRIER : args.set_mask |= VC_IATTR_BARRIER; break;
206 case CMD_WRITE : args.set_mask |= VC_IATTR_WRITE; break;
207 case CMD_UNSET_IMMU : args.del_mask |= VC_IATTR_IMMUTABLE; /*@fallthrough@*/
208 case CMD_UNSET_IMMUX : args.del_mask |= VC_IATTR_IUNLINK; break;
209 case CMD_UNSET_IMMUTABLE : args.del_mask |= VC_IATTR_IMMUTABLE; break;
210 case CMD_UNSET_ADMIN : args.del_mask |= VC_IATTR_ADMIN; break;
211 case CMD_UNSET_WATCH : args.del_mask |= VC_IATTR_WATCH; break;
212 case CMD_UNSET_HIDE : args.del_mask |= VC_IATTR_HIDE; break;
213 case CMD_UNSET_BARRIER : args.del_mask |= VC_IATTR_BARRIER; break;
214 case CMD_UNSET_WRITE : args.del_mask |= VC_IATTR_WRITE; break;
215 case 'R' : args.do_recurse = true; break;
216 case 'a' : args.do_display_dot = true; break;
217 case 'd' : args.do_display_dir = true; break;
218 case 'n' : args.do_mapping = false; break;
219 case 's' : args.do_set = true; break;
220 case 'u' : args.do_unset = true; break;
221 case 'c' : args.ctx_str = optarg; break;
222 case 'x' : args.local_fs = true; break;
223 case 'U' : args.no_unified = true; break;
225 WRITE_MSG(2, "Try '");
226 WRITE_STR(2, argv[0]);
227 WRITE_MSG(2, " --help' for more information.\n");
233 fixupParams(&args, argc);
236 err_cnt = processFile(".");
237 else for (i=optind; i<argc; ++i)
238 err_cnt += processFile(argv[i]);
240 return err_cnt>0 ? EXIT_FAILURE : EXIT_SUCCESS;