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==1)
  104. ohshit(_("One requested control component is missing"));
  105. else if (re>1)
  106. ohshit(_("%d requested control components are missing"), re);
  107. }
  108. static void info_list(const char *debar, const char *directory) {
  109. char interpreter[INTERPRETER_MAX+1], *p;
  110. int il, lines;
  111. struct dirent **cdlist, *cdep;
  112. int cdn, n;
  113. FILE *cc;
  114. struct stat stab;
  115. int c;
  116. cdn= scandir(".", &cdlist, &ilist_select, alphasort);
  117. if (cdn == -1) ohshite(_("cannot scan directory `%.255s'"),directory);
  118. for (n = 0; n < cdn; n++) {
  119. cdep = cdlist[n];
  120. if (stat(cdep->d_name,&stab))
  121. ohshite(_("cannot stat `%.255s' (in `%.255s')"),cdep->d_name,directory);
  122. if (S_ISREG(stab.st_mode)) {
  123. if (!(cc= fopen(cdep->d_name,"r")))
  124. ohshite(_("cannot open `%.255s' (in `%.255s')"),cdep->d_name,directory);
  125. lines = 0;
  126. interpreter[0] = '\0';
  127. if (getc(cc) == '#') {
  128. if (getc(cc) == '!') {
  129. while ((c= getc(cc))== ' ');
  130. p=interpreter; *p++='#'; *p++='!'; il=2;
  131. while (il<INTERPRETER_MAX && !isspace(c) && c!=EOF) {
  132. *p++= c; il++; c= getc(cc);
  133. }
  134. *p = '\0';
  135. if (c=='\n') lines++;
  136. }
  137. }
  138. while ((c= getc(cc))!= EOF) { if (c == '\n') lines++; }
  139. if (ferror(cc)) ohshite(_("failed to read `%.255s' (in `%.255s')"),
  140. cdep->d_name,directory);
  141. fclose(cc);
  142. printf(_(" %7ld bytes, %5d lines %c %-20.127s %.127s\n"),
  143. (long)stab.st_size, lines, S_IXUSR & stab.st_mode ? '*' : ' ',
  144. cdep->d_name, interpreter);
  145. } else {
  146. printf(_(" not a plain file %.255s\n"), cdep->d_name);
  147. }
  148. free(cdep);
  149. }
  150. free(cdlist);
  151. if (!(cc= fopen("control","r"))) {
  152. if (errno != ENOENT)
  153. ohshite(_("failed to read `%.255s' (in `%.255s')"), "control", directory);
  154. fputs(_("(no `control' file in control archive!)\n"), stdout);
  155. } else {
  156. lines= 1;
  157. while ((c= getc(cc))!= EOF) {
  158. if (lines)
  159. putc(' ', stdout);
  160. putc(c, stdout);
  161. lines= c=='\n';
  162. }
  163. if (!lines)
  164. putc('\n', stdout);
  165. if (ferror(cc))
  166. ohshite(_("failed to read `%.255s' (in `%.255s')"), "control", directory);
  167. fclose(cc);
  168. }
  169. m_output(stdout, _("<standard output>"));
  170. }
  171. static void info_field(const char *debar, const char *directory,
  172. const char *const *fields, bool showfieldname)
  173. {
  174. FILE *cc;
  175. char fieldname[MAXFIELDNAME+1];
  176. char *pf;
  177. const char *const *fp;
  178. int c, lno, fnl;
  179. bool doing;
  180. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  181. doing = true;
  182. lno = 1;
  183. for (;;) {
  184. c = getc(cc);
  185. if (c == EOF) {
  186. doing = false;
  187. break;
  188. }
  189. if (c == '\n') {
  190. lno++;
  191. doing = true;
  192. continue;
  193. }
  194. if (!isspace(c)) {
  195. for (pf=fieldname, fnl=0;
  196. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  197. c= getc(cc)) { *pf++= c; fnl++; }
  198. *pf = '\0';
  199. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  200. for (fp=fields; !doing && *fp; fp++)
  201. if (!strcasecmp(*fp, fieldname))
  202. doing = true;
  203. if (showfieldname) {
  204. if (doing)
  205. fputs(fieldname,stdout);
  206. } else {
  207. if (c==':') c= getc(cc);
  208. while (c != '\n' && isspace(c)) c= getc(cc);
  209. }
  210. }
  211. for(;;) {
  212. if (c == EOF) break;
  213. if (doing) putc(c,stdout);
  214. if (c == '\n') { lno++; break; }
  215. c= getc(cc);
  216. }
  217. if (c == EOF) break;
  218. }
  219. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  220. if (fclose(cc))
  221. ohshite(_("error closing the '%s' component"), "control");
  222. if (doing) putc('\n',stdout);
  223. m_output(stdout, _("<standard output>"));
  224. }
  225. void do_showinfo(const char* const* argv) {
  226. const char *debar, *directory;
  227. struct pkginfo *pkg;
  228. struct pkg_format_node *fmt = pkg_format_parse(showformat);
  229. if (!fmt)
  230. ohshit(_("Error in format"));
  231. info_prepare(&argv,&debar,&directory,1);
  232. parsedb(CONTROLFILE, pdb_ignorefiles, &pkg, NULL, NULL);
  233. pkg_format_show(fmt, pkg, &pkg->installed);
  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. */