parse.c 18 KB

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