parse.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 <iwj10@cus.cam.ac.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 <stdio.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <stdarg.h>
  25. #include <config.h>
  26. #include <dpkg.h>
  27. #include <dpkg-db.h>
  28. #include "parsedump.h"
  29. const struct fieldinfo fieldinfos[]= {
  30. /* NB: capitalisation of these strings is important. */
  31. { "Package", f_name, w_name },
  32. { "Essential", f_boolean, w_booleandefno, PKGIFPOFF(essential) },
  33. { "Status", f_status, w_status },
  34. { "Priority", f_priority, w_priority },
  35. { "Section", f_section, w_section },
  36. { "Installed-Size", f_charfield, w_charfield, PKGIFPOFF(installedsize) },
  37. { "Maintainer", f_charfield, w_charfield, PKGIFPOFF(maintainer) },
  38. { "Architecture", f_charfield, w_charfield, PKGIFPOFF(architecture) },
  39. { "Source", f_charfield, w_charfield, PKGIFPOFF(source) },
  40. { "Version", f_version, w_version },
  41. { "Revision", f_revision, w_null },
  42. { "Config-Version", f_configversion, w_configversion },
  43. { "Replaces", f_dependency, w_dependency, dep_replaces },
  44. { "Provides", f_dependency, w_dependency, dep_provides },
  45. { "Depends", f_dependency, w_dependency, dep_depends },
  46. { "Pre-Depends", f_dependency, w_dependency, dep_predepends },
  47. { "Recommends", f_dependency, w_dependency, dep_recommends },
  48. { "Suggests", f_dependency, w_dependency, dep_suggests },
  49. { "Conflicts", f_dependency, w_dependency, dep_conflicts },
  50. { "Conffiles", f_conffiles, w_conffiles },
  51. { "Filename", f_filecharf, w_filecharf, FILEFOFF(name) },
  52. { "Size", f_filecharf, w_filecharf, FILEFOFF(size) },
  53. { "MD5sum", f_filecharf, w_filecharf, FILEFOFF(md5sum) },
  54. { "MSDOS-Filename", f_filecharf, w_filecharf, FILEFOFF(msdosname) },
  55. { "Description", f_charfield, w_charfield, PKGIFPOFF(description) },
  56. /* Note that aliases are added to the nicknames table in parsehelp.c. */
  57. { 0 /* sentinel - tells code that list is ended */ }
  58. };
  59. #define NFIELDS (sizeof(fieldinfos)/sizeof(struct fieldinfo))
  60. const int nfields= NFIELDS;
  61. static void cu_parsedb(int argc, void **argv) { fclose((FILE*)*argv); }
  62. int parsedb(const char *filename, enum parsedbflags flags,
  63. struct pkginfo **donep, FILE *warnto, int *warncount) {
  64. /* warnto, warncount and donep may be null.
  65. * If donep is not null only one package's information is expected.
  66. */
  67. static char readbuf[16384];
  68. FILE *file;
  69. struct pkginfo newpig, *pigp;
  70. struct pkginfoperfile *newpifp, *pifp;
  71. struct arbitraryfield *arp, **larpp;
  72. struct varbuf field, value;
  73. int lno;
  74. int pdone;
  75. int fieldencountered[NFIELDS];
  76. const struct fieldinfo *fip;
  77. const struct nickname *nick;
  78. const char *fieldname;
  79. int *ip, i, c;
  80. if (warncount) *warncount= 0;
  81. newpifp= (flags & pdb_recordavailable) ? &newpig.available : &newpig.installed;
  82. file= fopen(filename,"r");
  83. if (!file) ohshite(_("failed to open package info file `%.255s' for reading"),filename);
  84. if (!donep) /* Reading many packages, use a nice big buffer. */
  85. if (setvbuf(file,readbuf,_IOFBF,sizeof(readbuf)))
  86. ohshite(_("unable to set buffering on status file"));
  87. push_cleanup(cu_parsedb,~0, 0,0, 1,(void*)file);
  88. varbufinit(&field); varbufinit(&value);
  89. lno= 1;
  90. pdone= 0;
  91. for (;;) { /* loop per package */
  92. i= sizeof(fieldencountered)/sizeof(int); ip= fieldencountered;
  93. while (i--) *ip++= 0;
  94. blankpackage(&newpig);
  95. blankpackageperfile(newpifp);
  96. for (;;) {
  97. c= getc(file); if (c!='\n' && c!=MSDOS_EOF_CHAR) break;
  98. lno++;
  99. }
  100. if (c == EOF) break;
  101. for (;;) { /* loop per field */
  102. varbufreset(&field);
  103. while (c!=EOF && !isspace(c) && c!=':' && c!=MSDOS_EOF_CHAR) {
  104. varbufaddc(&field,c);
  105. c= getc(file);
  106. }
  107. varbufaddc(&field,0);
  108. while (c != EOF && c != '\n' && isspace(c)) c= getc(file);
  109. if (c == EOF)
  110. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  111. _("EOF after field name `%.50s'"),field.buf);
  112. if (c == '\n')
  113. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  114. _("newline in field name `%.50s'"),field.buf);
  115. if (c == MSDOS_EOF_CHAR)
  116. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  117. _("MSDOS EOF (^Z) in field name `%.50s'"), field.buf);
  118. if (c != ':')
  119. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  120. _("field name `%.50s' must be followed by colon"), field.buf);
  121. varbufreset(&value);
  122. for (;;) {
  123. c= getc(file);
  124. if (c == EOF || c == '\n' || !isspace(c)) break;
  125. }
  126. if (c == EOF)
  127. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  128. _("EOF before value of field `%.50s' (missing final newline)"),
  129. field.buf);
  130. if (c == MSDOS_EOF_CHAR)
  131. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  132. _("MSDOS EOF char in value of field `%.50s' (missing newline?)"),
  133. field.buf);
  134. for (;;) {
  135. if (c == '\n' || c == MSDOS_EOF_CHAR) {
  136. lno++;
  137. c= getc(file);
  138. if (c == EOF || c == '\n' || !isspace(c)) break;
  139. ungetc(c,file);
  140. c= '\n';
  141. } else if (c == EOF) {
  142. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  143. _("EOF during value of field `%.50s' (missing final newline)"),
  144. field.buf);
  145. }
  146. varbufaddc(&value,c);
  147. c= getc(file);
  148. }
  149. while (value.used && isspace(value.buf[value.used-1])) value.used--;
  150. varbufaddc(&value,0);
  151. fieldname= field.buf;
  152. for (nick= nicknames; nick->nick && strcasecmp(nick->nick,fieldname); nick++);
  153. if (nick->nick) fieldname= nick->canon;
  154. for (fip= fieldinfos, ip= fieldencountered;
  155. fip->name && strcasecmp(fieldname,fip->name);
  156. fip++, ip++);
  157. if (fip->name) {
  158. if (*ip++)
  159. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  160. _("duplicate value for `%s' field"), fip->name);
  161. fip->rcall(&newpig,newpifp,flags,filename,lno-1,warnto,warncount,value.buf,fip);
  162. } else {
  163. if (strlen(fieldname)<2)
  164. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  165. _("user-defined field name `%s' too short"), fieldname);
  166. larpp= &newpifp->arbs;
  167. while ((arp= *larpp) != 0) {
  168. if (!strcasecmp(arp->name,fieldname))
  169. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  170. _("duplicate value for user-defined field `%.50s'"), fieldname);
  171. larpp= &arp->next;
  172. }
  173. arp= nfmalloc(sizeof(struct arbitraryfield));
  174. arp->name= nfstrsave(fieldname);
  175. arp->value= nfstrsave(value.buf);
  176. arp->next= 0;
  177. *larpp= arp;
  178. }
  179. if (c == EOF || c == '\n' || c == MSDOS_EOF_CHAR) break;
  180. } /* loop per field */
  181. if (pdone && donep)
  182. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  183. _("several package info entries found, only one allowed"));
  184. parsemustfield(file,filename,lno, warnto,warncount,&newpig,0,
  185. &newpig.name, "package name");
  186. if ((flags & pdb_recordavailable) || newpig.status != stat_notinstalled) {
  187. parsemustfield(file,filename,lno, warnto,warncount,&newpig,1,
  188. &newpifp->description, "description");
  189. parsemustfield(file,filename,lno, warnto,warncount,&newpig,1,
  190. &newpifp->maintainer, "maintainer");
  191. if (newpig.status != stat_halfinstalled)
  192. parsemustfield(file,filename,lno, warnto,warncount,&newpig,0,
  193. (char **)&newpifp->version.version, "version");
  194. }
  195. if (flags & pdb_recordavailable)
  196. parsemustfield(file,filename,lno, warnto,warncount,&newpig,1,
  197. &newpifp->architecture, "architecture");
  198. else if (newpifp->architecture && *newpifp->architecture)
  199. newpifp->architecture= 0;
  200. /* Check the Config-Version information:
  201. * If there is a Config-Version it is definitely to be used, but
  202. * there shouldn't be one if the package is `installed' (in which case
  203. * the Version and/or Revision will be copied) or if the package is
  204. * `not-installed' (in which case there is no Config-Version).
  205. */
  206. if (!(flags & pdb_recordavailable)) {
  207. if (newpig.configversion.version) {
  208. if (newpig.status == stat_installed || newpig.status == stat_notinstalled)
  209. parseerr(file,filename,lno, warnto,warncount,&newpig,0,
  210. _("Configured-Version for package with inappropriate Status"));
  211. } else {
  212. if (newpig.status == stat_installed) newpig.configversion= newpifp->version;
  213. }
  214. }
  215. /* There was a bug that could make a not-installed package have
  216. * conffiles, so we check for them here and remove them (rather than
  217. * calling it an error, which will do at some point -- fixme).
  218. */
  219. if (!(flags & pdb_recordavailable) &&
  220. newpig.status == stat_notinstalled &&
  221. newpifp->conffiles) {
  222. parseerr(file,filename,lno, warnto,warncount,&newpig,1,
  223. _("Package which in state not-installed has conffiles, forgetting them"));
  224. newpifp->conffiles= 0;
  225. }
  226. pigp= findpackage(newpig.name);
  227. pifp= (flags & pdb_recordavailable) ? &pigp->available : &pigp->installed;
  228. if (!pifp->valid) blankpackageperfile(pifp);
  229. /* Copy the priority and section across, but don't overwrite existing
  230. * values if the pdb_weakclassification flag is set.
  231. */
  232. if (newpig.section && *newpig.section &&
  233. !((flags & pdb_weakclassification) && pigp->section && *pigp->section))
  234. pigp->section= newpig.section;
  235. if (newpig.priority != pri_unknown &&
  236. !((flags & pdb_weakclassification) && pigp->priority != pri_unknown)) {
  237. pigp->priority= newpig.priority;
  238. if (newpig.priority == pri_other) pigp->otherpriority= newpig.otherpriority;
  239. }
  240. /* Sort out the dependency mess. */
  241. copy_dependency_links(pigp,&pifp->depends,newpifp->depends,
  242. (flags & pdb_recordavailable) ? 1 : 0);
  243. /* Leave the `depended' pointer alone, we've just gone to such
  244. * trouble to get it right :-). The `depends' pointer in
  245. * pifp was indeed also updated by copy_dependency_links,
  246. * but since the value was that from newpifp anyway there's
  247. * no need to copy it back.
  248. */
  249. newpifp->depended= pifp->depended;
  250. /* Copy across data */
  251. memcpy(pifp,newpifp,sizeof(struct pkginfoperfile));
  252. if (!(flags & pdb_recordavailable)) {
  253. pigp->want= newpig.want;
  254. pigp->eflag= newpig.eflag;
  255. pigp->status= newpig.status;
  256. pigp->configversion= newpig.configversion;
  257. pigp->files= 0;
  258. } else if (!(flags & pdb_ignorefiles)) {
  259. pigp->files= newpig.files;
  260. }
  261. if (donep) *donep= pigp;
  262. pdone++;
  263. if (c == EOF) break;
  264. if (c == '\n') lno++;
  265. }
  266. if (ferror(file)) ohshite(_("failed to read from `%.255s'"),filename);
  267. pop_cleanup(0);
  268. if (fclose(file)) ohshite(_("failed to close after read: `%.255s'"),filename);
  269. if (donep && !pdone) ohshit(_("no package information in `%.255s'"),filename);
  270. varbuffree(&field); varbuffree(&value);
  271. return pdone;
  272. }
  273. void copy_dependency_links(struct pkginfo *pkg,
  274. struct dependency **updateme,
  275. struct dependency *newdepends,
  276. int available) {
  277. /* This routine is used to update the `reverse' dependency pointers
  278. * when new `forwards' information has been constructed. It first
  279. * removes all the links based on the old information. The old
  280. * information starts in *updateme; after much brou-ha-ha
  281. * the reverse structures are created and *updateme is set
  282. * to the value from newdepends.
  283. *
  284. * Parameters are:
  285. * pkg - the package we're doing this for. This is used to
  286. * construct correct uplinks.
  287. * updateme - the forwards dependency pointer that we are to
  288. * update. This starts out containing the old forwards
  289. * info, which we use to unthread the old reverse
  290. * links. After we're done it is updated.
  291. * newdepends - the value that we ultimately want to have in
  292. * updateme.
  293. * It is likely that the backward pointer for the package in
  294. * question (`depended') will be updated by this routine,
  295. * but this will happen by the routine traversing the dependency
  296. * data structures. It doesn't need to be told where to update
  297. * that; I just mention it as something that one should be
  298. * cautious about.
  299. */
  300. struct dependency *dyp;
  301. struct deppossi *dop;
  302. struct pkginfoperfile *addtopifp;
  303. /* Delete `backward' (`depended') links from other packages to
  304. * dependencies listed in old version of this one. We do this by
  305. * going through all the dependencies in the old version of this
  306. * one and following them down to find which deppossi nodes to
  307. * remove.
  308. */
  309. for (dyp= *updateme; dyp; dyp= dyp->next) {
  310. for (dop= dyp->list; dop; dop= dop->next) {
  311. if (dop->backrev)
  312. dop->backrev->nextrev= dop->nextrev;
  313. else
  314. if (available)
  315. dop->ed->available.depended= dop->nextrev;
  316. else
  317. dop->ed->installed.depended= dop->nextrev;
  318. if (dop->nextrev)
  319. dop->nextrev->backrev= dop->backrev;
  320. }
  321. }
  322. /* Now fill in new `ed' links from other packages to dependencies listed
  323. * in new version of this one, and set our uplinks correctly.
  324. */
  325. for (dyp= newdepends; dyp; dyp= dyp->next) {
  326. dyp->up= pkg;
  327. for (dop= dyp->list; dop; dop= dop->next) {
  328. addtopifp= available ? &dop->ed->available : &dop->ed->installed;
  329. if (!addtopifp->valid) blankpackageperfile(addtopifp);
  330. dop->nextrev= addtopifp->depended;
  331. dop->backrev= 0;
  332. if (addtopifp->depended)
  333. addtopifp->depended->backrev= dop;
  334. addtopifp->depended= dop;
  335. }
  336. }
  337. /* Finally, we fill in the new value. */
  338. *updateme= newdepends;
  339. }