2010-11-16 00:57:39 -05:00
|
|
|
/*
|
|
|
|
* diskspace.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-11-16 01:15:21 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2010-11-16 21:12:26 -05:00
|
|
|
#if defined(HAVE_MNTENT_H)
|
2010-11-16 01:15:21 -05:00
|
|
|
#include <mntent.h>
|
2010-11-16 21:12:26 -05:00
|
|
|
#endif
|
|
|
|
#if defined(HAVE_SYS_STATVFS_H)
|
2010-11-16 01:15:21 -05:00
|
|
|
#include <sys/statvfs.h>
|
2010-11-16 21:12:26 -05:00
|
|
|
#endif
|
|
|
|
#if defined(HAVE_SYS_PARAM_H)
|
2010-11-16 01:15:21 -05:00
|
|
|
#include <sys/param.h>
|
2010-11-16 21:12:26 -05:00
|
|
|
#endif
|
|
|
|
#if defined(HAVE_SYS_MOUNT_H)
|
2010-11-16 01:15:21 -05:00
|
|
|
#include <sys/mount.h>
|
2010-11-16 21:12:26 -05:00
|
|
|
#endif
|
|
|
|
#if defined(HAVE_SYS_UCRED_H)
|
2010-11-16 01:15:21 -05:00
|
|
|
#include <sys/ucred.h>
|
|
|
|
#endif
|
2010-11-16 21:12:26 -05:00
|
|
|
#if defined(HAVE_SYS_TYPES_H)
|
2010-11-16 01:15:21 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
2010-11-16 01:30:33 -05:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
/* libarchive */
|
|
|
|
#include <archive.h>
|
|
|
|
#include <archive_entry.h>
|
|
|
|
|
2010-11-16 00:57:39 -05:00
|
|
|
/* libalpm */
|
|
|
|
#include "diskspace.h"
|
2010-11-16 01:15:21 -05:00
|
|
|
#include "alpm_list.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "log.h"
|
2010-11-16 02:07:43 -05:00
|
|
|
#include "trans.h"
|
2010-11-16 01:24:52 -05:00
|
|
|
#include "handle.h"
|
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
static int mount_point_cmp(const void *p1, const void *p2)
|
2010-11-16 01:24:52 -05:00
|
|
|
{
|
2010-11-16 21:12:27 -05:00
|
|
|
const alpm_mountpoint_t *mp1 = p1;
|
|
|
|
const alpm_mountpoint_t *mp2 = p2;
|
2010-11-16 01:24:52 -05:00
|
|
|
return(strcmp(mp1->mount_dir, mp2->mount_dir));
|
|
|
|
}
|
2010-11-16 01:15:21 -05:00
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
static alpm_list_t *mount_point_list(void)
|
2010-11-16 01:15:21 -05:00
|
|
|
{
|
|
|
|
alpm_list_t *mount_points = NULL;
|
|
|
|
alpm_mountpoint_t *mp;
|
|
|
|
|
|
|
|
#if defined HAVE_GETMNTENT
|
|
|
|
struct mntent *mnt;
|
|
|
|
FILE *fp;
|
2010-11-16 21:12:26 -05:00
|
|
|
FSSTATSTYPE fsp;
|
2010-11-16 01:15:21 -05:00
|
|
|
|
|
|
|
fp = setmntent(MOUNTED, "r");
|
|
|
|
|
|
|
|
if (fp == NULL) {
|
2010-11-16 21:12:27 -05:00
|
|
|
return(NULL);
|
2010-11-16 01:15:21 -05:00
|
|
|
}
|
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
while((mnt = getmntent(fp))) {
|
2010-11-16 01:15:21 -05:00
|
|
|
if(statvfs(mnt->mnt_dir, &fsp) != 0) {
|
2010-11-16 21:12:27 -05:00
|
|
|
_alpm_log(PM_LOG_WARNING,
|
|
|
|
_("could not get filesystem information for %s\n"), mnt->mnt_dir);
|
2010-11-16 01:15:21 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
|
|
|
|
mp->mount_dir = strdup(mnt->mnt_dir);
|
2010-11-16 21:12:27 -05:00
|
|
|
memcpy(&(mp->fsp), &fsp, sizeof(FSSTATSTYPE));
|
2010-11-16 01:15:21 -05:00
|
|
|
|
|
|
|
mp->blocks_needed = 0;
|
|
|
|
mp->max_blocks_needed = 0;
|
|
|
|
mp->used = 0;
|
|
|
|
|
|
|
|
mount_points = alpm_list_add(mount_points, mp);
|
|
|
|
}
|
|
|
|
|
|
|
|
endmntent(fp);
|
2010-11-16 21:12:26 -05:00
|
|
|
#elif defined HAVE_GETMNTINFO
|
2010-11-16 01:15:21 -05:00
|
|
|
int entries;
|
2010-11-16 21:12:26 -05:00
|
|
|
FSSTATSTYPE *fsp;
|
2010-11-16 01:15:21 -05:00
|
|
|
|
|
|
|
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);
|
2010-11-16 21:12:27 -05:00
|
|
|
memcpy(&(mp->fsp), fsp, sizeof(FSSTATSTYPE));
|
2010-11-16 01:15:21 -05:00
|
|
|
|
|
|
|
mp->blocks_needed = 0;
|
|
|
|
mp->max_blocks_needed = 0;
|
|
|
|
|
|
|
|
mount_points = alpm_list_add(mount_points, mp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-11-16 01:24:52 -05:00
|
|
|
mount_points = alpm_list_msort(mount_points, alpm_list_count(mount_points),
|
2010-11-16 21:12:27 -05:00
|
|
|
mount_point_cmp);
|
2010-11-16 01:15:21 -05:00
|
|
|
return(mount_points);
|
|
|
|
}
|
2010-11-16 00:57:39 -05:00
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
static alpm_list_t *match_mount_point(const alpm_list_t *mount_points,
|
|
|
|
const char *file)
|
2010-11-16 01:24:52 -05:00
|
|
|
{
|
|
|
|
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) {
|
2010-11-16 21:12:27 -05:00
|
|
|
return(mp);
|
2010-11-16 01:24:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mp = mp->prev;
|
|
|
|
} while (mp != alpm_list_last(mount_points));
|
|
|
|
|
|
|
|
/* should not get here... */
|
2010-11-16 21:12:27 -05:00
|
|
|
return(NULL);
|
2010-11-16 01:24:52 -05:00
|
|
|
}
|
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
static int calculate_removed_size(const alpm_list_t *mount_points,
|
|
|
|
pmpkg_t *pkg)
|
2010-11-16 01:30:33 -05:00
|
|
|
{
|
|
|
|
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];
|
2010-11-16 21:12:27 -05:00
|
|
|
const char *filename = file->data;
|
2010-11-16 01:30:33 -05:00
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
/* 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) == '/') {
|
2010-11-16 01:30:33 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
mp = match_mount_point(mount_points, filename);
|
2010-11-16 01:30:33 -05:00
|
|
|
if(mp == NULL) {
|
2010-11-16 21:12:27 -05:00
|
|
|
_alpm_log(PM_LOG_WARNING,
|
|
|
|
_("could not determine mount point for file %s"), filename);
|
2010-11-16 01:30:33 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
snprintf(path, PATH_MAX, "%s%s", handle->root, filename);
|
2010-11-16 01:30:33 -05:00
|
|
|
_alpm_lstat(path, &st);
|
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
/* skip symlinks to be consistent with libarchive that reports them to
|
|
|
|
* be zero size */
|
2010-11-16 01:30:33 -05:00
|
|
|
if(S_ISLNK(st.st_mode)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = mp->data;
|
|
|
|
data->blocks_needed -= ceil((double)(st.st_size) /
|
2010-11-16 21:12:27 -05:00
|
|
|
(double)(data->fsp.f_bsize));
|
2010-11-16 01:30:33 -05:00
|
|
|
data->used = 1;
|
|
|
|
}
|
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
return(0);
|
2010-11-16 01:30:33 -05:00
|
|
|
}
|
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
static int calculate_installed_size(const alpm_list_t *mount_points,
|
|
|
|
pmpkg_t *pkg)
|
2010-11-16 01:30:33 -05:00
|
|
|
{
|
|
|
|
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 */
|
2010-11-16 21:12:27 -05:00
|
|
|
if(file[0] == '.') {
|
2010-11-16 01:30:33 -05:00
|
|
|
file = alpm_option_get_dbpath();
|
|
|
|
}
|
|
|
|
|
|
|
|
mp = match_mount_point(mount_points, file);
|
|
|
|
if(mp == NULL) {
|
2010-11-16 21:12:27 -05:00
|
|
|
_alpm_log(PM_LOG_WARNING,
|
|
|
|
_("could not determine mount point for file %s"), file);
|
2010-11-16 01:30:33 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = mp->data;
|
|
|
|
data->blocks_needed += ceil((double)(archive_entry_size(entry)) /
|
2010-11-16 21:12:27 -05:00
|
|
|
(double)(data->fsp.f_bsize));
|
2010-11-16 01:30:33 -05:00
|
|
|
data->used = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
archive_read_finish(archive);
|
|
|
|
|
|
|
|
cleanup:
|
2010-11-16 21:12:27 -05:00
|
|
|
return(ret);
|
2010-11-16 01:30:33 -05:00
|
|
|
}
|
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
int _alpm_check_diskspace(pmtrans_t *trans, pmdb_t *db_local)
|
2010-11-16 00:57:39 -05:00
|
|
|
{
|
2010-11-16 01:43:45 -05:00
|
|
|
alpm_list_t *mount_points, *i;
|
|
|
|
int replaces = 0, abort = 0;
|
|
|
|
alpm_list_t *targ;
|
|
|
|
pmpkg_t *pkg;
|
2010-11-16 02:07:43 -05:00
|
|
|
int numtargs = alpm_list_count(trans->add);
|
|
|
|
int current = 0;
|
2010-11-16 01:15:21 -05:00
|
|
|
|
|
|
|
mount_points = mount_point_list();
|
|
|
|
if(mount_points == NULL) {
|
2010-11-16 21:12:27 -05:00
|
|
|
_alpm_log(PM_LOG_ERROR, _("could not determine filesystem mount points"));
|
|
|
|
return(-1);
|
2010-11-16 01:15:21 -05:00
|
|
|
}
|
|
|
|
|
2010-11-16 01:43:45 -05:00
|
|
|
replaces = alpm_list_count(trans->remove);
|
|
|
|
if(replaces) {
|
2010-11-16 02:07:43 -05:00
|
|
|
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),
|
2010-11-16 21:12:27 -05:00
|
|
|
numtargs, current);
|
2010-11-16 02:07:43 -05:00
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
pkg = targ->data;
|
|
|
|
calculate_removed_size(mount_points, pkg);
|
2010-11-16 01:43:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-16 02:07:43 -05:00
|
|
|
for(targ = trans->add; targ; targ = targ->next, current++) {
|
|
|
|
double percent = (double)current / numtargs;
|
|
|
|
PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", (percent * 100),
|
2010-11-16 21:12:27 -05:00
|
|
|
numtargs, current);
|
2010-11-16 02:07:43 -05:00
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
pkg = targ->data;
|
|
|
|
/* is this package already installed? */
|
|
|
|
if(_alpm_db_get_pkgfromcache(db_local, pkg->name)) {
|
|
|
|
calculate_removed_size(mount_points, pkg);
|
2010-11-16 01:43:45 -05:00
|
|
|
}
|
2010-11-16 21:12:27 -05:00
|
|
|
calculate_installed_size(mount_points, pkg);
|
2010-11-16 01:43:45 -05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-16 02:07:43 -05:00
|
|
|
PROGRESS(trans, PM_TRANS_PROGRESS_DISKSPACE_START, "", 100,
|
2010-11-16 21:12:27 -05:00
|
|
|
numtargs, current);
|
2010-11-16 02:07:43 -05:00
|
|
|
|
2010-11-16 01:43:45 -05:00
|
|
|
for(i = mount_points; i; i = alpm_list_next(i)) {
|
|
|
|
alpm_mountpoint_t *data = i->data;
|
|
|
|
if(data->used == 1) {
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "partition %s, needed %ld, free %ld\n",
|
2010-11-16 21:12:27 -05:00
|
|
|
data->mount_dir, data->max_blocks_needed,
|
|
|
|
(unsigned long)data->fsp.f_bfree);
|
|
|
|
if(data->max_blocks_needed > data->fsp.f_bfree) {
|
2010-11-16 01:43:45 -05:00
|
|
|
abort = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-16 01:15:21 -05:00
|
|
|
for(i = mount_points; i; i = alpm_list_next(i)) {
|
|
|
|
alpm_mountpoint_t *data = i->data;
|
|
|
|
FREE(data->mount_dir);
|
|
|
|
}
|
|
|
|
FREELIST(mount_points);
|
|
|
|
|
2010-11-16 01:43:45 -05:00
|
|
|
if(abort) {
|
|
|
|
RET_ERR(PM_ERR_DISK_SPACE, -1);
|
|
|
|
}
|
|
|
|
|
2010-11-16 21:12:27 -05:00
|
|
|
return(0);
|
2010-11-16 00:57:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=2 sw=2 noet: */
|