Refactor statfs/statvfs type check

Turn it into a configure-type typedef, which allows us to reduce the
amount of duplicated code and clean up some #ifdef magic in the code
itself. Adjust some of the other defined checks to look at the headers
available rather than trying to pull in the right ones based on
configure checks.

Signed-off-by: Dan McGee <dan@archlinux.org>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Dan McGee 2010-11-16 20:12:26 -06:00
parent 24684a616e
commit ec136784d4
4 changed files with 50 additions and 62 deletions

View file

@ -101,3 +101,27 @@ AC_DEFUN([GCC_GNU89_INLINE_CC],[
fi fi
]) ])
dnl Checks for getmntinfo and determines whether it uses statfs or statvfs
AC_DEFUN([FS_STATS_TYPE],
[AC_CACHE_CHECK([filesystem statistics type], fs_stats_cv_type,
[AC_CHECK_FUNC(getmntinfo,
[AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM([[
# include <sys/param.h>
# include <sys/mount.h>
#if HAVE_SYS_UCRED_H
#include <sys/ucred.h>
#endif
extern int getmntinfo (struct statfs **, int);
]],
[])],
[fs_stats_cv_type="struct statfs"],
[fs_stats_cv_type="struct statvfs"])],
[AC_CHECK_FUNC(getmntent,
[fs_stats_cv_type="struct statvfs"])]
)]
)
AC_DEFINE_UNQUOTED(FSSTATSTYPE, [$fs_stats_cv_type],
[Defined as the filesystem stats type ('statvfs' or 'statfs')])
])

View file

@ -191,27 +191,11 @@ AC_FUNC_FORK
AC_FUNC_GETMNTENT AC_FUNC_GETMNTENT
AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK
AC_FUNC_MKTIME AC_FUNC_MKTIME
AC_CHECK_FUNCS([geteuid realpath regcomp strcasecmp \ AC_CHECK_FUNCS([geteuid getmntinfo realpath regcomp strcasecmp \
strndup strrchr strsep swprintf \ strndup strrchr strsep swprintf \
wcwidth uname]) wcwidth uname])
# For the diskspace code
# Checks for getmntinfo and determines whether it uses statfs or statvfs FS_STATS_TYPE
AC_CHECK_FUNC(getmntinfo,
[AC_MSG_CHECKING(parameter style for getmntinfo)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
# include <sys/param.h>
# include <sys/mount.h>
#if HAVE_SYS_UCRED_H
#include <sys/ucred.h>
#endif
extern int getmntinfo (struct statfs **, int);
]], [])],
[AC_DEFINE(HAVE_GETMNTINFO_STATFS, [], [getmntinfo uses statfs])
AC_MSG_RESULT(statfs)],
[AC_DEFINE(HAVE_GETMNTINFO_STATVFS, [], [getmntinfo uses statvfs])
AC_MSG_RESULT(statvfs)])
])
# Enable large file support if available # Enable large file support if available
AC_SYS_LARGEFILE AC_SYS_LARGEFILE

View file

