- moved here from sysv/
[util-vserver.git] / src / vfiles.cc
1 // $Id: vfiles.cc,v 1.1 2003/09/29 22:01:57 ensc Exp $
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vfiles.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 extract the list of non unified files in
22         a vserver.
23 */
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <sys/ioctl.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <dirent.h>
31
32 #include <string>
33 #include <list>
34 #include <set>
35 #include "vutil.h"
36
37 using namespace std;
38
39 static bool ignorelink = false;
40
41
42 static void usage()
43 {
44         cerr <<
45                 "vfiles version " << VERSION <<
46                 "\n\n"
47                 "vfiles [ options ] reference-server vserver\n"
48                 "\n"
49                 "--debug: Prints some debugging messages.\n"
50                 "--ignorelink: Do not print symbolic links (they are never unified)\n"
51                 "\n"
52                 ;
53 }
54
55
56 static int vfiles_walk (
57         string absdir,
58         dev_t dev,                      // We stay on the same volume
59         string logical_dir,
60         set<string> &files)
61 {
62         int ret = -1;
63         if (debug > 0) printf ("Entering directory %s\n",logical_dir.c_str());
64         DIR *dir = opendir (absdir.c_str());
65         if (dir == NULL){
66                 fprintf (stderr,"Can't open directory %s (%s)\n",absdir.c_str()
67                         ,strerror(errno));
68         }else{
69                 struct dirent *ent;
70                 ret = 0;
71                 while (ret == 0 && (ent=readdir(dir))!=NULL){
72                         if (strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0){
73                                 continue;
74                         }
75                         string file = absdir + "/" + ent->d_name;
76                         struct stat st;
77                         if (vutil_lstat(file,st) == -1){
78                                 ret = -1;
79                         }else if (st.st_dev != dev){
80                                 if (debug > 0) printf ("Ignore sub-directory %s\n",file.c_str());
81                         }else{
82                                 if (S_ISDIR(st.st_mode)){
83                                         ret |= vfiles_walk (file,dev
84                                                         ,logical_dir + "/" + ent->d_name,files);
85                                 }else if (S_ISLNK(st.st_mode)){
86                                         if (!ignorelink) printf ("%s\n",file.c_str());  
87                                 }else if (S_ISBLK(st.st_mode)
88                                         || S_ISCHR(st.st_mode)
89                                         || S_ISFIFO(st.st_mode)){
90                                         printf ("%s\n",file.c_str());   
91                                 }else if (S_ISSOCK(st.st_mode)){
92                                         // Do nothing
93                                 }else{
94                                         // Ok, this is a file. We either copy it or do a link
95                                         string logical_file = logical_dir + "/" + ent->d_name;
96                                         if (files.find (logical_file)==files.end()){
97                                                 printf ("%s\n",file.c_str());   
98                                         }
99                                 }
100                         }
101                 }
102                 closedir(dir);
103         }
104         return ret;
105 }
106
107 int main (int argc, char *argv[])
108 {
109         int ret = -1;
110         int i;
111         for (i=1; i<argc; i++){
112                 const char *arg = argv[i];
113                 //const char *opt = argv[i+1];
114                 if (strcmp(arg,"--debug")==0){
115                         debug++;
116                 }else if (strcmp(arg,"--ignorelink")==0){
117                         ignorelink=true;
118                 }else{
119                         break;
120                 }
121         }
122         if (i!=argc-2){
123                 usage();
124         }else{
125                 string refserv = argv[i++];
126                 string newserv = argv[i];
127                 list<PACKAGE> packages;
128                 // Load the files which are not configuration files from
129                 // the packages
130                 vutil_loadallpkg (refserv,packages);
131                 set<string> files;
132                 for (list<PACKAGE>::iterator it=packages.begin(); it!=packages.end(); it++){
133                         (*it).loadfiles(refserv,files);
134                 }
135                 struct stat st;
136                 if (vutil_lstat(newserv,st)!=-1){
137                         // Now, we do a recursive walk of newserv and prints
138                         // all files not unifiable
139                         ret = vfiles_walk (newserv,st.st_dev,"",files);
140                 }
141         }
142         return ret;
143 }
144
145
146