info.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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, DPKG_TAR_EXTRACT | DPKG_TAR_NOMTIME, 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. struct dpkg_error err;
  82. const char *component;
  83. struct varbuf controlfile = VARBUF_INIT;
  84. int fd;
  85. int re= 0;
  86. while ((component = *argv++) != NULL) {
  87. varbuf_reset(&controlfile);
  88. varbuf_printf(&controlfile, "%s/%s", dir, component);
  89. fd = open(controlfile.buf, O_RDONLY);
  90. if (fd >= 0) {
  91. if (fd_fd_copy(fd, 1, -1, &err) < 0)
  92. ohshit(_("cannot extract control file '%s' from '%s': %s"),
  93. controlfile.buf, debar, err.str);
  94. close(fd);
  95. } else if (errno == ENOENT) {
  96. notice(_("'%.255s' contains no control component '%.255s'"),
  97. debar, component);
  98. re++;
  99. } else {
  100. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  101. component, dir);
  102. }
  103. }
  104. varbuf_destroy(&controlfile);
  105. if (re > 0)
  106. ohshit(P_("%d requested control component is missing",
  107. "%d requested control components are missing", re), re);
  108. }
  109. static void
  110. info_list(const char *debar, const char *dir)
  111. {
  112. char interpreter[INTERPRETER_MAX+1], *p;
  113. int il, lines;
  114. struct varbuf controlfile = VARBUF_INIT;
  115. struct dirent **cdlist, *cdep;
  116. int cdn, n;
  117. FILE *cc;
  118. struct stat stab;
  119. int c;
  120. cdn = scandir(dir, &cdlist, &ilist_select, alphasort);
  121. if (cdn == -1)
  122. ohshite(_("cannot scan directory `%.255s'"), dir);
  123. for (n = 0; n < cdn; n++) {
  124. cdep = cdlist[n];
  125. varbuf_reset(&controlfile);
  126. varbuf_printf(&controlfile, "%s/%s", dir, cdep->d_name);
  127. if (stat(controlfile.buf, &stab))
  128. ohshite(_("cannot stat `%.255s' (in `%.255s')"), cdep->d_name, dir);
  129. if (S_ISREG(stab.st_mode)) {
  130. cc = fopen(controlfile.buf, "r");
  131. if (!cc)
  132. ohshite(_("cannot open `%.255s' (in `%.255s')"), cdep->d_name, dir);
  133. lines = 0;
  134. interpreter[0] = '\0';
  135. if (getc(cc) == '#') {
  136. if (getc(cc) == '!') {
  137. while ((c= getc(cc))== ' ');
  138. p=interpreter; *p++='#'; *p++='!'; il=2;
  139. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  140. *p++= c; il++; c= getc(cc);
  141. }
  142. *p = '\0';
  143. if (c=='\n') lines++;
  144. }
  145. }
  146. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  147. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  148. cdep->d_name, dir);
  149. fclose(cc);
  150. printf(_(" %7jd bytes, %5d lines %c %-20.127s %.127s\n"),
  151. (intmax_t)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  152. cdep->d_name, interpreter);
  153. } else {
  154. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  155. }
  156. free(cdep);
  157. }
  158. free(cdlist);
  159. varbuf_reset(&controlfile);
  160. varbuf_printf(&controlfile, "%s/%s", dir, CONTROLFILE);
  161. cc = fopen(controlfile.buf, "r");
  162. if (!cc) {
  163. if (errno != ENOENT)
  164. ohshite(_("failed to read `%.255s' (in `%.255s')"), CONTROLFILE, dir);
  165. fputs(_("(no `control' file in control archive!)\n"), stdout);
  166. } else {
  167. lines= 1;
  168. while ((c= getc(cc))!= EOF) {
  169. if (lines)
  170. putc(' ', stdout);
  171. putc(c, stdout);
  172. lines= c=='\n';
  173. }
  174. if (!lines)
  175. putc('\n', stdout);
  176. if (ferror(cc))
  177. ohshite(_("failed to read `%.255s' (in `%.255s')"), CONTROLFILE, dir);
  178. fclose(cc);
  179. }
  180. m_output(stdout, _("<standard output>"));
  181. varbuf_destroy(&controlfile);
  182. }
  183. static void
  184. info_field(const char *debar, const char *dir, const char *const *fields,
  185. bool showfieldname)
  186. {
  187. FILE *cc;
  188. char *controlfile;
  189. char fieldname[MAXFIELDNAME+1];
  190. char *pf;
  191. const char *const *fp;
  192. int c, lno, fnl;
  193. bool doing;
  194. m_asprintf(&controlfile, "%s/%s", dir, CONTROLFILE);
  195. cc = fopen(controlfile, "r");
  196. if (!cc)
  197. ohshite(_("could not open the `control' component"));
  198. doing = true;
  199. lno = 1;
  200. for (;;) {
  201. c = getc(cc);
  202. if (c == EOF) {
  203. doing = false;
  204. break;
  205. }
  206. if (c == '\n') {
  207. lno++;
  208. doing = true;
  209. continue;
  210. }
  211. if (!isspace(c)) {
  212. for (pf=fieldname, fnl=0;
  213. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  214. c= getc(cc)) { *pf++= c; fnl++; }
  215. *pf = '\0';
  216. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  217. for (fp=fields; !doing && *fp; fp++)
  218. if (strcasecmp(*fp, fieldname) == 0)
  219. doing = true;
  220. if (showfieldname) {
  221. if (doing)
  222. fputs(fieldname,stdout);
  223. } else {
  224. if (c==':') c= getc(cc);
  225. while (c != '\n' && isspace(c)) c= getc(cc);
  226. }
  227. }
  228. for(;;) {
  229. if (c == EOF) break;
  230. if (doing) putc(c,stdout);
  231. if (c == '\n') { lno++; break; }
  232. c= getc(cc);
  233. }
  234. if (c == EOF) break;
  235. }
  236. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  237. if (fclose(cc))
  238. ohshite(_("error closing the '%s' component"), CONTROLFILE);
  239. if (doing) putc('\n',stdout);
  240. m_output(stdout, _("<standard output>"));
  241. free(controlfile);
  242. }
  243. int
  244. do_showinfo(const char *const *argv)
  245. {
  246. const char *debar, *dir;
  247. char *controlfile;
  248. struct dpkg_error err;
  249. struct pkginfo *pkg;
  250. struct pkg_format_node *fmt;
  251. fmt = pkg_format_parse(showformat, &err);
  252. if (!fmt)
  253. ohshit(_("error in show format: %s"), err.str);
  254. info_prepare(&argv, &debar, &dir, 1);
  255. m_asprintf(&controlfile, "%s/%s", dir, CONTROLFILE);
  256. parsedb(controlfile, pdb_parse_binary | pdb_ignorefiles, &pkg);
  257. pkg_format_show(fmt, pkg, &pkg->available);
  258. pkg_format_free(fmt);
  259. free(controlfile);
  260. return 0;
  261. }
  262. int
  263. do_info(const char *const *argv)
  264. {
  265. const char *debar, *dir;
  266. if (*argv && argv[1]) {
  267. info_prepare(&argv, &debar, &dir, 1);
  268. info_spew(debar, dir, argv);
  269. } else {
  270. info_prepare(&argv, &debar, &dir, 2);
  271. info_list(debar, dir);
  272. }
  273. return 0;
  274. }
  275. int
  276. do_field(const char *const *argv)
  277. {
  278. const char *debar, *dir;
  279. info_prepare(&argv, &debar, &dir, 1);
  280. if (*argv) {
  281. info_field(debar, dir, argv, argv[1] != NULL);
  282. } else {
  283. static const char *const controlonly[] = { CONTROLFILE, NULL };
  284. info_spew(debar, dir, controlonly);
  285. }
  286. return 0;
  287. }
  288. int
  289. do_contents(const char *const *argv)
  290. {
  291. const char *debar = *argv++;
  292. if (debar == NULL || *argv)
  293. badusage(_("--%s takes exactly one argument"), cipaction->olong);
  294. extracthalf(debar, NULL, DPKG_TAR_LIST, 0);
  295. return 0;
  296. }