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