2001-10-18 00:43:36 +08:00
|
|
|
/*@-mods@*/
|
2000-08-28 03:27:03 +08:00
|
|
|
/**
|
|
|
|
* \file lib/fs.c
|
|
|
|
*/
|
|
|
|
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "system.h"
|
1999-07-14 05:37:57 +08:00
|
|
|
#include <rpmlib.h>
|
2000-12-13 04:03:45 +08:00
|
|
|
#include <rpmmacro.h> /* XXX for rpmGetPath */
|
|
|
|
#include "debug.h"
|
1997-05-31 05:57:31 +08:00
|
|
|
|
2001-04-30 06:43:01 +08:00
|
|
|
/*@-usereleased -onlytrans@*/
|
|
|
|
|
1997-05-31 05:57:31 +08:00
|
|
|
struct fsinfo {
|
2000-10-21 00:47:00 +08:00
|
|
|
/*@only@*/ const char * mntPoint; /*!< path to mount point. */
|
|
|
|
dev_t dev; /*!< devno for mount point. */
|
|
|
|
int rdonly; /*!< is mount point read only? */
|
1997-05-31 05:57:31 +08:00
|
|
|
};
|
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
1999-09-18 06:02:44 +08:00
|
|
|
/*@only@*/ /*@null@*/ static struct fsinfo * filesystems = NULL;
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
1999-09-18 06:02:44 +08:00
|
|
|
/*@only@*/ /*@null@*/ static const char ** fsnames = NULL;
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@unchecked@*/
|
1999-09-18 06:02:44 +08:00
|
|
|
static int numFilesystems = 0;
|
1997-05-31 05:57:31 +08:00
|
|
|
|
1999-10-30 00:06:01 +08:00
|
|
|
void freeFilesystems(void)
|
|
|
|
{
|
|
|
|
if (filesystems) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < numFilesystems; i++)
|
2001-04-29 09:05:43 +08:00
|
|
|
filesystems[i].mntPoint = _free(filesystems[i].mntPoint);
|
|
|
|
filesystems = _free(filesystems);
|
1999-10-30 00:06:01 +08:00
|
|
|
}
|
|
|
|
if (fsnames) {
|
2000-11-15 23:28:46 +08:00
|
|
|
#if 0 /* XXX leak/segfault on exit of "rpm -qp --qf '%{#fsnames}' pkg" */
|
1999-10-30 00:06:01 +08:00
|
|
|
free(fsnames);
|
2000-11-15 23:28:46 +08:00
|
|
|
#endif
|
1999-10-30 00:06:01 +08:00
|
|
|
fsnames = NULL;
|
|
|
|
}
|
|
|
|
numFilesystems = 0;
|
|
|
|
}
|
|
|
|
|
1997-06-17 22:49:07 +08:00
|
|
|
#if HAVE_MNTCTL
|
|
|
|
|
|
|
|
/* modeled after sample code from Till Bubeck */
|
|
|
|
|
|
|
|
#include <sys/mntctl.h>
|
|
|
|
#include <sys/vmount.h>
|
|
|
|
|
1997-06-19 21:13:50 +08:00
|
|
|
/*
|
|
|
|
* There is NO mntctl prototype in any header file of AIX 3.2.5!
|
|
|
|
* So we have to declare it by ourself...
|
|
|
|
*/
|
|
|
|
int mntctl(int command, int size, char *buffer);
|
|
|
|
|
2000-10-21 00:47:00 +08:00
|
|
|
/**
|
|
|
|
* Get information for mounted file systems.
|
|
|
|
* @todo determine rdonly for non-linux file systems.
|
|
|
|
* @return 0 on success, 1 on error
|
|
|
|
*/
|
1999-07-14 06:17:16 +08:00
|
|
|
static int getFilesystemList(void)
|
2001-10-14 03:35:58 +08:00
|
|
|
/*@*/
|
1999-07-14 06:17:16 +08:00
|
|
|
{
|
1997-06-17 22:49:07 +08:00
|
|
|
int size;
|
|
|
|
void * buf;
|
|
|
|
struct vmount * vm;
|
1997-06-19 21:13:50 +08:00
|
|
|
struct stat sb;
|
2000-10-21 00:47:00 +08:00
|
|
|
int rdonly = 0;
|
1997-06-17 22:49:07 +08:00
|
|
|
int num;
|
|
|
|
int fsnameLength;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
num = mntctl(MCTL_QUERY, sizeof(size), (char *) &size);
|
|
|
|
if (num < 0) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_MTAB, _("mntctl() failed to return size: %s\n"),
|
1997-06-17 22:49:07 +08:00
|
|
|
strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1997-06-19 21:13:50 +08:00
|
|
|
/*
|
|
|
|
* Double the needed size, so that even when the user mounts a
|
|
|
|
* filesystem between the previous and the next call to mntctl
|
|
|
|
* the buffer still is large enough.
|
|
|
|
*/
|
|
|
|
size *= 2;
|
|
|
|
|
1997-06-17 22:49:07 +08:00
|
|
|
buf = alloca(size);
|
|
|
|
num = mntctl(MCTL_QUERY, size, buf);
|
1997-06-19 21:13:50 +08:00
|
|
|
if ( num <= 0 ) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_MTAB, _("mntctl() failed to return mount points: %s\n"),
|
1997-06-19 21:13:50 +08:00
|
|
|
strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
1997-06-17 22:49:07 +08:00
|
|
|
|
|
|
|
numFilesystems = num;
|
|
|
|
|
1999-09-21 11:22:53 +08:00
|
|
|
filesystems = xcalloc((numFilesystems + 1), sizeof(*filesystems));
|
|
|
|
fsnames = xcalloc((numFilesystems + 1), sizeof(char *));
|
1997-06-17 22:49:07 +08:00
|
|
|
|
|
|
|
for (vm = buf, i = 0; i < num; i++) {
|
1999-01-07 01:33:50 +08:00
|
|
|
char *fsn;
|
1997-06-17 22:49:07 +08:00
|
|
|
fsnameLength = vm->vmt_data[VMT_STUB].vmt_size;
|
1999-09-21 11:22:53 +08:00
|
|
|
fsn = xmalloc(fsnameLength + 1);
|
1999-01-07 01:33:50 +08:00
|
|
|
strncpy(fsn, (char *)vm + vm->vmt_data[VMT_STUB].vmt_off,
|
1997-06-19 21:13:50 +08:00
|
|
|
fsnameLength);
|
1997-06-17 22:49:07 +08:00
|
|
|
|
1999-01-07 01:33:50 +08:00
|
|
|
filesystems[i].mntPoint = fsnames[i] = fsn;
|
1997-06-19 21:13:50 +08:00
|
|
|
|
|
|
|
if (stat(filesystems[i].mntPoint, &sb)) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_STAT, _("failed to stat %s: %s\n"), fsnames[i],
|
1997-06-17 22:49:07 +08:00
|
|
|
strerror(errno));
|
|
|
|
|
2000-03-18 22:24:21 +08:00
|
|
|
freeFilesystems();
|
1999-10-30 00:06:01 +08:00
|
|
|
return 1;
|
1997-06-17 22:49:07 +08:00
|
|
|
}
|
|
|
|
|
1997-06-19 21:13:50 +08:00
|
|
|
filesystems[i].dev = sb.st_dev;
|
2000-10-21 00:47:00 +08:00
|
|
|
filesystems[i].rdonly = rdonly;
|
1997-06-17 22:49:07 +08:00
|
|
|
|
|
|
|
/* goto the next vmount structure: */
|
|
|
|
vm = (struct vmount *)((char *)vm + vm->vmt_length);
|
|
|
|
}
|
|
|
|
|
1997-06-19 21:13:50 +08:00
|
|
|
filesystems[i].mntPoint = NULL;
|
|
|
|
fsnames[i] = NULL;
|
|
|
|
|
1997-06-17 22:49:07 +08:00
|
|
|
return 0;
|
|
|
|
}
|
1999-10-30 00:06:01 +08:00
|
|
|
|
|
|
|
#else /* HAVE_MNTCTL */
|
|
|
|
|
2000-10-21 00:47:00 +08:00
|
|
|
/**
|
|
|
|
* Get information for mounted file systems.
|
|
|
|
* @todo determine rdonly for non-linux file systems.
|
|
|
|
* @return 0 on success, 1 on error
|
|
|
|
*/
|
1999-07-14 06:17:16 +08:00
|
|
|
static int getFilesystemList(void)
|
2001-10-15 11:22:10 +08:00
|
|
|
/*@globals fileSystem, internalState@*/
|
|
|
|
/*@modifies fileSystem, internalState@*/
|
1999-07-14 06:17:16 +08:00
|
|
|
{
|
1997-05-31 05:57:31 +08:00
|
|
|
int numAlloced = 10;
|
|
|
|
struct stat sb;
|
|
|
|
int i;
|
2000-10-21 00:47:00 +08:00
|
|
|
const char * mntdir;
|
|
|
|
int rdonly = 0;
|
1998-03-05 00:52:59 +08:00
|
|
|
# if GETMNTENT_ONE || GETMNTENT_TWO
|
2000-02-28 04:50:52 +08:00
|
|
|
our_mntent item;
|
1997-06-20 06:18:02 +08:00
|
|
|
FILE * mtab;
|
1998-03-05 00:52:59 +08:00
|
|
|
# elif HAVE_GETMNTINFO_R
|
1997-06-20 06:18:02 +08:00
|
|
|
struct statfs * mounts = NULL;
|
|
|
|
int mntCount = 0, bufSize = 0, flags = MNT_NOWAIT;
|
|
|
|
int nextMount = 0;
|
1998-03-05 00:52:59 +08:00
|
|
|
# endif
|
1997-06-20 06:18:02 +08:00
|
|
|
|
1999-09-06 00:56:44 +08:00
|
|
|
rpmMessage(RPMMESS_DEBUG, _("getting list of mounted filesystems\n"));
|
|
|
|
|
1998-03-05 00:52:59 +08:00
|
|
|
# if GETMNTENT_ONE || GETMNTENT_TWO
|
1997-06-20 06:18:02 +08:00
|
|
|
mtab = fopen(MOUNTED, "r");
|
|
|
|
if (!mtab) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_MTAB, _("failed to open %s: %s\n"), MOUNTED,
|
1997-06-20 06:18:02 +08:00
|
|
|
strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
1998-03-05 00:52:59 +08:00
|
|
|
# elif HAVE_GETMNTINFO_R
|
1997-06-20 06:18:02 +08:00
|
|
|
getmntinfo_r(&mounts, flags, &mntCount, &bufSize);
|
1998-03-05 00:52:59 +08:00
|
|
|
# endif
|
1997-05-31 05:57:31 +08:00
|
|
|
|
1999-09-22 01:21:57 +08:00
|
|
|
filesystems = xcalloc((numAlloced + 1), sizeof(*filesystems)); /* XXX memory leak */
|
1997-05-31 05:57:31 +08:00
|
|
|
|
1999-10-30 00:06:01 +08:00
|
|
|
numFilesystems = 0;
|
1997-05-31 05:57:31 +08:00
|
|
|
while (1) {
|
1998-03-05 00:52:59 +08:00
|
|
|
# if GETMNTENT_ONE
|
1997-05-31 05:57:31 +08:00
|
|
|
/* this is Linux */
|
2001-10-14 03:35:58 +08:00
|
|
|
/*@-modunconnomods -moduncon @*/
|
2000-02-28 04:50:52 +08:00
|
|
|
our_mntent * itemptr = getmntent(mtab);
|
1997-05-31 05:57:31 +08:00
|
|
|
if (!itemptr) break;
|
2000-10-21 00:47:00 +08:00
|
|
|
item = *itemptr; /* structure assignment */
|
1997-06-20 06:18:02 +08:00
|
|
|
mntdir = item.our_mntdir;
|
2000-10-21 00:47:00 +08:00
|
|
|
#if defined(MNTOPT_RO)
|
2001-04-30 06:43:01 +08:00
|
|
|
/*@-compdef@*/
|
2000-10-21 00:47:00 +08:00
|
|
|
if (hasmntopt(itemptr, MNTOPT_RO) != NULL)
|
|
|
|
rdonly = 1;
|
2001-04-30 06:43:01 +08:00
|
|
|
/*@=compdef@*/
|
2000-10-21 00:47:00 +08:00
|
|
|
#endif
|
2001-10-14 03:35:58 +08:00
|
|
|
/*@=modunconnomods =moduncon @*/
|
1998-03-05 00:52:59 +08:00
|
|
|
# elif GETMNTENT_TWO
|
1997-05-31 05:57:31 +08:00
|
|
|
/* Solaris, maybe others */
|
|
|
|
if (getmntent(mtab, &item)) break;
|
1997-06-20 06:18:02 +08:00
|
|
|
mntdir = item.our_mntdir;
|
1998-03-05 00:52:59 +08:00
|
|
|
# elif HAVE_GETMNTINFO_R
|
1997-06-20 06:18:02 +08:00
|
|
|
if (nextMount == mntCount) break;
|
|
|
|
mntdir = mounts[nextMount++].f_mntonname;
|
1998-03-05 00:52:59 +08:00
|
|
|
# endif
|
1997-05-31 05:57:31 +08:00
|
|
|
|
1997-06-20 06:18:02 +08:00
|
|
|
if (stat(mntdir, &sb)) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_STAT, _("failed to stat %s: %s\n"), mntdir,
|
1997-05-31 05:57:31 +08:00
|
|
|
strerror(errno));
|
|
|
|
|
1999-10-30 00:06:01 +08:00
|
|
|
freeFilesystems();
|
1999-04-07 14:11:01 +08:00
|
|
|
return 1;
|
1997-05-31 05:57:31 +08:00
|
|
|
}
|
|
|
|
|
1999-10-30 00:06:01 +08:00
|
|
|
numFilesystems++;
|
|
|
|
if ((numFilesystems + 1) == numAlloced) {
|
1997-05-31 05:57:31 +08:00
|
|
|
numAlloced += 10;
|
1999-09-21 11:22:53 +08:00
|
|
|
filesystems = xrealloc(filesystems,
|
1997-05-31 05:57:31 +08:00
|
|
|
sizeof(*filesystems) * (numAlloced + 1));
|
|
|
|
}
|
|
|
|
|
1999-10-30 00:06:01 +08:00
|
|
|
filesystems[numFilesystems-1].dev = sb.st_dev;
|
|
|
|
filesystems[numFilesystems-1].mntPoint = xstrdup(mntdir);
|
2000-10-21 00:47:00 +08:00
|
|
|
filesystems[numFilesystems-1].rdonly = rdonly;
|
1997-05-31 05:57:31 +08:00
|
|
|
}
|
|
|
|
|
1998-03-05 00:52:59 +08:00
|
|
|
# if GETMNTENT_ONE || GETMNTENT_TWO
|
2001-05-01 06:32:22 +08:00
|
|
|
(void) fclose(mtab);
|
1998-03-05 00:52:59 +08:00
|
|
|
# elif HAVE_GETMNTINFO_R
|
2001-05-01 06:32:22 +08:00
|
|
|
mounts = _free(mounts);
|
1998-03-05 00:52:59 +08:00
|
|
|
# endif
|
1997-06-20 06:18:02 +08:00
|
|
|
|
1999-10-30 00:06:01 +08:00
|
|
|
filesystems[numFilesystems].dev = 0;
|
|
|
|
filesystems[numFilesystems].mntPoint = NULL;
|
2000-10-21 00:47:00 +08:00
|
|
|
filesystems[numFilesystems].rdonly = 0;
|
1997-05-31 05:57:31 +08:00
|
|
|
|
1999-10-30 00:06:01 +08:00
|
|
|
fsnames = xcalloc((numFilesystems + 1), sizeof(*fsnames));
|
|
|
|
for (i = 0; i < numFilesystems; i++)
|
1997-05-31 05:57:31 +08:00
|
|
|
fsnames[i] = filesystems[i].mntPoint;
|
1999-10-30 00:06:01 +08:00
|
|
|
fsnames[numFilesystems] = NULL;
|
1997-05-31 05:57:31 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
1999-10-30 00:06:01 +08:00
|
|
|
#endif /* HAVE_MNTCTL */
|
1997-05-31 05:57:31 +08:00
|
|
|
|
1999-07-14 06:17:16 +08:00
|
|
|
int rpmGetFilesystemList(const char *** listptr, int * num)
|
|
|
|
{
|
1997-05-31 05:57:31 +08:00
|
|
|
if (!fsnames)
|
|
|
|
if (getFilesystemList())
|
|
|
|
return 1;
|
|
|
|
|
1997-08-21 09:20:16 +08:00
|
|
|
if (listptr) *listptr = fsnames;
|
|
|
|
if (num) *num = numFilesystems;
|
|
|
|
|
1997-05-31 05:57:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-01-07 01:33:50 +08:00
|
|
|
int rpmGetFilesystemUsage(const char ** fileList, int_32 * fssizes, int numFiles,
|
2000-10-29 01:16:25 +08:00
|
|
|
uint_32 ** usagesPtr, /*@unused@*/ int flags)
|
1999-07-14 06:17:16 +08:00
|
|
|
{
|
1997-05-31 05:57:31 +08:00
|
|
|
int_32 * usages;
|
|
|
|
int i, len, j;
|
|
|
|
char * buf, * dirName;
|
|
|
|
char * chptr;
|
|
|
|
int maxLen;
|
1999-07-14 06:17:16 +08:00
|
|
|
char * lastDir;
|
|
|
|
const char * sourceDir;
|
1997-05-31 05:57:31 +08:00
|
|
|
int lastfs = 0;
|
|
|
|
int lastDev = -1; /* I hope nobody uses -1 for a st_dev */
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (!fsnames)
|
|
|
|
if (getFilesystemList())
|
|
|
|
return 1;
|
|
|
|
|
1999-09-21 11:22:53 +08:00
|
|
|
usages = xcalloc(numFilesystems, sizeof(usages));
|
1997-05-31 05:57:31 +08:00
|
|
|
|
1999-10-30 00:06:01 +08:00
|
|
|
sourceDir = rpmGetPath("%{_sourcedir}", NULL);
|
1999-07-14 06:17:16 +08:00
|
|
|
|
|
|
|
maxLen = strlen(sourceDir);
|
1997-05-31 05:57:31 +08:00
|
|
|
for (i = 0; i < numFiles; i++) {
|
|
|
|
len = strlen(fileList[i]);
|
|
|
|
if (maxLen < len) maxLen = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = alloca(maxLen + 1);
|
|
|
|
lastDir = alloca(maxLen + 1);
|
|
|
|
dirName = alloca(maxLen + 1);
|
|
|
|
*lastDir = '\0';
|
|
|
|
|
1998-04-03 01:48:34 +08:00
|
|
|
/* cut off last filename */
|
1997-05-31 05:57:31 +08:00
|
|
|
for (i = 0; i < numFiles; i++) {
|
1999-04-07 13:54:45 +08:00
|
|
|
if (*fileList[i] == '/') {
|
|
|
|
strcpy(buf, fileList[i]);
|
|
|
|
chptr = buf + strlen(buf) - 1;
|
|
|
|
while (*chptr != '/') chptr--;
|
|
|
|
if (chptr == buf)
|
|
|
|
buf[1] = '\0';
|
|
|
|
else
|
|
|
|
*chptr-- = '\0';
|
|
|
|
} else {
|
|
|
|
/* this should only happen for source packages (gulp) */
|
1999-07-14 06:17:16 +08:00
|
|
|
strcpy(buf, sourceDir);
|
1999-04-07 13:54:45 +08:00
|
|
|
}
|
1997-05-31 05:57:31 +08:00
|
|
|
|
|
|
|
if (strcmp(lastDir, buf)) {
|
|
|
|
strcpy(dirName, buf);
|
|
|
|
chptr = dirName + strlen(dirName) - 1;
|
|
|
|
while (stat(dirName, &sb)) {
|
|
|
|
if (errno != ENOENT) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_STAT, _("failed to stat %s: %s\n"), buf,
|
1997-05-31 05:57:31 +08:00
|
|
|
strerror(errno));
|
2001-04-29 09:05:43 +08:00
|
|
|
sourceDir = _free(sourceDir);
|
|
|
|
usages = _free(usages);
|
1997-05-31 05:57:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-04-03 01:48:34 +08:00
|
|
|
/* cut off last directory part, because it was not found. */
|
1997-05-31 05:57:31 +08:00
|
|
|
while (*chptr != '/') chptr--;
|
1998-04-03 01:48:34 +08:00
|
|
|
|
|
|
|
if (chptr == dirName)
|
|
|
|
dirName[1] = '\0';
|
|
|
|
else
|
|
|
|
*chptr-- = '\0';
|
1997-05-31 05:57:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lastDev != sb.st_dev) {
|
|
|
|
for (j = 0; j < numFilesystems; j++)
|
2001-06-06 03:26:22 +08:00
|
|
|
if (filesystems && filesystems[j].dev == sb.st_dev)
|
|
|
|
/*@innerbreak@*/ break;
|
1997-05-31 05:57:31 +08:00
|
|
|
|
|
|
|
if (j == numFilesystems) {
|
|
|
|
rpmError(RPMERR_BADDEV,
|
2001-01-16 07:09:42 +08:00
|
|
|
_("file %s is on an unknown device\n"), buf);
|
2001-04-29 09:05:43 +08:00
|
|
|
sourceDir = _free(sourceDir);
|
|
|
|
usages = _free(usages);
|
1997-05-31 05:57:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastfs = j;
|
|
|
|
lastDev = sb.st_dev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(lastDir, buf);
|
|
|
|
usages[lastfs] += fssizes[i];
|
|
|
|
}
|
|
|
|
|
2001-04-29 09:05:43 +08:00
|
|
|
sourceDir = _free(sourceDir);
|
1999-04-07 13:54:45 +08:00
|
|
|
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@-branchstate@*/
|
2001-04-29 09:05:43 +08:00
|
|
|
if (usagesPtr)
|
|
|
|
*usagesPtr = usages;
|
|
|
|
else
|
|
|
|
usages = _free(usages);
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@=branchstate@*/
|
1997-05-31 05:57:31 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2001-04-30 06:43:01 +08:00
|
|
|
/*@=usereleased =onlytrans@*/
|
2001-10-18 00:43:36 +08:00
|
|
|
/*@=mods@*/
|