lib/manifest.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include <rpmio_internal.h>
00008 #include <rpmlib.h>
00009 #include <rpmmacro.h>
00010 
00011 #include "stringbuf.h"
00012 #include "manifest.h"
00013 #include "misc.h"
00014 #include "debug.h"
00015 
00016 /*@access StringBuf @*/
00017 
00018 /*@-boundswrite@*/
00019 char * rpmPermsString(int mode)
00020 {
00021     char *perms = xstrdup("----------");
00022    
00023     if (S_ISREG(mode)) 
00024         perms[0] = '-';
00025     else if (S_ISDIR(mode)) 
00026         perms[0] = 'd';
00027     else if (S_ISLNK(mode))
00028         perms[0] = 'l';
00029     else if (S_ISFIFO(mode)) 
00030         perms[0] = 'p';
00031     /*@-unrecog@*/
00032     else if (S_ISSOCK(mode)) 
00033         perms[0] = 's';
00034     /*@=unrecog@*/
00035     else if (S_ISCHR(mode))
00036         perms[0] = 'c';
00037     else if (S_ISBLK(mode))
00038         perms[0] = 'b';
00039     else
00040         perms[0] = '?';
00041 
00042     if (mode & S_IRUSR) perms[1] = 'r';
00043     if (mode & S_IWUSR) perms[2] = 'w';
00044     if (mode & S_IXUSR) perms[3] = 'x';
00045  
00046     if (mode & S_IRGRP) perms[4] = 'r';
00047     if (mode & S_IWGRP) perms[5] = 'w';
00048     if (mode & S_IXGRP) perms[6] = 'x';
00049 
00050     if (mode & S_IROTH) perms[7] = 'r';
00051     if (mode & S_IWOTH) perms[8] = 'w';
00052     if (mode & S_IXOTH) perms[9] = 'x';
00053 
00054     if (mode & S_ISUID)
00055         perms[3] = ((mode & S_IXUSR) ? 's' : 'S'); 
00056 
00057     if (mode & S_ISGID)
00058         perms[6] = ((mode & S_IXGRP) ? 's' : 'S'); 
00059 
00060     if (mode & S_ISVTX)
00061         perms[9] = ((mode & S_IXOTH) ? 't' : 'T');
00062 
00063     return perms;
00064 }
00065 /*@=boundswrite@*/
00066 
00068 /*@-boundsread@*/
00069 rpmRC rpmReadPackageManifest(FD_t fd, int * argcPtr, const char *** argvPtr)
00070 {
00071     StringBuf sb = newStringBuf();
00072     char * s = NULL;
00073     char * se;
00074     int ac = 0;
00075     const char ** av = NULL;
00076     int argc = (argcPtr ? *argcPtr : 0);
00077     const char ** argv = (argvPtr ? *argvPtr : NULL);
00078 /*@+voidabstract@*/
00079     FILE * f = (FILE *) fdGetFp(fd);
00080 /*@=voidabstract@*/
00081     rpmRC rpmrc = RPMRC_OK;
00082     int i, j, next, npre;
00083 
00084 /*@-boundswrite@*/
00085     if (f != NULL)
00086     while (1) {
00087         char line[BUFSIZ];
00088 
00089         /* Read next line. */
00090         s = fgets(line, sizeof(line) - 1, f);
00091         if (s == NULL) {
00092             /* XXX Ferror check needed */
00093             break;
00094         }
00095 
00096         /* Skip comments. */
00097         if ((se = strchr(s, '#')) != NULL) *se = '\0';
00098 
00099         /* Trim white space. */
00100         se = s + strlen(s);
00101         while (se > s && (se[-1] == '\n' || se[-1] == '\r'))
00102             *(--se) = '\0';
00103         while (*s && strchr(" \f\n\r\t\v", *s) != NULL)
00104             s++;
00105         if (*s == '\0') continue;
00106 
00107         /* Insure that file contains only ASCII */
00108         if (*s < 32) {
00109             rpmrc = RPMRC_NOTFOUND;
00110             goto exit;
00111         }
00112 
00113         /* Concatenate next line in buffer. */
00114         *se++ = ' ';
00115         *se = '\0';
00116         appendStringBuf(sb, s);
00117     }
00118 
00119     /*@-branchstate@*/
00120     if (s == NULL)              /* XXX always true */
00121         s = getStringBuf(sb);
00122     /*@=branchstate@*/
00123 
00124     if (!(s && *s)) {
00125         rpmrc = RPMRC_NOTFOUND;
00126         goto exit;
00127     }
00128 
00129     /* Glob manifest items. */
00130     rpmrc = rpmGlob(s, &ac, &av);
00131     if (rpmrc != RPMRC_OK) goto exit;
00132 
00133     rpmMessage(RPMMESS_DEBUG, _("adding %d args from manifest.\n"), ac);
00134 
00135     /* Count non-NULL args, keeping track of 1st arg after last NULL. */
00136     npre = 0;
00137     next = 0;
00138     if (argv != NULL)
00139     for (i = 0; i < argc; i++) {
00140         if (argv[i] != NULL)
00141             npre++;
00142         else if (i >= next)
00143             next = i + 1;
00144     }
00145 
00146     /* Copy old arg list, inserting manifest before argv[next]. */
00147     if (argv != NULL) {
00148         int nac = npre + ac;
00149         const char ** nav = xcalloc((nac + 1), sizeof(*nav));
00150 
00151         for (i = 0, j = 0; i < next; i++) {
00152             if (argv[i] != NULL)
00153                 nav[j++] = argv[i];
00154         }
00155 
00156         if (ac)
00157             memcpy(nav + j, av, ac * sizeof(*nav));
00158         if ((argc - next) > 0)
00159             memcpy(nav + j + ac, argv + next, (argc - next) * sizeof(*nav));
00160         nav[nac] = NULL;
00161 
00162         if (argvPtr)
00163             *argvPtr = argv = _free(argv);
00164         av = _free(av);
00165         av = nav;
00166         ac = nac;
00167     }
00168 
00169     /* Save new argc/argv list. */
00170     if (argvPtr) {
00171         *argvPtr = _free(*argvPtr);
00172         *argvPtr = av;
00173     }
00174     if (argcPtr)
00175         *argcPtr = ac;
00176 /*@=boundswrite@*/
00177 
00178 exit:
00179     /*@-branchstate@*/
00180     if (argvPtr == NULL || (rpmrc != RPMRC_OK && av)) {
00181         if (av)
00182 /*@-boundswrite@*/
00183         for (i = 0; i < ac; i++)
00184             /*@-unqualifiedtrans@*/av[i] = _free(av[i]); /*@=unqualifiedtrans@*/
00185 /*@=boundswrite@*/
00186         /*@-dependenttrans@*/ av = _free(av); /*@=dependenttrans@*/
00187     }
00188     /*@=branchstate@*/
00189     sb = freeStringBuf(sb);
00190     /*@-nullstate@*/ /* FIX: *argvPtr may be NULL. */
00191     return rpmrc;
00192     /*@=nullstate@*/
00193 }
00194 /*@=boundsread@*/

Generated on Fri Oct 12 08:44:53 2007 for rpm by  doxygen 1.5.2