3 // Copyright (C) 2007 Daniel Hokka Zakrisson
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/pathinfo.h"
27 #include "lib_internal/unify.h"
28 #include "lib_internal/matchlist.h"
38 #include <sys/param.h>
40 #define ENSC_WRAPPERS_PREFIX "vclone: "
41 #define ENSC_WRAPPERS_UNISTD 1
42 #define ENSC_WRAPPERS_FCNTL 1
43 #define ENSC_WRAPPERS_DIRENT 1
44 #define ENSC_WRAPPERS_VSERVER 1
47 #define CMD_HELP 0x8000
48 #define CMD_VERSION 0x8001
49 #define CMD_XID 0x8002
56 struct MatchList excludes;
60 unsigned int verbosity;
62 const char * exclude_list;
65 static struct WalkdownInfo global_info;
66 static struct Arguments const * global_args;
68 int wrapper_exit_code = 1;
72 { "help", no_argument, 0, CMD_HELP },
73 { "version", no_argument, 0, CMD_VERSION },
74 { "xid", required_argument, 0, CMD_XID },
75 { "exclude-from", required_argument, 0, 'X' },
81 showHelp(int fd, char const *cmd, int res)
83 VSERVER_DECLARE_CMD(cmd);
85 WRITE_MSG(fd, "Usage:\n ");
88 " [--xid <xid>] [--exclude-from <exclude-list>]\n"
89 " <source> <absolute path to destination>\n\n"
90 "Please report bugs to " PACKAGE_BUGREPORT "\n");
98 "vclone " VERSION " -- clones a guest\n"
99 "This program is part of " PACKAGE_STRING "\n\n"
100 "Copyright (C) 2007 Daniel Hokka Zakrisson\n"
101 VERSION_COPYRIGHT_DISCLAIMER);
105 int Global_getVerbosity() {
106 return global_args->verbosity;
109 bool Global_doRenew() {
113 #include "vserver-visitdir.hc"
116 handleDirEntry(const PathInfo *src_path, const PathInfo *basename,
117 bool *is_dir, struct stat *st)
123 if (lstat(basename->d, st)==-1)
124 PERROR_Q(ENSC_WRAPPERS_PREFIX "lstat", src_path->d);
126 PathInfo dst_path = global_info.dst;
127 char dst_path_buf[ENSC_PI_APPSZ(dst_path, *src_path)];
129 if (S_ISDIR(st->st_mode))
132 if (MatchList_compare(&global_info.excludes, src_path->d) != stINCLUDE) {
133 if (Global_getVerbosity() > 1) {
134 WRITE_MSG(1, " skipping '");
135 Vwrite(1, src_path->d, src_path->l);
136 WRITE_MSG(1, "' (excluded)\n");
141 PathInfo_append(&dst_path, src_path, dst_path_buf);
143 /* skip files that already exist */
144 if (access(dst_path.d, F_OK)!=-1) {
145 if (Global_getVerbosity() > 1) {
146 WRITE_MSG(1, " skipping '");
147 Vwrite(1, src_path->d, src_path->l);
148 WRITE_MSG(1, "' (exists in destination)\n");
153 /* create directory that might have been skipped */
154 if (global_info.excludes.skip_depth > 0) {
155 if (Global_getVerbosity() > 4) {
156 WRITE_MSG(1, " creating directories for '");
157 Vwrite(1, dst_path.d, dst_path.l);
160 if (mkdirRecursive(dst_path.d) == -1)
161 PERROR_Q(ENSC_WRAPPERS_PREFIX "mkdirRecursive", dst_path.d);
164 /* already unified file */
165 if (S_ISREG(st->st_mode) && Unify_isIUnlinkable(basename->d) == unifyBUSY) {
166 if (Global_getVerbosity() > 2) {
167 WRITE_MSG(1, " linking unified file '");
168 Vwrite(1, src_path->d, src_path->l);
171 Elink(basename->d, dst_path.d);
174 /* something we have to copy */
176 if (Global_getVerbosity() > 2) {
177 WRITE_MSG(1, " copying non-unified file '");
178 Vwrite(1, src_path->d, src_path->l);
181 if (!Unify_copy(basename->d, st, dst_path.d))
182 PERROR_Q(ENSC_WRAPPERS_PREFIX "Unify_copy", dst_path.d);
183 else if (!S_ISSOCK(st->st_mode) &&
184 global_args->xid != VC_NOCTX &&
185 vc_set_iattr(dst_path.d, global_args->xid, 0, VC_IATTR_XID) == -1 &&
187 PERROR_Q(ENSC_WRAPPERS_PREFIX "vc_set_iattr", dst_path.d);
197 /* returns 1 on error, 0 on success */
199 visitDirEntry(struct dirent const *ent)
201 char const * dirname = ent->d_name;
202 if (isDotfile(dirname)) return 0;
205 PathInfo src_path = global_info.state;
206 PathInfo src_d_path = {
210 char path_buf[ENSC_PI_APPSZ(src_path, src_d_path)];
211 struct stat f_stat = { .st_dev = 0 };
214 PathInfo_append(&src_path, &src_d_path, path_buf);
216 if (handleDirEntry(&src_path, &src_d_path, &is_dir, &f_stat))
220 if (res || global_info.excludes.skip_depth > 0)
221 global_info.excludes.skip_depth++;
222 res = res + visitDir(dirname, &f_stat);
223 if (global_info.excludes.skip_depth > 0)
224 global_info.excludes.skip_depth--;
230 int main(int argc, char *argv[])
232 struct Arguments args = {
235 .exclude_list = NULL,
242 int c = getopt_long(argc, argv, "+vX:",
247 case CMD_HELP : showHelp(1, argv[0], 0);
248 case CMD_VERSION : showVersion();
249 case 'v' : args.verbosity++; break;
250 case 'X' : args.exclude_list = optarg; break;
251 case CMD_XID : args.xid = Evc_xidopt2xid(optarg,true); break;
253 WRITE_MSG(2, "Try '");
254 WRITE_STR(2, argv[0]);
255 WRITE_MSG(2, " --help' for more information.\n");
261 num_args = argc - optind;
263 WRITE_MSG(2, "Source is missing; try '");
264 WRITE_STR(2, argv[0]);
265 WRITE_MSG(2, " --help' for more information.\n");
268 else if (num_args < 2) {
269 WRITE_MSG(2, "Destination is missing; try '");
270 WRITE_STR(2, argv[0]);
271 WRITE_MSG(2, " --help' for more information.\n");
274 else if (num_args > 2) {
275 WRITE_MSG(2, "Too many arguments; try '");
276 WRITE_STR(2, argv[0]);
277 WRITE_MSG(2, " --help' for more information.\n");
280 else if (*argv[optind+1] != '/') {
281 WRITE_MSG(2, "The destination must be an absolute path; try '");
282 WRITE_STR(2, argv[0]);
283 WRITE_MSG(2, " --help' for more information.\n");
286 ENSC_PI_SETSTR(global_info.src, argv[optind]);
287 ENSC_PI_SETSTR(global_info.dst, argv[optind+1]);
289 if (global_args->exclude_list)
290 MatchList_initManually(&global_info.excludes, 0, strdup(argv[optind]),
291 global_args->exclude_list);
293 MatchList_init(&global_info.excludes, argv[optind], 0);
295 if (global_args->verbosity>3)
296 WRITE_MSG(1, "Starting to traverse directories...\n");
298 Echdir(global_info.src.d);
299 res = visitDir("/", 0);
301 MatchList_destroy(&global_info.excludes);
303 return res>0 ? 1 : 0;