parse.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * parse.c - database file parsing, main package/field loop
  4. *
  5. * Copyright (C) 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <stdarg.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <dpkg.h>
  31. #include <dpkg-db.h>
  32. #include "parsedump.h"
  33. #ifdef HAVE_MMAP
  34. #include <sys/mman.h>
  35. #endif
  36. const struct fieldinfo fieldinfos[]= {
  37. /* NB: capitalisation of these strings is important. */
  38. { "Package", f_name, w_name },
  39. { "Essential", f_boolean, w_booleandefno, PKGIFPOFF(essential) },
  40. { "Status", f_status, w_status },
  41. { "Priority", f_priority, w_priority },
  42. { "Section", f_section, w_section },
  43. { "Installed-Size", f_charfield, w_charfield, PKGIFPOFF(installedsize) },
  44. { "Origin", f_charfield, w_charfield, PKGIFPOFF(origin) },
  45. { "Maintainer", f_charfield, w_charfield, PKGIFPOFF(maintainer) },
  46. { "Bugs", f_charfield, w_charfield, PKGIFPOFF(bugs) },
  47. { "Architecture", f_charfield, w_charfield, PKGIFPOFF(architecture) },
  48. { "Source", f_charfield, w_charfield, PKGIFPOFF(source) },
  49. { "Version", f_version, w_version, PKGIFPOFF(version) },
  50. { "Revision", f_revision, w_null },
  51. { "Config-Version", f_configversion, w_configversion },
  52. { "Replaces", f_dependency, w_dependency, dep_replaces },
  53. { "Provides", f_dependency, w_dependency, dep_provides },
  54. { "Depends", f_dependency, w_dependency, dep_depends },
  55. { "Pre-Depends", f_dependency, w_dependency, dep_predepends },
  56. { "Recommends", f_dependency, w_dependency, dep_recommends },
  57. { "Suggests", f_dependency, w_dependency, dep_suggests },
  58. { "Conflicts", f_dependency, w_dependency, dep_conflicts },
  59. { "Enhances", f_dependency, w_dependency, dep_enhances },
  60. { "Conffiles", f_conffiles, w_conffiles },
  61. { "Filename", f_filecharf, w_filecharf, FILEFOFF(name) },
  62. { "Size", f_filecharf, w_filecharf, FILEFOFF(size) },
  63. { "MD5sum", f_filecharf, w_filecharf, FILEFOFF(md5sum) },
  64. { "MSDOS-Filename", f_filecharf, w_filecharf, FILEFOFF(msdosname) },
  65. { "Description", f_charfield, w_charfield, PKGIFPOFF(description) },
  66. /* Note that aliases are added to the nicknames table in parsehelp.c. */
  67. { NULL /* sentinel - tells code that list is ended */ }
  68. };
  69. #define NFIELDS (sizeof(fieldinfos)/sizeof(struct fieldinfo))
  70. const int nfields= NFIELDS;
  71. static void cu_parsedb(int argc, void **argv) { close(*(int *)argv); }
  72. int parsedb(const char *filename, enum parsedbflags flags,
  73. struct pkginfo **donep, FILE *warnto, int *warncount) {
  74. /* warnto, warncount and donep may be null.
  75. * If donep is not null only one package's information is expected.
  76. */
  77. int fd;
  78. struct pkginfo newpig, *pigp;
  79. struct pkginfoperfile *newpifp, *pifp;
  80. struct arbitraryfield *arp, **larpp;
  81. int lno;
  82. int pdone;
  83. int fieldencountered[NFIELDS];
  84. const struct fieldinfo *fip;
  85. const struct nickname *nick;
  86. char *data, *dataptr, *endptr;
  87. const char *fieldstart, *valuestart;
  88. char *value= NULL;
  89. int fieldlen= 0, valuelen= 0;
  90. int *ip, c;
  91. struct stat stat;
  92. if (warncount) *warncount= 0;
  93. newpifp= (flags & pdb_recordavailable) ? &newpig.available : &newpig.installed;
  94. fd= open(filename, O_RDONLY);
  95. if (fd == -1) ohshite(_("failed to open package info file `%.255s' for reading"),filename);
  96. push_cleanup(cu_parsedb,~0, NULL,0, 1,&fd);
  97. if (fstat(fd, &stat) == -1)
  98. ohshite(_("can't stat package info file `%.255s'"),filename);
  99. if (stat.st_size > 0) {
  100. #ifdef HAVE_MMAP
  101. if ((dataptr= (char *)mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
  102. ohshite(_("can't mmap package info file `%.255s'"),filename);
  103. #else
  104. if ((dataptr= malloc(stat.st_size)) == NULL)
  105. ohshite(_("failed to malloc for info file `%.255s'"),filename);
  106. fd_buf_copy(fd, dataptr, stat.st_size, _("copy info file `%.255s'"),filename);
  107. #endif
  108. data= dataptr;
  109. endptr= dataptr + stat.st_size;
  110. } else {
  111. data= dataptr= endptr= NULL;
  112. }
  113. lno= 1;
  114. pdone= 0;
  115. #define EOF_mmap(dataptr, endptr) (dataptr >= endptr)
  116. #define getc_mmap(dataptr) *dataptr++;
  117. #define ungetc_mmap(c, dataptr, data) dataptr--;
  118. for (;;) { /* loop per package */
  119. memset(fieldencountered, 0, sizeof(fieldencountered));
  120. blankpackage(&newpig);
  121. blankpackageperfile(newpifp);
  122. /* Skip adjacent new lines */
  123. while(!EOF_mmap(dataptr, endptr)) {
  124. c= getc_mmap(dataptr); if (c!='\n' && c!=MSDOS_EOF_CHAR ) break;
  125. lno++;
  126. }
  127. if (EOF_mmap(dataptr, endptr)) break;
  128. for (;;) { /* loop per field */
  129. fieldstart= dataptr - 1;
  130. while (!EOF_mmap(dataptr, endptr) && !isspace(c) && c!=':' && c!=MSDOS_EOF_CHAR)
  131. c= getc_mmap(dataptr);
  132. fieldlen= dataptr - fieldstart - 1;
  133. while (!EOF_mmap(dataptr, endptr) && c != '\n' && isspace(c)) c= getc_mmap(dataptr);
  134. if (EOF_mmap(dataptr, endptr))
  135. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  136. _("EOF after field name `%.*s'"),fieldlen,fieldstart);
  137. if (c == '\n')
  138. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  139. _("newline in field name `%.*s'"),fieldlen,fieldstart);
  140. if (c == MSDOS_EOF_CHAR)
  141. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  142. _("MSDOS EOF (^Z) in field name `%.*s'"),fieldlen,fieldstart);
  143. if (c != ':')
  144. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  145. _("field name `%.*s' must be followed by colon"),fieldlen,fieldstart);
  146. /* Skip space after ':' but before value and eol */
  147. while(!EOF_mmap(dataptr, endptr)) {
  148. c= getc_mmap(dataptr);
  149. if (c == '\n' || !isspace(c)) break;
  150. }
  151. if (EOF_mmap(dataptr, endptr))
  152. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  153. _("EOF before value of field `%.*s' (missing final newline)"),
  154. fieldlen,fieldstart);
  155. if (c == MSDOS_EOF_CHAR)
  156. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  157. _("MSDOS EOF char in value of field `%.*s' (missing newline?)"),
  158. fieldlen,fieldstart);
  159. valuestart= dataptr - 1;
  160. for (;;) {
  161. if (c == '\n' || c == MSDOS_EOF_CHAR) {
  162. lno++;
  163. if (EOF_mmap(dataptr, endptr)) break;
  164. c= getc_mmap(dataptr);
  165. /* Found double eol, or start of new field */
  166. if (EOF_mmap(dataptr, endptr) || c == '\n' || !isspace(c)) break;
  167. ungetc_mmap(c,dataptr, data);
  168. c= '\n';
  169. } else if (EOF_mmap(dataptr, endptr)) {
  170. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  171. _("EOF during value of field `%.*s' (missing final newline)"),
  172. fieldlen,fieldstart);
  173. }
  174. c= getc_mmap(dataptr);
  175. }
  176. valuelen= dataptr - valuestart - 1;
  177. /* trim ending space on value */
  178. while (valuelen && isspace(*(valuestart+valuelen-1)))
  179. valuelen--;
  180. for (nick= nicknames; nick->nick && (strncasecmp(nick->nick,fieldstart, fieldlen) || nick->nick[fieldlen] != 0); nick++);
  181. if (nick->nick) {
  182. fieldstart= nick->canon;
  183. fieldlen= strlen(fieldstart);
  184. }
  185. for (fip= fieldinfos, ip= fieldencountered;
  186. fip->name && strncasecmp(fieldstart,fip->name, fieldlen);
  187. fip++, ip++);
  188. if (fip->name) {
  189. value= realloc(value,valuelen+1);
  190. memcpy(value,valuestart,valuelen);
  191. *(value+valuelen)= 0;
  192. if (*ip++)
  193. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  194. _("duplicate value for `%s' field"), fip->name);
  195. fip->rcall(&newpig,newpifp,flags,filename,lno-1,warnto,warncount,value,fip);
  196. } else {
  197. if (fieldlen<2)
  198. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  199. _("user-defined field name `%.*s' too short"), fieldlen,fieldstart);
  200. larpp= &newpifp->arbs;
  201. while ((arp= *larpp) != NULL) {
  202. if (!strncasecmp(arp->name,fieldstart,fieldlen))
  203. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  204. _("duplicate value for user-defined field `%.*s'"), fieldlen,fieldstart);
  205. larpp= &arp->next;
  206. }
  207. arp= nfmalloc(sizeof(struct arbitraryfield));
  208. arp->name= nfstrnsave(fieldstart,fieldlen);
  209. arp->value= nfstrnsave(valuestart,valuelen);
  210. arp->next= NULL;
  211. *larpp= arp;
  212. }
  213. if (EOF_mmap(dataptr, endptr) || c == '\n' || c == MSDOS_EOF_CHAR) break;
  214. } /* loop per field */
  215. if (pdone && donep)
  216. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  217. _("several package info entries found, only one allowed"));
  218. parsemustfield(NULL,filename,lno, warnto,warncount,&newpig,0,
  219. &newpig.name, "package name");
  220. if ((flags & pdb_recordavailable) || newpig.status != stat_notinstalled) {
  221. parsemustfield(NULL,filename,lno, warnto,warncount,&newpig,1,
  222. (const char **)&newpifp->description, "description");
  223. parsemustfield(NULL,filename,lno, warnto,warncount,&newpig,1,
  224. (const char **)&newpifp->maintainer, "maintainer");
  225. if (newpig.status != stat_halfinstalled)
  226. parsemustfield(NULL,filename,lno, warnto,warncount,&newpig,0,
  227. &newpifp->version.version, "version");
  228. }
  229. if (flags & pdb_recordavailable)
  230. parsemustfield(NULL,filename,lno, warnto,warncount,&newpig,1,
  231. (const char **)&newpifp->architecture, "architecture");
  232. /* Check the Config-Version information:
  233. * If there is a Config-Version it is definitely to be used, but
  234. * there shouldn't be one if the package is `installed' (in which case
  235. * the Version and/or Revision will be copied) or if the package is
  236. * `not-installed' (in which case there is no Config-Version).
  237. */
  238. if (!(flags & pdb_recordavailable)) {
  239. if (newpig.configversion.version) {
  240. if (newpig.status == stat_installed || newpig.status == stat_notinstalled)
  241. parseerr(NULL,filename,lno, warnto,warncount,&newpig,0,
  242. _("Configured-Version for package with inappropriate Status"));
  243. } else {
  244. if (newpig.status == stat_installed) newpig.configversion= newpifp->version;
  245. }
  246. }
  247. /* There was a bug that could make a not-installed package have
  248. * conffiles, so we check for them here and remove them (rather than
  249. * calling it an error, which will do at some point -- fixme).
  250. */
  251. if (!(flags & pdb_recordavailable) &&
  252. newpig.status == stat_notinstalled &&
  253. newpifp->conffiles) {
  254. parseerr(NULL,filename,lno, warnto,warncount,&newpig,1,
  255. _("Package which in state not-installed has conffiles, forgetting them"));
  256. newpifp->conffiles= NULL;
  257. }
  258. pigp= findpackage(newpig.name);
  259. pifp= (flags & pdb_recordavailable) ? &pigp->available : &pigp->installed;
  260. if (!pifp->valid) blankpackageperfile(pifp);
  261. /* Copy the priority and section across, but don't overwrite existing
  262. * values if the pdb_weakclassification flag is set.
  263. */
  264. if (newpig.section && *newpig.section &&
  265. !((flags & pdb_weakclassification) && pigp->section && *pigp->section))
  266. pigp->section= newpig.section;
  267. if (newpig.priority != pri_unknown &&
  268. !((flags & pdb_weakclassification) && pigp->priority != pri_unknown)) {
  269. pigp->priority= newpig.priority;
  270. if (newpig.priority == pri_other) pigp->otherpriority= newpig.otherpriority;
  271. }
  272. /* Sort out the dependency mess. */
  273. copy_dependency_links(pigp,&pifp->depends,newpifp->depends,
  274. (flags & pdb_recordavailable) ? 1 : 0);
  275. /* Leave the `depended' pointer alone, we've just gone to such
  276. * trouble to get it right :-). The `depends' pointer in
  277. * pifp was indeed also updated by copy_dependency_links,
  278. * but since the value was that from newpifp anyway there's
  279. * no need to copy it back.
  280. */
  281. newpifp->depended= pifp->depended;
  282. /* Copy across data */
  283. memcpy(pifp,newpifp,sizeof(struct pkginfoperfile));
  284. if (!(flags & pdb_recordavailable)) {
  285. pigp->want= newpig.want;
  286. pigp->eflag= newpig.eflag;
  287. pigp->status= newpig.status;
  288. pigp->configversion= newpig.configversion;
  289. pigp->files= NULL;
  290. } else if (!(flags & pdb_ignorefiles)) {
  291. pigp->files= newpig.files;
  292. }
  293. if (donep) *donep= pigp;
  294. pdone++;
  295. if (EOF_mmap(dataptr, endptr)) break;
  296. if (c == '\n') lno++;
  297. }
  298. pop_cleanup(0);
  299. if (data != NULL) {
  300. #ifdef HAVE_MMAP
  301. munmap(data, stat.st_size);
  302. #else
  303. free(data);
  304. #endif
  305. }
  306. free(value);
  307. if (close(fd)) ohshite(_("failed to close after read: `%.255s'"),filename);
  308. if (donep && !pdone) ohshit(_("no package information in `%.255s'"),filename);
  309. return pdone;
  310. }
  311. void copy_dependency_links(struct pkginfo *pkg,
  312. struct dependency **updateme,
  313. struct dependency *newdepends,
  314. int available) {
  315. /* This routine is used to update the `reverse' dependency pointers
  316. * when new `forwards' information has been constructed. It first
  317. * removes all the links based on the old information. The old
  318. * information starts in *updateme; after much brou-ha-ha
  319. * the reverse structures are created and *updateme is set
  320. * to the value from newdepends.
  321. *
  322. * Parameters are:
  323. * pkg - the package we're doing this for. This is used to
  324. * construct correct uplinks.
  325. * updateme - the forwards dependency pointer that we are to
  326. * update. This starts out containing the old forwards
  327. * info, which we use to unthread the old reverse
  328. * links. After we're done it is updated.
  329. * newdepends - the value that we ultimately want to have in
  330. * updateme.
  331. * It is likely that the backward pointer for the package in
  332. * question (`depended') will be updated by this routine,
  333. * but this will happen by the routine traversing the dependency
  334. * data structures. It doesn't need to be told where to update
  335. * that; I just mention it as something that one should be
  336. * cautious about.
  337. */
  338. struct dependency *dyp;
  339. struct deppossi *dop;
  340. struct pkginfoperfile *addtopifp;
  341. /* Delete `backward' (`depended') links from other packages to
  342. * dependencies listed in old version of this one. We do this by
  343. * going through all the dependencies in the old version of this
  344. * one and following them down to find which deppossi nodes to
  345. * remove.
  346. */
  347. for (dyp= *updateme; dyp; dyp= dyp->next) {
  348. for (dop= dyp->list; dop; dop= dop->next) {
  349. if (dop->backrev)
  350. dop->backrev->nextrev= dop->nextrev;
  351. else
  352. if (available)
  353. dop->ed->available.depended= dop->nextrev;
  354. else
  355. dop->ed->installed.depended= dop->nextrev;
  356. if (dop->nextrev)
  357. dop->nextrev->backrev= dop->backrev;
  358. }
  359. }
  360. /* Now fill in new `ed' links from other packages to dependencies listed
  361. * in new version of this one, and set our uplinks correctly.
  362. */
  363. for (dyp= newdepends; dyp; dyp= dyp->next) {
  364. dyp->up= pkg;
  365. for (dop= dyp->list; dop; dop= dop->next) {
  366. addtopifp= available ? &dop->ed->available : &dop->ed->installed;
  367. if (!addtopifp->valid) blankpackageperfile(addtopifp);
  368. dop->nextrev= addtopifp->depended;
  369. dop->backrev= NULL;
  370. if (addtopifp->depended)
  371. addtopifp->depended->backrev= dop;
  372. addtopifp->depended= dop;
  373. }
  374. }
  375. /* Finally, we fill in the new value. */
  376. *updateme= newdepends;
  377. }