@ -19,18 +19,23 @@
#include "config.h" #include "config.h"
#if defined HAVE_GETMNTENT #if defined(HAVE_MNTENT_H)
#include <mntent.h> #include <mntent.h>
#endif
#if defined(HAVE_SYS_STATVFS_H)
#include <sys/statvfs.h> #include <sys/statvfs.h>
#elif defined HAVE_GETMNTINFO_STATFS #endif
#if defined(HAVE_SYS_PARAM_H)
#include <sys/param.h> #include <sys/param.h>
#endif
#if defined(HAVE_SYS_MOUNT_H)
#include <sys/mount.h> #include <sys/mount.h>
#if HAVE_SYS_UCRED_H #endif
#if defined(HAVE_SYS_UCRED_H)
#include <sys/ucred.h> #include <sys/ucred.h>
#endif #endif
#elif defined HAVE_GETMNTINFO_STATVFS #if defined(HAVE_SYS_TYPES_H)
#include <sys/types.h> #include <sys/types.h>
#include <sys/statvfs.h>
#endif #endif
#include <math.h> #include <math.h>
@ -60,7 +65,7 @@ static alpm_list_t *mount_point_list()
#if defined HAVE_GETMNTENT #if defined HAVE_GETMNTENT
struct mntent *mnt; struct mntent *mnt;
FILE *fp; FILE *fp;
struct statvfs fsp; FSSTATSTYPE fsp;
fp = setmntent(MOUNTED, "r"); fp = setmntent(MOUNTED, "r");
@ -77,8 +82,8 @@ static alpm_list_t *mount_point_list()
MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL)); MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
mp->mount_dir = strdup(mnt->mnt_dir); mp->mount_dir = strdup(mnt->mnt_dir);
MALLOC(mp->fsp, sizeof(struct statvfs), RET_ERR(PM_ERR_MEMORY, NULL)); MALLOC(mp->fsp, sizeof(FSSTATSTYPE), RET_ERR(PM_ERR_MEMORY, NULL));
memcpy((void *)(mp->fsp), (void *)(&fsp), sizeof(struct statvfs)); memcpy((void *)(mp->fsp), (void *)(&fsp), sizeof(FSSTATSTYPE));
mp->blocks_needed = 0; mp->blocks_needed = 0;
mp->max_blocks_needed = 0; mp->max_blocks_needed = 0;
@ -88,9 +93,9 @@ static alpm_list_t *mount_point_list()
} }
endmntent(fp); endmntent(fp);
#elif defined HAVE_GETMNTINFO_STATFS #elif defined HAVE_GETMNTINFO
int entries; int entries;
struct statfs *fsp; FSSTATSTYPE *fsp;
entries = getmntinfo(&fsp, MNT_NOWAIT); entries = getmntinfo(&fsp, MNT_NOWAIT);
@ -102,30 +107,8 @@ static alpm_list_t *mount_point_list()
MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL)); MALLOC(mp, sizeof(alpm_mountpoint_t), RET_ERR(PM_ERR_MEMORY, NULL));
mp->mount_dir = strdup(fsp->f_mntonname); mp->mount_dir = strdup(fsp->f_mntonname);
MALLOC(mp->fsp, sizeof(struct statfs), RET_ERR(PM_ERR_MEMORY, NULL)); MALLOC(mp->fsp, sizeof(FSSTATSTYPE), RET_ERR(PM_ERR_MEMORY, NULL));
memcpy((void *)(mp->fsp), (void *)fsp, sizeof(struct statfs)); memcpy((void *)(mp->fsp), (void *)fsp, sizeof(FSSTATSTYPE));
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->blocks_needed = 0;
mp->max_blocks_needed = 0; mp->max_blocks_needed = 0;

View file

@ -20,9 +20,10 @@
#ifndef _ALPM_DISKSPACE_H #ifndef _ALPM_DISKSPACE_H
#define _ALPM_DISKSPACE_H #define _ALPM_DISKSPACE_H
#if defined HAVE_GETMNTINFO_STATFS #if defined(HAVE_SYS_MOUNT_H)
#include <sys/mount.h> #include <sys/mount.h>
#else #endif
#if defined(HAVE_SYS_STATVFS_H)
#include <sys/statvfs.h> #include <sys/statvfs.h>
#endif #endif
@ -31,15 +32,11 @@
typedef struct __alpm_mountpoint_t { typedef struct __alpm_mountpoint_t {
/* mount point information */ /* mount point information */
char *mount_dir; char *mount_dir;
#if defined HAVE_GETMNTINFO_STATFS
struct statfs *fsp;
#else
struct statvfs *fsp;
#endif
/* storage for additional disk usage calculations */ /* storage for additional disk usage calculations */
long blocks_needed; long blocks_needed;
long max_blocks_needed; long max_blocks_needed;
int used; int used;
FSSTATSTYPE *fsp;
} alpm_mountpoint_t; } alpm_mountpoint_t;
int _alpm_check_diskspace(pmtrans_t *trans, pmdb_t *db); int _alpm_check_diskspace(pmtrans_t *trans, pmdb_t *db);