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)))
154 // these are the same files
158 static struct MatchList const *
159 checkDirEntry(PathInfo const *path,
160 PathInfo const *d_path, bool *is_dir,
161 struct stat *src_stat, struct stat *dst_stat)
163 struct WalkdownInfo const * const info = &global_info;
164 struct MatchList const * mlist;
165 struct stat const * cache_stat;
167 // Check if it is in the exclude/include list of the destination vserver and
168 // abort when it is not matching an allowed entry
169 skip_reason.r = rsEXCL_DST;
170 skip_reason.d.list = &info->dst_list;
171 if (MatchList_compare(&info->dst_list, path->d)!=stINCLUDE) return 0;
173 // Now, go through the reference vservers and do the lightweigt list-check
174 // first and compare then the fstat's.
175 for (mlist=info->src_lists.v; mlist<info->src_lists.v+info->src_lists.l; ++mlist) {
177 skip_reason.r = rsEXCL_SRC;
178 skip_reason.d.list = mlist;
179 if (MatchList_compare(mlist, path->d)==stINCLUDE &&
180 checkFstat(mlist, d_path, path, &cache_stat, dst_stat, src_stat)) {
182 // Failed the check or is it a symlink which can not be handled
183 if (cache_stat==0) return 0;
185 skip_reason.r = rsSYMLINK;
186 if (S_ISLNK(dst_stat->st_mode)) return 0;
188 skip_reason.r = rsSPECIAL;
189 if (!S_ISREG(dst_stat->st_mode) &&
190 !S_ISDIR(dst_stat->st_mode)) return 0;
192 *is_dir = S_ISDIR(dst_stat->st_mode);
195 else if (cache_stat!=0 && !global_args->do_revert &&
196 skip_reason.r == rsDIFFERENT &&
197 Unify_isUnified(cache_stat, src_stat)) {
198 skip_reason.r = rsUNIFIED;
199 skip_reason.d.list = mlist;
209 updateSkipDepth(PathInfo const *path, bool walk_down)
211 struct WalkdownInfo const * const info = &global_info;
212 struct MatchList * mlist;
215 for (mlist=info->src_lists.v; mlist<info->src_lists.v+info->src_lists.l; ++mlist) {
216 // The easy way... this path is being skipped already
217 if (mlist->skip_depth>0) {
218 if (walk_down) ++mlist->skip_depth;
219 else --mlist->skip_depth;
222 else if (walk_down) {
223 PathInfo src_path = mlist->root;
224 char src_path_buf[ENSC_PI_APPSZ(src_path, *path)];
225 struct stat src_fstat;
227 PathInfo_append(&src_path, path, src_path_buf);
229 // when the file/dir exist, we have do go deeper.
230 // else skip it in deeper runs for *this* matchlist
231 if (lstat(src_path.d, &src_fstat)!=-1) result = true;
232 else ++mlist->skip_depth;
243 doit(struct MatchList const *mlist,
244 PathInfo const *src_path, struct stat const *src_stat,
245 char const *dst_path, struct stat const UNUSED *dst_stat)
247 PathInfo path = mlist->root;
248 char path_buf[ENSC_PI_APPSZ(path, *src_path)];
250 if (global_args->do_dry_run || Global_getVerbosity()>=2) {
251 if (global_args->do_revert) WRITE_MSG(1, "deunifying '");
252 else WRITE_MSG(1, "unifying '");
254 Vwrite(1, src_path->d, src_path->l);
257 if (Global_getVerbosity()>=4) {
258 WRITE_MSG(1, " (from ");
259 if (Global_getVerbosity()==4 && mlist->id.d)
260 Vwrite(1, mlist->id.d, mlist->id.l);
262 Vwrite(1, mlist->root.d, mlist->root.l);
268 PathInfo_append(&path, src_path, path_buf);
269 return (global_args->do_dry_run ||
270 (!global_args->do_revert && Unify_unify (path.d, src_stat, dst_path, false)) ||
271 ( global_args->do_revert && Unify_deUnify(dst_path)));
279 switch (skip_reason.r) {
280 case rsDOTFILE : WRITE_MSG(1, "dotfile"); break;
283 WRITE_MSG(1, "excluded by ");
284 MatchList_printId(skip_reason.d.list, 1);
286 case rsFSTAT : WRITE_MSG(1, "fstat error"); break;
287 case rsNOEXISTS : WRITE_MSG(1, "does not exist in refserver(s)"); break;
288 case rsSYMLINK : WRITE_MSG(1, "symlink"); break;
289 case rsSPECIAL : WRITE_MSG(1, "non regular file"); break;
290 case rsUNIFIED : WRITE_MSG(1, "already unified"); break;
291 case rsDIFFERENT : WRITE_MSG(1, "different"); break;
292 default : assert(false); abort();
297 #include "vserver-visitdir.hc"
300 visitDirEntry(struct dirent const *ent)
303 struct MatchList const * match;
304 struct stat f_stat = { .st_dev = 0 };
305 char const * dirname = ent->d_name;
306 PathInfo path = global_info.state;
311 char path_buf[ENSC_PI_APPSZ(path, d_path)];
313 struct stat src_stat;
316 PathInfo_append(&path, &d_path, path_buf);
318 is_dotfile = isDotfile(dirname);
319 skip_reason.r = rsDOTFILE;
322 (match=checkDirEntry(&path, &d_path, &is_dir, &src_stat, &f_stat))==0) {
323 bool is_link = is_dotfile ? false : S_ISLNK(f_stat.st_mode);
325 if (Global_getVerbosity()>=1 &&
326 (Global_getVerbosity()>=3 || skip_reason.r!=rsUNIFIED) &&
327 ((!is_dotfile && !is_link) ||
328 (Global_getVerbosity()>=6 && is_dotfile) ||
329 (Global_getVerbosity()>=6 && is_link)) ) {
330 WRITE_MSG(1, " skipping '");
331 Vwrite(1, path.d, path.l);
333 if (Global_getVerbosity()>=2) printSkipReason();
340 if (updateSkipDepth(&path, true)) {
341 res = visitDir(dirname, &f_stat);
342 updateSkipDepth(&path, false);
347 else if (!doit(match, &path, &src_stat, dirname, &f_stat)) {
356 #include "vunify-init.hc"
358 int main(int argc, char *argv[])
360 struct Arguments args = {
371 int c = getopt_long(argc, argv, "Rnvcx",
376 case CMD_HELP : showHelp(1, argv[0], 0);
377 case CMD_VERSION : showVersion();
378 case CMD_MANUALLY : args.mode = mdMANUALLY; break;
379 case 'R' : args.do_revert = true; break;
380 case 'n' : args.do_dry_run = true; break;
381 case 'x' : args.local_fs = true; break;
382 //case 'C' : args.do_renew = false; break;
383 case 'v' : ++args.verbosity; break;
385 WRITE_MSG(2, "Try '");
386 WRITE_STR(2, argv[0]);
387 WRITE_MSG(2, " --help' for more information.\n");
394 WRITE_MSG(2, "No directory/vserver given\n");
399 case mdMANUALLY : initModeManually(&args, argc-optind, argv+optind); break;
400 case mdVSERVER : initModeVserver (&args, argc-optind, argv+optind); break;
401 default : assert(false); return EXIT_FAILURE;
404 global_info.state.d = "";
405 global_info.state.l = 0;
408 if (Global_getVerbosity()>=1) WRITE_MSG(1, "Starting to traverse directories...\n");
409 Echdir(global_info.dst_list.root.d);
415 MatchList_destroy(&global_info.dst_list);
416 for (i=0; i<global_info.src_lists.l; ++i)
417 MatchList_destroy(global_info.src_lists.v+i);
419 free(global_info.src_lists.v);