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
38 #include <sys/ioctl.h>
46 int file_copy (const char *src, const char *dst, struct stat &st)
49 FILE *fin = fopen (src,"r");
51 fprintf (stderr,"Can't open file %s (%s)\n",src,strerror(errno));
53 FILE *fout = fopen (dst,"w");
55 fprintf (stderr,"Can't open file %s (%s)\n",dst,strerror(errno));
59 while ((len=fread(buf,1,sizeof(buf),fin))>0){
60 fwrite (buf,1,len,fout);
64 if (fchown (fileno(fout),st.st_uid,st.st_gid)==-1){
65 fprintf (stderr,"Can't chown file %s (%s)\n"
66 ,dst,strerror(errno));
68 }else if (fchmod (fileno(fout),st.st_mode)==-1){
69 fprintf (stderr,"Can't chmod file %s (%s)\n"
70 ,dst,strerror(errno));
74 struct utimbuf timbuf;
75 timbuf.modtime = st.st_mtime;
76 timbuf.actime = st.st_atime;
77 if (utime (dst,&timbuf)==-1){
78 fprintf (stderr,"Can't set time stamp on file %s (%s)\n"
79 ,dst,strerror(errno));
88 Set the immutable flag on a file
90 int setext2flag (const char *fname, bool set, int ext2flags)
96 int fd = open (fname,O_RDONLY);
98 fprintf (stderr,"Can't open file %s (%s)\n",fname
101 int flags = set ? ext2flags : 0;
102 ret = ioctl (fd,EXT2_IOC_SETFLAGS,&flags);
105 fprintf (stderr,"Can't %s immutable flag on file %s (%s)\n"
106 ,(set ? "set" : "unset")
115 int vbuild_mkdir (const char *path, mode_t mode)
119 printf ("mkdir %s; chmod %o %s\n",path,mode,path);
122 ret = mkdir (path,mode);
123 if (ret == -1 && errno == EEXIST){
125 if (lstat(path,&st)!=-1 && S_ISDIR(st.st_mode)){
126 ret = chmod (path,mode);
133 int vbuild_mknod(const char *path, mode_t mode, dev_t dev)
137 printf ("mknod %s %o %02x:%02x\n",path,mode,major(dev),minor(dev));
140 ret = mknod (path,mode,dev);
141 if (ret == -1 && errno == EEXIST){
144 if (lstat(path,&st)!=-1
145 && (st.st_mode & S_IFMT) == (mode & S_IFMT)
146 && st.st_rdev == dev){
147 ret = chmod (path,mode);
153 int vbuild_symlink(const char *src, const char *dst)
157 printf ("ln -s %s %s\n",src,dst);
160 ret = symlink (src,dst);
165 int vbuild_link(const char *src, const char *dst)
169 printf ("ln %s %s\n",src,dst);
172 ret = link (src,dst);
177 int vbuild_unlink(const char *path)
181 printf ("unlink %s\n",path);
189 int vbuild_chown(const char *path, uid_t uid, gid_t gid)
193 printf ("chown %d.%d %s\n",uid,gid,path);
196 ret = chown (path,uid,gid);
201 int vbuild_file_copy(
208 printf ("cp -a %s %s\n",src,dst);
211 ret = file_copy (src,dst,st);
217 Load the list of all packages in a vserver
219 void vutil_loadallpkg (string &refserver, list<PACKAGE> &packages)
221 FILE *fin = vutil_execdistcmd (K_PKGVERSION,refserver,NULL);
224 while (fgets(line,sizeof(line)-1,fin)!=NULL){
225 int last = strlen(line)-1;
226 if (last >= 0 && line[last] == '\n') line[last] = '\0';
227 packages.push_back (PACKAGE(line));
233 int vutil_lstat (string path, struct stat &st)
236 if (lstat(path.c_str(),&st) == -1){
237 fprintf (stderr,"Can't lstat file %s (%s)\n"
238 ,path.c_str(),strerror(errno));
244 const char K_PKGVERSION[]="pkgversion";
245 const char K_DUMPFILES[]="dumpfiles";
246 const char K_UNIFILES[]="unifiles";
248 FILE *vutil_execdistcmd (const char *key, const string &vserver, const char *args)
250 string cmd = PKGLIBDIR "/distrib-info ";
258 FILE *ret = popen (cmd.c_str(),"r");
260 fprintf (stderr,"Can't execute command %s\n",cmd.c_str());
264 while (fgets(buf,sizeof(buf)-1,fin)!=NULL){
265 int last = strlen(buf)-1;
266 if (last >= 0) buf[last] = '\0';