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