info.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * info.c - providing information
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2001 Wichert Akkerman
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <errno.h>
  27. #include <limits.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <fcntl.h>
  31. #include <dirent.h>
  32. #include <unistd.h>
  33. #include <stdint.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <dpkg/i18n.h>
  37. #include <dpkg/dpkg.h>
  38. #include <dpkg/dpkg-db.h>
  39. #include <dpkg/pkg-format.h>
  40. #include <dpkg/buffer.h>
  41. #include <dpkg/path.h>
  42. #include <dpkg/subproc.h>
  43. #include <dpkg/options.h>
  44. #include "dpkg-deb.h"
  45. static void cu_info_prepare(int argc, void **argv) {
  46. pid_t pid;
  47. char *dir;
  48. struct stat stab;
  49. dir = argv[0];
  50. if (lstat(dir, &stab) && errno == ENOENT)
  51. return;
  52. pid = subproc_fork();
  53. if (pid == 0) {
  54. if (chdir("/"))
  55. ohshite(_("failed to chdir to `/' for cleanup"));
  56. execlp(RM, "rm", "-rf", dir, NULL);
  57. ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
  58. }
  59. subproc_wait_check(pid, _("rm command for cleanup"), 0);
  60. }
  61. static void info_prepare(const char *const **argvp,
  62. const char **debarp,
  63. const char **dirp,
  64. int admininfo) {
  65. char *dbuf;
  66. *debarp= *(*argvp)++;
  67. if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  68. dbuf = mkdtemp(path_make_temp_template("dpkg-deb"));
  69. if (!dbuf)
  70. ohshite(_("unable to create temporary directory"));
  71. *dirp = dbuf;
  72. push_cleanup(cu_info_prepare, -1, NULL, 0, 1, (void *)dbuf);
  73. extracthalf(*debarp, dbuf, "mx", admininfo);
  74. }
  75. static int ilist_select(const struct dirent *de) {
  76. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  77. }
  78. static void
  79. info_spew(const char *debar, const char *dir, const char *const *argv)
  80. {
  81. const char *component;
  82. struct varbuf controlfile = VARBUF_INIT;
  83. int fd;
  84. int re= 0;
  85. while ((component = *argv++) != NULL) {
  86. varbuf_reset(&controlfile);
  87. varbuf_printf(&controlfile, "%s/%s", dir, component);
  88. fd = open(controlfile.buf, O_RDONLY);
  89. if (fd >= 0) {
  90. fd_fd_copy(fd, 1, -1, _("control file '%s'"), controlfile.buf);
  91. close(fd);
  92. } else if (errno == ENOENT) {
  93. fprintf(stderr,
  94. _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  95. debar, component);
  96. re++;
  97. } else {
  98. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  99. component, dir);
  100. }
  101. }
  102. varbuf_destroy(&controlfile);
  103. if (re > 0)
  104. ohshit(P_("%d requested control component is missing",
  105. "%d requested control components are missing", re), re);
  106. }
  107. static void
  108. info_list(const char *debar, const char *dir)
  109. {
  110. char interpreter[INTERPRETER_MAX+1], *p;
  111. int il, lines;
  112. struct varbuf controlfile = VARBUF_INIT;
  113. struct dirent **cdlist, *cdep;
  114. int cdn, n;
  115. FILE *cc;
  116. struct stat stab;
  117. int c;
  118. cdn = scandir(dir, &cdlist, &ilist_select, alphasort);
  119. if (cdn == -1)
  120. ohshite(_("cannot scan directory `%.255s'"), dir);
  121. for (n = 0; n < cdn; n++) {
  122. cdep = cdlist[n];
  123. varbuf_reset(&controlfile);
  124. varbuf_printf(&controlfile, "%s/%s", dir, cdep->d_name);
  125. if (stat(controlfile.buf, &stab))
  126. ohshite(_("cannot stat `%.255s' (in `%.255s')"), cdep->d_name, dir);
  127. if (S_ISREG(stab.st_mode)) {
  128. cc = fopen(controlfile.buf, "r");
  129. if (!cc)
  130. ohshite(_("cannot open `%.255s' (in `%.255s')"), cdep->d_name, dir);
  131. lines = 0;
  132. interpreter[0] = '\0';
  133. if (getc(cc) == '#') {
  134. if (getc(cc) == '!') {
  135. while ((c= getc(cc))== ' ');
  136. p=interpreter; *p++='#'; *p++='!'; il=2;
  137. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  138. *p++= c; il++; c= getc(cc);
  139. }
  140. *p = '\0';
  141. if (c=='\n') lines++;
  142. }
  143. }
  144. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  145. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  146. cdep->d_name, dir);
  147. fclose(cc);
  148. printf(_(" %7jd bytes, %5d lines %c %-20.127s %.127s\n"),
  149. (intmax_t)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  150. cdep->d_name, interpreter);
  151. } else {
  152. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  153. }
  154. free(cdep);
  155. }
  156. free(cdlist);
  157. varbuf_reset(&controlfile);
  158. varbuf_printf(&controlfile, "%s/%s", dir, CONTROLFILE);
  159. cc = fopen(controlfile.buf, "r");
  160. if (!cc) {
  161. if (errno != ENOENT)
  162. ohshite(_("failed to read `%.255s' (in `%.255s')"), CONTROLFILE, dir);
  163. fputs(_("(no `control' file in control archive!)\n"), stdout);
  164. } else {
  165. lines= 1;
  166. while ((c= getc(cc))!= EOF) {
  167. if (lines)
  168. putc(' ', stdout);
  169. putc(c, stdout);
  170. lines= c=='\n';
  171. }
  172. if (!lines)
  173. putc('\n', stdout);
  174. if (ferror(cc))
  175. ohshite(_("failed to read `%.255s' (in `%.255s')"), CONTROLFILE, dir);
  176. fclose(cc);
  177. }
  178. m_output(stdout, _("<standard output>"));
  179. varbuf_destroy(&controlfile);
  180. }
  181. static void
  182. info_field(const char *debar, const char *dir, const char *const *fields,
  183. bool showfieldname)
  184. {
  185. FILE *cc;
  186. char *controlfile;
  187. char fieldname[MAXFIELDNAME+1];
  188. char *pf;
  189. const char *const *fp;
  190. int c, lno, fnl;
  191. bool doing;
  192. m_asprintf(&controlfile, "%s/%s", dir, CONTROLFILE);
  193. cc = fopen(controlfile, "r");
  194. if (!cc)
  195. ohshite(_("could not open the `control' component"));
  196. doing = true;
  197. lno = 1;
  198. for (;;) {
  199. c = getc(cc);
  200. if (c == EOF) {
  201. doing = false;
  202. break;
  203. }
  204. if (c == '\n') {
  205. lno++;
  206. doing = true;
  207. continue;
  208. }
  209. if (!isspace(c)) {
  210. for (pf=fieldname, fnl=0;
  211. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  212. c= getc(cc)) { *pf++= c; fnl++; }
  213. *pf = '\0';
  214. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  215. for (fp=fields; !doing && *fp; fp++)
  216. if (!strcasecmp(*fp, fieldname))
  217. doing = true;
  218. if (showfieldname) {
  219. if (doing)
  220. fputs(fieldname,stdout);
  221. } else {
  222. if (c==':') c= getc(cc);
  223. while (c != '\n' && isspace(c)) c= getc(cc);
  224. }
  225. }
  226. for(;;) {
  227. if (c == EOF) break;
  228. if (doing) putc(c,stdout);
  229. if (c == '\n') { lno++; break; }
  230. c= getc(cc);
  231. }
  232. if (c == EOF) break;
  233. }
  234. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  235. if (fclose(cc))
  236. ohshite(_("error closing the '%s' component"), CONTROLFILE);
  237. if (doing) putc('\n',stdout);
  238. m_output(stdout, _("<standard output>"));
  239. free(controlfile);
  240. }
  241. int
  242. do_showinfo(const char *const *argv)
  243. {
  244. const char *debar, *dir;
  245. char *controlfile;
  246. struct pkginfo *pkg;
  247. struct pkg_format_node *fmt = pkg_format_parse(showformat);
  248. if (!fmt)
  249. ohshit(_("Error in format"));
  250. info_prepare(&argv, &debar, &dir, 1);
  251. m_asprintf(&controlfile, "%s/%s", dir, CONTROLFILE);
  252. parsedb(controlfile,
  253. pdb_recordavailable | pdb_rejectstatus | pdb_ignorefiles, &pkg);
  254. pkg_format_show(fmt, pkg, &pkg->available);
  255. free(controlfile);
  256. return 0;
  257. }
  258. int
  259. do_info(const char *const *argv)
  260. {
  261. const char *debar, *dir;
  262. if (*argv && argv[1]) {
  263. info_prepare(&argv, &debar, &dir, 1);
  264. info_spew(debar, dir, argv);
  265. } else {
  266. info_prepare(&argv, &debar, &dir, 2);
  267. info_list(debar, dir);
  268. }
  269. return 0;
  270. }
  271. int
  272. do_field(const char *const *argv)
  273. {
  274. const char *debar, *dir;
  275. info_prepare(&argv, &debar, &dir, 1);
  276. if (*argv) {
  277. info_field(debar, dir, argv, argv[1] != NULL);
  278. } else {
  279. static const char *const controlonly[] = { CONTROLFILE, NULL };
  280. info_spew(debar, dir, controlonly);
  281. }
  282. return 0;
  283. }
  284. int
  285. do_contents(const char *const *argv)
  286. {
  287. const char *debar;
  288. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  289. extracthalf(debar, NULL, "tv", 0);
  290. return 0;
  291. }
  292. /* vi: sw=2
  293. */