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