parse.c 18 KB

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