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.
26 #include "lib_internal/unify.h"
27 #include "lib_internal/matchlist.h"
28 #include "lib_internal/util-dotfile.h"
29 #include "lib_internal/util-safechdir.h"
30 #include <lib/vserver.h>
34 #include <sys/types.h>
43 #define ENSC_WRAPPERS_IO 1
44 #define ENSC_WRAPPERS_FCNTL 1
45 #define ENSC_WRAPPERS_DIRENT 1
46 #define ENSC_WRAPPERS_UNISTD 1
47 #define ENSC_WRAPPERS_STDLIB 1
50 int wrapper_exit_code = 1;
53 #define CMD_HELP 0x8000
54 #define CMD_VERSION 0x8001
55 #define CMD_MANUALLY 0x8002
59 { "help", no_argument, 0, CMD_HELP },
60 { "version", no_argument, 0, CMD_VERSION },
61 { "manually", no_argument, 0, CMD_MANUALLY },
65 static struct WalkdownInfo global_info;
66 static struct SkipReason skip_reason;
67 static struct Arguments const * global_args;
69 int Global_getVerbosity() {
70 return global_args->verbosity;
73 bool Global_doRenew() {
74 return global_args->do_renew;
78 showHelp(int fd, char const *cmd, int res)
80 WRITE_MSG(fd, "Usage:\n ");
83 " [-Rnv] <vserver>\n or\n ");
86 " --manually [-Rnvx] [--] <path> <excludelist> [<path> <excludelist>]+\n\n"
87 " --manually ... unify generic paths; excludelists must be generated\n"
89 " -R ... revert operation; deunify files\n"
90 " -n ... do not modify anything; just show what there will be\n"
91 " done (in combination with '-v')\n"
92 " -v ... verbose mode\n"
93 " -x ... do not cross filesystems; this is valid in manual\n"
94 " mode only and will be ignored for vserver unification\n\n"
95 "Please report bugs to " PACKAGE_BUGREPORT "\n");
97 " -C ... use cached excludelists; usually they will be\n"
98 " regenerated after package installation to reflect e.g.\n"
99 " added/removed configuration files\n\n"
108 "vunify " VERSION " -- unifies vservers and/or directories\n"
109 "This program is part of " PACKAGE_STRING "\n\n"
110 "Copyright (C) 2003,2004 Enrico Scholz\n"
111 VERSION_COPYRIGHT_DISCLAIMER);
115 // Returns 'false' iff one of the files is not existing, or of the files are different/not unifyable
117 checkFstat(struct MatchList const * const mlist,
118 PathInfo const * const basename,
119 PathInfo const * const path,
120 struct stat const ** const dst_fstat, struct stat * const dst_fstat_buf,
121 struct stat * const src_fstat)
123 assert(basename->d[0] != '/');
126 // local file does not exist... strange
128 skip_reason.r = rsFSTAT;
129 if (lstat(basename->d, dst_fstat_buf)==-1) return false;
130 *dst_fstat = dst_fstat_buf;
133 assert(*dst_fstat!=0);
136 PathInfo src_path = mlist->root;
137 char src_path_buf[ENSC_PI_APPSZ(src_path, *path)];
139 PathInfo_append(&src_path, path, src_path_buf);
141 // source file does not exist
142 skip_reason.r = rsNOEXISTS;
143 if (lstat(src_path.d, src_fstat)==-1) return false;
145 // these are directories; this succeeds everytime
146 if (S_ISDIR((*dst_fstat)->st_mode) && S_ISDIR(src_fstat->st_mode)) return true;
148 // both files are different, so return false
149 skip_reason.r = rsDIFFERENT;
150 if ((!global_args->do_revert && !Unify_isUnifyable(*dst_fstat, src_fstat)) ||
151 ( global_args->do_revert && !Unify_isUnified (*dst_fstat, src_fstat)))
155 // these are the same files
159 static struct MatchList const *
160 checkDirEntry(PathInfo const *path,
161 PathInfo const *d_path, bool *is_dir,
162 struct stat *src_stat, struct stat *dst_stat)
164 struct WalkdownInfo const * const info = &global_info;
165 struct MatchList const * mlist;
166 struct stat const * cache_stat;
168 // Check if it is in the exclude/include list of the destination vserver and
169 // abort when it is not matching an allowed entry
170 skip_reason.r = rsEXCL_DST;
171 skip_reason.d.list = &info->dst_list;
172 if (MatchList_compare(&info->dst_list, path->d)!=stINCLUDE) return 0;
174 // Now, go through the reference vservers and do the lightweigt list-check
175 // first and compare then the fstat's.
176 for (mlist=info->src_lists.v; mlist<info->src_lists.v+info->src_lists.l; ++mlist) {
178 skip_reason.r = rsEXCL_SRC;
179 skip_reason.d.list = mlist;
180 if (MatchList_compare(mlist, path->d)==stINCLUDE &&
181 checkFstat(mlist, d_path, path, &cache_stat, dst_stat, src_stat)) {
183 // Failed the check or is it a symlink which can not be handled
184 if (cache_stat==0) return 0;
186 skip_reason.r = rsSYMLINK;
187 if (S_ISLNK(dst_stat->st_mode)) return 0;
189 skip_reason.r = rsSPECIAL;
190 if (!S_ISREG(dst_stat->st_mode) &&
191 !S_ISDIR(dst_stat->st_mode)) return 0;
193 *is_dir = S_ISDIR(dst_stat->st_mode);
196 else if (cache_stat!=0 && !global_args->do_revert &&
197 skip_reason.r == rsDIFFERENT &&
198 Unify_isUnified(cache_stat, src_stat)) {
199 skip_reason.r = rsUNIFIED;
200 skip_reason.d.list = mlist;
210 updateSkipDepth(PathInfo const *path, bool walk_down)
212 struct WalkdownInfo const * const info = &global_info;
213 struct MatchList * mlist;
216 for (mlist=info->src_lists.v; mlist<info->src_lists.v+info->src_lists.l; ++mlist) {
217 // The easy way... this path is being skipped already
218 if (mlist->skip_depth>0) {
219 if (walk_down) ++mlist->skip_depth;
220 else --mlist->skip_depth;
223 else if (walk_down) {
224 PathInfo src_path = mlist->root;
225 char src_path_buf[ENSC_PI_APPSZ(src_path, *path)];
226 struct stat src_fstat;
228 PathInfo_append(&src_path, path, src_path_buf);
230 // when the file/dir exist, we have do go deeper.
231 // else skip it in deeper runs for *this* matchlist
232 if (lstat(src_path.d, &src_fstat)!=-1) result = true;
233 else ++mlist->skip_depth;
244 doit(struct MatchList const *mlist,
245 PathInfo const *src_path, struct stat const *src_stat,
246 char const *dst_path, struct stat const UNUSED *dst_stat)
248 PathInfo path = mlist->root;
249 char path_buf[ENSC_PI_APPSZ(path, *src_path)];
251 if (global_args->do_dry_run || Global_getVerbosity()>=2) {
252 if (global_args->do_revert) WRITE_MSG(1, "deunifying '");
253 else WRITE_MSG(1, "unifying '");
255 write(1, src_path->d, src_path->l);
258 if (Global_getVerbosity()>=4) {
259 WRITE_MSG(1, " (from ");
260 if (Global_getVerbosity()==4 && mlist->id.d)
261 write(1, mlist->id.d, mlist->id.l);
263 write(1, mlist->root.d, mlist->root.l);
269 PathInfo_append(&path, src_path, path_buf);
270 return (global_args->do_dry_run ||
271 (!global_args->do_revert && Unify_unify (path.d, src_stat, dst_path)) ||
272 ( global_args->do_revert && Unify_deUnify(dst_path)));
280 switch (skip_reason.r) {
281 case rsDOTFILE : WRITE_MSG(1, "dotfile"); break;
284 WRITE_MSG(1, "excluded by ");
285 MatchList_printId(skip_reason.d.list, 1);
287 case rsFSTAT : WRITE_MSG(1, "fstat error"); break;
288 case rsNOEXISTS : WRITE_MSG(1, "does not exists in refserver(s)"); break;
289 case rsSYMLINK : WRITE_MSG(1, "symlink"); break;
290 case rsSPECIAL : WRITE_MSG(1, "non regular file"); break;
291 case rsUNIFIED : WRITE_MSG(1, "already unified"); break;
292 case rsDIFFERENT : WRITE_MSG(1, "different"); break;
293 default : assert(false); abort();
298 #include "vserver-visitdir.hc"
301 visitDirEntry(struct dirent const *ent)
304 struct MatchList const * match;
305 struct stat f_stat = { .st_dev = 0 };
306 char const * dirname = ent->d_name;
307 PathInfo path = global_info.state;
312 char path_buf[ENSC_PI_APPSZ(path, d_path)];
314 struct stat src_stat;
317 PathInfo_append(&path, &d_path, path_buf);
319 is_dotfile = isDotfile(dirname);
320 skip_reason.r = rsDOTFILE;
323 (match=checkDirEntry(&path, &d_path, &is_dir, &src_stat, &f_stat))==0) {
324 bool is_link = is_dotfile ? false : S_ISLNK(f_stat.st_mode);
326 if (Global_getVerbosity()>=1 &&
327 (Global_getVerbosity()>=3 || skip_reason.r!=rsUNIFIED) &&
328 ((!is_dotfile && !is_link) ||
329 (Global_getVerbosity()>=6 && is_dotfile) ||
330 (Global_getVerbosity()>=6 && is_link)) ) {
331 WRITE_MSG(1, " skipping '");
332 write(1, path.d, path.l);
334 if (Global_getVerbosity()>=2) printSkipReason();
341 if (updateSkipDepth(&path, true)) {
342 res = visitDir(dirname, &f_stat);
343 updateSkipDepth(&path, false);
348 else if (!doit(match, &path, &src_stat, dirname, &f_stat)) {
357 #include "vunify-init.hc"
359 int main(int argc, char *argv[])
361 struct Arguments args = {
372 int c = getopt_long(argc, argv, "Rnvcx",
377 case CMD_HELP : showHelp(1, argv[0], 0);
378 case CMD_VERSION : showVersion();
379 case CMD_MANUALLY : args.mode = mdMANUALLY; break;
380 case 'R' : args.do_revert = true; break;
381 case 'n' : args.do_dry_run = true; break;
382 case 'x' : args.local_fs = true; break;
383 //case 'C' : args.do_renew = false; break;
384 case 'v' : ++args.verbosity; break;
386 WRITE_MSG(2, "Try '");
387 WRITE_STR(2, argv[0]);
388 WRITE_MSG(2, " --help\" for more information.\n");
395 WRITE_MSG(2, "No directory/vserver given\n");
400 case mdMANUALLY : initModeManually(&args, argc-optind, argv+optind); break;
401 case mdVSERVER : initModeVserver (&args, argc-optind, argv+optind); break;
402 default : assert(false); return EXIT_FAILURE;
405 global_info.state.d = "";
406 global_info.state.l = 0;
409 if (Global_getVerbosity()>=1) WRITE_MSG(1, "Starting to traverse directories...\n");
410 Echdir(global_info.dst_list.root.d);
416 MatchList_destroy(&global_info.dst_list);
417 for (i=0; i<global_info.src_lists.l; ++i)
418 MatchList_destroy(global_info.src_lists.v+i);
420 free(global_info.src_lists.v);