From f4e9deb6d74614ac427b37513359d91588dd1542 Mon Sep 17 00:00:00 2001 From: Allan McRae Date: Tue, 16 Nov 2010 16:15:21 +1000 Subject: Add function for listing system mount points Add a mount_point_list() function that attempts to portably obtain a list of system mount points and a struct to hold needed mount point information. Abort the transaction if we are unable to determine the mount points. Signed-off-by: Allan McRae Signed-off-by: Dan McGee --- lib/libalpm/diskspace.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) (limited to 'lib/libalpm/diskspace.c') diff --git a/lib/libalpm/diskspace.c b/lib/libalpm/diskspace.c index 31a2760d..a5beb84f 100644 --- a/lib/libalpm/diskspace.c +++ b/lib/libalpm/diskspace.c @@ -17,11 +17,130 @@ * along with this program. If not, see . */ +#include "config.h" + +#if defined HAVE_GETMNTENT +#include +#include +#elif defined HAVE_GETMNTINFO_STATFS +#include +#include +#if HAVE_SYS_UCRED_H +#include +#endif +#elif defined HAVE_GETMNTINFO_STATVFS +#include +#include +#endif + /* libalpm */ #include "diskspace.h" +#include "alpm_list.h" +#include "util.h" +#include "log.h" + +static alpm_list_t *mount_point_list() +{ + alpm_list_t *mount_points = NULL; + alpm_mountpoint_t *mp; + +#if defined HAVE_GETMNTENT + struct mntent *mnt; + FILE *fp; + struct statvfs 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); + + MALLOC(mp->fsp, sizeof(struct statvfs), RET_ERR(PM_ERR_MEMORY, NULL)); + memcpy((void *)(mp->fsp), (void *)(&fsp), sizeof(struct statvfs)); + + 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_STATFS + int entries; + struct statfs *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); + + MALLOC(mp->fsp, sizeof(struct statfs), RET_ERR(PM_ERR_MEMORY, NULL)); + memcpy((void *)(mp->fsp), (void *)fsp, sizeof(struct statfs)); + + mp->blocks_needed = 0; + mp->max_blocks_needed = 0; + + mount_points = alpm_list_add(mount_points, mp); + } +#elif defined HAVE_GETMNTINFO_STATVFS + int entries; + struct statvfs *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); + + MALLOC(mp->fsp, sizeof(struct statvfs), RET_ERR(PM_ERR_MEMORY, NULL)); + memcpy((void *)(mp->fsp), (void *)fsp, sizeof(struct statvfs)); + + mp->blocks_needed = 0; + mp->max_blocks_needed = 0; + + mount_points = alpm_list_add(mount_points, mp); + } +#endif + + return(mount_points); +} int _alpm_check_diskspace(pmtrans_t *trans, pmdb_t *db) { + alpm_list_t *mount_points; + + mount_points = mount_point_list(); + if(mount_points == NULL) { + _alpm_log(PM_LOG_ERROR, _("count not determine filesystem mount points")); + return -1; + } + + for(i = mount_points; i; i = alpm_list_next(i)) { + alpm_mountpoint_t *data = i->data; + FREE(data->mount_dir); + FREE(data->fsp); + } + FREELIST(mount_points); + return 0; } -- cgit v1.2.3-24-g4f1b