parse.c 18 KB

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