info.c 8.1 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 <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 <stdlib.h>
  34. #include <stdio.h>
  35. #include <dpkg/i18n.h>
  36. #include <dpkg/dpkg.h>
  37. #include <dpkg/dpkg-db.h>
  38. #include <dpkg/pkg-format.h>
  39. #include <dpkg/buffer.h>
  40. #include <dpkg/path.h>
  41. #include <dpkg/subproc.h>
  42. #include <dpkg/myopt.h>
  43. #include "dpkg-deb.h"
  44. static void cu_info_prepare(int argc, void **argv) {
  45. pid_t pid;
  46. char *dir;
  47. struct stat stab;
  48. dir = argv[0];
  49. if (chdir("/"))
  50. ohshite(_("failed to chdir to `/' for cleanup"));
  51. if (lstat(dir, &stab) && errno == ENOENT)
  52. return;
  53. pid = subproc_fork();
  54. if (pid == 0) {
  55. execlp(RM, "rm", "-rf", dir, NULL);
  56. ohshite(_("unable to execute %s (%s)"), _("rm command for cleanup"), RM);
  57. }
  58. subproc_wait_check(pid, _("rm command for cleanup"), 0);
  59. }
  60. static void info_prepare(const char *const **argvp,
  61. const char **debarp,
  62. const char **dirp,
  63. int admininfo) {
  64. char *dbuf;
  65. *debarp= *(*argvp)++;
  66. if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  67. dbuf = mkdtemp(path_make_temp_template("dpkg-deb"));
  68. if (!dbuf)
  69. ohshite(_("unable to create temporary directory"));
  70. *dirp = dbuf;
  71. push_cleanup(cu_info_prepare, -1, NULL, 0, 1, (void *)dbuf);
  72. extracthalf(*debarp, dbuf, "mx", admininfo);
  73. }
  74. static int ilist_select(const struct dirent *de) {
  75. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  76. }
  77. static void
  78. info_spew(const char *debar, const char *dir, const char *const *argv)
  79. {
  80. const char *component;
  81. struct varbuf controlfile = VARBUF_INIT;
  82. int fd;
  83. int re= 0;
  84. while ((component = *argv++) != NULL) {
  85. varbuf_reset(&controlfile);
  86. varbuf_add_str(&controlfile, dir);
  87. varbuf_add_char(&controlfile, '/');
  88. varbuf_add_str(&controlfile, component);
  89. varbuf_end_str(&controlfile);
  90. fd = open(controlfile.buf, O_RDONLY);
  91. if (fd >= 0) {
  92. fd_fd_copy(fd, 1, -1, _("control file '%s'"), controlfile.buf);
  93. close(fd);
  94. } else if (errno == ENOENT) {
  95. fprintf(stderr,
  96. _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  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 dirent **cdlist, *cdep;
  115. int cdn, n;
  116. FILE *cc;
  117. struct stat stab;
  118. int c;
  119. cdn= scandir(".", &cdlist, &ilist_select, alphasort);
  120. if (cdn == -1)
  121. ohshite(_("cannot scan directory `%.255s'"), dir);
  122. for (n = 0; n < cdn; n++) {
  123. cdep = cdlist[n];
  124. if (stat(cdep->d_name,&stab))
  125. ohshite(_("cannot stat `%.255s' (in `%.255s')"), cdep->d_name, dir);
  126. if (S_ISREG(stab.st_mode)) {
  127. if (!(cc= fopen(cdep->d_name,"r")))
  128. ohshite(_("cannot open `%.255s' (in `%.255s')"), cdep->d_name, dir);
  129. lines = 0;
  130. interpreter[0] = '\0';
  131. if (getc(cc) == '#') {
  132. if (getc(cc) == '!') {
  133. while ((c= getc(cc))== ' ');
  134. p=interpreter; *p++='#'; *p++='!'; il=2;
  135. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  136. *p++= c; il++; c= getc(cc);
  137. }
  138. *p = '\0';
  139. if (c=='\n') lines++;
  140. }
  141. }
  142. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  143. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  144. cdep->d_name, dir);
  145. fclose(cc);
  146. printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  147. (long)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  148. cdep->d_name, interpreter);
  149. } else {
  150. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  151. }
  152. free(cdep);
  153. }
  154. free(cdlist);
  155. if (!(cc= fopen("control","r"))) {
  156. if (errno != ENOENT)
  157. ohshite(_("failed to read `%.255s' (in `%.255s')"), "control", dir);
  158. fputs(_("(no `control' file in control archive!)\n"), stdout);
  159. } else {
  160. lines= 1;
  161. while ((c= getc(cc))!= EOF) {
  162. if (lines)
  163. putc(' ', stdout);
  164. putc(c, stdout);
  165. lines= c=='\n';
  166. }
  167. if (!lines)
  168. putc('\n', stdout);
  169. if (ferror(cc))
  170. ohshite(_("failed to read `%.255s' (in `%.255s')"), "control", dir);
  171. fclose(cc);
  172. }
  173. m_output(stdout, _("<standard output>"));
  174. }
  175. static void
  176. info_field(const char *debar, const char *dir, const char *const *fields,
  177. bool showfieldname)
  178. {
  179. FILE *cc;
  180. char fieldname[MAXFIELDNAME+1];
  181. char *pf;
  182. const char *const *fp;
  183. int c, lno, fnl;
  184. bool doing;
  185. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  186. doing = true;
  187. lno = 1;
  188. for (;;) {
  189. c = getc(cc);
  190. if (c == EOF) {
  191. doing = false;
  192. break;
  193. }
  194. if (c == '\n') {
  195. lno++;
  196. doing = true;
  197. continue;
  198. }
  199. if (!isspace(c)) {
  200. for (pf=fieldname, fnl=0;
  201. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  202. c= getc(cc)) { *pf++= c; fnl++; }
  203. *pf = '\0';
  204. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  205. for (fp=fields; !doing && *fp; fp++)
  206. if (!strcasecmp(*fp, fieldname))
  207. doing = true;
  208. if (showfieldname) {
  209. if (doing)
  210. fputs(fieldname,stdout);
  211. } else {
  212. if (c==':') c= getc(cc);
  213. while (c != '\n' && isspace(c)) c= getc(cc);
  214. }
  215. }
  216. for(;;) {
  217. if (c == EOF) break;
  218. if (doing) putc(c,stdout);
  219. if (c == '\n') { lno++; break; }
  220. c= getc(cc);
  221. }
  222. if (c == EOF) break;
  223. }
  224. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  225. if (fclose(cc))
  226. ohshite(_("error closing the '%s' component"), "control");
  227. if (doing) putc('\n',stdout);
  228. m_output(stdout, _("<standard output>"));
  229. }
  230. void do_showinfo(const char* const* argv) {
  231. const char *debar, *dir;
  232. struct pkginfo *pkg;
  233. struct pkg_format_node *fmt = pkg_format_parse(showformat);
  234. if (!fmt)
  235. ohshit(_("Error in format"));
  236. info_prepare(&argv, &debar, &dir, 1);
  237. parsedb(CONTROLFILE, pdb_recordavailable | pdb_rejectstatus | pdb_ignorefiles,
  238. &pkg);
  239. pkg_format_show(fmt, pkg, &pkg->available);
  240. }
  241. void do_info(const char *const *argv) {
  242. const char *debar, *dir;
  243. if (*argv && argv[1]) {
  244. info_prepare(&argv, &debar, &dir, 1);
  245. info_spew(debar, dir, argv);
  246. } else {
  247. info_prepare(&argv, &debar, &dir, 2);
  248. info_list(debar, dir);
  249. }
  250. }
  251. void do_field(const char *const *argv) {
  252. const char *debar, *dir;
  253. info_prepare(&argv, &debar, &dir, 1);
  254. if (*argv) {
  255. info_field(debar, dir, argv, argv[1] != NULL);
  256. } else {
  257. static const char *const controlonly[] = { "control", NULL };
  258. info_spew(debar, dir, controlonly);
  259. }
  260. }
  261. void do_contents(const char *const *argv) {
  262. const char *debar;
  263. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  264. extracthalf(debar, NULL, "tv", 0);
  265. }
  266. /* vi: sw=2
  267. */