info.c 7.9 KB

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