Add functions to calculate approximate disk usage by packages
[pacman-ng.git] / lib / libalpm / diskspace.c
blob919d15904011d2555736f6d72ba0ad18ceada38c
1 /*
2 * diskspace.c
4 * Copyright (c) 2010 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 #if defined HAVE_GETMNTENT
23 #include <mntent.h>
24 #include <sys/statvfs.h>
25 #elif defined HAVE_GETMNTINFO_STATFS
26 #include <sys/param.h>
27 #include <sys/mount.h>
28 #if HAVE_SYS_UCRED_H
29 #include <sys/ucred.h>
30 #endif
31 #elif defined HAVE_GETMNTINFO_STATVFS
32 #include <sys/types.h>
33 #include <sys/statvfs.h>
34 #endif
36 #include <math.h>
38 /* libarchive */
39 #include <archive.h>
40 #include <archive_entry.h>
42 /* libalpm */
43 #include "diskspace.h"
44 #include "alpm_list.h"
45 #include "util.h"
46 #include "log.h"
47 #include "handle.h"
49 static int mount_point_cmp(const alpm_mountpoint_t *mp1, const alpm_mountpoint_t *mp2)
51 return(strcmp(mp1->mount_dir, mp2->mount_dir));
54 static alpm_list_t *mount_point_list()
56 alpm_list_t *mount_points = NULL;
57 alpm_mountpoint_t *mp;
59 #if defined HAVE_GETMNTENT
60 struct mntent *mnt;
61 FILE *fp;
62 struct statvfs fsp;
64 fp = setmntent(MOUNTED, "r");
66 if (fp == NULL) {
67 return NULL;
70 while((mnt = getmntent (fp))) {
71 if(statvfs(mnt->mnt_dir, &fsp) != 0) {
72 _alpm_log(PM_LOG_WARNING, "could not get filesystem information for %s\n", mnt->mnt_dir);
73 continue;
76 MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
77 mp->mount_dir = strdup(mnt->mnt_dir);
79 MALLOC(mp->fsp, sizeof(struct statvfs), RET_ERR(PM_ERR_MEMORY, NULL));
80 memcpy((void *)(mp->fsp), (void *)(&fsp), sizeof(struct statvfs));
82 mp->blocks_needed = 0;
83 mp->max_blocks_needed = 0;
84 mp->used = 0;
86 mount_points = alpm_list_add(mount_points, mp);
89 endmntent(fp);
90 #elif defined HAVE_GETMNTINFO_STATFS
91 int entries;
92 struct statfs *fsp;
94 entries = getmntinfo(&fsp, MNT_NOWAIT);
96 if (entries < 0) {
97 return NULL;
100 for(; entries-- > 0; fsp++) {
101 MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
102 mp->mount_dir = strdup(fsp->f_mntonname);
104 MALLOC(mp->fsp, sizeof(struct statfs), RET_ERR(PM_ERR_MEMORY, NULL));
105 memcpy((void *)(mp->fsp), (void *)fsp, sizeof(struct statfs));
107 mp->blocks_needed = 0;
108 mp->max_blocks_needed = 0;
110 mount_points = alpm_list_add(mount_points, mp);
112 #elif defined HAVE_GETMNTINFO_STATVFS
113 int entries;
114 struct statvfs *fsp;
116 entries = getmntinfo(&fsp, MNT_NOWAIT);
118 if (entries < 0) {
119 return NULL;
122 for (; entries-- > 0; fsp++) {
123 MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
124 mp->mount_dir = strdup(fsp->f_mntonname);
126 MALLOC(mp->fsp, sizeof(struct statvfs), RET_ERR(PM_ERR_MEMORY, NULL));
127 memcpy((void *)(mp->fsp), (void *)fsp, sizeof(struct statvfs));
129 mp->blocks_needed = 0;
130 mp->max_blocks_needed = 0;
132 mount_points = alpm_list_add(mount_points, mp);
134 #endif
136 mount_points = alpm_list_msort(mount_points, alpm_list_count(mount_points),
137 (alpm_list_fn_cmp)mount_point_cmp);
138 return(mount_points);
141 static alpm_list_t *match_mount_point(const alpm_list_t *mount_points, const char *file)
143 char real_path[PATH_MAX];
144 snprintf(real_path, PATH_MAX, "%s%s", handle->root, file);
146 alpm_list_t *mp = alpm_list_last(mount_points);
147 do {
148 alpm_mountpoint_t *data = mp->data;
150 if(strncmp(data->mount_dir, real_path, strlen(data->mount_dir)) == 0) {
151 return mp;
154 mp = mp->prev;
155 } while (mp != alpm_list_last(mount_points));
157 /* should not get here... */
158 return NULL;
161 static int calculate_removed_size(pmpkg_t *pkg, const alpm_list_t *mount_points)
163 alpm_list_t *file;
165 alpm_list_t *files = alpm_pkg_get_files(pkg);
166 for(file = files; file; file = file->next) {
167 alpm_list_t *mp;
168 alpm_mountpoint_t *data;
169 struct stat st;
170 char path[PATH_MAX];
172 /* skip directories to be consistent with libarchive that reports them to be zero size
173 and to prevent multiple counting across packages */
174 if(*((char *)(file->data) + strlen(file->data) - 1) == '/') {
175 continue;
178 mp = match_mount_point(mount_points, file->data);
179 if(mp == NULL) {
180 _alpm_log(PM_LOG_WARNING, _("could not determine mount point for file %s"), (char *)(file->data));
181 continue;
184 snprintf(path, PATH_MAX, "%s%s", handle->root, (char *)file->data);
185 _alpm_lstat(path, &st);
187 /* skip symlinks to be consistent with libarchive that reports them to be zero size */
188 if(S_ISLNK(st.st_mode)) {
189 continue;
192 data = mp->data;
193 data->blocks_needed -= ceil((double)(st.st_size) /
194 (double)(data->fsp->f_bsize));
195 data->used = 1;
198 return 0;
201 static int calculate_installed_size(pmpkg_t *pkg, const alpm_list_t *mount_points)
203 int ret=0;
204 struct archive *archive;
205 struct archive_entry *entry;
206 const char *file;
208 if ((archive = archive_read_new()) == NULL) {
209 pm_errno = PM_ERR_LIBARCHIVE;
210 ret = -1;
211 goto cleanup;
214 archive_read_support_compression_all(archive);
215 archive_read_support_format_all(archive);
217 if(archive_read_open_filename(archive, pkg->origin_data.file,
218 ARCHIVE_DEFAULT_BYTES_PER_BLOCK) != ARCHIVE_OK) {
219 pm_errno = PM_ERR_PKG_OPEN;
220 ret = -1;
221 goto cleanup;
224 while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
225 alpm_list_t *mp;
226 alpm_mountpoint_t *data;
228 file = archive_entry_pathname(entry);
230 /* approximate space requirements for db entries */
231 if(strcmp(file, ".PKGINFO") == 0 ||
232 strcmp(file, ".INSTALL") == 0 ||
233 strcmp(file, ".CHANGELOG") == 0) {
234 file = alpm_option_get_dbpath();
237 mp = match_mount_point(mount_points, file);
238 if(mp == NULL) {
239 _alpm_log(PM_LOG_WARNING, _("could not determine mount point for file %s"), archive_entry_pathname(entry));
240 continue;
243 data = mp->data;
244 data->blocks_needed += ceil((double)(archive_entry_size(entry)) /
245 (double)(data->fsp->f_bsize));
246 data->used = 1;
249 archive_read_finish(archive);
251 cleanup:
252 return ret;
255 int _alpm_check_diskspace(pmtrans_t *trans, pmdb_t *db)
257 alpm_list_t *mount_points;
259 mount_points = mount_point_list();
260 if(mount_points == NULL) {
261 _alpm_log(PM_LOG_ERROR, _("count not determine filesystem mount points"));
262 return -1;
265 for(i = mount_points; i; i = alpm_list_next(i)) {
266 alpm_mountpoint_t *data = i->data;
267 FREE(data->mount_dir);
268 FREE(data->fsp);
270 FREELIST(mount_points);
272 return 0;
275 /* vim: set ts=2 sw=2 noet: */