3 // Copyright (C) 2003,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.
24 #include "wrappers-dirent.h"
25 #include "vunify-operations.h"
28 #include <lib/vserver.h>
32 #include <sys/types.h>
37 int wrapper_exit_code = 1;
40 static struct WalkdownInfo global_info;
41 static struct Operations operations;
44 #define CMD_HELP 0x8000
45 #define CMD_VERSION 0x8001
46 #define CMD_MANUALLY 0x8002
50 { "help", no_argument, 0, CMD_HELP },
51 { "version", no_argument, 0, CMD_VERSION },
52 { "manually", no_argument, 0, CMD_MANUALLY },
57 enum {mdMANUALLY, mdVSERVER} mode;
60 unsigned int verbosity;
65 struct Arguments const * global_args;
68 showHelp(int fd, char const *cmd, int res)
70 WRITE_MSG(fd, "Usage:\n ");
73 " [-Rnv] <vserver>\n or\n ");
76 " --manually [-Rnvx] [--] <path> <excludelist> [<path> <excludelist>]+\n\n"
77 " --manually ... unify generic paths; excludelists must be generated\n"
79 " -R ... revert operation; deunify files\n"
80 " -n ... do not modify anything; just show what there will be\n"
81 " done (in combination with '-v')\n"
82 " -v ... verbose mode\n"
83 " -x ... do not cross filesystems; this is valid in manual\n"
84 " mode only and will be ignored for vserver unification\n"
85 "Please report bugs to " PACKAGE_BUGREPORT "\n");
87 " -C ... use cached excludelists; usually they will be\n"
88 " regenerated after package installation to reflect e.g.\n"
89 " added/removed configuration files\n\n"
98 "vunify " VERSION " -- unifies vservers and/or directories\n"
99 "This program is part of " PACKAGE_STRING "\n\n"
100 "Copyright (C) 2003,2004 Enrico Scholz\n"
101 VERSION_COPYRIGHT_DISCLAIMER);
106 // Returns 'false' iff one of the files is not existing, or of the files are different/not unifyable
108 checkFstat(struct MatchList const * const mlist,
109 PathInfo const * const basename,
110 PathInfo const * const path,
111 struct stat const ** const result, struct stat * const result_buf)
113 assert(basename->d[0] != '/');
116 // local file does not exist... strange
118 if (lstat(basename->d, result_buf)==-1) return false;
119 *result = result_buf;
125 struct stat src_fstat;
126 PathInfo src_path = mlist->root;
127 char src_path_buf[ENSC_PI_APPSZ(src_path, *path)];
129 PathInfo_append(&src_path, path, src_path_buf);
131 // source file does not exist
132 if (lstat(src_path.d, &src_fstat)==-1) return false;
134 // these are directories
135 if (S_ISDIR((*result)->st_mode) && S_ISDIR(src_fstat.st_mode)) return true;
137 // both files are different, so return false
138 if (!(operations.compare)(*result, &src_fstat)) return false;
141 // these are the same files
145 static struct MatchList const *
146 checkDirEntry(PathInfo const *path,
147 PathInfo const *d_path, bool *is_dir, struct stat *f_stat)
149 struct WalkdownInfo const * const info = &global_info;
150 struct MatchList const * mlist;
151 struct stat const * cache_stat = 0;
153 // Check if it is in the exclude/include list of the destination vserver and
154 // abort when it is not matching an allowed entry
155 if (!MatchList_compare(&info->dst_list, path->d)) return 0;
157 // Now, go through the reference vservers and do the lightweigt list-check
158 // first and compare then the fstat's.
159 for (mlist=info->src_lists.v; mlist<info->src_lists.v+info->src_lists.l; ++mlist) {
160 if (MatchList_compare(mlist, path->d) &&
161 checkFstat(mlist, d_path, path, &cache_stat, f_stat)) {
163 // Failed the check or is it a symlink which can not be handled
164 if (cache_stat==0 || S_ISLNK(f_stat->st_mode)) return 0;
166 *is_dir = S_ISDIR(f_stat->st_mode);
176 updateSkipDepth(PathInfo const *path, bool walk_down)
178 struct WalkdownInfo const * const info = &global_info;
179 struct MatchList * mlist;
182 for (mlist=info->src_lists.v; mlist<info->src_lists.v+info->src_lists.l; ++mlist) {
183 // The easy way... this path is being skipped already
184 if (mlist->skip_depth>0) {
185 if (walk_down) ++mlist->skip_depth;
186 else --mlist->skip_depth;
189 else if (walk_down) {
190 PathInfo src_path = mlist->root;
191 char src_path_buf[ENSC_PI_APPSZ(src_path, *path)];
192 struct stat src_fstat;
194 PathInfo_append(&src_path, path, src_path_buf);
196 // when the file/dir exist, we have do go deeper.
197 // else skip it in deeper runs for *this* matchlist
198 if (lstat(src_path.d, &src_fstat)!=-1) result = true;
199 else ++mlist->skip_depth;
210 EsafeChdir(char const *path, struct stat const *exp_stat)
212 FatalErrnoError(safeChdir(path, exp_stat)==-1, "safeChdir()");
216 doit(struct MatchList const *mlist, PathInfo const *src_path,
217 char const *dst_path)
219 PathInfo path = mlist->root;
220 char path_buf[ENSC_PI_APPSZ(path, *src_path)];
222 if (global_args->do_dry_run || global_args->verbosity>0) {
223 if (global_args->do_revert) WRITE_MSG(1, "deunifying '");
224 else WRITE_MSG(1, "unifying '");
226 write(1, src_path->d, src_path->l);
229 if (global_args->verbosity>2) {
230 WRITE_MSG(1, " (from ");
231 if (global_args->verbosity==2 && mlist->id.d)
232 write(1, mlist->id.d, mlist->id.l);
234 write(1, mlist->root.d, mlist->root.l);
240 PathInfo_append(&path, src_path, path_buf);
241 return (operations.doit)(path.d, dst_path);
246 visitDirEntry(struct dirent const *ent)
249 struct MatchList const * match;
251 char const * dirname = ent->d_name;
252 PathInfo path = global_info.state;
255 .l = strlen(ent->d_name)
257 char path_buf[ENSC_PI_APPSZ(path, d_path)];
260 PathInfo_append(&path, &d_path, path_buf);
262 is_dotfile = (dirname[0]=='.' &&
263 (dirname[1]=='\0' || (dirname[1]=='.' && dirname[2]=='\0')));
264 memset(&f_stat, 0, sizeof f_stat);
266 (match=checkDirEntry(&path, &d_path, &is_dir, &f_stat))==0) {
267 bool is_link = is_dotfile ? false : S_ISLNK(f_stat.st_mode);
269 if (global_args->verbosity>1 &&
270 ((!is_dotfile && !is_link) ||
271 (global_args->verbosity>4 && is_dotfile) ||
272 (global_args->verbosity>4 && is_link)) ) {
273 WRITE_MSG(1, " skipping '");
274 write(1, path.d, path.l);
281 if (updateSkipDepth(&path, true)) {
282 visitDir(dirname, &f_stat);
283 updateSkipDepth(&path, false);
286 else if (!doit(match, &path, dirname)) {
292 visitDir(char const *name, struct stat const *expected_stat)
294 int fd = Eopen(".", O_RDONLY, 0);
295 PathInfo old_state = global_info.state;
296 PathInfo rhs_path = {
300 char new_path[ENSC_PI_APPSZ(global_info.state, rhs_path)];
303 PathInfo_append(&global_info.state, &rhs_path, new_path);
305 if (expected_stat!=0)
306 EsafeChdir(name, expected_stat);
311 struct dirent *ent = Ereaddir(dir);
322 global_info.state = old_state;
325 #include "vunify-init.ic"
327 int main(int argc, char *argv[])
329 struct Arguments args = {
340 int c = getopt_long(argc, argv, "Rnvcx",
345 case CMD_HELP : showHelp(1, argv[0], 0);
346 case CMD_VERSION : showVersion();
347 case CMD_MANUALLY : args.mode = mdMANUALLY; break;
348 case 'R' : args.do_revert = true; break;
349 case 'n' : args.do_dry_run = true; break;
350 case 'x' : args.local_fs = true; break;
351 //case 'C' : args.do_renew = false; break;
352 case 'v' : ++args.verbosity; break;
354 WRITE_MSG(2, "Try '");
355 WRITE_STR(2, argv[0]);
356 WRITE_MSG(2, " --help\" for more information.\n");
363 WRITE_MSG(2, "No directory/vserver given\n");
367 Operations_init(&operations,
368 args.do_revert ? opDEUNIFY : opUNIFY,
372 case mdMANUALLY : initModeManually(&args, argc-optind, argv+optind); break;
373 case mdVSERVER : initModeVserver(&args, argc-optind, argv+optind); break;
374 default : assert(false); return EXIT_FAILURE;
377 global_info.state.d = "";
378 global_info.state.l = 0;
381 Echdir(global_info.dst_list.root.d);
387 MatchList_destroy(&global_info.dst_list);
388 for (i=0; i<global_info.src_lists.l; ++i)
389 MatchList_destroy(global_info.src_lists.v+i);
391 free(global_info.src_lists.v);