1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
#include "config.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include "remove.h"
#include "alpm_list.h"
#include "alpm.h"
#include "trans.h"
#include "util.h"
#include "log.h"
#include "backup.h"
#include "package.h"
#include "db.h"
#include "deps.h"
#include "handle.h"
int SYMEXPORT alpm_remove_pkg(alpm_handle_t *handle, pmpkg_t *pkg)
{
const char *pkgname;
pmtrans_t *trans;
CHECK_HANDLE(handle, return -1);
ASSERT(pkg != NULL, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
ASSERT(handle == pkg->handle, RET_ERR(handle, PM_ERR_WRONG_ARGS, -1));
trans = handle->trans;
ASSERT(trans != NULL, RET_ERR(handle, PM_ERR_TRANS_NULL, -1));
ASSERT(trans->state == STATE_INITIALIZED,
RET_ERR(handle, PM_ERR_TRANS_NOT_INITIALIZED, -1));
pkgname = pkg->name;
if(_alpm_pkg_find(trans->remove, pkgname)) {
RET_ERR(handle, PM_ERR_TRANS_DUP_TARGET, -1);
}
_alpm_log(handle, PM_LOG_DEBUG, "adding package %s to the transaction remove list\n",
pkgname);
trans->remove = alpm_list_add(trans->remove, _alpm_pkg_dup(pkg));
return 0;
}
static void remove_prepare_cascade(alpm_handle_t *handle, alpm_list_t *lp)
{
pmtrans_t *trans = handle->trans;
while(lp) {
alpm_list_t *i;
for(i = lp; i; i = i->next) {
pmdepmissing_t *miss = (pmdepmissing_t *)i->data;
pmpkg_t *info = _alpm_db_get_pkgfromcache(handle->db_local, miss->target);
if(info) {
if(!_alpm_pkg_find(trans->remove, alpm_pkg_get_name(info))) {
_alpm_log(handle, PM_LOG_DEBUG, "pulling %s in target list\n",
alpm_pkg_get_name(info));
trans->remove = alpm_list_add(trans->remove, _alpm_pkg_dup(info));
}
} else {
_alpm_log(handle, PM_LOG_ERROR, _("could not find %s in database -- skipping\n"),
miss->target);
}
}
alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
alpm_list_free(lp);
lp = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
trans->remove, NULL, 1);
}
}
static void remove_prepare_keep_needed(alpm_handle_t *handle, alpm_list_t *lp)
{
pmtrans_t *trans = handle->trans;
while(lp != NULL) {
alpm_list_t *i;
for(i = lp; i; i = i->next) {
pmdepmissing_t *miss = (pmdepmissing_t *)i->data;
void *vpkg;
pmpkg_t *pkg = _alpm_pkg_find(trans->remove, miss->causingpkg);
if(pkg == NULL) {
continue;
}
trans->remove = alpm_list_remove(trans->remove, pkg, _alpm_pkg_cmp,
&vpkg);
pkg = vpkg;
if(pkg) {
_alpm_log(handle, PM_LOG_WARNING, _("removing %s from target list\n"),
alpm_pkg_get_name(pkg));
_alpm_pkg_free(pkg);
}
}
alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
alpm_list_free(lp);
lp = alpm_checkdeps(handle, _alpm_db_get_pkgcache(handle->db_local),
trans->remove, NULL, 1);
}
}
int _alpm_remove_prepare(alpm_handle_t *handle, alpm_list_t **data)
{
alpm_list_t *lp;
pmtrans_t *trans = handle->trans;
alpm_db_t *db = handle->db_local;
if((trans->flags & PM_TRANS_FLAG_RECURSE) && !(trans->flags & PM_TRANS_FLAG_CASCADE)) {
_alpm_log(handle, PM_LOG_DEBUG, "finding removable dependencies\n");
_alpm_recursedeps(db, trans->remove,
trans->flags & PM_TRANS_FLAG_RECURSEALL);
}
if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
EVENT(trans, PM_TRANS_EVT_CHECKDEPS_START, NULL, NULL);
_alpm_log(handle, PM_LOG_DEBUG, "looking for unsatisfied dependencies\n");
lp = alpm_checkdeps(handle, _alpm_db_get_pkgcache(db), trans->remove, NULL, 1);
if(lp != NULL) {
if(trans->flags & PM_TRANS_FLAG_CASCADE) {
remove_prepare_cascade(handle, lp);
} else if(trans->flags & PM_TRANS_FLAG_UNNEEDED) {
remove_prepare_keep_needed(handle, lp);
} else {
if(data) {
*data = lp;
} else {
alpm_list_free_inner(lp, (alpm_list_fn_free)_alpm_depmiss_free);
alpm_list_free(lp);
}
RET_ERR(handle, PM_ERR_UNSATISFIED_DEPS, -1);
}
}
}
_alpm_log(handle, PM_LOG_DEBUG, "sorting by dependencies\n");
lp = _alpm_sortbydeps(handle, trans->remove, 1);
alpm_list_free(trans->remove);
trans->remove = lp;
if((trans->flags & PM_TRANS_FLAG_CASCADE) && (trans->flags & PM_TRANS_FLAG_RECURSE)) {
_alpm_log(handle, PM_LOG_DEBUG, "finding removable dependencies\n");
_alpm_recursedeps(db, trans->remove, trans->flags & PM_TRANS_FLAG_RECURSEALL);
}
if(!(trans->flags & PM_TRANS_FLAG_NODEPS)) {
EVENT(trans, PM_TRANS_EVT_CHECKDEPS_DONE, NULL, NULL);
}
return 0;
}
static int can_remove_file(alpm_handle_t *handle, const char *path,
alpm_list_t *skip_remove)
{
char file[PATH_MAX];
snprintf(file, PATH_MAX, "%s%s", handle->root, path);
if(alpm_list_find_str(skip_remove, file)) {
return 1;
}
if(access(file, W_OK) == -1) {
if(errno != EACCES && errno != ETXTBSY && access(file, F_OK) == 0) {
_alpm_log(handle, PM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
file, strerror(errno));
return 0;
}
}
return 1;
}
static void unlink_file(alpm_handle_t *handle, pmpkg_t *info, const char *filename,
alpm_list_t *skip_remove, int nosave)
{
struct stat buf;
char file[PATH_MAX];
snprintf(file, PATH_MAX, "%s%s", handle->root, filename);
if(alpm_list_find_str(skip_remove, filename)) {
_alpm_log(handle, PM_LOG_DEBUG, "%s is in skip_remove, skipping removal\n",
file);
return;
}
if(lstat(file, &buf)) {
_alpm_log(handle, PM_LOG_DEBUG, "file %s does not exist\n", file);
return;
}
if(S_ISDIR(buf.st_mode)) {
if(rmdir(file)) {
_alpm_log(handle, PM_LOG_DEBUG, "keeping directory %s\n", file);
} else {
_alpm_log(handle, PM_LOG_DEBUG, "removing directory %s\n", file);
}
} else {
pmbackup_t *backup = _alpm_needbackup(filename, alpm_pkg_get_backup(info));
if(backup) {
if(nosave) {
_alpm_log(handle, PM_LOG_DEBUG, "transaction is set to NOSAVE, not backing up '%s'\n", file);
} else {
char *filehash = alpm_compute_md5sum(file);
int cmp = filehash ? strcmp(filehash, backup->hash) : 0;
FREE(filehash);
if(cmp != 0) {
char newpath[PATH_MAX];
snprintf(newpath, PATH_MAX, "%s.pacsave", file);
rename(file, newpath);
_alpm_log(handle, PM_LOG_WARNING, _("%s saved as %s\n"), file, newpath);
alpm_logaction(handle, "warning: %s saved as %s\n", file, newpath);
return;
}
}
}
_alpm_log(handle, PM_LOG_DEBUG, "unlinking %s\n", file);
if(unlink(file) == -1) {
_alpm_log(handle, PM_LOG_ERROR, _("cannot remove file '%s': %s\n"),
filename, strerror(errno));
}
}
}
int _alpm_upgraderemove_package(alpm_handle_t *handle,
pmpkg_t *oldpkg, pmpkg_t *newpkg)
{
alpm_list_t *skip_remove, *b;
alpm_list_t *newfiles, *lp;
size_t filenum = 0;
alpm_list_t *files = alpm_pkg_get_files(oldpkg);
const char *pkgname = alpm_pkg_get_name(oldpkg);
_alpm_log(handle, PM_LOG_DEBUG, "removing old package first (%s-%s)\n",
oldpkg->name, oldpkg->version);
if(handle->trans->flags & PM_TRANS_FLAG_DBONLY) {
goto db;
}
skip_remove = alpm_list_join(
alpm_list_strdup(handle->trans->skip_remove),
alpm_list_strdup(handle->noupgrade));
alpm_list_t *filelist = alpm_pkg_get_files(newpkg);
for(b = alpm_pkg_get_backup(newpkg); b; b = b->next) {
const pmbackup_t *backup = b->data;
if(!alpm_list_find_str(filelist, backup->name)) {
continue;
}
_alpm_log(handle, PM_LOG_DEBUG, "adding %s to the skip_remove array\n",
backup->name);
skip_remove = alpm_list_add(skip_remove, strdup(backup->name));
}
for(lp = files; lp; lp = lp->next) {
if(!can_remove_file(handle, lp->data, skip_remove)) {
_alpm_log(handle, PM_LOG_DEBUG,
"not removing package '%s', can't remove all files\n", pkgname);
RET_ERR(handle, PM_ERR_PKG_CANT_REMOVE, -1);
}
filenum++;
}
_alpm_log(handle, PM_LOG_DEBUG, "removing %ld files\n", (unsigned long)filenum);
newfiles = alpm_list_reverse(files);
for(lp = newfiles; lp; lp = alpm_list_next(lp)) {
unlink_file(handle, oldpkg, lp->data, skip_remove, 0);
}
alpm_list_free(newfiles);
FREELIST(skip_remove);
db:
_alpm_log(handle, PM_LOG_DEBUG, "updating database\n");
_alpm_log(handle, PM_LOG_DEBUG, "removing database entry '%s'\n", pkgname);
if(_alpm_local_db_remove(handle->db_local, oldpkg) == -1) {
_alpm_log(handle, PM_LOG_ERROR, _("could not remove database entry %s-%s\n"),
pkgname, alpm_pkg_get_version(oldpkg));
}
if(_alpm_db_remove_pkgfromcache(handle->db_local, oldpkg) == -1) {
_alpm_log(handle, PM_LOG_ERROR, _("could not remove entry '%s' from cache\n"),
pkgname);
}
return 0;
}
int _alpm_remove_packages(alpm_handle_t *handle)
{
pmpkg_t *info;
alpm_list_t *targ, *lp;
size_t pkg_count;
pmtrans_t *trans = handle->trans;
pkg_count = alpm_list_count(trans->remove);
for(targ = trans->remove; targ; targ = targ->next) {
int position = 0;
char scriptlet[PATH_MAX];
info = (pmpkg_t *)targ->data;
const char *pkgname = NULL;
size_t targcount = alpm_list_count(targ);
if(trans->state == STATE_INTERRUPTED) {
return 0;
}
pkgname = alpm_pkg_get_name(info);
snprintf(scriptlet, PATH_MAX, "%s%s-%s/install",
_alpm_db_path(handle->db_local), pkgname, alpm_pkg_get_version(info));
EVENT(trans, PM_TRANS_EVT_REMOVE_START, info, NULL);
_alpm_log(handle, PM_LOG_DEBUG, "removing package %s-%s\n",
pkgname, alpm_pkg_get_version(info));
if(alpm_pkg_has_scriptlet(info) && !(trans->flags & PM_TRANS_FLAG_NOSCRIPTLET)) {
_alpm_runscriptlet(handle, scriptlet, "pre_remove",
alpm_pkg_get_version(info), NULL);
}
if(!(trans->flags & PM_TRANS_FLAG_DBONLY)) {
alpm_list_t *files = alpm_pkg_get_files(info);
alpm_list_t *newfiles;
size_t filenum = 0;
for(lp = files; lp; lp = lp->next) {
if(!can_remove_file(handle, lp->data, NULL)) {
_alpm_log(handle, PM_LOG_DEBUG, "not removing package '%s', can't remove all files\n",
pkgname);
RET_ERR(handle, PM_ERR_PKG_CANT_REMOVE, -1);
}
filenum++;
}
_alpm_log(handle, PM_LOG_DEBUG, "removing %ld files\n", (unsigned long)filenum);
PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, info->name, 0,
pkg_count, (pkg_count - targcount + 1));
newfiles = alpm_list_reverse(files);
for(lp = newfiles; lp; lp = alpm_list_next(lp)) {
int percent;
unlink_file(handle, info, lp->data, NULL, trans->flags & PM_TRANS_FLAG_NOSAVE);
percent = (position * 100) / filenum;
PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, info->name,
percent, pkg_count, (pkg_count - targcount + 1));
position++;
}
alpm_list_free(newfiles);
}
PROGRESS(trans, PM_TRANS_PROGRESS_REMOVE_START, pkgname, 100,
pkg_count, (pkg_count - targcount + 1));
if(alpm_pkg_has_scriptlet(info) && !(trans->flags & PM_TRANS_FLAG_NOSCRIPTLET)) {
_alpm_runscriptlet(handle, scriptlet, "post_remove",
alpm_pkg_get_version(info), NULL);
}
_alpm_log(handle, PM_LOG_DEBUG, "updating database\n");
_alpm_log(handle, PM_LOG_DEBUG, "removing database entry '%s'\n", pkgname);
if(_alpm_local_db_remove(handle->db_local, info) == -1) {
_alpm_log(handle, PM_LOG_ERROR, _("could not remove database entry %s-%s\n"),
pkgname, alpm_pkg_get_version(info));
}
if(_alpm_db_remove_pkgfromcache(handle->db_local, info) == -1) {
_alpm_log(handle, PM_LOG_ERROR, _("could not remove entry '%s' from cache\n"),
pkgname);
}
EVENT(trans, PM_TRANS_EVT_REMOVE_DONE, info, NULL);
}
_alpm_ldconfig(handle);
return 0;
}
|