CRUX-ARM : Home

Home :: Documentation :: Download :: Development :: Community :: Ports :: Packages :: Bugs :: Links :: About :: Donors
a5721fee0a8ffdb7218b69aa0e83c3888924f8f2
[pkgutils-cross.git] / src / pkgadd.cc
1 //
2 // pkgutils
3 //
4 // Copyright (c) 2000-2005 Per Liden
5 // Copyright (c) 2006-2010 by CRUX team (http://crux.nu)
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21 //
22
23 #include "pkgadd.h"
24 #include <fstream>
25 #include <iterator>
26 #include <cstdio>
27 #include <regex.h>
28 #include <unistd.h>
29
30 void pkgadd::run(int argc, char** argv)
31 {
32 //
33 // Check command line options
34 //
35 string o_root;
36 string o_config;
37 string o_package;
38 bool o_upgrade = false;
39 bool o_force = false;
40
41 for (int i = 1; i < argc; i++) {
42 string option(argv[i]);
43 if (option == "-r" || option == "--root") {
44 assert_argument(argv, argc, i);
45 o_root = argv[i + 1];
46 i++;
47 } else if (option == "-c" || option == "--config") {
48 assert_argument(argv, argc, i);
49 o_config = argv[i + 1];
50 i++;
51 } else if (option == "-u" || option == "--upgrade") {
52 o_upgrade = true;
53 } else if (option == "-f" || option == "--force") {
54 o_force = true;
55 } else if (option[0] == '-' || !o_package.empty()) {
56 throw runtime_error("invalid option " + option);
57 } else {
58 o_package = option;
59 }
60 }
61
62 if (o_package.empty())
63 throw runtime_error("option missing");
64
65 //
66 // Check UID
67 //
68 if (getuid())
69 throw runtime_error("only root can install/upgrade packages");
70
71 //
72 // Install/upgrade package
73 //
74 {
75 db_lock lock(o_root, true);
76 db_open(o_root);
77
78 pair<string, pkginfo_t> package = pkg_open(o_package);
79 vector<rule_t> config_rules = read_config(o_config);
80
81 bool installed = db_find_pkg(package.first);
82 if (installed && !o_upgrade)
83 throw runtime_error("package " + package.first + " already installed (use -u to upgrade)");
84 else if (!installed && o_upgrade)
85 throw runtime_error("package " + package.first + " not previously installed (skip -u to install)");
86
87 set<string> non_install_files = apply_install_rules(package.first, package.second, config_rules);
88 set<string> conflicting_files = db_find_conflicts(package.first, package.second);
89
90 if (!conflicting_files.empty()) {
91 if (o_force) {
92 set<string> keep_list;
93 if (o_upgrade) // Don't remove files matching the rules in configuration
94 keep_list = make_keep_list(conflicting_files, config_rules);
95 db_rm_files(conflicting_files, keep_list); // Remove unwanted conflicts
96 } else {
97 copy(conflicting_files.begin(), conflicting_files.end(), ostream_iterator<string>(cerr, "\n"));
98 throw runtime_error("listed file(s) already installed (use -f to ignore and overwrite)");
99 }
100 }
101
102 set<string> keep_list;
103
104 if (o_upgrade) {
105 keep_list = make_keep_list(package.second.files, config_rules);
106 db_rm_pkg(package.first, keep_list);
107 }
108
109 db_add_pkg(package.first, package.second);
110 db_commit();
111 pkg_install(o_package, keep_list, non_install_files);
112 ldconfig();
113 }
114 }
115
116 void pkgadd::print_help() const
117 {
118 cout << "usage: " << utilname << " [options] <file>" << endl
119 << "options:" << endl
120 << " -u, --upgrade upgrade package with the same name" << endl
121 << " -f, --force force install, overwrite conflicting files" << endl
122 << " -r, --root <path> specify alternative installation root" << endl
123 << " -c, --config <file> use alternate configuration file" << endl
124 << " -v, --version print version and exit" << endl
125 << " -h, --help print help and exit" << endl;
126 }
127
128 vector<rule_t> pkgadd::read_config(string file) const
129 {
130 vector<rule_t> rules;
131 unsigned int linecount = 0;
132 string filename = root + PKGADD_CONF;
133
134 if (!file.empty()) filename = file;
135 ifstream in(filename.c_str());
136
137 if (in) {
138 while (!in.eof()) {
139 string line;
140 getline(in, line);
141 linecount++;
142 if (!line.empty() && line[0] != '#') {
143 if (line.length() >= PKGADD_CONF_MAXLINE)
144 throw runtime_error(filename + ":" + itos(linecount) + ": line too long, aborting");
145
146 char event[PKGADD_CONF_MAXLINE];
147 char pattern[PKGADD_CONF_MAXLINE];
148 char action[PKGADD_CONF_MAXLINE];
149 char dummy[PKGADD_CONF_MAXLINE];
150 if (sscanf(line.c_str(), "%s %s %s %s", event, pattern, action, dummy) != 3)
151 throw runtime_error(filename + ":" + itos(linecount) + ": wrong number of arguments, aborting");
152
153 if (!strcmp(event, "UPGRADE") || !strcmp(event, "INSTALL")) {
154 rule_t rule;
155 rule.event = strcmp(event, "UPGRADE") ? INSTALL : UPGRADE;
156 rule.pattern = pattern;
157 if (!strcmp(action, "YES")) {
158 rule.action = true;
159 } else if (!strcmp(action, "NO")) {
160 rule.action = false;
161 } else
162 throw runtime_error(filename + ":" + itos(linecount) + ": '" +
163 string(action) + "' unknown action, should be YES or NO, aborting");
164
165 rules.push_back(rule);
166 } else
167 throw runtime_error(filename + ":" + itos(linecount) + ": '" +
168 string(event) + "' unknown event, aborting");
169 }
170 }
171 in.close();
172 }
173
174 #ifndef NDEBUG
175 cerr << "Configuration:" << endl;
176 for (vector<rule_t>::const_iterator j = rules.begin(); j != rules.end(); j++) {
177 cerr << "\t" << (*j).pattern << "\t" << (*j).action << endl;
178 }
179 cerr << endl;
180 #endif
181
182 return rules;
183 }
184
185 set<string> pkgadd::make_keep_list(const set<string>& files, const vector<rule_t>& rules) const
186 {
187 set<string> keep_list;
188 vector<rule_t> found;
189
190 find_rules(rules, UPGRADE, found);
191
192 for (set<string>::const_iterator i = files.begin(); i != files.end(); i++) {
193 for (vector<rule_t>::reverse_iterator j = found.rbegin(); j != found.rend(); j++) {
194 if (rule_applies_to_file(*j, *i)) {
195 if (!(*j).action)
196 keep_list.insert(keep_list.end(), *i);
197
198 break;
199 }
200 }
201 }
202
203 #ifndef NDEBUG
204 cerr << "Keep list:" << endl;
205 for (set<string>::const_iterator j = keep_list.begin(); j != keep_list.end(); j++) {
206 cerr << " " << (*j) << endl;
207 }
208 cerr << endl;
209 #endif
210
211 return keep_list;
212 }
213
214 set<string> pkgadd::apply_install_rules(const string& name, pkginfo_t& info, const vector<rule_t>& rules)
215 {
216 // TODO: better algo(?)
217 set<string> install_set;
218 set<string> non_install_set;
219 vector<rule_t> found;
220
221 find_rules(rules, INSTALL, found);
222
223 for (set<string>::const_iterator i = info.files.begin(); i != info.files.end(); i++) {
224 bool install_file = true;
225
226 for (vector<rule_t>::reverse_iterator j = found.rbegin(); j != found.rend(); j++) {
227 if (rule_applies_to_file(*j, *i)) {
228 install_file = (*j).action;
229 break;
230 }
231 }
232
233 if (install_file)
234 install_set.insert(install_set.end(), *i);
235 else
236 non_install_set.insert(*i);
237 }
238
239 info.files.clear();
240 info.files = install_set;
241
242 #ifndef NDEBUG
243 cerr << "Install set:" << endl;
244 for (set<string>::iterator j = info.files.begin(); j != info.files.end(); j++) {
245 cerr << " " << (*j) << endl;
246 }
247 cerr << endl;
248
249 cerr << "Non-Install set:" << endl;
250 for (set<string>::iterator j = non_install_set.begin(); j != non_install_set.end(); j++) {
251 cerr << " " << (*j) << endl;
252 }
253 cerr << endl;
254 #endif
255
256 return non_install_set;
257 }
258
259 void pkgadd::find_rules(const vector<rule_t>& rules, rule_event_t event, vector<rule_t>& found) const
260 {
261 for (vector<rule_t>::const_iterator i = rules.begin(); i != rules.end(); i++)
262 if (i->event == event)
263 found.push_back(*i);
264 }
265
266 bool pkgadd::rule_applies_to_file(const rule_t& rule, const string& file) const
267 {
268 regex_t preg;
269 bool ret;
270
271 if (regcomp(&preg, rule.pattern.c_str(), REG_EXTENDED | REG_NOSUB))
272 throw runtime_error("error compiling regular expression '" + rule.pattern + "', aborting");
273
274 ret = !regexec(&preg, file.c_str(), 0, 0, 0);
275 regfree(&preg);
276
277 return ret;
278 }