parse.c 18 KB

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