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"
37 #include <sys/param.h>
39 #define ENSC_WRAPPERS_PREFIX "vclone: "
40 #define ENSC_WRAPPERS_UNISTD 1
41 #define ENSC_WRAPPERS_FCNTL 1
42 #define ENSC_WRAPPERS_DIRENT 1
45 #define CMD_HELP 0x8000
46 #define CMD_VERSION 0x8001
47 #define CMD_SOURCE 0x8002
48 #define CMD_DEST 0x8003
58 unsigned int verbosity;
61 static struct WalkdownInfo global_info;
62 static struct Arguments const * global_args;
64 int wrapper_exit_code = 1;
68 { "help", no_argument, 0, CMD_HELP },
69 { "version", no_argument, 0, CMD_VERSION },
70 { "source", required_argument, 0, CMD_SOURCE },
71 { "dest", required_argument, 0, CMD_DEST },
77 showHelp(int fd, char const *cmd, int res)
79 VSERVER_DECLARE_CMD(cmd);
81 WRITE_MSG(fd, "Usage:\n ");
84 " --source <source> --dest <destination>\n\n"
85 "Please report bugs to " PACKAGE_BUGREPORT "\n");
93 "vclone " VERSION " -- clones a guest\n"
94 "This program is part of " PACKAGE_STRING "\n\n"
95 "Copyright (C) 2007 Daniel Hokka Zakrisson\n"
96 VERSION_COPYRIGHT_DISCLAIMER);
100 int Global_getVerbosity() {
101 return global_args->verbosity;
104 bool Global_doRenew() {
108 #include "vserver-visitdir.hc"
111 visitDirEntry(struct dirent const *ent)
113 char const * dirname = ent->d_name;
114 if (isDotfile(dirname)) return 0;
117 PathInfo src_path = global_info.state;
118 PathInfo src_d_path = {
122 char path_buf[ENSC_PI_APPSZ(src_path, src_d_path)];
123 struct stat f_stat = { .st_dev = 0 };
125 PathInfo_append(&src_path, &src_d_path, path_buf);
128 if (lstat(dirname, &f_stat)==-1)
129 perror(ENSC_WRAPPERS_PREFIX "lstat()");
131 PathInfo dst_path = global_info.dst;
132 char dst_path_buf[ENSC_PI_APPSZ(dst_path, src_path)];
134 PathInfo_append(&dst_path, &src_path, dst_path_buf);
136 /* skip files that already exist */
137 if (access(dst_path.d, F_OK)!=-1)
139 else if (S_ISREG(f_stat.st_mode) && Unify_isIUnlinkable(src_d_path.d) == unifyBUSY) {
140 Elink(src_d_path.d, dst_path.d);
144 if (!Unify_copy(src_d_path.d, &f_stat, dst_path.d)) {
145 perror(ENSC_WRAPPERS_PREFIX "Unify_copy()");
146 exit(wrapper_exit_code);
150 if (S_ISDIR(f_stat.st_mode))
151 res = visitDir(dirname, &f_stat);
157 int main(int argc, char *argv[])
159 struct Arguments args = {
166 int c = getopt_long(argc, argv, "+",
171 case CMD_HELP : showHelp(1, argv[0], 0);
172 case CMD_VERSION : showVersion();
173 case CMD_SOURCE : ENSC_PI_SETSTR(global_info.src, optarg); break;
174 case CMD_DEST : ENSC_PI_SETSTR(global_info.dst, optarg); break;
176 WRITE_MSG(2, "Try '");
177 WRITE_STR(2, argv[0]);
178 WRITE_MSG(2, " --help' for more information.\n");
184 if (global_args->verbosity>3)
185 WRITE_MSG(1, "Starting to traverse directories...\n");
187 Echdir(global_info.src.d);
188 res = visitDir("/", 0);
190 return res>0 ? 1 : 0;