info.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <https://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/parsedump.h>
  40. #include <dpkg/pkg-format.h>
  41. #include <dpkg/buffer.h>
  42. #include <dpkg/path.h>
  43. #include <dpkg/subproc.h>
  44. #include <dpkg/options.h>
  45. #include "dpkg-deb.h"
  46. static void cu_info_prepare(int argc, void **argv) {
  47. pid_t pid;
  48. char *dir;
  49. struct stat stab;
  50. dir = argv[0];
  51. if (lstat(dir, &stab) && errno == ENOENT)
  52. return;
  53. pid = subproc_fork();
  54. if (pid == 0) {
  55. if (chdir("/"))
  56. ohshite(_("failed to chdir to `/' for cleanup"));
  57. execlp(RM, "rm", "-rf", dir, NULL);
  58. ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
  59. }
  60. subproc_wait_check(pid, _("rm command for cleanup"), 0);
  61. }
  62. static void info_prepare(const char *const **argvp,
  63. const char **debarp,
  64. const char **dirp,
  65. int admininfo) {
  66. char *dbuf;
  67. *debarp= *(*argvp)++;
  68. if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  69. dbuf = mkdtemp(path_make_temp_template("dpkg-deb"));
  70. if (!dbuf)
  71. ohshite(_("unable to create temporary directory"));
  72. *dirp = dbuf;
  73. push_cleanup(cu_info_prepare, -1, NULL, 0, 1, (void *)dbuf);
  74. extracthalf(*debarp, dbuf, DPKG_TAR_EXTRACT | DPKG_TAR_NOMTIME, admininfo);
  75. }
  76. static int ilist_select(const struct dirent *de) {
  77. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  78. }
  79. static void
  80. info_spew(const char *debar, const char *dir, const char *const *argv)
  81. {
  82. struct dpkg_error err;
  83. const char *component;
  84. struct varbuf controlfile = VARBUF_INIT;
  85. int fd;
  86. int re= 0;
  87. while ((component = *argv++) != NULL) {
  88. varbuf_reset(&controlfile);
  89. varbuf_printf(&controlfile, "%s/%s", dir, component);
  90. fd = open(controlfile.buf, O_RDONLY);
  91. if (fd >= 0) {
  92. if (fd_fd_copy(fd, 1, -1, &err) < 0)
  93. ohshit(_("cannot extract control file '%s' from '%s': %s"),
  94. controlfile.buf, debar, err.str);
  95. close(fd);
  96. } else if (errno == ENOENT) {
  97. notice(_("'%.255s' contains no control component '%.255s'"),
  98. debar, component);
  99. re++;
  100. } else {
  101. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  102. component, dir);
  103. }
  104. }
  105. varbuf_destroy(&controlfile);
  106. if (re > 0)
  107. ohshit(P_("%d requested control component is missing",
  108. "%d requested control components are missing", re), re);
  109. }
  110. static void
  111. info_list(const char *debar, const char *dir)
  112. {
  113. char interpreter[INTERPRETER_MAX+1], *p;
  114. int il, lines;
  115. struct varbuf controlfile = VARBUF_INIT;
  116. struct dirent **cdlist, *cdep;
  117. int cdn, n;
  118. FILE *cc;
  119. struct stat stab;
  120. int c;
  121. cdn = scandir(dir, &cdlist, &ilist_select, alphasort);
  122. if (cdn == -1)
  123. ohshite(_("cannot scan directory `%.255s'"), dir);
  124. for (n = 0; n < cdn; n++) {
  125. cdep = cdlist[n];
  126. varbuf_reset(&controlfile);
  127. varbuf_printf(&controlfile, "%s/%s", dir, cdep->d_name);
  128. if (stat(controlfile.buf, &stab))
  129. ohshite(_("cannot stat `%.255s' (in `%.255s')"), cdep->d_name, dir);
  130. if (S_ISREG(stab.st_mode)) {
  131. cc = fopen(controlfile.buf, "r");
  132. if (!cc)
  133. ohshite(_("cannot open `%.255s' (in `%.255s')"), cdep->d_name, dir);
  134. lines = 0;
  135. interpreter[0] = '\0';
  136. if (getc(cc) == '#') {
  137. if (getc(cc) == '!') {
  138. while ((c= getc(cc))== ' ');
  139. p=interpreter; *p++='#'; *p++='!'; il=2;
  140. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  141. *p++= c; il++; c= getc(cc);
  142. }
  143. *p = '\0';
  144. if (c=='\n') lines++;
  145. }
  146. }
  147. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  148. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  149. cdep->d_name, dir);
  150. fclose(cc);
  151. printf(_(" %7jd bytes, %5d lines %c %-20.127s %.127s\n"),
  152. (intmax_t)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  153. cdep->d_name, interpreter);
  154. } else {
  155. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  156. }
  157. free(cdep);
  158. }
  159. free(cdlist);
  160. varbuf_reset(&controlfile);
  161. varbuf_printf(&controlfile, "%s/%s", dir, CONTROLFILE);
  162. cc = fopen(controlfile.buf, "r");
  163. if (!cc) {
  164. if (errno != ENOENT)
  165. ohshite(_("failed to read `%.255s' (in `%.255s')"), CONTROLFILE, dir);
  166. fputs(_("(no `control' file in control archive!)\n"), stdout);
  167. } else {
  168. lines= 1;
  169. while ((c= getc(cc))!= EOF) {
  170. if (lines)
  171. putc(' ', stdout);
  172. putc(c, stdout);
  173. lines= c=='\n';
  174. }
  175. if (!lines)
  176. putc('\n', stdout);
  177. if (ferror(cc))
  178. ohshite(_("failed to read `%.255s' (in `%.255s')"), CONTROLFILE, dir);
  179. fclose(cc);
  180. }
  181. m_output(stdout, _("<standard output>"));
  182. varbuf_destroy(&controlfile);
  183. }
  184. static void
  185. info_field(const char *debar, const char *dir, const char *const *fields,
  186. enum fwriteflags fieldflags)
  187. {
  188. char *controlfile;
  189. struct varbuf str = VARBUF_INIT;
  190. struct pkginfo *pkg;
  191. int i;
  192. m_asprintf(&controlfile, "%s/%s", dir, CONTROLFILE);
  193. parsedb(controlfile, pdb_parse_binary | pdb_ignorefiles, &pkg);
  194. free(controlfile);
  195. for (i = 0; fields[i]; i++) {
  196. const struct fieldinfo *field;
  197. const struct arbitraryfield *arbfield;
  198. varbuf_reset(&str);
  199. field = find_field_info(fieldinfos, fields[i]);
  200. if (field) {
  201. field->wcall(&str, pkg, &pkg->available, fieldflags, field);
  202. } else {
  203. arbfield = find_arbfield_info(pkg->available.arbs, fields[i]);
  204. if (arbfield)
  205. varbuf_add_arbfield(&str, arbfield, fieldflags);
  206. }
  207. varbuf_end_str(&str);
  208. if (fieldflags & fw_printheader)
  209. printf("%s", str.buf);
  210. else
  211. printf("%s\n", str.buf);
  212. }
  213. m_output(stdout, _("<standard output>"));
  214. varbuf_destroy(&str);
  215. }
  216. int
  217. do_showinfo(const char *const *argv)
  218. {
  219. const char *debar, *dir;
  220. char *controlfile;
  221. struct dpkg_error err;
  222. struct pkginfo *pkg;
  223. struct pkg_format_node *fmt;
  224. fmt = pkg_format_parse(showformat, &err);
  225. if (!fmt)
  226. ohshit(_("error in show format: %s"), err.str);
  227. info_prepare(&argv, &debar, &dir, 1);
  228. m_asprintf(&controlfile, "%s/%s", dir, CONTROLFILE);
  229. parsedb(controlfile, pdb_parse_binary | pdb_ignorefiles, &pkg);
  230. pkg_format_show(fmt, pkg, &pkg->available);
  231. pkg_format_free(fmt);
  232. free(controlfile);
  233. return 0;
  234. }
  235. int
  236. do_info(const char *const *argv)
  237. {
  238. const char *debar, *dir;
  239. if (*argv && argv[1]) {
  240. info_prepare(&argv, &debar, &dir, 1);
  241. info_spew(debar, dir, argv);
  242. } else {
  243. info_prepare(&argv, &debar, &dir, 2);
  244. info_list(debar, dir);
  245. }
  246. return 0;
  247. }
  248. int
  249. do_field(const char *const *argv)
  250. {
  251. const char *debar, *dir;
  252. info_prepare(&argv, &debar, &dir, 1);
  253. if (*argv) {
  254. info_field(debar, dir, argv, argv[1] != NULL ? fw_printheader : 0);
  255. } else {
  256. static const char *const controlonly[] = { CONTROLFILE, NULL };
  257. info_spew(debar, dir, controlonly);
  258. }
  259. return 0;
  260. }
  261. int
  262. do_contents(const char *const *argv)
  263. {
  264. const char *debar = *argv++;
  265. if (debar == NULL || *argv)
  266. badusage(_("--%s takes exactly one argument"), cipaction->olong);
  267. extracthalf(debar, NULL, DPKG_TAR_LIST, 0);
  268. return 0;
  269. }