summaryrefslogtreecommitdiffstats
path: root/lib/libalpm/diskspace.c
blob: 5c9f74e578186058306e0f81c80636591de62207 (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
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
/*
 *  diskspace.c
 *
 *  Copyright (c) 2010-2011 Pacman Development Team <pacman-dev@archlinux.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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#if defined(HAVE_MNTENT_H)
#include <mntent.h>
#endif
#if defined(HAVE_SYS_STATVFS_H)
#include <sys/statvfs.h>
#endif
#if defined(HAVE_SYS_PARAM_H)
#include <sys/param.h>
#endif
#if defined(HAVE_SYS_MOUNT_H)
#include <sys/mount.h>
#endif
#if defined(HAVE_SYS_UCRED_H)
#include <sys/ucred.h>
#endif
#if defined(HAVE_SYS_TYPES_H)
#include <sys/types.h>
#endif

#include <math.h>

/* libarchive */
#include <archive.h>
#include <archive_entry.h>

/* libalpm */
#include "diskspace.h"
#include "alpm_list.h"
#include "util.h"
#include "log.h"
#include "trans.h"
#include "handle.h"

static int mount_point_cmp(const void *p1, const void *p2)
{
	const alpm_mountpoint_t *mp1 = p1;
	const alpm_mountpoint_t *mp2 = p2;
	return(strcmp(mp1->mount_dir, mp2->mount_dir));
}

static alpm_list_t *mount_point_list(void)
{
	alpm_list_t *mount_points = NULL;
	alpm_mountpoint_t *mp;

#if defined HAVE_GETMNTENT
	struct mntent *mnt;
	FILE *fp;
	FSSTATSTYPE fsp;

	fp = setmntent(MOUNTED, "r");

	if (fp == NULL) {
		return(NULL);
	}

	while((mnt = getmntent(fp))) {
		if(statvfs(mnt->mnt_dir, &fsp) != 0) {
			_alpm_log(PM_LOG_WARNING,
					_("could not get filesystem information for %s\n"), mnt->mnt_dir);
			continue;
		}

		MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
		mp->mount_dir = strdup(mnt->mnt_dir);
		memcpy(&(mp->fsp), &fsp, sizeof(FSSTATSTYPE));

		mp->blocks_needed = 0;
		mp->max_blocks_needed = 0;
		mp->used = 0;

		mount_points = alpm_list_add(mount_points, mp);
	}

	endmntent(fp);
#elif defined HAVE_GETMNTINFO
	int entries;
	FSSTATSTYPE *fsp;

	entries = getmntinfo(&fsp, MNT_NOWAIT);

	if (entries < 0) {
		return NULL;
	}

	for(; entries-- > 0; fsp++) {
		MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
		mp->mount_dir = strdup(fsp->f_mntonname);
		memcpy(&(mp->fsp), fsp, sizeof(FSSTATSTYPE));

		mp->blocks_needed = 0;
		mp->max_blocks_needed = 0;

		mount_points = alpm_list_add(mount_points, mp);
	}
#endif

	mount_points = alpm_list_msort(mount_points, alpm_list_count(mount_points),
			mount_point_cmp);
	return(mount_points);
}

static alpm_list_t *match_mount_point(const alpm_list_t *mount_points,
		const char *file)
{
	char real_path[PATH_MAX];
	snprintf(real_path, PATH_MAX, "%s%s", handle->root, file);

	alpm_list_t *mp = alpm_list_last(mount_points);
	do {
		alpm_mountpoint_t *data = mp->data;

		if(strncmp(data->mount_dir, real_path, strlen(data->mount_dir)) == 0) {
			return(mp);
		}

		mp = mp->prev;
	} while (mp != alpm_list_last(mount_points));

	/* should not get here... */
	return(NULL);
}

static int calculate_removed_size(const alpm_list_t *mount_points,
		pmpkg_t *pkg)
{
	alpm_list_t *file;

	alpm_list_t *files = alpm_pkg_get_files(pkg);
	for(file = files; file; file = file->next) {
		alpm_list_t *mp;
		alpm_mountpoint_t *data;
		struct stat st;
		char path[PATH_MAX];
		const char *filename = file->data;

		/* skip directories to be consistent with libarchive that reports them
		 * to be zero size and to prevent multiple counting across packages */
		if(*(filename + strlen(filename) - 1) == '/') {
			continue;
		}

		mp = match_mount_point(mount_points, filename);
		if(mp == NULL) {
			_alpm_log(PM_LOG_WARNING,
					_("could not determine mount point for file %s"), filename);
			continue;
		}

		snprintf(path, PATH_MAX, "%s%s", handle->root, filename);
		_alpm_lstat(path, &st);

		/* skip symlinks to be consistent with libarchive that reports them to
		 * be zero size */
		if(S_ISLNK(st.st_mode)) {
			continue;
		}

		data = mp->data;
		data->blocks_needed -= ceil((double)(st.st_size) /
		                            (double)(data->fsp.f_bsize));
		data->used = 1;
	}

	return(0);
}

static int calculate_installed_size(const alpm_list_t *mount_points,
		pmpkg_t *pkg)
{
	int ret=0;
	struct archive *archive;
	struct archive_entry *entry;
	const char *file;

	if ((archive = archive_read_new()) == NULL) {
		pm_errno = PM_ERR_LIBARCHIVE;
		ret = -1;
		goto cleanup;
	}

	archive_read_support_compression_all(archive);
	archive_read_support_format_all(archive);

	if(archive_read_open_filename(archive, pkg->origin_data.file,
				ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
		pm_errno = PM_ERR_PKG_OPEN;
		ret = -1;
		goto cleanup;
	}

	while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
		alpm_list_t *mp;
		alpm_mountpoint_t *data;

		file = archive_entry_pathname(entry);

		/* approximate space requirements for db entries */
		if(file[0] == '.') {
			file = alpm_option_get_dbpath();
		}

		mp = match_mount_point(mount_points, file);
		if(mp == NULL) {
			_alpm_log(PM_LOG_WARNING,
					_("could not determine mount point for file %s"), file);
			continue;
		}

		data = mp->data;
		data->blocks_needed += ceil((double)(archive_entry_size(entry)) /
		                            (double)(data->fsp.f_bsize));
		data->used = 1;
	}

	archive_read_finish(archive);

cleanup:
	return(ret);
}

int _alpm_check_diskspace(pmtrans_t *trans, pmdb_t *db_local)
{
	alpm_list_t *mount_points, *i;
	size_t replaces = 0, current = 0;
	int abort = 0;
	alpm_list_t *targ;
	pmpkg_t *pkg;
	size_t numtargs = alpm_list_count(trans->add);

	mount_points = mount_point_list();
	if(mount_points == NULL) {
		_alpm_log(PM_LOG_ERROR, _("could not determine filesystem mount points"));
		return(-1);
	}

	replaces = alpm_list_count(trans->remove);
	if(replaces) {
		numtargs += replaces;
		for(targ = trans->remove; targ; targ = targ->next, current++) {
			double percent = (double)current / numtargs;
			PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", (percent * 100),
					numtargs, current);

			pkg = targ->data;
			calculate_removed_size(mount_points, pkg);
		}
	}

	for(targ = trans->add; targ; targ = targ->next, current++) {
		double percent = (double)current / numtargs;
		PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", (percent * 100),
				numtargs, current);

		pkg = targ->data;
		/* is this package already installed? */
		if(_alpm_db_get_pkgfromcache(db_local, pkg->name)) {
			calculate_removed_size(mount_points, pkg);
		}
		calculate_installed_size(mount_points, pkg);

		for(i = mount_points; i; i = alpm_list_next(i)) {
			alpm_mountpoint_t *data = i->data;
			if(data->blocks_needed > data->max_blocks_needed) {
				data->max_blocks_needed = data->blocks_needed;
			}
		}
	}

	PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", 100,
			numtargs, current);

	for(i = mount_points; i; i = alpm_list_next(i)) {
		alpm_mountpoint_t *data = i->data;
		if(data->used == 1) {
			/* cushion is roughly min(5% capacity, 20MiB) */
			long fivepc = (data->fsp.f_blocks / 20) + 1;
			long twentymb = (20 * 1024 * 1024 / data->fsp.f_bsize) + 1;
			long cushion = fivepc < twentymb ? fivepc : twentymb;

			_alpm_log(PM_LOG_DEBUG, "partition %s, needed %ld, cushion %ld, free %ld\n",
					data->mount_dir, data->max_blocks_needed, cushion,
					(unsigned long)data->fsp.f_bfree);
			if(data->max_blocks_needed + cushion >= 0 &&
			   (unsigned long)(data->max_blocks_needed + cushion) > data->fsp.f_bfree) {
				abort = 1;
			}
		}
	}

	for(i = mount_points; i; i = alpm_list_next(i)) {
		alpm_mountpoint_t *data = i->data;
		FREE(data->mount_dir);
	}
	FREELIST(mount_points);

	if(abort) {
		RET_ERR(PM_ERR_DISK_SPACE, -1);
	}

	return(0);
}

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