parse.c 17 KB

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