90101c0989c6b831fbc788d86e58384a286dcc4b
[util-vserver.git] / src / vutil.h
1 // $Id: vutil.h,v 1.3 2003/10/21 13:23:28 ensc Exp $
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vutil.h 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 #pragma interface
21 #ifndef VUTIL_H
22 #define VUTIL_H
23
24 #include "vserver.hh"
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/wait.h>
29 #include <string>
30 #include <set>
31 #include <algorithm>
32 #include <iostream>
33 #include <list>
34
35 using namespace std;
36
37 extern int debug;
38 extern bool testmode;
39
40 // Patch to help compile this utility on unpatched kernel source
41 #ifndef EXT2_IMMUTABLE_FILE_FL
42         #define EXT2_IMMUTABLE_FILE_FL  0x00000010
43         /* Set both bits for backward compatibility */
44         #define EXT2_IMMUTABLE_LINK_FL  0x08008000
45 #endif
46
47
48 FILE *vutil_execdistcmd (const char *, Vserver const &, const char *);
49 extern const char K_DUMPFILES[];
50 extern const char K_UNIFILES[];
51 extern const char K_PKGVERSION[];
52
53 class Package{
54 public:
55         string name;
56         string version; // version + release
57         Package(string &_name, string &_version)
58                 : name (_name), version(_version)
59         {
60         }
61         Package(const char *_name, const char *_version)
62                 : name (_name), version(_version)
63         {
64         }
65         Package(const string &line)
66         {
67                 *this = line;
68         }
69         Package & operator = (const string &_line)
70         {
71                 string line (_line);
72                 string::iterator pos = find (line.begin(),line.end(),'=');
73                 if (pos != line.end()){
74                         name = string(line.begin(),pos);
75                         version = string(pos + 1,line.end());
76                 }
77                 return *this;
78         }
79         Package (const Package &pkg)
80         {
81                 name = pkg.name;
82                 version = pkg.version;
83         }
84         bool operator == (const Package &v) const
85         {
86                 return name == v.name && version == v.version;
87         }
88         bool operator < (const Package &v) const
89         {
90                 bool ret = false;
91                 if (name < v.name){
92                         ret = true;
93                 }else if (name == v.name && version < v.version){
94                         ret = true;
95                 }
96                 return ret;
97         }
98         // Load the file member of the package, but exclude configuration file
99         void loadfiles(Vserver const &ref, set<string> &files)
100         {
101                 if (debug > 2) cout << "Loading files for package " << name << endl;
102                 string namever = name + '-' + version;
103                 FILE *fin = vutil_execdistcmd (K_UNIFILES,ref,namever.c_str());
104                 if (fin != NULL){
105                         char tmp[1000];
106                         while (fgets(tmp,sizeof(tmp)-1,fin)!=NULL){
107                                 int last = strlen(tmp)-1;
108                                 if (last >= 0 && tmp[last] == '\n') tmp[last] = '\0';
109                                 files.insert (tmp);
110                         }
111                 }
112                 if (debug > 2) cout << "Done\n";
113         }
114         #if 0
115         bool locate(const string &path)
116         {
117                 return find (files.begin(),files.end(),path) != files.end();
118         }
119         #endif
120 };
121
122 // Check if two package have the same name (but potentially different version)
123 class same_name{
124         const Package &pkg;
125 public:
126         same_name(const Package &_pkg) : pkg(_pkg) {}
127         bool operator()(const Package &p)
128         {
129                 return pkg.name == p.name;
130         }
131 };
132
133
134 #include "vutil.p"
135
136 #endif
137