parse.c 15 KB

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