parse.c 16 KB

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