info.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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, int showfieldname) {
  173. FILE *cc;
  174. char fieldname[MAXFIELDNAME+1];
  175. char *pf;
  176. const char *const *fp;
  177. int doing, c, lno, fnl;
  178. if (!(cc= fopen("control","r"))) ohshite(_("could not open the `control' component"));
  179. doing= 1; lno= 1;
  180. for (;;) {
  181. c= getc(cc); if (c==EOF) { doing=0; break; }
  182. if (c == '\n') { lno++; doing=1; continue; }
  183. if (!isspace(c)) {
  184. for (pf=fieldname, fnl=0;
  185. fnl <= MAXFIELDNAME && c!=EOF && !isspace(c) && c!=':';
  186. c= getc(cc)) { *pf++= c; fnl++; }
  187. *pf = '\0';
  188. doing= fnl >= MAXFIELDNAME || c=='\n' || c==EOF;
  189. for (fp=fields; !doing && *fp; fp++)
  190. if (!strcasecmp(*fp,fieldname)) doing=1;
  191. if (showfieldname) {
  192. if (doing)
  193. fputs(fieldname,stdout);
  194. } else {
  195. if (c==':') c= getc(cc);
  196. while (c != '\n' && isspace(c)) c= getc(cc);
  197. }
  198. }
  199. for(;;) {
  200. if (c == EOF) break;
  201. if (doing) putc(c,stdout);
  202. if (c == '\n') { lno++; break; }
  203. c= getc(cc);
  204. }
  205. if (c == EOF) break;
  206. }
  207. if (ferror(cc)) ohshite(_("failed during read of `control' component"));
  208. if (fclose(cc))
  209. ohshite(_("error closing the '%s' component"), "control");
  210. if (doing) putc('\n',stdout);
  211. m_output(stdout, _("<standard output>"));
  212. }
  213. void do_showinfo(const char* const* argv) {
  214. const char *debar, *directory;
  215. struct pkginfo *pkg;
  216. struct pkg_format_node *fmt = pkg_format_parse(showformat);
  217. if (!fmt)
  218. ohshit(_("Error in format"));
  219. info_prepare(&argv,&debar,&directory,1);
  220. parsedb(CONTROLFILE, pdb_ignorefiles, &pkg, NULL, NULL);
  221. pkg_format_show(fmt, pkg, &pkg->installed);
  222. }
  223. void do_info(const char *const *argv) {
  224. const char *debar, *directory;
  225. if (*argv && argv[1]) {
  226. info_prepare(&argv,&debar,&directory,1);
  227. info_spew(debar,directory, argv);
  228. } else {
  229. info_prepare(&argv,&debar,&directory,2);
  230. info_list(debar,directory);
  231. }
  232. }
  233. void do_field(const char *const *argv) {
  234. const char *debar, *directory;
  235. info_prepare(&argv,&debar,&directory,1);
  236. if (*argv) {
  237. info_field(debar, directory, argv, argv[1] != NULL);
  238. } else {
  239. static const char *const controlonly[] = { "control", NULL };
  240. info_spew(debar,directory, controlonly);
  241. }
  242. }
  243. void do_contents(const char *const *argv) {
  244. const char *debar;
  245. if (!(debar= *argv++) || *argv) badusage(_("--contents takes exactly one argument"));
  246. extracthalf(debar, NULL, "tv", 0);
  247. }
  248. /* vi: sw=2
  249. */