summaryrefslogtreecommitdiffstats
path: root/lib/libalpm/remove.c
blob: c53ffa4de242e9f808d03b039408720cd10505b9 (plain)
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
/*
 *  remove.c
 * 
 *  Copyright (c) 2002-2005 by Judd Vinet <jvinet@zeroflux.org>
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
 *  USA.
 */

#include "config.h"
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#include <zlib.h>
#include <libtar.h>
/* pacman */
#include "util.h"
#include "error.h"
#include "rpmvercmp.h"
#include "md5.h"
#include "log.h"
#include "backup.h"
#include "package.h"
#include "db.h"
#include "cache.h"
#include "deps.h"
#include "provide.h"
#include "remove.h"
#include "handle.h"
#include "alpm.h"

extern pmhandle_t *handle;

int remove_loadtarget(pmdb_t *db, pmtrans_t *trans, char *name)
{
	pmpkg_t *info;

	ASSERT(db != NULL, PM_RET_ERR(PM_ERR_DB_NULL, -1));
	ASSERT(trans != NULL, PM_RET_ERR(PM_ERR_TRANS_NULL, -1));
	ASSERT(name != NULL, PM_RET_ERR(PM_ERR_WRONG_ARGS, -1));

	if((info = db_scan(db, name, INFRQ_ALL)) == NULL) {
		_alpm_log(PM_LOG_ERROR, "could not find %s in database", name);
		PM_RET_ERR(PM_ERR_PKG_NOT_FOUND, -1);
	}
	trans->packages = pm_list_add(trans->packages, info);

	return(0);
}

int remove_prepare(pmdb_t *db, pmtrans_t *trans, PMList **data)
{
	pmpkg_t *info;
	PMList *lp;

	ASSERT(db != NULL, PM_RET_ERR(PM_ERR_DB_NULL, -1));
	ASSERT(trans != NULL, PM_RET_ERR(PM_ERR_TRANS_NULL, -1));
	ASSERT(data != NULL, PM_RET_ERR(PM_ERR_WRONG_ARGS, -1));

	if(!(trans->flags & (PM_TRANS_FLAG_NODEPS)) && (trans->type != PM_TRANS_TYPE_UPGRADE)) {
		TRANS_CB(trans, PM_TRANS_CB_DEPS_START, NULL, NULL);

		if((lp = checkdeps(db, trans->type, trans->packages)) != NULL) {
			if(trans->flags & PM_TRANS_FLAG_CASCADE) {
				while(lp) {
					PMList *j;
					for(j = lp; j; j = j->next) {
						pmdepmissing_t* miss = (pmdepmissing_t*)j->data;
						info = db_scan(db, miss->depend.name, INFRQ_ALL);
						if(!pkg_isin(info, trans->packages)) {
							trans->packages = pm_list_add(trans->packages, info);
						}
					}
					FREELIST(lp);
					lp = checkdeps(db, trans->type, trans->packages);
				}
			} else {
				*data = lp;
				PM_RET_ERR(PM_ERR_UNSATISFIED_DEPS, -1);
			}
		}

		if(trans->flags & PM_TRANS_FLAG_RECURSE) {
			_alpm_log(PM_LOG_FLOW1, "finding removable dependencies...");
			trans->packages = removedeps(db, trans->packages);
		}

		TRANS_CB(trans, PM_TRANS_CB_DEPS_DONE, NULL, NULL);
	}

	return(0);
}

