info.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/myopt.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 (chdir("/"))
  51. ohshite(_("failed to chdir to `/' for cleanup"));
  52. if (lstat(dir, &stab) && errno == ENOENT)
  53. return;
  54. pid = subproc_fork();
  55. if (pid == 0) {
  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_add_str(&controlfile, dir);
  88. varbuf_add_char(&controlfile, '/');
  89. varbuf_add_str(&controlfile, component);
  90. varbuf_end_str(&controlfile);
  91. fd = open(controlfile.buf, O_RDONLY);
  92. if (fd >= 0) {
  93. fd_fd_copy(fd, 1, -1, _("control file '%s'"), controlfile.buf);
  94. close(fd);
  95. } else if (errno == ENOENT) {
  96. fprintf(stderr,
  97. _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  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 dirent **cdlist, *cdep;
  116. int cdn, n;
  117. FILE *cc;
  118. struct stat stab;
  119. int c;
  120. cdn= scandir(".", &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. if (stat(cdep->d_name,&stab))
  126. ohshite(_("cannot stat `%.255s' (in `%.255s')"), cdep->d_name, dir);
  127. if (S_ISREG(stab.st_mode)) {
  128. if (!(cc= fopen(cdep->d_name,"r")))
  129. ohshite(_("cannot open `%.255s' (in `%.255s')"), cdep->d_name, dir);
  130. lines = 0;
  131. interpreter[0] = '\0';
  132. if (getc(cc) == '#') {
  133. if (getc(cc) == '!') {
  134. while ((c= getc(cc))== ' ');
  135. p=interpreter; *p++='#'; *p++='!'; il=2;
  136. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  137. *p++= c; il++; c= getc(cc);
  138. }
  139. *p = '\0';
  140. if (c=='\n') lines++;
  141. }
  142. }
  143. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  144. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  145. cdep->d_name, dir);
  146. fclose(cc);
  147. printf(_(" %7jd bytes, %5d lines %c %-20.127s %.127s\n"),
  148. (intmax_t)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  149. cdep->d_name, interpreter);
  150. } else {
  151. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  152. }
  153. free(cdep);
  154. }
  155. free(cdlist);
  156. if (!(cc= fopen("control","r"))) {
  157. if (errno != ENOENT)
  158. ohshite(_("failed to read `%.255s' (in `%.255s')"), "control", dir);
  159. fputs(_("(no `control' file in control archive!)\n"), stdout);
  160. } else {
  161. lines= 1;
  162. while ((c= getc(cc))!= EOF) {
  163. if (lines)
  164. putc(' ', stdout);
  165. putc(c, stdout);
  166. lines= c=='\n';
  167. }
  168. if (!lines)
  169. putc('\n', stdout);
  170. if (ferror(cc))
  171. ohshite(_("failed to read `%.255s' (in `%.255s')"), "control", dir);
  172. fclose(cc);
  173. }
  174. m_output(stdout, _("<standard output>"));
  175. }
  176. static void
  177. info_field(const char *debar, const char *dir, const char *const *fields,
  178. bool showfieldname)
  179. {
  180. FILE *cc;
  181. char fieldname[MAXFIELDNAME+1];
  182. char *pf;
  183. const char *const *fp;
  184. int c, lno, fnl;
  185. bool doing;
  186. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  187. doing = true;
  188. lno = 1;
  189. for (;;) {
  190. c = getc(cc);
  191. if (c == EOF) {
  192. doing = false;
  193. break;
  194. }
  195. if (c == '\n') {
  196. lno++;
  197. doing = true;
  198. continue;
  199. }
  200. if (!isspace(c)) {
  201. for (pf=fieldname, fnl=0;
  202. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  203. c= getc(cc)) { *pf++= c; fnl++; }
  204. *pf = '\0';
  205. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  206. for (fp=fields; !doing && *fp; fp++)
  207. if (!strcasecmp(*fp, fieldname))
  208. doing = true;
  209. if (showfieldname) {
  210. if (doing)
  211. fputs(fieldname,stdout);
  212. } else {
  213. if (c==':') c= getc(cc);
  214. while (c != '\n' && isspace(c)) c= getc(cc);
  215. }
  216. }
  217. for(;;) {
  218. if (c == EOF) break;
  219. if (doing) putc(c,stdout);
  220. if (c == '\n') { lno++; break; }
  221. c= getc(cc);
  222. }
  223. if (c == EOF) break;
  224. }
  225. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  226. if (fclose(cc))
  227. ohshite(_("error closing the '%s' component"), "control");
  228. if (doing) putc('\n',stdout);
  229. m_output(stdout, _("<standard output>"));
  230. }
  231. int
  232. do_showinfo(const char *const *argv)
  233. {
  234. const char *debar, *dir;
  235. struct pkginfo *pkg;
  236. struct pkg_format_node *fmt = pkg_format_parse(showformat);
  237. if (!fmt)
  238. ohshit(_("Error in format"));
  239. info_prepare(&argv, &debar, &dir, 1);
  240. parsedb(CONTROLFILE, pdb_recordavailable | pdb_rejectstatus | pdb_ignorefiles,
  241. &pkg);
  242. pkg_format_show(fmt, pkg, &pkg->available);
  243. return 0;
  244. }
  245. int
  246. do_info(const char *const *argv)
  247. {
  248. const char *debar, *dir;
  249. if (*argv && argv[1]) {
  250. info_prepare(&argv, &debar, &dir, 1);
  251. info_spew(debar, dir, argv);
  252. } else {
  253. info_prepare(&argv, &debar, &dir, 2);
  254. info_list(debar, dir);
  255. }
  256. return 0;
  257. }
  258. int
  259. do_field(const char *const *argv)
  260. {
  261. const char *debar, *dir;
  262. info_prepare(&argv, &debar, &dir, 1);
  263. if (*argv) {
  264. info_field(debar, dir, argv, argv[1] != NULL);
  265. } else {
  266. static const char *const controlonly[] = { "control", NULL };
  267. info_spew(debar, dir, controlonly);
  268. }
  269. return 0;
  270. }
  271. int
  272. do_contents(const char *const *argv)
  273. {
  274. const char *debar;
  275. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  276. extracthalf(debar, NULL, "tv", 0);
  277. return 0;
  278. }
  279. /* vi: sw=2
  280. */