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
30 #include <sys/ioctl.h>
31 #include <linux/ext2_fs.h>
36 int file_copy (const char *src, const char *dst, struct stat &st)
39 FILE *fin = fopen (src,"r");
41 fprintf (stderr,"Can't open file %s (%s)\n",src,strerror(errno));
43 FILE *fout = fopen (dst,"w");
45 fprintf (stderr,"Can't open file %s (%s)\n",dst,strerror(errno));
49 while ((len=fread(buf,1,sizeof(buf),fin))>0){
50 fwrite (buf,1,len,fout);
54 if (fchown (fileno(fout),st.st_uid,st.st_gid)==-1){
55 fprintf (stderr,"Can't chown file %s (%s)\n"
56 ,dst,strerror(errno));
58 }else if (fchmod (fileno(fout),st.st_mode)==-1){
59 fprintf (stderr,"Can't chmod file %s (%s)\n"
60 ,dst,strerror(errno));
64 struct utimbuf timbuf;
65 timbuf.modtime = st.st_mtime;
66 timbuf.actime = st.st_atime;
67 if (utime (dst,&timbuf)==-1){
68 fprintf (stderr,"Can't set time stamp on file %s (%s)\n"
69 ,dst,strerror(errno));
78 Set the immutable flag on a file
80 int setext2flag (const char *fname, bool set, int ext2flags)
86 int fd = open (fname,O_RDONLY);
88 fprintf (stderr,"Can't open file %s (%s)\n",fname
91 int flags = set ? ext2flags : 0;
92 ret = ioctl (fd,EXT2_IOC_SETFLAGS,&flags);
95 fprintf (stderr,"Can't %s immutable flag on file %s (^s)\n"
96 ,(set ? "set" : "unset")
105 int vbuild_mkdir (const char *path, mode_t mode)
109 printf ("mkdir %s; chmod %o %s\n",path,mode,path);
112 ret = mkdir (path,mode);
113 if (ret == -1 && errno == EEXIST){
115 if (lstat(path,&st)!=-1 && S_ISDIR(st.st_mode)){
116 ret = chmod (path,mode);
123 int vbuild_mknod(const char *path, mode_t mode, dev_t dev)
127 printf ("mknod %s %o %02x:%02x\n",path,mode,major(dev),minor(dev));
130 ret = mknod (path,mode,dev);
131 if (ret == -1 && errno == EEXIST){
134 if (lstat(path,&st)!=-1
135 && (st.st_mode & S_IFMT) == (mode & S_IFMT)
136 && st.st_rdev == dev){
137 ret = chmod (path,mode);
143 int vbuild_symlink(const char *src, const char *dst)
147 printf ("ln -s %s %s\n",src,dst);
150 ret = symlink (src,dst);
155 int vbuild_link(const char *src, const char *dst)
159 printf ("ln %s %s\n",src,dst);
162 ret = link (src,dst);
167 int vbuild_unlink(const char *path)
171 printf ("unlink %s\n",path);
179 int vbuild_chown(const char *path, uid_t uid, gid_t gid)
183 printf ("chown %d.%d %s\n",uid,gid,path);
186 ret = chown (path,uid,gid);
191 int vbuild_file_copy(
198 printf ("cp -a %s %s\n",src,dst);
201 ret = file_copy (src,dst,st);
207 Load the list of all packages in a vserver
209 void vutil_loadallpkg (string &refserver, list<PACKAGE> &packages)
211 FILE *fin = vutil_execdistcmd (K_PKGVERSION,refserver,NULL);
214 while (fgets(line,sizeof(line)-1,fin)!=NULL){
215 int last = strlen(line)-1;
216 if (last >= 0 && line[last] == '\n') line[last] = '\0';
217 packages.push_back (PACKAGE(line));
223 int vutil_lstat (string path, struct stat &st)
226 if (lstat(path.c_str(),&st) == -1){
227 fprintf (stderr,"Can't lstat file %s (%s)\n"
228 ,path.c_str(),strerror(errno));
234 const char K_PKGVERSION[]="pkgversion";
235 const char K_DUMPFILES[]="dumpfiles";
236 const char K_UNIFILES[]="unifiles";
238 FILE *vutil_execdistcmd (const char *key, const string &vserver, const char *args)
240 string cmd = PKGLIBDIR "/distrib-info ";
248 FILE *ret = popen (cmd.c_str(),"r");
250 fprintf (stderr,"Can't execute command %s\n",cmd.c_str());
254 while (fgets(buf,sizeof(buf)-1,fin)!=NULL){
255 int last = strlen(buf)-1;
256 if (last >= 0) buf[last] = '\0';