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);
165 resolveCtx(char const *str)
169 if (*str==':') ++str;
172 long result = strtol(str, &end_ptr, 0);
174 if (end_ptr>str && *end_ptr==0) return result;
177 res = vc_getVserverCtx(str, vcCFG_AUTO, true, 0);
179 WRITE_MSG(2, "Can not find a vserver with name '");
181 WRITE_MSG(2, "', or vserver does not have a static context\n");
188 int main(int argc, char *argv[])
190 uint64_t err_cnt = 0;
192 struct Arguments args = {
194 .do_display_dot = false,
195 .do_display_dir = false,
207 int c = getopt_long(argc, argv, CMDLINE_OPTIONS_SHORT,
212 case CMD_HELP : showHelp(1, argv[0], 0);
213 case CMD_VERSION : showVersion();
214 case CMD_IMMUTABLE : args.immutable = true; break;
215 case CMD_IMMULINK : args.immulink = true; break;
216 case CMD_LEGACY : args.is_legacy = true; break;
217 case 'R' : args.do_recurse = true; break;
218 case 'a' : args.do_display_dot = true; break;
219 case 'd' : args.do_display_dir = true; break;
220 case 'n' : args.do_mapping = false; break;
221 case 's' : args.do_set = true; break;
222 case 'u' : args.do_unset = true; break;
223 case 'c' : args.ctx = resolveCtx(optarg); break;
225 WRITE_MSG(2, "Try '");
226 WRITE_STR(2, argv[0]);
227 WRITE_MSG(2, " --help\" for more information.\n");
233 checkParams(&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;