- renamed 'PACKAGE' class to 'Package' to avoid naming-clashes with
[util-vserver.git] / util-vserver / src / vbuild.cc
1 // $Id$
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vbuild.cc by Jacques Gelinas
5 //  
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //  
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //  
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 /*
21         This utility is used to build a new vserver using a reference vserver.
22         It uses hard link whenever possible instead of duplicating files.
23         Once done, it sets the immutable bits.
24 */
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <errno.h>
35 #include <dirent.h>
36
37 #include <string>
38 #include <vector>
39 #include <list>
40 #include <set>
41 #include "vutil.h"
42
43 using namespace std;
44
45 struct EXCLDIR{
46         string prefix;
47         int len;
48         EXCLDIR(const char *s)
49         {
50                 prefix = s;
51                 prefix += '/';
52                 len = prefix.size();
53         }
54 };
55 static vector<EXCLDIR> excldirs;
56
57
58 static int  ext2flags = EXT2_IMMUTABLE_FILE_FL | EXT2_IMMUTABLE_LINK_FL;
59 static struct {
60         int nblink;
61         int nbcopy;
62         long size_copy;
63         int nbdir;
64         int nbsymlink;
65         int nbspc;
66 } stats;
67
68
69 static void usage()
70 {
71         cerr <<
72                 "vbuild version " << VERSION <<
73                 "\n\n"
74                 "vbuild [ options ] reference-server new-vservers\n"
75                 "\n"
76                 "--test: Show what will be done, do not do it.\n"
77                 "--debug: Prints some debugging messages.\n"
78                 "\n"
79                 "--excldir: None of the files under a given directory will be copied\n"
80                 "\tThe directory is expressed in absolute/logical form (relative\n"
81                 "\tto the vserver root (ex: /var/log)\n"
82                 "\n"
83                 "\n"
84                 "--noflags: Do not put any immutable flags on the file\n"
85                 "--immutable: Set the immutable_file bit on the files.\n"
86                 "--immutable-mayunlink: Sets the immutable_link flag on files.\n"
87                 "--stats: Produce statistics on the number of file linked\n"
88                 "         copied and so on.\n"
89                 "\n"
90                 "By default, the immutable_file and     immutable_link flags are\n"
91                 "set on the files. So if you want no immutable flags, you must\n"
92                 "use --noflags. If you want a single flag, you must use\n"
93                 "--noflags first, then the --immutable or --immutable-mayunlink\n"
94                 "flag.\n"
95                 ;
96 }
97
98 /*
99         Return true if a directory lies inside a directory set
100 */
101 static bool vbuild_inside (vector<EXCLDIR> &dirs, const char *path)
102 {
103         bool found = false;
104         for (unsigned i=0; i<dirs.size(); i++){
105                 if (strncmp(dirs[i].prefix.c_str(),path,dirs[i].len)==0){
106                         found = true;
107                         break;
108                 }
109         }
110         return found;
111 }
112
113
114
115 static int vbuild_copy (
116         string refserv,
117         string newserv,
118         dev_t dev,                      // We stay on the same volume
119         string logical_dir,
120         set<string> &files)
121 {
122         int ret = -1;
123         if (debug > 0) printf ("Copying directory %s\n",logical_dir.c_str());
124         DIR *dir = opendir (refserv.c_str());
125         if (dir == NULL){
126                 fprintf (stderr,"Can't open directory %s (%s)\n",refserv.c_str()
127                         ,strerror(errno));
128         }else{
129                 logical_dir += "/";
130                 bool copy_files = !vbuild_inside(excldirs,logical_dir.c_str());
131                 struct dirent *ent;
132                 ret = 0;
133                 while (ret == 0 && (ent=readdir(dir))!=NULL){
134                         if (strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0){
135                                 continue;
136                         }
137                         string file = refserv + "/" + ent->d_name;
138                         struct stat st;
139                         if (vutil_lstat(file.c_str(),st) == -1){
140                                 ret = -1;
141                         }else if (st.st_dev != dev){
142                                 if (debug > 0) printf ("Ignore sub-directory %s\n",file.c_str());
143                         }else{
144                                 string newfile = newserv + "/" + ent->d_name;
145                                 if (S_ISDIR(st.st_mode)){
146                                         if (vbuild_mkdir (newfile.c_str(),st.st_mode)==-1){
147                                                 fprintf (stderr,"Can't mkdir %s (%s)\n"
148                                                         ,newfile.c_str(),strerror(errno));
149                                                 ret = -1;
150                                         }else{
151                                                 stats.nbdir++;
152                                                 if (vbuild_chown(newfile.c_str(),st.st_uid,st.st_gid)==-1){
153                                                         fprintf (stderr,"Can't chown %s (%s)\n"
154                                                                 ,newfile.c_str(),strerror(errno));
155                                                         ret = -1;
156                                                 }
157                                                 ret |= vbuild_copy (file,newfile,dev
158                                                         ,logical_dir + ent->d_name,files);
159                                         }
160                                 }else if (S_ISLNK(st.st_mode)){
161                                         char path[PATH_MAX];
162                                         int len = readlink(file.c_str(),path,sizeof(path)-1);
163                                         if (len < 0){
164                                                 fprintf (stderr,"Can't readlink %s (%s)\n"
165                                                         ,file.c_str(),strerror(errno));
166                                                 ret = -1;
167                                         }else{
168                                                 path[len] = '\0';
169                                                 stats.nbsymlink++;
170                                                 if (vbuild_symlink (path,newfile.c_str())==-1){
171                                                         fprintf (stderr,"Can't symlink %s to %s (%s)\n",
172                                                                 newfile.c_str(),path,strerror(errno));
173                                                 }
174                                         }
175                                 }else if (S_ISBLK(st.st_mode)
176                                         || S_ISCHR(st.st_mode)
177                                         || S_ISFIFO(st.st_mode)){
178                                         stats.nbspc++;
179                                         if (vbuild_mknod (newfile.c_str(),st.st_mode,st.st_rdev)==-1){
180                                                 fprintf (stderr,"Can't mknod %s (%s)\n"
181                                                         ,newfile.c_str(),strerror(errno));
182                                                 ret = -1;
183                                         }
184                                 }else if (S_ISSOCK(st.st_mode)){
185                                         // Do nothing
186                                 }else if (copy_files){
187                                         // Ok, this is a file. We either copy it or do a link
188                                         string logical_file = logical_dir + ent->d_name;
189                                         if (files.find (logical_file)==files.end()){
190                                                 if (debug > 1) printf ("Copying file %s\n",file.c_str());
191                                                 if (vbuild_file_copy (file.c_str(),newfile.c_str(),st)==-1){
192                                                         fprintf (stderr,"Can't copy %s to %s (%s)\n",
193                                                                 file.c_str(),newfile.c_str(),strerror(errno));
194                                                         ret = -1;
195                                                 }else{
196                                                         stats.size_copy += st.st_size;
197                                                         stats.nbcopy++;
198                                                 }
199                                         }else{
200                                                 if (debug > 2) printf ("Linking file %s\n",file.c_str());
201                                                 setext2flag (file.c_str(),false,ext2flags);
202                                                 stats.nblink++;
203                                                 if (vbuild_link (file.c_str(),newfile.c_str())==-1){
204                                                         fprintf (stderr,"Can't link %s to %s (%s)\n",
205                                                                 file.c_str(),newfile.c_str(),strerror(errno));
206                                                         ret = -1;
207                                                 }
208                                                 setext2flag (file.c_str(),true,ext2flags);
209                                         }
210                                 }
211                         }
212                 }
213                 closedir(dir);
214         }
215         return ret;
216 }
217
218 int main (int argc, char *argv[])
219 {
220         int ret = -1;
221         bool statistics = false;
222         int i;
223         for (i=1; i<argc; i++){
224                 const char *arg = argv[i];
225                 //const char *opt = argv[i+1];
226                 if (strcmp(arg,"--test")==0){
227                         testmode = true;
228                 }else if (strcmp(arg,"--debug")==0){
229                         debug++;
230                 }else if (strcmp(arg,"--stats")==0){
231                         statistics = true;
232                 }else if (strcmp(arg,"--noflags")==0){
233                         ext2flags = 0;
234                 }else if (strcmp(arg,"--immutable")==0){
235                         ext2flags |= EXT2_IMMUTABLE_FILE_FL;
236                 }else if (strcmp(arg,"--immutable-mayunlink")==0){
237                         ext2flags |= EXT2_IMMUTABLE_LINK_FL;
238                 }else if (strcmp(arg,"--excldir")==0){
239                         i++;
240                         excldirs.push_back (EXCLDIR(argv[i]));
241                 }else{
242                         break;
243                 }
244         }
245         if (i!=argc-2){
246                 usage();
247         }else{
248                 string refserv = argv[i++];
249                 string newserv = argv[i];
250                 list<Package> packages;
251                 // Load the files which are not configuration files from
252                 // the packages
253                 vutil_loadallpkg (refserv,packages);
254                 set<string> files;
255                 for (list<Package>::iterator it=packages.begin(); it!=packages.end(); it++){
256                         (*it).loadfiles(refserv,files);
257                 }
258                 // Now, we do a recursive copy of refserv into newserv
259                 umask (0);
260                 mkdir (newserv.c_str(),0755);
261                 // Check if it is on the same volume
262                 struct stat refst,newst;
263                 if (vutil_lstat(refserv,refst)!=-1
264                         && vutil_lstat(newserv,newst)!=1){
265                         if (refst.st_dev != newst.st_dev){
266                                 fprintf (stderr,"Can't vbuild %s because it is not on the same volume as %s\n"
267                                         ,newserv.c_str(),refserv.c_str());
268                         }else{
269                                 stats.nbdir = stats.nblink = stats.nbcopy = stats.nbsymlink = 0;
270                                 stats.nbspc = 0;
271                                 stats.size_copy = 0;
272                                 ret = vbuild_copy (refserv,newserv,refst.st_dev,"",files);
273                                 if (statistics){
274                                         printf ("Directory created: %d\n",stats.nbdir);
275                                         printf ("Files copied     : %d\n",stats.nbcopy);
276                                         printf ("Bytes copied     : %ld\n",stats.size_copy);
277                                         printf ("Files linked     : %d\n",stats.nblink);
278                                         printf ("Files symlinked  : %d\n",stats.nbsymlink);
279                                         printf ("Special files    : %d\n",stats.nbspc);
280                                 }
281                         }
282                 }
283         }
284         return ret;
285 }
286
287