parse.c 15 KB

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