info.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 c1;
  46. char *directory;
  47. struct stat stab;
  48. directory= (char*)(argv[0]);
  49. if (chdir("/"))
  50. ohshite(_("failed to chdir to `/' for cleanup"));
  51. if (lstat(directory,&stab) && errno==ENOENT) return;
  52. c1 = subproc_fork();
  53. if (!c1) {
  54. execlp(RM, "rm", "-rf", directory, NULL);
  55. ohshite(_("failed to exec rm for cleanup"));
  56. }
  57. subproc_wait_check(c1, "rm cleanup", 0);
  58. }
  59. static void info_prepare(const char *const **argvp,
  60. const char **debarp,
  61. const char **directoryp,
  62. int admininfo) {
  63. char *dbuf;
  64. *debarp= *(*argvp)++;
  65. if (!*debarp) badusage(_("--%s needs a .deb filename argument"),cipaction->olong);
  66. dbuf = mkdtemp(path_make_temp_template("dpkg-deb"));
  67. if (!dbuf)
  68. ohshite(_("failed to create temporary directory"));
  69. *directoryp= dbuf;
  70. push_cleanup(cu_info_prepare, -1, NULL, 0, 1, (void *)dbuf);
  71. extracthalf(*debarp, dbuf, "mx", admininfo);
  72. }
  73. static int ilist_select(const struct dirent *de) {
  74. return strcmp(de->d_name,".") && strcmp(de->d_name,"..");
  75. }
  76. static void info_spew(const char *debar, const char *directory,
  77. const char *const *argv) {
  78. const char *component;
  79. struct varbuf controlfile = VARBUF_INIT;
  80. int fd;
  81. int re= 0;
  82. while ((component = *argv++) != NULL) {
  83. varbufreset(&controlfile);
  84. varbufaddstr(&controlfile, directory);
  85. varbufaddc(&controlfile, '/');
  86. varbufaddstr(&controlfile, component);
  87. varbufaddc(&controlfile, '\0');
  88. fd = open(controlfile.buf, O_RDONLY);
  89. if (fd >= 0) {
  90. fd_fd_copy(fd, 1, -1, _("info_spew"));
  91. close(fd);
  92. } else if (errno == ENOENT) {
  93. fprintf(stderr,
  94. _("dpkg-deb: `%.255s' contains no control component `%.255s'\n"),
  95. debar, component);
  96. re++;
  97. } else {
  98. ohshite(_("open component `%.255s' (in %.255s) failed in an unexpected way"),
  99. component, directory);
  100. }
  101. }
  102. varbuf_destroy(&controlfile);
  103. if (re > 0)
  104. ohshit(P_("%d requested control component is missing",
  105. "%d requested control components are missing", re), re);
  106. }
  107. static void info_list(const char *debar, const char *directory) {
  108. char interpreter[INTERPRETER_MAX+1], *p;
  109. int il, lines;
  110. struct dirent **cdlist, *cdep;
  111. int cdn, n;
  112. FILE *cc;
  113. struct stat stab;
  114. int c;
  115. cdn= scandir(".", &cdlist, &ilist_select, alphasort);
  116. if (cdn == -1) ohshite(_("cannot scan directory `%.255s'"),directory);
  117. for (n = 0; n < cdn; n++) {
  118. cdep = cdlist[n];
  119. if (stat(cdep->d_name,&stab))
  120. ohshite(_("cannot stat `%.255s' (in `%.255s')"),cdep->d_name,directory);
  121. if (S_ISREG(stab.st_mode)) {
  122. if (!(cc= fopen(cdep->d_name,"r")))
  123. ohshite(_("cannot open `%.255s' (in `%.255s')"),cdep->d_name,directory);
  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 && !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)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  139. cdep->d_name,directory);
  140. fclose(cc);
  141. printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  142. (long)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. if (!(cc= fopen("control","r"))) {
  151. if (errno != ENOENT)
  152. ohshite(_("failed to read `%.255s' (in `%.255s')"), "control", directory);
  153. fputs(_("(no `control' file in control archive!)\n"), stdout);
  154. } else {
  155. lines= 1;
  156. while ((c= getc(cc))!= EOF) {
  157. if (lines)
  158. putc(' ', stdout);
  159. putc(c, stdout);
  160. lines= c=='\n';
  161. }
  162. if (!lines)
  163. putc('\n', stdout);
  164. if (ferror(cc))
  165. ohshite(_("failed to read `%.255s' (in `%.255s')"), "control", directory);
  166. fclose(cc);
  167. }
  168. m_output(stdout, _("<standard output>"));
  169. }
  170. static void info_field(const char *debar, const char *directory,
  171. const char *const *fields, bool showfieldname)
  172. {
  173. FILE *cc;
  174. char fieldname[MAXFIELDNAME+1];
  175. char *pf;
  176. const char *const *fp;
  177. int c, lno, fnl;
  178. bool doing;
  179. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  180. doing = true;
  181. lno = 1;
  182. for (;;) {
  183. c = getc(cc);
  184. if (c == EOF) {
  185. doing = false;
  186. break;
  187. }
  188. if (c == '\n') {
  189. lno++;
  190. doing = true;
  191. continue;
  192. }
  193. if (!isspace(c)) {
  194. for (pf=fieldname, fnl=0;
  195. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  196. c= getc(cc)) { *pf++= c; fnl++; }
  197. *pf = '\0';
  198. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  199. for (fp=fields; !doing && *fp; fp++)
  200. if (!strcasecmp(*fp, fieldname))
  201. doing = true;
  202. if (showfieldname) {
  203. if (doing)
  204. fputs(fieldname,stdout);
  205. } else {
  206. if (c==':') c= getc(cc);
  207. while (c != '\n' && isspace(c)) c= getc(cc);
  208. }
  209. }
  210. for(;;) {
  211. if (c == EOF) break;
  212. if (doing) putc(c,stdout);
  213. if (c == '\n') { lno++; break; }
  214. c= getc(cc);
  215. }
  216. if (c == EOF) break;
  217. }
  218. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  219. if (fclose(cc))
  220. ohshite(_("error closing the '%s' component"), "control");
  221. if (doing) putc('\n',stdout);
  222. m_output(stdout, _("<standard output>"));
  223. }
  224. void do_showinfo(const char* const* argv) {
  225. const char *debar, *directory;
  226. struct pkginfo *pkg;
  227. struct pkg_format_node *fmt = pkg_format_parse(showformat);
  228. if (!fmt)
  229. ohshit(_("Error in format"));
  230. info_prepare(&argv,&debar,&directory,1);
  231. parsedb(CONTROLFILE, pdb_recordavailable | pdb_rejectstatus | pdb_ignorefiles,
  232. &pkg, NULL, NULL);
  233. pkg_format_show(fmt, pkg, &pkg->available);
  234. }
  235. void do_info(const char *const *argv) {
  236. const char *debar, *directory;
  237. if (*argv && argv[1]) {
  238. info_prepare(&argv,&debar,&directory,1);
  239. info_spew(debar,directory, argv);
  240. } else {
  241. info_prepare(&argv,&debar,&directory,2);
  242. info_list(debar,directory);
  243. }
  244. }
  245. void do_field(const char *const *argv) {
  246. const char *debar, *directory;
  247. info_prepare(&argv,&debar,&directory,1);
  248. if (*argv) {
  249. info_field(debar, directory, argv, argv[1] != NULL);
  250. } else {
  251. static const char *const controlonly[] = { "control", NULL };
  252. info_spew(debar,directory, controlonly);
  253. }
  254. }
  255. void do_contents(const char *const *argv) {
  256. const char *debar;
  257. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  258. extracthalf(debar, NULL, "tv", 0);
  259. }
  260. /* vi: sw=2
  261. */