info.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/wait.h>
  27. #include <errno.h>
  28. #include <limits.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/c-ctype.h>
  38. #include <dpkg/dpkg.h>
  39. #include <dpkg/dpkg-db.h>
  40. #include <dpkg/parsedump.h>
  41. #include <dpkg/pkg-format.h>
  42. #include <dpkg/buffer.h>
  43. #include <dpkg/path.h>
  44. #include <dpkg/options.h>
  45. #include "dpkg-deb.h"
  46. static void cu_info_prepare(int argc, void **argv) {
  47. char *dir;
  48. dir = argv[0];
  49. path_remove_tree(dir);
  50. free(dir);
  51. }
  52. static void info_prepare(const char *const **argvp,
  53. const char **debarp,
  54. const char **dirp,
  55. int admininfo) {
  56. char *dbuf;
  57. *debarp= *(*argvp)++;
  58. if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  59. dbuf = mkdtemp(path_make_temp_template("dpkg-deb"));
  60. if (!dbuf)
  61. ohshite(_("unable to create temporary directory"));
  62. *dirp = dbuf;
  63. push_cleanup(cu_info_prepare, -1, NULL, 0, 1, (void *)dbuf);
  64. extracthalf(*debarp, dbuf, DPKG_TAR_EXTRACT | DPKG_TAR_NOMTIME, admininfo);
  65. }
  66. static int ilist_select(const struct dirent *de) {
  67. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  68. }
  69. static void
  70. info_spew(const char *debar, const char *dir, const char *const *argv)
  71. {
  72. struct dpkg_error err;
  73. const char *component;
  74. struct varbuf controlfile = VARBUF_INIT;
  75. int fd;
  76. int re= 0;
  77. while ((component = *argv++) != NULL) {
  78. varbuf_reset(&controlfile);
  79. varbuf_printf(&controlfile, "%s/%s", dir, component);
  80. fd = open(controlfile.buf, O_RDONLY);
  81. if (fd >= 0) {
  82. if (fd_fd_copy(fd, 1, -1, &err) < 0)
  83. ohshit(_("cannot extract control file '%s' from '%s': %s"),
  84. controlfile.buf, debar, err.str);
  85. close(fd);
  86. } else if (errno == ENOENT) {
  87. notice(_("'%.255s' contains no control component '%.255s'"),
  88. debar, component);
  89. re++;
  90. } else {
  91. ohshite(_("open component '%.255s' (in %.255s) failed in an unexpected way"),
  92. component, dir);
  93. }
  94. }
  95. varbuf_destroy(&controlfile);
  96. if (re > 0)
  97. ohshit(P_("%d requested control component is missing",
  98. "%d requested control components are missing", re), re);
  99. }
  100. static void
  101. info_list(const char *debar, const char *dir)
  102. {
  103. char interpreter[INTERPRETER_MAX+1], *p;
  104. int il, lines;
  105. struct varbuf controlfile = VARBUF_INIT;
  106. struct dirent **cdlist, *cdep;
  107. int cdn, n;
  108. FILE *cc;
  109. struct stat stab;
  110. int c;
  111. cdn = scandir(dir, &cdlist, &ilist_select, alphasort);
  112. if (cdn == -1)
  113. ohshite(_("cannot scan directory '%.255s'"), dir);
  114. for (n = 0; n < cdn; n++) {
  115. cdep = cdlist[n];
  116. varbuf_reset(&controlfile);
  117. varbuf_printf(&controlfile, "%s/%s", dir, cdep->d_name);
  118. if (stat(controlfile.buf, &stab))
  119. ohshite(_("cannot stat '%.255s' (in '%.255s')"), cdep->d_name, dir);
  120. if (S_ISREG(stab.st_mode)) {
  121. cc = fopen(controlfile.buf, "r");
  122. if (!cc)
  123. ohshite(_("cannot open '%.255s' (in '%.255s')"), cdep->d_name, dir);
  124. lines = 0;
  125. interpreter[0] = '\0';
  126. if (getc(cc) == '#') {
  127. if (getc(cc) == '!') {
  128. while ((c= getc(cc))== ' ');
  129. p=interpreter; *p++='#'; *p++='!'; il=2;
  130. while (il < INTERPRETER_MAX && !c_isspace(c) && c != EOF) {
  131. *p++= c; il++; c= getc(cc);
  132. }
  133. *p = '\0';
  134. if (c=='\n') lines++;
  135. }
  136. }
  137. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  138. if (ferror(cc))
  139. ohshite(_("failed to read '%.255s' (in '%.255s')"), cdep->d_name, dir);
  140. fclose(cc);
  141. printf(_(" %7jd bytes, %5d lines %c %-20.127s %.127s\n"),
  142. (intmax_t)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  143. cdep->d_name, interpreter);
  144. } else {
  145. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  146. }
  147. free(cdep);
  148. }
  149. free(cdlist);
  150. varbuf_reset(&controlfile);
  151. varbuf_printf(&controlfile, "%s/%s", dir, CONTROLFILE);
  152. cc = fopen(controlfile.buf, "r");
  153. if (!cc) {
  154. if (errno != ENOENT)
  155. ohshite(_("failed to read '%.255s' (in '%.255s')"), CONTROLFILE, dir);
  156. fputs(_("(no 'control' file in control archive!)\n"), stdout);
  157. } else {
  158. lines= 1;
  159. while ((c= getc(cc))!= EOF) {
  160. if (lines)
  161. putc(' ', stdout);
  162. putc(c, stdout);
  163. lines= c=='\n';
  164. }
  165. if (!lines)
  166. putc('\n', stdout);
  167. if (ferror(cc))
  168. ohshite(_("failed to read '%.255s' (in '%.255s')"), CONTROLFILE, dir);
  169. fclose(cc);
  170. }
  171. m_output(stdout, _("<standard output>"));
  172. varbuf_destroy(&controlfile);
  173. }
  174. static void
  175. info_field(const char *debar, const char *dir, const char *const *fields,
  176. enum fwriteflags fieldflags)
  177. {
  178. char *controlfile;
  179. struct varbuf str = VARBUF_INIT;
  180. struct pkginfo *pkg;
  181. int i;
  182. m_asprintf(&controlfile, "%s/%s", dir, CONTROLFILE);
  183. parsedb(controlfile, pdb_parse_binary | pdb_ignorefiles, &pkg);
  184. free(controlfile);
  185. for (i = 0; fields[i]; i++) {
  186. const struct fieldinfo *field;
  187. const struct arbitraryfield *arbfield;
  188. varbuf_reset(&str);
  189. field = find_field_info(fieldinfos, fields[i]);
  190. if (field) {
  191. field->wcall(&str, pkg, &pkg->available, fieldflags, field);
  192. } else {
  193. arbfield = find_arbfield_info(pkg->available.arbs, fields[i]);
  194. if (arbfield)
  195. varbuf_add_arbfield(&str, arbfield, fieldflags);
  196. }
  197. varbuf_end_str(&str);
  198. if (fieldflags & fw_printheader)
  199. printf("%s", str.buf);
  200. else
  201. printf("%s\n", str.buf);
  202. }
  203. m_output(stdout, _("<standard output>"));
  204. varbuf_destroy(&str);
  205. }
  206. int
  207. do_showinfo(const char *const *argv)
  208. {
  209. const char *debar, *dir;
  210. char *controlfile;
  211. struct dpkg_error err;
  212. struct pkginfo *pkg;
  213. struct pkg_format_node *fmt;
  214. fmt = pkg_format_parse(showformat, &err);
  215. if (!fmt)
  216. ohshit(_("error in show format: %s"), err.str);
  217. info_prepare(&argv, &debar, &dir, 1);
  218. m_asprintf(&controlfile, "%s/%s", dir, CONTROLFILE);
  219. parsedb(controlfile, pdb_parse_binary | pdb_ignorefiles, &pkg);
  220. pkg_format_show(fmt, pkg, &pkg->available);
  221. pkg_format_free(fmt);
  222. free(controlfile);
  223. return 0;
  224. }
  225. int
  226. do_info(const char *const *argv)
  227. {
  228. const char *debar, *dir;
  229. if (*argv && argv[1]) {
  230. info_prepare(&argv, &debar, &dir, 1);
  231. info_spew(debar, dir, argv);
  232. } else {
  233. info_prepare(&argv, &debar, &dir, 2);
  234. info_list(debar, dir);
  235. }
  236. return 0;
  237. }
  238. int
  239. do_field(const char *const *argv)
  240. {
  241. const char *debar, *dir;
  242. info_prepare(&argv, &debar, &dir, 1);
  243. if (*argv) {
  244. info_field(debar, dir, argv, argv[1] != NULL ? fw_printheader : 0);
  245. } else {
  246. static const char *const controlonly[] = { CONTROLFILE, NULL };
  247. info_spew(debar, dir, controlonly);
  248. }
  249. return 0;
  250. }
  251. int
  252. do_contents(const char *const *argv)
  253. {
  254. const char *debar = *argv++;
  255. if (debar == NULL || *argv)
  256. badusage(_("--%s takes exactly one argument"), cipaction->olong);
  257. extracthalf(debar, NULL, DPKG_TAR_LIST, 0);
  258. return 0;
  259. }