1 /*
2  *  diskspace.c
3  *
4  *  Copyright (c) 2010-2018 Pacman Development Team <pacman-dev@archlinux.org>
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 of the License, or
9  *  (at your option) 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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <stdio.h>
21 #include <errno.h>
22 
23 #if defined(HAVE_MNTENT_H)
24 #include <mntent.h>
25 #endif
26 #if defined(HAVE_SYS_MNTTAB_H)
27 #include <sys/mnttab.h>
28 #endif
29 #if defined(HAVE_SYS_STATVFS_H)
30 #include <sys/statvfs.h>
31 #endif
32 #if defined(HAVE_SYS_PARAM_H)
33 #include <sys/param.h>
34 #endif
35 #if defined(HAVE_SYS_MOUNT_H)
36 #include <sys/mount.h>
37 #endif
38 #if defined(HAVE_SYS_UCRED_H)
39 #include <sys/ucred.h>
40 #endif
41 #if defined(HAVE_SYS_TYPES_H)
42 #include <sys/types.h>
43 #endif
44 
45 /* libalpm */
46 #include "diskspace.h"
47 #include "alpm_list.h"
48 #include "util.h"
49 #include "log.h"
50 #include "trans.h"
51 #include "handle.h"
52 
mount_point_cmp(const void * p1,const void * p2)53 static int mount_point_cmp(const void *p1, const void *p2)
54 {
55 	const alpm_mountpoint_t *mp1 = p1;
56 	const alpm_mountpoint_t *mp2 = p2;
57 	/* the negation will sort all mountpoints before their parent */
58 	return -strcmp(mp1->mount_dir, mp2->mount_dir);
59 }
60 
mount_point_list_free(alpm_list_t * mount_points)61 static void mount_point_list_free(alpm_list_t *mount_points)
62 {
63 	alpm_list_t *i;
64 
65 	for(i = mount_points; i; i = i->next) {
66 		alpm_mountpoint_t *data = i->data;
67 		FREE(data->mount_dir);
68 	}
69 	FREELIST(mount_points);
70 }
71 
mount_point_load_fsinfo(alpm_handle_t * handle,alpm_mountpoint_t * mountpoint)72 static int mount_point_load_fsinfo(alpm_handle_t *handle, alpm_mountpoint_t *mountpoint)
73 {
74 #if defined(HAVE_GETMNTENT)
75 	/* grab the filesystem usage */
76 	if(statvfs(mountpoint->mount_dir, &(mountpoint->fsp)) != 0) {
77 		_alpm_log(handle, ALPM_LOG_WARNING,
78 				_("could not get filesystem information for %s: %s\n"),
79 				mountpoint->mount_dir, strerror(errno));
80 		mountpoint->fsinfo_loaded = MOUNT_FSINFO_FAIL;
81 		return -1;
82 	}
83 
84 	_alpm_log(handle, ALPM_LOG_DEBUG, "loading fsinfo for %s\n", mountpoint->mount_dir);
85 	mountpoint->read_only = mountpoint->fsp.f_flag & ST_RDONLY;
86 	mountpoint->fsinfo_loaded = MOUNT_FSINFO_LOADED;
87 #else
88 	(void)handle;
89 	(void)mountpoint;
90 #endif
91 
92 	return 0;
93 }
94 
mount_point_list(alpm_handle_t * handle)95 static alpm_list_t *mount_point_list(alpm_handle_t *handle)
96 {
97 	alpm_list_t *mount_points = NULL, *ptr;
98 	alpm_mountpoint_t *mp;
99 
100 #if defined(HAVE_GETMNTENT) && defined(HAVE_MNTENT_H)
101 	/* Linux */
102 	struct mntent *mnt;
103 	FILE *fp;
104 
105 	fp = setmntent(MOUNTED, "r");
106 
107 	if(fp == NULL) {
108 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not open file: %s: %s\n"),
109 				MOUNTED, strerror(errno));
110 		return NULL;
111 	}
112 
113 	while((mnt = getmntent(fp))) {
114 		CALLOC(mp, 1, sizeof(alpm_mountpoint_t), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
115 		STRDUP(mp->mount_dir, mnt->mnt_dir, free(mp); RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
116 		mp->mount_dir_len = strlen(mp->mount_dir);
117 
118 		mount_points = alpm_list_add(mount_points, mp);
119 	}
120 
121 	endmntent(fp);
122 #elif defined(HAVE_GETMNTENT) && defined(HAVE_MNTTAB_H)
123 	/* Solaris, Illumos */
124 	struct mnttab mnt;
125 	FILE *fp;
126 	int ret;
127 
128 	fp = fopen("/etc/mnttab", "r");
129 
130 	if(fp == NULL) {
131 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not open file %s: %s\n"),
132 				"/etc/mnttab", strerror(errno));
133 		return NULL;
134 	}
135 
136 	while((ret = getmntent(fp, &mnt)) == 0) {
137 		CALLOC(mp, 1, sizeof(alpm_mountpoint_t), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
138 		STRDUP(mp->mount_dir, mnt->mnt_mountp,  free(mp); RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
139 		mp->mount_dir_len = strlen(mp->mount_dir);
140 
141 		mount_points = alpm_list_add(mount_points, mp);
142 	}
143 	/* -1 == EOF */
144 	if(ret != -1) {
145 		_alpm_log(handle, ALPM_LOG_WARNING,
146 				_("could not get filesystem information\n"));
147 	}
148 
149 	fclose(fp);
150 #elif defined(HAVE_GETMNTINFO)
151 	/* FreeBSD (statfs), NetBSD (statvfs), OpenBSD (statfs), OS X (statfs) */
152 	int entries;
153 	FSSTATSTYPE *fsp;
154 
155 	entries = getmntinfo(&fsp, MNT_NOWAIT);
156 
157 	if(entries < 0) {
158 		_alpm_log(handle, ALPM_LOG_ERROR,
159 				_("could not get filesystem information\n"));
160 		return NULL;
161 	}
162 
163 	for(; entries-- > 0; fsp++) {
164 		CALLOC(mp, 1, sizeof(alpm_mountpoint_t), RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
165 		STRDUP(mp->mount_dir, fsp->f_mntonname, free(mp); RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
166 		mp->mount_dir_len = strlen(mp->mount_dir);
167 		memcpy(&(mp->fsp), fsp, sizeof(FSSTATSTYPE));
168 #if defined(HAVE_GETMNTINFO_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_FLAG)
169 		mp->read_only = fsp->f_flag & ST_RDONLY;
170 #elif defined(HAVE_GETMNTINFO_STATFS) && defined(HAVE_STRUCT_STATFS_F_FLAGS)
171 		mp->read_only = fsp->f_flags & MNT_RDONLY;
172 #endif
173 
174 		/* we don't support lazy loading on this platform */
175 		mp->fsinfo_loaded = MOUNT_FSINFO_LOADED;
176 
177 		mount_points = alpm_list_add(mount_points, mp);
178 	}
179 #endif
180 
181 	mount_points = alpm_list_msort(mount_points, alpm_list_count(mount_points),
182 			mount_point_cmp);
183 	for(ptr = mount_points; ptr != NULL; ptr = ptr->next) {
184 		mp = ptr->data;
185 		_alpm_log(handle, ALPM_LOG_DEBUG, "discovered mountpoint: %s\n", mp->mount_dir);
186 	}
187 	return mount_points;
188 }
189 
match_mount_point(const alpm_list_t * mount_points,const char * real_path)190 static alpm_mountpoint_t *match_mount_point(const alpm_list_t *mount_points,
191 		const char *real_path)
192 {
193 	const alpm_list_t *mp;
194 
195 	for(mp = mount_points; mp != NULL; mp = mp->next) {
196 		alpm_mountpoint_t *data = mp->data;
197 
198 		/* first, check if the prefix matches */
199 		if(strncmp(data->mount_dir, real_path, data->mount_dir_len) == 0) {
200 			/* now, the hard work- a file like '/etc/myconfig' shouldn't map to a
201 			 * mountpoint '/e', but only '/etc'. If the mountpoint ends in a trailing
202 			 * slash, we know we didn't have a mismatch, otherwise we have to do some
203 			 * more sanity checks. */
204 			if(data->mount_dir[data->mount_dir_len - 1] == '/') {
205 				return data;
206 			} else if(strlen(real_path) >= data->mount_dir_len) {
207 				const char next = real_path[data->mount_dir_len];
208 				if(next == '/' || next == '\0') {
209 					return data;
210 				}
211 			}
212 		}
213 	}
214 
215 	/* should not get here... */
216 	return NULL;
217 }
218 
calculate_removed_size(alpm_handle_t * handle,const alpm_list_t * mount_points,alpm_pkg_t * pkg)219 static int calculate_removed_size(alpm_handle_t *handle,
220 		const alpm_list_t *mount_points, alpm_pkg_t *pkg)
221 {
222 	size_t i;
223 	alpm_filelist_t *filelist = alpm_pkg_get_files(pkg);
224 
225 	if(!filelist->count) {
226 		return 0;
227 	}
228 
229 	for(i = 0; i < filelist->count; i++) {
230 		const alpm_file_t *file = filelist->files + i;
231 		alpm_mountpoint_t *mp;
232 		struct stat st;
233 		char path[PATH_MAX];
234 		blkcnt_t remove_size;
235 		const char *filename = file->name;
236 
237 		snprintf(path, PATH_MAX, "%s%s", handle->root, filename);
238 
239 		if(llstat(path, &st) == -1) {
240 			if(alpm_option_match_noextract(handle, filename)) {
241 				_alpm_log(handle, ALPM_LOG_WARNING,
242 						_("could not get file information for %s\n"), filename);
243 			}
244 			continue;
245 		}
246 
247 		/* skip directories and symlinks to be consistent with libarchive that
248 		 * reports them to be zero size */
249 		if(S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) {
250 			continue;
251 		}
252 
253 		mp = match_mount_point(mount_points, path);
254 		if(mp == NULL) {
255 			_alpm_log(handle, ALPM_LOG_WARNING,
256 					_("could not determine mount point for file %s\n"), filename);
257 			continue;
258 		}
259 
260 		/* don't check a mount that we know we can't stat */
261 		if(mp && mp->fsinfo_loaded == MOUNT_FSINFO_FAIL) {
262 			continue;
263 		}
264 
265 		/* lazy load filesystem info */
266 		if(mp->fsinfo_loaded == MOUNT_FSINFO_UNLOADED) {
267 			if(mount_point_load_fsinfo(handle, mp) < 0) {
268 				continue;
269 			}
270 		}
271 
272 		/* the addition of (divisor - 1) performs ceil() with integer division */
273 		remove_size = (st.st_size + mp->fsp.f_bsize - 1) / mp->fsp.f_bsize;
274 		mp->blocks_needed -= remove_size;
275 		mp->used |= USED_REMOVE;
276 	}
277 
278 	return 0;
279 }
280 
calculate_installed_size(alpm_handle_t * handle,const alpm_list_t * mount_points,alpm_pkg_t * pkg)281 static int calculate_installed_size(alpm_handle_t *handle,
282 		const alpm_list_t *mount_points, alpm_pkg_t *pkg)
283 {
284 	size_t i;
285 	alpm_filelist_t *filelist = alpm_pkg_get_files(pkg);
286 
287 	if(!filelist->count) {
288 		return 0;
289 	}
290 
291 	for(i = 0; i < filelist->count; i++) {
292 		const alpm_file_t *file = filelist->files + i;
293 		alpm_mountpoint_t *mp;
294 		char path[PATH_MAX];
295 		blkcnt_t install_size;
296 		const char *filename = file->name;
297 
298 		/* libarchive reports these as zero size anyways */
299 		/* NOTE: if we do start accounting for directory size, a dir matching a
300 		 * mountpoint needs to be attributed to the parent, not the mountpoint. */
301 		if(S_ISDIR(file->mode) || S_ISLNK(file->mode)) {
302 			continue;
303 		}
304 
305 		/* approximate space requirements for db entries */
306 		if(filename[0] == '.') {
307 			filename = handle->dbpath;
308 		}
309 
310 		snprintf(path, PATH_MAX, "%s%s", handle->root, filename);
311 
312 		mp = match_mount_point(mount_points, path);
313 		if(mp == NULL) {
314 			_alpm_log(handle, ALPM_LOG_WARNING,
315 					_("could not determine mount point for file %s\n"), filename);
316 			continue;
317 		}
318 
319 		/* don't check a mount that we know we can't stat */
320 		if(mp && mp->fsinfo_loaded == MOUNT_FSINFO_FAIL) {
321 			continue;
322 		}
323 
324 		/* lazy load filesystem info */
325 		if(mp->fsinfo_loaded == MOUNT_FSINFO_UNLOADED) {
326 			if(mount_point_load_fsinfo(handle, mp) < 0) {
327 				continue;
328 			}
329 		}
330 
331 		/* the addition of (divisor - 1) performs ceil() with integer division */
332 		install_size = (file->size + mp->fsp.f_bsize - 1) / mp->fsp.f_bsize;
333 		mp->blocks_needed += install_size;
334 		mp->used |= USED_INSTALL;
335 	}
336 
337 	return 0;
338 }
339 
check_mountpoint(alpm_handle_t * handle,alpm_mountpoint_t * mp)340 static int check_mountpoint(alpm_handle_t *handle, alpm_mountpoint_t *mp)
341 {
342 	/* cushion is roughly min(5% capacity, 20MiB) */
343 	fsblkcnt_t fivepc = (mp->fsp.f_blocks / 20) + 1;
344 	fsblkcnt_t twentymb = (20 * 1024 * 1024 / mp->fsp.f_bsize) + 1;
345 	fsblkcnt_t cushion = fivepc < twentymb ? fivepc : twentymb;
346 	blkcnt_t needed = mp->max_blocks_needed + cushion;
347 
348 	_alpm_log(handle, ALPM_LOG_DEBUG,
349 			"partition %s, needed %jd, cushion %ju, free %ju\n",
350 			mp->mount_dir, (intmax_t)mp->max_blocks_needed,
351 			(uintmax_t)cushion, (uintmax_t)mp->fsp.f_bavail);
352 	if(needed >= 0 && (fsblkcnt_t)needed > mp->fsp.f_bavail) {
353 		_alpm_log(handle, ALPM_LOG_ERROR,
354 				_("Partition %s too full: %jd blocks needed, %ju blocks free\n"),
355 				mp->mount_dir, (intmax_t)needed, (uintmax_t)mp->fsp.f_bavail);
356 		return 1;
357 	}
358 	return 0;
359 }
360 
_alpm_check_downloadspace(alpm_handle_t * handle,const char * cachedir,size_t num_files,off_t * file_sizes)361 int _alpm_check_downloadspace(alpm_handle_t *handle, const char *cachedir,
362 		size_t num_files, off_t *file_sizes)
363 {
364 	alpm_list_t *mount_points;
365 	alpm_mountpoint_t *cachedir_mp;
366 	char resolved_cachedir[PATH_MAX];
367 	size_t j;
368 	int error = 0;
369 
370 	/* resolve the cachedir path to ensure we check the right mountpoint. We
371 	 * handle failures silently, and continue to use the possibly unresolved
372 	 * path. */
373 	if(realpath(cachedir, resolved_cachedir) != NULL) {
374 		cachedir = resolved_cachedir;
375 	}
376 
377 	mount_points = mount_point_list(handle);
378 	if(mount_points == NULL) {
379 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not determine filesystem mount points\n"));
380 		return -1;
381 	}
382 
383 	cachedir_mp = match_mount_point(mount_points, cachedir);
384 	if(cachedir_mp == NULL) {
385 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not determine cachedir mount point %s\n"),
386 				cachedir);
387 		error = 1;
388 		goto finish;
389 	}
390 
391 	if(cachedir_mp->fsinfo_loaded == MOUNT_FSINFO_UNLOADED) {
392 		if(mount_point_load_fsinfo(handle, cachedir_mp)) {
393 			error = 1;
394 			goto finish;
395 		}
396 	}
397 
398 	/* there's no need to check for a R/O mounted filesystem here, as
399 	 * _alpm_filecache_setup will never give us a non-writable directory */
400 
401 	/* round up the size of each file to the nearest block and accumulate */
402 	for(j = 0; j < num_files; j++) {
403 		cachedir_mp->max_blocks_needed += (file_sizes[j] + cachedir_mp->fsp.f_bsize + 1) /
404 			cachedir_mp->fsp.f_bsize;
405 	}
406 
407 	if(check_mountpoint(handle, cachedir_mp)) {
408 		error = 1;
409 	}
410 
411 finish:
412 	mount_point_list_free(mount_points);
413 
414 	if(error) {
415 		RET_ERR(handle, ALPM_ERR_DISK_SPACE, -1);
416 	}
417 
418 	return 0;
419 }
420 
_alpm_check_diskspace(alpm_handle_t * handle)421 int _alpm_check_diskspace(alpm_handle_t *handle)
422 {
423 	alpm_list_t *mount_points, *i;
424 	alpm_mountpoint_t *root_mp;
425 	size_t replaces = 0, current = 0, numtargs;
426 	int error = 0;
427 	alpm_list_t *targ;
428 	alpm_trans_t *trans = handle->trans;
429 
430 	numtargs = alpm_list_count(trans->add);
431 	mount_points = mount_point_list(handle);
432 	if(mount_points == NULL) {
433 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not determine filesystem mount points\n"));
434 		return -1;
435 	}
436 	root_mp = match_mount_point(mount_points, handle->root);
437 	if(root_mp == NULL) {
438 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not determine root mount point %s\n"),
439 				handle->root);
440 		error = 1;
441 		goto finish;
442 	}
443 
444 	replaces = alpm_list_count(trans->remove);
445 	if(replaces) {
446 		numtargs += replaces;
447 		for(targ = trans->remove; targ; targ = targ->next, current++) {
448 			alpm_pkg_t *local_pkg;
449 			int percent = (current * 100) / numtargs;
450 			PROGRESS(handle, ALPM_PROGRESS_DISKSPACE_START, "", percent,
451 					numtargs, current);
452 
453 			local_pkg = targ->data;
454 			calculate_removed_size(handle, mount_points, local_pkg);
455 		}
456 	}
457 
458 	for(targ = trans->add; targ; targ = targ->next, current++) {
459 		alpm_pkg_t *pkg, *local_pkg;
460 		int percent = (current * 100) / numtargs;
461 		PROGRESS(handle, ALPM_PROGRESS_DISKSPACE_START, "", percent,
462 				numtargs, current);
463 
464 		pkg = targ->data;
465 		/* is this package already installed? */
466 		local_pkg = _alpm_db_get_pkgfromcache(handle->db_local, pkg->name);
467 		if(local_pkg) {
468 			calculate_removed_size(handle, mount_points, local_pkg);
469 		}
470 		calculate_installed_size(handle, mount_points, pkg);
471 
472 		for(i = mount_points; i; i = i->next) {
473 			alpm_mountpoint_t *data = i->data;
474 			if(data->blocks_needed > data->max_blocks_needed) {
475 				data->max_blocks_needed = data->blocks_needed;
476 			}
477 		}
478 	}
479 
480 	PROGRESS(handle, ALPM_PROGRESS_DISKSPACE_START, "", 100,
481 			numtargs, current);
482 
483 	for(i = mount_points; i; i = i->next) {
484 		alpm_mountpoint_t *data = i->data;
485 		if(data->used && data->read_only) {
486 			_alpm_log(handle, ALPM_LOG_ERROR, _("Partition %s is mounted read only\n"),
487 					data->mount_dir);
488 			error = 1;
489 		} else if(data->used & USED_INSTALL && check_mountpoint(handle, data)) {
490 			error = 1;
491 		}
492 	}
493 
494 finish:
495 	mount_point_list_free(mount_points);
496 
497 	if(error) {
498 		RET_ERR(handle, ALPM_ERR_DISK_SPACE, -1);
499 	}
500 
501 	return 0;
502 }
503