Rename pmtrans_t to alpm_trans_t
[pacman-ng.git] / lib / libalpm / diskspace.c
blob7ca72c3c0008c20b136c605c791606a757f05df7
1 /*
2 * diskspace.c
4 * Copyright (c) 2010-2011 Pacman Development Team <pacman-dev@archlinux.org>
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.
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.
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/>.
20 #include "config.h"
22 #include <errno.h>
23 #if defined(HAVE_MNTENT_H)
24 #include <mntent.h>
25 #endif
26 #if defined(HAVE_SYS_STATVFS_H)
27 #include <sys/statvfs.h>
28 #endif
29 #if defined(HAVE_SYS_PARAM_H)
30 #include <sys/param.h>
31 #endif
32 #if defined(HAVE_SYS_MOUNT_H)
33 #include <sys/mount.h>
34 #endif
35 #if defined(HAVE_SYS_UCRED_H)
36 #include <sys/ucred.h>
37 #endif
38 #if defined(HAVE_SYS_TYPES_H)
39 #include <sys/types.h>
40 #endif
42 /* libarchive */
43 #include <archive.h>
44 #include <archive_entry.h>
46 /* libalpm */
47 #include "diskspace.h"
48 #include "alpm_list.h"
49 #include "util.h"
50 #include "log.h"
51 #include "trans.h"
52 #include "handle.h"
54 static int mount_point_cmp(const void *p1, const void *p2)
56 const alpm_mountpoint_t *mp1 = p1;
57 const alpm_mountpoint_t *mp2 = p2;
58 /* the negation will sort all mountpoints before their parent */
59 return -strcmp(mp1->mount_dir, mp2->mount_dir);
62 static alpm_list_t *mount_point_list(alpm_handle_t *handle)
64 alpm_list_t *mount_points = NULL, *ptr;
65 alpm_mountpoint_t *mp;
67 #if defined HAVE_GETMNTENT
68 struct mntent *mnt;
69 FILE *fp;
70 struct statvfs fsp;
72 fp = setmntent(MOUNTED, "r");
74 if(fp == NULL) {
75 return NULL;
78 while((mnt = getmntent(fp))) {
79 if(!mnt) {
80 _alpm_log(handle, PM_LOG_WARNING, _("could not get filesystem information\n"));
81 continue;
83 if(statvfs(mnt->mnt_dir, &fsp) != 0) {
84 _alpm_log(handle, PM_LOG_WARNING,
85 _("could not get filesystem information for %s: %s\n"),
86 mnt->mnt_dir, strerror(errno));
87 continue;
90 CALLOC(mp, 1, sizeof(alpm_mountpoint_t), RET_ERR(handle, PM_ERR_MEMORY, NULL));
91 mp->mount_dir = strdup(mnt->mnt_dir);
92 mp->mount_dir_len = strlen(mp->mount_dir);
93 memcpy(&(mp->fsp), &fsp, sizeof(struct statvfs));
94 mp->read_only = fsp.f_flag & ST_RDONLY;
96 mount_points = alpm_list_add(mount_points, mp);
99 endmntent(fp);
100 #elif defined HAVE_GETMNTINFO
101 int entries;
102 FSSTATSTYPE *fsp;
104 entries = getmntinfo(&fsp, MNT_NOWAIT);
106 if(entries < 0) {
107 return NULL;
110 for(; entries-- > 0; fsp++) {
111 CALLOC(mp, 1, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
112 mp->mount_dir = strdup(fsp->f_mntonname);
113 mp->mount_dir_len = strlen(mp->mount_dir);
114 memcpy(&(mp->fsp), fsp, sizeof(FSSTATSTYPE));
115 #if defined(HAVE_GETMNTINFO_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_FLAG)
116 mp->read_only = fsp->f_flag & ST_RDONLY;
117 #elif defined(HAVE_GETMNTINFO_STATFS) && defined(HAVE_STRUCT_STATFS_F_FLAGS)
118 mp->read_only = fsp->f_flags & MNT_RDONLY;
119 #endif
121 mount_points = alpm_list_add(mount_points, mp);
123 #endif
125 mount_points = alpm_list_msort(mount_points, alpm_list_count(mount_points),
126 mount_point_cmp);
127 for(ptr = mount_points; ptr != NULL; ptr = ptr->next) {
128 mp = ptr->data;
129 _alpm_log(handle, PM_LOG_DEBUG, "mountpoint: %s\n", mp->mount_dir);
131 return mount_points;
134 static alpm_mountpoint_t *match_mount_point(const alpm_list_t *mount_points,
135 const char *real_path)
137 const alpm_list_t *mp;
139 for(mp = mount_points; mp != NULL; mp = mp->next) {
140 alpm_mountpoint_t *data = mp->data;
142 if(strncmp(data->mount_dir, real_path, data->mount_dir_len) == 0) {
143 return data;
147 /* should not get here... */
148 return NULL;
151 static int calculate_removed_size(alpm_handle_t *handle,
152 const alpm_list_t *mount_points, alpm_pkg_t *pkg)
154 alpm_list_t *file;
156 alpm_list_t *files = alpm_pkg_get_files(pkg);
157 for(file = files; file; file = file->next) {
158 alpm_mountpoint_t *mp;
159 struct stat st;
160 char path[PATH_MAX];
161 const char *filename = file->data;
163 snprintf(path, PATH_MAX, "%s%s", handle->root, filename);
164 _alpm_lstat(path, &st);
166 /* skip directories and symlinks to be consistent with libarchive that
167 * reports them to be zero size */
168 if(S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) {
169 continue;
172 mp = match_mount_point(mount_points, path);
173 if(mp == NULL) {
174 _alpm_log(handle, PM_LOG_WARNING,
175 _("could not determine mount point for file %s\n"), filename);
176 continue;
179 /* the addition of (divisor - 1) performs ceil() with integer division */
180 mp->blocks_needed -=
181 (st.st_size + mp->fsp.f_bsize - 1l) / mp->fsp.f_bsize;
182 mp->used |= USED_REMOVE;
185 return 0;
188 static int calculate_installed_size(alpm_handle_t *handle,
189 const alpm_list_t *mount_points, alpm_pkg_t *pkg)
191 int ret=0;
192 struct archive *archive;
193 struct archive_entry *entry;
195 if((archive = archive_read_new()) == NULL) {
196 handle->pm_errno = PM_ERR_LIBARCHIVE;
197 ret = -1;
198 goto cleanup;
201 archive_read_support_compression_all(archive);
202 archive_read_support_format_all(archive);
204 if(archive_read_open_filename(archive, pkg->origin_data.file,
205 ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
206 handle->pm_errno = PM_ERR_PKG_OPEN;
207 ret = -1;
208 goto cleanup;
211 while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
212 alpm_mountpoint_t *mp;
213 const char *filename;
214 mode_t mode;
215 char path[PATH_MAX];
217 filename = archive_entry_pathname(entry);
218 mode = archive_entry_mode(entry);
220 /* libarchive reports these as zero size anyways */
221 /* NOTE: if we do start accounting for directory size, a dir matching a
222 * mountpoint needs to be attributed to the parent, not the mountpoint. */
223 if(S_ISDIR(mode) || S_ISLNK(mode)) {
224 continue;
227 /* approximate space requirements for db entries */
228 if(filename[0] == '.') {
229 filename = alpm_option_get_dbpath(handle);
232 snprintf(path, PATH_MAX, "%s%s", handle->root, filename);
234 mp = match_mount_point(mount_points, path);
235 if(mp == NULL) {
236 _alpm_log(handle, PM_LOG_WARNING,
237 _("could not determine mount point for file %s\n"), filename);
238 continue;
241 /* the addition of (divisor - 1) performs ceil() with integer division */
242 mp->blocks_needed +=
243 (archive_entry_size(entry) + mp->fsp.f_bsize - 1l) / mp->fsp.f_bsize;
244 mp->used |= USED_INSTALL;
246 if(archive_read_data_skip(archive)) {
247 _alpm_log(handle, PM_LOG_ERROR, _("error while reading package %s: %s\n"),
248 pkg->name, archive_error_string(archive));
249 handle->pm_errno = PM_ERR_LIBARCHIVE;
250 break;
254 archive_read_finish(archive);
256 cleanup:
257 return ret;
260 int _alpm_check_diskspace(alpm_handle_t *handle)
262 alpm_list_t *mount_points, *i;
263 alpm_mountpoint_t *root_mp;
264 size_t replaces = 0, current = 0, numtargs;
265 int error = 0;
266 alpm_list_t *targ;
267 alpm_trans_t *trans = handle->trans;
269 numtargs = alpm_list_count(trans->add);
270 mount_points = mount_point_list(handle);
271 if(mount_points == NULL) {
272 _alpm_log(handle, PM_LOG_ERROR, _("could not determine filesystem mount points\n"));
273 return -1;
275 root_mp = match_mount_point(mount_points, handle->root);
276 if(root_mp == NULL) {
277 _alpm_log(handle, PM_LOG_ERROR, _("could not determine root mount point %s\n"),
278 handle->root);
279 return -1;
282 replaces = alpm_list_count(trans->remove);
283 if(replaces) {
284 numtargs += replaces;
285 for(targ = trans->remove; targ; targ = targ->next, current++) {
286 alpm_pkg_t *local_pkg;
287 int percent = (current * 100) / numtargs;
288 PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", percent,
289 numtargs, current);
291 local_pkg = targ->data;
292 calculate_removed_size(handle, mount_points, local_pkg);
296 for(targ = trans->add; targ; targ = targ->next, current++) {
297 alpm_pkg_t *pkg, *local_pkg;
298 int percent = (current * 100) / numtargs;
299 PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", percent,
300 numtargs, current);
302 pkg = targ->data;
303 /* is this package already installed? */
304 local_pkg = _alpm_db_get_pkgfromcache(handle->db_local, pkg->name);
305 if(local_pkg) {
306 calculate_removed_size(handle, mount_points, local_pkg);
308 calculate_installed_size(handle, mount_points, pkg);
310 for(i = mount_points; i; i = alpm_list_next(i)) {
311 alpm_mountpoint_t *data = i->data;
312 if(data->blocks_needed > data->max_blocks_needed) {
313 data->max_blocks_needed = data->blocks_needed;
318 PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", 100,
319 numtargs, current);
321 for(i = mount_points; i; i = alpm_list_next(i)) {
322 alpm_mountpoint_t *data = i->data;
323 if(data->used && data->read_only) {
324 _alpm_log(handle, PM_LOG_ERROR, _("Partition %s is mounted read only\n"),
325 data->mount_dir);
326 error = 1;
327 } else if(data->used & USED_INSTALL) {
328 /* cushion is roughly min(5% capacity, 20MiB) */
329 long fivepc = ((long)data->fsp.f_blocks / 20) + 1;
330 long twentymb = (20 * 1024 * 1024 / (long)data->fsp.f_bsize) + 1;
331 long cushion = fivepc < twentymb ? fivepc : twentymb;
333 _alpm_log(handle, PM_LOG_DEBUG, "partition %s, needed %ld, cushion %ld, free %ld\n",
334 data->mount_dir, data->max_blocks_needed, cushion,
335 (unsigned long)data->fsp.f_bfree);
336 if(data->max_blocks_needed + cushion >= 0 &&
337 (unsigned long)(data->max_blocks_needed + cushion) > data->fsp.f_bfree) {
338 _alpm_log(handle, PM_LOG_ERROR, _("Partition %s too full: %ld blocks needed, %ld blocks free\n"),
339 data->mount_dir, data->max_blocks_needed + cushion,
340 (unsigned long)data->fsp.f_bfree);
341 error = 1;
346 for(i = mount_points; i; i = alpm_list_next(i)) {
347 alpm_mountpoint_t *data = i->data;
348 FREE(data->mount_dir);
350 FREELIST(mount_points);
352 if(error) {
353 RET_ERR(handle, PM_ERR_DISK_SPACE, -1);
356 return 0;
359 /* vim: set ts=2 sw=2 noet: */