Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

lib/fs.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 #include <rpmlib.h>
00007 #include <rpmmacro.h>   /* XXX for rpmGetPath */
00008 #include "debug.h"
00009 
00010 /*@-usereleased -onlytrans@*/
00011 
00012 struct fsinfo {
00013 /*@only@*/ const char * mntPoint;       
00014     dev_t dev;                          
00015     int rdonly;                         
00016 };
00017 
00018 /*@only@*/ /*@null@*/ static struct fsinfo * filesystems = NULL;
00019 /*@only@*/ /*@null@*/ static const char ** fsnames = NULL;
00020 static int numFilesystems = 0;
00021 
00022 void freeFilesystems(void)
00023 {
00024     if (filesystems) {
00025         int i;
00026         for (i = 0; i < numFilesystems; i++)
00027             filesystems[i].mntPoint = _free(filesystems[i].mntPoint);
00028         filesystems = _free(filesystems);
00029     }
00030     if (fsnames) {
00031 #if 0   /* XXX leak/segfault on exit of "rpm -qp --qf '%{#fsnames}' pkg" */
00032         free(fsnames);
00033 #endif
00034         fsnames = NULL;
00035     }
00036     numFilesystems = 0;
00037 }
00038 
00039 #if HAVE_MNTCTL
00040 
00041 /* modeled after sample code from Till Bubeck */
00042 
00043 #include <sys/mntctl.h>
00044 #include <sys/vmount.h>
00045 
00046 /* 
00047  * There is NO mntctl prototype in any header file of AIX 3.2.5! 
00048  * So we have to declare it by ourself...
00049  */
00050 int mntctl(int command, int size, char *buffer);
00051 
00057 static int getFilesystemList(void)
00058 {
00059     int size;
00060     void * buf;
00061     struct vmount * vm;
00062     struct stat sb;
00063     int rdonly = 0;
00064     int num;
00065     int fsnameLength;
00066     int i;
00067 
00068     num = mntctl(MCTL_QUERY, sizeof(size), (char *) &size);
00069     if (num < 0) {
00070         rpmError(RPMERR_MTAB, _("mntctl() failed to return size: %s\n"), 
00071                  strerror(errno));
00072         return 1;
00073     }
00074 
00075     /*
00076      * Double the needed size, so that even when the user mounts a 
00077      * filesystem between the previous and the next call to mntctl
00078      * the buffer still is large enough.
00079      */
00080     size *= 2;
00081 
00082     buf = alloca(size);
00083     num = mntctl(MCTL_QUERY, size, buf);
00084     if ( num <= 0 ) {
00085         rpmError(RPMERR_MTAB, _("mntctl() failed to return mount points: %s\n"), 
00086                  strerror(errno));
00087         return 1;
00088     }
00089 
00090     numFilesystems = num;
00091 
00092     filesystems = xcalloc((numFilesystems + 1), sizeof(*filesystems));
00093     fsnames = xcalloc((numFilesystems + 1), sizeof(char *));
00094     
00095     for (vm = buf, i = 0; i < num; i++) {
00096         char *fsn;
00097         fsnameLength = vm->vmt_data[VMT_STUB].vmt_size;
00098         fsn = xmalloc(fsnameLength + 1);
00099         strncpy(fsn, (char *)vm + vm->vmt_data[VMT_STUB].vmt_off, 
00100                 fsnameLength);
00101 
00102         filesystems[i].mntPoint = fsnames[i] = fsn;
00103         
00104         if (stat(filesystems[i].mntPoint, &sb)) {
00105             rpmError(RPMERR_STAT, _("failed to stat %s: %s\n"), fsnames[i],
00106                         strerror(errno));
00107 
00108             freeFilesystems();
00109             return 1;
00110         }
00111         
00112         filesystems[i].dev = sb.st_dev;
00113         filesystems[i].rdonly = rdonly;
00114 
00115         /* goto the next vmount structure: */
00116         vm = (struct vmount *)((char *)vm + vm->vmt_length);
00117     }
00118 
00119     filesystems[i].mntPoint = NULL;
00120     fsnames[i]              = NULL;
00121 
00122     return 0;
00123 }
00124 
00125 #else   /* HAVE_MNTCTL */
00126 
00132 static int getFilesystemList(void)
00133 {
00134     int numAlloced = 10;
00135     struct stat sb;
00136     int i;
00137     const char * mntdir;
00138     int rdonly = 0;
00139 #   if GETMNTENT_ONE || GETMNTENT_TWO
00140     our_mntent item;
00141     FILE * mtab;
00142 #   elif HAVE_GETMNTINFO_R
00143     struct statfs * mounts = NULL;
00144     int mntCount = 0, bufSize = 0, flags = MNT_NOWAIT;
00145     int nextMount = 0;
00146 #   endif
00147 
00148     rpmMessage(RPMMESS_DEBUG, _("getting list of mounted filesystems\n"));
00149 
00150 #   if GETMNTENT_ONE || GETMNTENT_TWO
00151         mtab = fopen(MOUNTED, "r");
00152         if (!mtab) {
00153             rpmError(RPMERR_MTAB, _("failed to open %s: %s\n"), MOUNTED, 
00154                      strerror(errno));
00155             return 1;
00156         }
00157 #   elif HAVE_GETMNTINFO_R
00158         getmntinfo_r(&mounts, flags, &mntCount, &bufSize);
00159 #   endif
00160 
00161     filesystems = xcalloc((numAlloced + 1), sizeof(*filesystems));      /* XXX memory leak */
00162 
00163     numFilesystems = 0;
00164     while (1) {
00165 #       if GETMNTENT_ONE
00166             /* this is Linux */
00167             /*@-modunconnomods@*/
00168             our_mntent * itemptr = getmntent(mtab);
00169             if (!itemptr) break;
00170             item = *itemptr;    /* structure assignment */
00171             mntdir = item.our_mntdir;
00172 #if defined(MNTOPT_RO)
00173             /*@-compdef@*/
00174             if (hasmntopt(itemptr, MNTOPT_RO) != NULL)
00175                 rdonly = 1;
00176             /*@=compdef@*/
00177 #endif
00178             /*@=modunconnomods@*/
00179 #       elif GETMNTENT_TWO
00180             /* Solaris, maybe others */
00181             if (getmntent(mtab, &item)) break;
00182             mntdir = item.our_mntdir;
00183 #       elif HAVE_GETMNTINFO_R
00184             if (nextMount == mntCount) break;
00185             mntdir = mounts[nextMount++].f_mntonname;
00186 #       endif
00187 
00188         if (strncmp(mntdir, "/mnt/", 5)) {
00189         if (stat(mntdir, &sb)) {
00190             rpmError(RPMERR_STAT, _("failed to stat %s: %s\n"), mntdir,
00191                         strerror(errno));
00192 
00193             freeFilesystems();
00194             return 1;
00195         }
00196         } else {
00197           sb.st_dev = 0;
00198         }
00199         
00200         numFilesystems++;
00201         if ((numFilesystems + 1) == numAlloced) {
00202             numAlloced += 10;
00203             filesystems = xrealloc(filesystems, 
00204                                   sizeof(*filesystems) * (numAlloced + 1));
00205         }
00206 
00207         filesystems[numFilesystems-1].dev = sb.st_dev;
00208         filesystems[numFilesystems-1].mntPoint = xstrdup(mntdir);
00209         filesystems[numFilesystems-1].rdonly = rdonly;
00210     }
00211 
00212 #   if GETMNTENT_ONE || GETMNTENT_TWO
00213         (void) fclose(mtab);
00214 #   elif HAVE_GETMNTINFO_R
00215         mounts = _free(mounts);
00216 #   endif
00217 
00218     filesystems[numFilesystems].dev = 0;
00219     filesystems[numFilesystems].mntPoint = NULL;
00220     filesystems[numFilesystems].rdonly = 0;
00221 
00222     fsnames = xcalloc((numFilesystems + 1), sizeof(*fsnames));
00223     for (i = 0; i < numFilesystems; i++)
00224         fsnames[i] = filesystems[i].mntPoint;
00225     fsnames[numFilesystems] = NULL;
00226 
00227     return 0; 
00228 }
00229 #endif  /* HAVE_MNTCTL */
00230 
00231 int rpmGetFilesystemList(const char *** listptr, int * num)
00232 {
00233     if (!fsnames) 
00234         if (getFilesystemList())
00235             return 1;
00236 
00237     if (listptr) *listptr = fsnames;
00238     if (num) *num = numFilesystems;
00239 
00240     return 0;
00241 }
00242 
00243 int rpmGetFilesystemUsage(const char ** fileList, int_32 * fssizes, int numFiles,
00244                           uint_32 ** usagesPtr, /*@unused@*/ int flags)
00245 {
00246     int_32 * usages;
00247     int i, len, j;
00248     char * buf, * dirName;
00249     char * chptr;
00250     int maxLen;
00251     char * lastDir;
00252     const char * sourceDir;
00253     int lastfs = 0;
00254     int lastDev = -1;           /* I hope nobody uses -1 for a st_dev */
00255     struct stat sb;
00256 
00257     if (!fsnames) 
00258         if (getFilesystemList())
00259             return 1;
00260 
00261     usages = xcalloc(numFilesystems, sizeof(usages));
00262 
00263     sourceDir = rpmGetPath("%{_sourcedir}", NULL);
00264 
00265     maxLen = strlen(sourceDir);
00266     for (i = 0; i < numFiles; i++) {
00267         len = strlen(fileList[i]);
00268         if (maxLen < len) maxLen = len;
00269     }
00270     
00271     buf = alloca(maxLen + 1);
00272     lastDir = alloca(maxLen + 1);
00273     dirName = alloca(maxLen + 1);
00274     *lastDir = '\0';
00275 
00276     /* cut off last filename */
00277     for (i = 0; i < numFiles; i++) {
00278         if (*fileList[i] == '/') {
00279             strcpy(buf, fileList[i]);
00280             chptr = buf + strlen(buf) - 1;
00281             while (*chptr != '/') chptr--;
00282             if (chptr == buf)
00283                 buf[1] = '\0';
00284             else
00285                 *chptr-- = '\0';
00286         } else {
00287             /* this should only happen for source packages (gulp) */
00288             strcpy(buf,  sourceDir);
00289         }
00290 
00291         if (strcmp(lastDir, buf)) {
00292             strcpy(dirName, buf);
00293             chptr = dirName + strlen(dirName) - 1;
00294             while (stat(dirName, &sb)) {
00295                 if (errno != ENOENT) {
00296                     rpmError(RPMERR_STAT, _("failed to stat %s: %s\n"), buf,
00297                                 strerror(errno));
00298                     sourceDir = _free(sourceDir);
00299                     usages = _free(usages);
00300                     return 1;
00301                 }
00302 
00303                 /* cut off last directory part, because it was not found. */
00304                 while (*chptr != '/') chptr--;
00305 
00306                 if (chptr == dirName)
00307                     dirName[1] = '\0';
00308                 else
00309                     *chptr-- = '\0';
00310             }
00311 
00312             if (lastDev != sb.st_dev) {
00313                 for (j = 0; j < numFilesystems; j++)
00314                     if (filesystems && filesystems[j].dev == sb.st_dev)
00315                         /*@innerbreak@*/ break;
00316 
00317                 if (j == numFilesystems) {
00318                     rpmError(RPMERR_BADDEV, 
00319                                 _("file %s is on an unknown device\n"), buf);
00320                     sourceDir = _free(sourceDir);
00321                     usages = _free(usages);
00322                     return 1;
00323                 }
00324 
00325                 lastfs = j;
00326                 lastDev = sb.st_dev;
00327             }
00328         }
00329 
00330         strcpy(lastDir, buf);
00331         usages[lastfs] += fssizes[i];
00332     }
00333 
00334     sourceDir = _free(sourceDir);
00335 
00336     if (usagesPtr)
00337         *usagesPtr = usages;
00338     else
00339         usages = _free(usages);
00340 
00341     return 0;
00342 }
00343 /*@=usereleased =onlytrans@*/

Generated on Wed Mar 13 15:34:47 2002 for rpm by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002