3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vutil.cc by Jacques Gelinas
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)
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.
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.
20 #pragma implementation
34 #include <sys/ioctl.h>
35 #include <linux/ext2_fs.h>
37 #include <pathconfig.h>
42 int file_copy (const char *src, const char *dst, struct stat &st)
45 FILE *fin = fopen (src,"r");
47 fprintf (stderr,"Can't open file %s (%s)\n",src,strerror(errno));
49 FILE *fout = fopen (dst,"w");
51 fprintf (stderr,"Can't open file %s (%s)\n",dst,strerror(errno));
55 while ((len=fread(buf,1,sizeof(buf),fin))>0){
56 fwrite (buf,1,len,fout);
60 if (fchown (fileno(fout),st.st_uid,st.st_gid)==-1){
61 fprintf (stderr,"Can't chown file %s (%s)\n"
62 ,dst,strerror(errno));
64 }else if (fchmod (fileno(fout),st.st_mode)==-1){
65 fprintf (stderr,"Can't chmod file %s (%s)\n"
66 ,dst,strerror(errno));
70 struct utimbuf timbuf;
71 timbuf.modtime = st.st_mtime;
72 timbuf.actime = st.st_atime;
73 if (utime (dst,&timbuf)==-1){
74 fprintf (stderr,"Can't set time stamp on file %s (%s)\n"
75 ,dst,strerror(errno));
84 Set the immutable flag on a file
86 int setext2flag (const char *fname, bool set, int ext2flags)
92 int fd = open (fname,O_RDONLY);
94 fprintf (stderr,"Can't open file %s (%s)\n",fname
97 int flags = set ? ext2flags : 0;
98 ret = ioctl (fd,EXT2_IOC_SETFLAGS,&flags);
101 fprintf (stderr,"Can't %s immutable flag on file %s (%s)\n"
102 ,(set ? "set" : "unset")
111 int vbuild_mkdir (const char *path, mode_t mode)
115 printf ("mkdir %s; chmod %o %s\n",path,mode,path);
118 ret = mkdir (path,mode);
119 if (ret == -1 && errno == EEXIST){
121 if (lstat(path,&st)!=-1 && S_ISDIR(st.st_mode)){
122 ret = chmod (path,mode);
129 int vbuild_mknod(const char *path, mode_t mode, dev_t dev)
133 printf ("mknod %s %o %02x:%02x\n",path,mode,major(dev),minor(dev));
136 ret = mknod (path,mode,dev);
137 if (ret == -1 && errno == EEXIST){
140 if (lstat(path,&st)!=-1
141 && (st.st_mode & S_IFMT) == (mode & S_IFMT)
142 && st.st_rdev == dev){
143 ret = chmod (path,mode);
149 int vbuild_symlink(const char *src, const char *dst)
153 printf ("ln -s %s %s\n",src,dst);
156 ret = symlink (src,dst);
161 int vbuild_link(const char *src, const char *dst)
165 printf ("ln %s %s\n",src,dst);
168 ret = link (src,dst);
173 int vbuild_unlink(const char *path)
177 printf ("unlink %s\n",path);
185 int vbuild_chown(const char *path, uid_t uid, gid_t gid)
189 printf ("chown %d.%d %s\n",uid,gid,path);
192 ret = chown (path,uid,gid);
197 int vbuild_file_copy(
204 printf ("cp -a %s %s\n",src,dst);
207 ret = file_copy (src,dst,st);
213 Load the list of all packages in a vserver
215 void vutil_loadallpkg (Vserver const &refserver, list<Package> &packages)
217 FILE *fin = vutil_execdistcmd (K_PKGVERSION,refserver,NULL);
220 while (fgets(line,sizeof(line)-1,fin)!=NULL){
221 int last = strlen(line)-1;
222 if (last >= 0 && line[last] == '\n') line[last] = '\0';
223 packages.push_back (Package(line));
229 int vutil_lstat (string path, struct stat &st)
232 if (lstat(path.c_str(),&st) == -1){
233 fprintf (stderr,"Can't lstat file %s (%s)\n"
234 ,path.c_str(),strerror(errno));
240 const char K_PKGVERSION[]="pkgversion";
241 const char K_DUMPFILES[]="dumpfiles";
242 const char K_UNIFILES[]="unifiles";
244 FILE *vutil_execdistcmd (const char *key, Vserver const &vserver, const char *args)
246 string cmd = PKGLIBDIR "/distrib-info ";
247 cmd += vserver.getConfDir();
254 FILE *ret = popen (cmd.c_str(),"r");
256 fprintf (stderr,"Can't execute command %s\n",cmd.c_str());
260 while (fgets(buf,sizeof(buf)-1,fin)!=NULL){
261 int last = strlen(buf)-1;
262 if (last >= 0) buf[last] = '\0';