rpm  4.5
manifest.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 
7 #include <rpmio_internal.h>
8 #include <rpmlib.h>
9 #include <rpmmacro.h>
10 
11 #include "stringbuf.h"
12 #include "manifest.h"
13 #include "misc.h"
14 #include "debug.h"
15 
16 /*@access StringBuf @*/
17 
18 /*@-boundswrite@*/
19 char * rpmPermsString(int mode)
20 {
21  char *perms = xstrdup("----------");
22 
23  if (S_ISREG(mode))
24  perms[0] = '-';
25  else if (S_ISDIR(mode))
26  perms[0] = 'd';
27  else if (S_ISLNK(mode))
28  perms[0] = 'l';
29  else if (S_ISFIFO(mode))
30  perms[0] = 'p';
31  /*@-unrecog@*/
32  else if (S_ISSOCK(mode))
33  perms[0] = 's';
34  /*@=unrecog@*/
35  else if (S_ISCHR(mode))
36  perms[0] = 'c';
37  else if (S_ISBLK(mode))
38  perms[0] = 'b';
39  else
40  perms[0] = '?';
41 
42  if (mode & S_IRUSR) perms[1] = 'r';
43  if (mode & S_IWUSR) perms[2] = 'w';
44  if (mode & S_IXUSR) perms[3] = 'x';
45 
46  if (mode & S_IRGRP) perms[4] = 'r';
47  if (mode & S_IWGRP) perms[5] = 'w';
48  if (mode & S_IXGRP) perms[6] = 'x';
49 
50  if (mode & S_IROTH) perms[7] = 'r';
51  if (mode & S_IWOTH) perms[8] = 'w';
52  if (mode & S_IXOTH) perms[9] = 'x';
53 
54  if (mode & S_ISUID)
55  perms[3] = ((mode & S_IXUSR) ? 's' : 'S');
56 
57  if (mode & S_ISGID)
58  perms[6] = ((mode & S_IXGRP) ? 's' : 'S');
59 
60  if (mode & S_ISVTX)
61  perms[9] = ((mode & S_IXOTH) ? 't' : 'T');
62 
63  return perms;
64 }
65 /*@=boundswrite@*/
66 
68 /*@-boundsread@*/
69 rpmRC rpmReadPackageManifest(FD_t fd, int * argcPtr, const char *** argvPtr)
70 {
71  StringBuf sb = newStringBuf();
72  char * s = NULL;
73  char * se;
74  int ac = 0;
75  const char ** av = NULL;
76  int argc = (argcPtr ? *argcPtr : 0);
77  const char ** argv = (argvPtr ? *argvPtr : NULL);
78  FD_t xfd;
79  FILE * f;
80  rpmRC rpmrc = RPMRC_OK;
81  int i, j, next, npre;
82 
83 /*@-boundswrite@*/
84 /*@-branchstate@*/
85  if (fdGetFp(fd) == NULL)
86  xfd = Fdopen(fd, "r.fpio");
87  else
88  xfd = fd;
89 /*@=branchstate@*/
90 
91 /*@+voidabstract@*/
92  if ((f = (FILE *) fdGetFp(xfd)) == NULL) {
93 /*@=voidabstract@*/
94  rpmrc = RPMRC_NOTFOUND;
95  goto exit;
96  }
97 
98  while (1) {
99  char line[BUFSIZ];
100 
101  /* Read next line. */
102  s = fgets(line, sizeof(line) - 1, f);
103  if (s == NULL) {
104  /* XXX Ferror check needed */
105  break;
106  }
107 
108  /* XXX stop processing manifest if HTML is found. */
109 #define DOCTYPE_HTML_PUBLIC "<!DOCTYPE HTML PUBLIC"
110  if (!strncmp(line, DOCTYPE_HTML_PUBLIC, sizeof(DOCTYPE_HTML_PUBLIC)-1)) {
111  rpmrc = RPMRC_NOTFOUND;
112  goto exit;
113  }
114 
115  /* Skip comments. */
116  if ((se = strchr(s, '#')) != NULL) *se = '\0';
117 
118  /* Trim white space. */
119  se = s + strlen(s);
120  while (se > s && (se[-1] == '\n' || se[-1] == '\r'))
121  *(--se) = '\0';
122  while (*s && strchr(" \f\n\r\t\v", *s) != NULL)
123  s++;
124  if (*s == '\0') continue;
125 
126  /* Insure that file contains only ASCII */
127  if (*s < 32) {
128  rpmrc = RPMRC_NOTFOUND;
129  goto exit;
130  }
131 
132  /* Concatenate next line in buffer. */
133  *se++ = ' ';
134  *se = '\0';
135  appendStringBuf(sb, s);
136  }
137 
138  /*@-branchstate@*/
139  if (s == NULL) /* XXX always true */
140  s = getStringBuf(sb);
141  /*@=branchstate@*/
142 
143  if (!(s && *s)) {
144  rpmrc = RPMRC_NOTFOUND;
145  goto exit;
146  }
147 
148  /* Glob manifest items. */
149  rpmrc = rpmGlob(s, &ac, &av);
150  if (rpmrc != RPMRC_OK) goto exit;
151 
152  rpmMessage(RPMMESS_DEBUG, D_("adding %d args from manifest.\n"), ac);
153 
154  /* Count non-NULL args, keeping track of 1st arg after last NULL. */
155  npre = 0;
156  next = 0;
157  if (argv != NULL)
158  for (i = 0; i < argc; i++) {
159  if (argv[i] != NULL)
160  npre++;
161  else if (i >= next)
162  next = i + 1;
163  }
164 
165  /* Copy old arg list, inserting manifest before argv[next]. */
166  if (argv != NULL) {
167  int nac = npre + ac;
168  const char ** nav = xcalloc((nac + 1), sizeof(*nav));
169 
170  for (i = 0, j = 0; i < next; i++) {
171  if (argv[i] != NULL)
172  nav[j++] = argv[i];
173  }
174 
175  if (ac)
176  memcpy(nav + j, av, ac * sizeof(*nav));
177  if ((argc - next) > 0)
178  memcpy(nav + j + ac, argv + next, (argc - next) * sizeof(*nav));
179  nav[nac] = NULL;
180 
181  if (argvPtr)
182  *argvPtr = argv = _free(argv);
183  av = _free(av);
184  av = nav;
185  ac = nac;
186  }
187 
188  /* Save new argc/argv list. */
189  if (argvPtr) {
190  *argvPtr = _free(*argvPtr);
191  *argvPtr = av;
192  }
193  if (argcPtr)
194  *argcPtr = ac;
195 /*@=boundswrite@*/
196 
197 exit:
198  /*@-branchstate@*/
199  if (argvPtr == NULL || (rpmrc != RPMRC_OK && av)) {
200  if (av)
201 /*@-boundswrite@*/
202  for (i = 0; i < ac; i++)
203  /*@-unqualifiedtrans@*/av[i] = _free(av[i]); /*@=unqualifiedtrans@*/
204 /*@=boundswrite@*/
205  /*@-dependenttrans@*/ av = _free(av); /*@=dependenttrans@*/
206  }
207  /*@=branchstate@*/
208  sb = freeStringBuf(sb);
209  /*@-nullstate@*/ /* FIX: *argvPtr may be NULL. */
210  return rpmrc;
211  /*@=nullstate@*/
212 }
213 /*@=boundsread@*/