int remove_commit(pmdb_t *db, pmtrans_t *trans)
{
	pmpkg_t *info;
	struct stat buf;
	PMList *targ, *lp;
	char line[PATH_MAX+1];

	ASSERT(db != NULL, PM_RET_ERR(PM_ERR_DB_NULL, -1));
	ASSERT(trans != NULL, PM_RET_ERR(PM_ERR_TRANS_NULL, -1));

	for(targ = trans->packages; targ; targ = targ->next) {
		char pm_install[PATH_MAX];
		info = (pmpkg_t*)targ->data;

		if(trans->type != PM_TRANS_TYPE_UPGRADE) {
			TRANS_CB(trans, PM_TRANS_CB_REMOVE_START, info, NULL);

			/* run the pre-remove scriptlet if it exists  */
			snprintf(pm_install, PATH_MAX, "%s%s/%s/%s-%s/install", handle->root, handle->dbpath, db->treename, info->name, info->version);
			_alpm_runscriptlet(handle->root, pm_install, "pre_remove", info->version, NULL);
		}

		if(!(trans->flags & PM_TRANS_FLAG_DBONLY)) {
			/* iterate through the list backwards, unlinking files */
			for(lp = pm_list_last(info->files); lp; lp = lp->prev) {
				char *newpath = NULL;
				int nb = 0;
				if(_alpm_needbackup(lp->data, info->backup)) {
					nb = 1;
				}
				if(!nb && trans->type == PM_TRANS_TYPE_UPGRADE) {
					/* check noupgrade */
					if(pm_list_is_strin(lp->data, handle->noupgrade)) {
						nb = 1;
					}
				}
				snprintf(line, PATH_MAX, "%s%s", handle->root, (char*)lp->data);
				if(lstat(line, &buf)) {
					_alpm_log(PM_LOG_ERROR, "file %s does not exist", line);
					continue;
				}
				if(S_ISDIR(buf.st_mode)) {
					_alpm_log(PM_LOG_DEBUG, "removing directory %s", line);
					if(rmdir(line)) {
						/* this is okay, other packages are probably using it. */
					}
				} else {
					/* if the file is flagged, back it up to .pacsave */
					if(nb) {
						if(trans->type == PM_TRANS_TYPE_UPGRADE) {
							/* we're upgrading so just leave the file as is.  pacman_add() will handle it */
						} else {
							if(!(trans->flags & PM_TRANS_FLAG_NOSAVE)) {
								newpath = (char*)realloc(newpath, strlen(line)+strlen(".pacsave")+1);
								sprintf(newpath, "%s.pacsave", line);
								rename(line, newpath);
								_alpm_log(PM_LOG_WARNING, "%s saved as %s", line, newpath);
								alpm_logaction("%s saved as %s", line, newpath);
							} else {
								_alpm_log(PM_LOG_DEBUG, "unlinking %s", line);
								if(unlink(line)) {
									_alpm_log(PM_LOG_ERROR, "cannot remove file %s", line);
								}
							}
						}
					} else {
						_alpm_log(PM_LOG_DEBUG, "unlinking %s", line);
						if(unlink(line)) {
							_alpm_log(PM_LOG_ERROR, "cannot remove file %s", line);
						}
					}
				}
			}
		}

		if(trans->type != PM_TRANS_TYPE_UPGRADE) {
			char pm_install[PATH_MAX];

			/* run the post-remove script if it exists  */
			snprintf(pm_install, PATH_MAX, "%s%s/%s/%s-%s/install", handle->root, handle->dbpath, db->treename, info->name, info->version);
			_alpm_runscriptlet(handle->root, pm_install, "post_remove", info->version, NULL);
		}

		/* remove the package from the database */
		if(db_remove(db, info) == -1) {
			_alpm_log(PM_LOG_ERROR, "failed to remove database entry %s/%s-%s", db->path, info->name, info->version);
		}

		/* update dependency packages' REQUIREDBY fields */
		for(lp = info->depends; lp; lp = lp->next) {
			PMList *last, *j;
			pmpkg_t *depinfo = NULL;
			pmdepend_t depend;

			splitdep((char*)lp->data, &depend);

			depinfo = db_scan(db, depend.name, INFRQ_DESC|INFRQ_DEPENDS);
			if(depinfo == NULL) {
				/* look for a provides package */
				PMList *provides = _alpm_db_whatprovides(db, depend.name);
				if(provides) {
					/* TODO: should check _all_ packages listed in provides, not just
					 *       the first one.
					 */
					/* use the first one */
					depinfo = db_scan(db, provides->data, INFRQ_DEPENDS);
					FREELIST(provides);
					if(depinfo == NULL) {
						/* wtf */
						continue;
					}
				} else {
					continue;
				}
			}
			/* splice out this entry from requiredby */
			last = pm_list_last(depinfo->requiredby);
			/* ORE - use list_remove here? */
			for(j = depinfo->requiredby; j; j = j->next) {
				if(!strcmp((char*)j->data, info->name)) {
					if(j == depinfo->requiredby) {
						depinfo->requiredby = j->next;
					}
					if(j->prev)	j->prev->next = j->next;
					if(j->next)	j->next->prev = j->prev;
					/* free the spliced node */
					j->prev = j->next = NULL;
					FREELIST(j);
					break;
				}
			}
			db_write(db, depinfo, INFRQ_DEPENDS);
			FREEPKG(depinfo);
		}

		if(trans->type != PM_TRANS_TYPE_UPGRADE) {
			TRANS_CB(trans, PM_TRANS_CB_REMOVE_DONE, info, NULL);
			alpm_logaction("removed %s (%s)", info->name, info->version);
		}
	}

	/* run ldconfig if it exists */
	_alpm_log(PM_LOG_FLOW2, "running \"%ssbin/ldconfig -r %s\"", handle->root, handle->root);
	_alpm_ldconfig(handle->root);

	/* cache needs to be rebuilt */
	db_free_pkgcache(db);

	return(0);
}

/* vim: set ts=2 sw=2 noet: */