parse.c 17 KB

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