parse.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 = 0;
  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;
  189. nick->nick && (strncasecmp(nick->nick, fieldstart, fieldlen) ||
  190. nick->nick[fieldlen] != '\0'); nick++) ;
  191. if (nick->nick) {
  192. fieldstart= nick->canon;
  193. fieldlen= strlen(fieldstart);
  194. }
  195. for (fip= fieldinfos, ip= fieldencountered;
  196. fip->name && strncasecmp(fieldstart,fip->name, fieldlen);
  197. fip++, ip++);
  198. if (fip->name) {
  199. value = m_realloc(value, valuelen + 1);
  200. memcpy(value,valuestart,valuelen);
  201. *(value + valuelen) = '\0';
  202. if (*ip++)
  203. parse_error(filename, lno, &newpig,
  204. _("duplicate value for `%s' field"), fip->name);
  205. fip->rcall(&newpig,newpifp,flags,filename,lno,warnto,warncount,value,fip);
  206. } else {
  207. if (fieldlen<2)
  208. parse_error(filename, lno, &newpig,
  209. _("user-defined field name `%.*s' too short"),
  210. fieldlen, fieldstart);
  211. larpp= &newpifp->arbs;
  212. while ((arp= *larpp) != NULL) {
  213. if (!strncasecmp(arp->name,fieldstart,fieldlen))
  214. parse_error(filename, lno, &newpig,
  215. _("duplicate value for user-defined field `%.*s'"),
  216. fieldlen, fieldstart);
  217. larpp= &arp->next;
  218. }
  219. arp= nfmalloc(sizeof(struct arbitraryfield));
  220. arp->name= nfstrnsave(fieldstart,fieldlen);
  221. arp->value= nfstrnsave(valuestart,valuelen);
  222. arp->next= NULL;
  223. *larpp= arp;
  224. }
  225. if (EOF_mmap(dataptr, endptr) || c == '\n' || c == MSDOS_EOF_CHAR) break;
  226. } /* loop per field */
  227. if (pdone && donep)
  228. parse_error(filename, lno, &newpig,
  229. _("several package info entries found, only one allowed"));
  230. parse_must_have_field(filename, lno, &newpig, newpig.name, "package name");
  231. if ((flags & pdb_recordavailable) || newpig.status != stat_notinstalled) {
  232. parse_ensure_have_field(filename, lno, warnto, warncount, &newpig,
  233. &newpifp->description, "description");
  234. parse_ensure_have_field(filename, lno, warnto, warncount, &newpig,
  235. &newpifp->maintainer, "maintainer");
  236. if (newpig.status != stat_halfinstalled)
  237. parse_must_have_field(filename, lno, &newpig,
  238. newpifp->version.version, "version");
  239. }
  240. if (flags & pdb_recordavailable)
  241. parse_ensure_have_field(filename, lno, warnto, warncount, &newpig,
  242. &newpifp->architecture, "architecture");
  243. /* Check the Config-Version information:
  244. * If there is a Config-Version it is definitely to be used, but
  245. * there shouldn't be one if the package is `installed' (in which case
  246. * the Version and/or Revision will be copied) or if the package is
  247. * `not-installed' (in which case there is no Config-Version).
  248. */
  249. if (!(flags & pdb_recordavailable)) {
  250. if (newpig.configversion.version) {
  251. if (newpig.status == stat_installed || newpig.status == stat_notinstalled)
  252. parse_error(filename, lno, &newpig,
  253. _("Configured-Version for package with inappropriate Status"));
  254. } else {
  255. if (newpig.status == stat_installed) newpig.configversion= newpifp->version;
  256. }
  257. }
  258. if (newpig.trigaw.head &&
  259. (newpig.status <= stat_configfiles ||
  260. newpig.status >= stat_triggerspending))
  261. parse_error(filename, lno, &newpig,
  262. _("package has status %s but triggers are awaited"),
  263. statusinfos[newpig.status].name);
  264. else if (newpig.status == stat_triggersawaited && !newpig.trigaw.head)
  265. parse_error(filename, lno, &newpig,
  266. _("package has status triggers-awaited but no triggers "
  267. "awaited"));
  268. if (!(newpig.status == stat_triggerspending ||
  269. newpig.status == stat_triggersawaited) &&
  270. newpig.trigpend_head)
  271. parse_error(filename, lno, &newpig,
  272. _("package has status %s but triggers are pending"),
  273. statusinfos[newpig.status].name);
  274. else if (newpig.status == stat_triggerspending && !newpig.trigpend_head)
  275. parse_error(filename, lno, &newpig,
  276. _("package has status triggers-pending but no triggers "
  277. "pending"));
  278. /* FIXME: There was a bug that could make a not-installed package have
  279. * conffiles, so we check for them here and remove them (rather than
  280. * calling it an error, which will do at some point).
  281. */
  282. if (!(flags & pdb_recordavailable) &&
  283. newpig.status == stat_notinstalled &&
  284. newpifp->conffiles) {
  285. parse_warn(filename, lno, warnto, warncount, &newpig,
  286. _("Package which in state not-installed has conffiles, "
  287. "forgetting them"));
  288. newpifp->conffiles= NULL;
  289. }
  290. pigp= findpackage(newpig.name);
  291. pifp= (flags & pdb_recordavailable) ? &pigp->available : &pigp->installed;
  292. if (!pifp->valid) blankpackageperfile(pifp);
  293. /* Copy the priority and section across, but don't overwrite existing
  294. * values if the pdb_weakclassification flag is set.
  295. */
  296. if (newpig.section && *newpig.section &&
  297. !((flags & pdb_weakclassification) && pigp->section && *pigp->section))
  298. pigp->section= newpig.section;
  299. if (newpig.priority != pri_unknown &&
  300. !((flags & pdb_weakclassification) && pigp->priority != pri_unknown)) {
  301. pigp->priority= newpig.priority;
  302. if (newpig.priority == pri_other) pigp->otherpriority= newpig.otherpriority;
  303. }
  304. /* Sort out the dependency mess. */
  305. copy_dependency_links(pigp,&pifp->depends,newpifp->depends,
  306. (flags & pdb_recordavailable) ? 1 : 0);
  307. /* Leave the `depended' pointer alone, we've just gone to such
  308. * trouble to get it right :-). The `depends' pointer in
  309. * pifp was indeed also updated by copy_dependency_links,
  310. * but since the value was that from newpifp anyway there's
  311. * no need to copy it back.
  312. */
  313. newpifp->depended= pifp->depended;
  314. /* Copy across data */
  315. memcpy(pifp,newpifp,sizeof(struct pkginfoperfile));
  316. if (!(flags & pdb_recordavailable)) {
  317. pigp->want= newpig.want;
  318. pigp->eflag= newpig.eflag;
  319. pigp->status= newpig.status;
  320. pigp->configversion= newpig.configversion;
  321. pigp->files= NULL;
  322. pigp->trigpend_head = newpig.trigpend_head;
  323. pigp->trigaw = newpig.trigaw;
  324. for (ta = pigp->trigaw.head; ta; ta = ta->sameaw.next) {
  325. assert(ta->aw == &newpig);
  326. ta->aw = pigp;
  327. /* ->othertrigaw_head is updated by trig_note_aw in *(findpackage())
  328. * rather than in newpig */
  329. }
  330. } else if (!(flags & pdb_ignorefiles)) {
  331. pigp->files= newpig.files;
  332. }
  333. if (donep) *donep= pigp;
  334. pdone++;
  335. if (EOF_mmap(dataptr, endptr)) break;
  336. if (c == '\n') lno++;
  337. }
  338. if (data != NULL) {
  339. #ifdef HAVE_MMAP
  340. munmap(data, stat.st_size);
  341. #else
  342. free(data);
  343. #endif
  344. }
  345. free(value);
  346. pop_cleanup(ehflag_normaltidy);
  347. if (close(fd)) ohshite(_("failed to close after read: `%.255s'"),filename);
  348. if (donep && !pdone) ohshit(_("no package information in `%.255s'"),filename);
  349. return pdone;
  350. }
  351. void copy_dependency_links(struct pkginfo *pkg,
  352. struct dependency **updateme,
  353. struct dependency *newdepends,
  354. int available) {
  355. /* This routine is used to update the `reverse' dependency pointers
  356. * when new `forwards' information has been constructed. It first
  357. * removes all the links based on the old information. The old
  358. * information starts in *updateme; after much brou-ha-ha
  359. * the reverse structures are created and *updateme is set
  360. * to the value from newdepends.
  361. *
  362. * Parameters are:
  363. * pkg - the package we're doing this for. This is used to
  364. * construct correct uplinks.
  365. * updateme - the forwards dependency pointer that we are to
  366. * update. This starts out containing the old forwards
  367. * info, which we use to unthread the old reverse
  368. * links. After we're done it is updated.
  369. * newdepends - the value that we ultimately want to have in
  370. * updateme.
  371. * It is likely that the backward pointer for the package in
  372. * question (`depended') will be updated by this routine,
  373. * but this will happen by the routine traversing the dependency
  374. * data structures. It doesn't need to be told where to update
  375. * that; I just mention it as something that one should be
  376. * cautious about.
  377. */
  378. struct dependency *dyp;
  379. struct deppossi *dop;
  380. struct pkginfoperfile *addtopifp;
  381. /* Delete `backward' (`depended') links from other packages to
  382. * dependencies listed in old version of this one. We do this by
  383. * going through all the dependencies in the old version of this
  384. * one and following them down to find which deppossi nodes to
  385. * remove.
  386. */
  387. for (dyp= *updateme; dyp; dyp= dyp->next) {
  388. for (dop= dyp->list; dop; dop= dop->next) {
  389. if (dop->backrev)
  390. dop->backrev->nextrev= dop->nextrev;
  391. else
  392. if (available)
  393. dop->ed->available.depended= dop->nextrev;
  394. else
  395. dop->ed->installed.depended= dop->nextrev;
  396. if (dop->nextrev)
  397. dop->nextrev->backrev= dop->backrev;
  398. }
  399. }
  400. /* Now fill in new `ed' links from other packages to dependencies listed
  401. * in new version of this one, and set our uplinks correctly.
  402. */
  403. for (dyp= newdepends; dyp; dyp= dyp->next) {
  404. dyp->up= pkg;
  405. for (dop= dyp->list; dop; dop= dop->next) {
  406. addtopifp= available ? &dop->ed->available : &dop->ed->installed;
  407. if (!addtopifp->valid) blankpackageperfile(addtopifp);
  408. dop->nextrev= addtopifp->depended;
  409. dop->backrev= NULL;
  410. if (addtopifp->depended)
  411. addtopifp->depended->backrev= dop;
  412. addtopifp->depended= dop;
  413. }
  414. }
  415. /* Finally, we fill in the new value. */
  416. *updateme= newdepends;
  417. }