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