parse.c 15 KB

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