2 * dpkg-deb - construction and deconstruction of *.deb archives
3 * info.c - providing information
5 * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2001 Wichert Akkerman
7 * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
9 * This is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
26 #include <sys/types.h>
40 #include <dpkg/i18n.h>
41 #include <dpkg/c-ctype.h>
42 #include <dpkg/dpkg.h>
43 #include <dpkg/dpkg-db.h>
44 #include <dpkg/parsedump.h>
45 #include <dpkg/pkg-format.h>
46 #include <dpkg/buffer.h>
47 #include <dpkg/path.h>
48 #include <dpkg/options.h>
52 static void cu_info_prepare(int argc
, void **argv
) {
56 path_remove_tree(dir
);
60 static void info_prepare(const char *const **argvp
,
67 if (!*debarp
) badusage(_("--%s needs a .deb filename argument"),cipaction
->olong
);
69 dbuf
= mkdtemp(path_make_temp_template("dpkg-deb"));
71 ohshite(_("unable to create temporary directory"));
74 push_cleanup(cu_info_prepare
, -1, 1, (void *)dbuf
);
75 extracthalf(*debarp
, dbuf
, DPKG_TAR_EXTRACT
| DPKG_TAR_NOMTIME
, admininfo
);
78 static int ilist_select(const struct dirent
*de
) {
79 return strcmp(de
->d_name
,".") && strcmp(de
->d_name
,"..");
83 info_spew(const char *debar
, const char *dir
, const char *const *argv
)
85 struct dpkg_error err
;
86 const char *component
;
87 struct varbuf controlfile
= VARBUF_INIT
;
90 while ((component
= *argv
++) != NULL
) {
93 varbuf_reset(&controlfile
);
94 varbuf_printf(&controlfile
, "%s/%s", dir
, component
);
96 fd
= open(controlfile
.buf
, O_RDONLY
);
98 if (fd_fd_copy(fd
, 1, -1, &err
) < 0)
99 ohshit(_("cannot extract control file '%s' from '%s': %s"),
100 controlfile
.buf
, debar
, err
.str
);
102 } else if (errno
== ENOENT
) {
103 notice(_("'%.255s' contains no control component '%.255s'"),
107 ohshite(_("open component '%.255s' (in %.255s) failed in an unexpected way"),
111 varbuf_destroy(&controlfile
);
114 ohshit(P_("%d requested control component is missing",
115 "%d requested control components are missing", re
), re
);
119 info_interpreter(FILE *cc
, int *lines
)
121 char interpreter
[INTERPRETER_MAX
+ 1];
125 interpreter
[0] = '\0';
126 if (getc(cc
) == '#' && getc(cc
) == '!') {
130 while ((c
= getc(cc
)) == ' ')
136 while (il
< INTERPRETER_MAX
&& !c_isspace(c
) && c
!= EOF
) {
145 while ((c
= getc(cc
)) != EOF
) {
150 return m_strdup(interpreter
);
154 info_list(const char *debar
, const char *dir
)
156 struct varbuf controlfile
= VARBUF_INIT
;
157 struct dirent
**cdlist
;
161 cdn
= scandir(dir
, &cdlist
, &ilist_select
, alphasort
);
163 ohshite(_("cannot scan directory '%.255s'"), dir
);
165 for (n
= 0; n
< cdn
; n
++) {
171 varbuf_reset(&controlfile
);
172 varbuf_printf(&controlfile
, "%s/%s", dir
, cdep
->d_name
);
174 if (stat(controlfile
.buf
, &stab
))
175 ohshite(_("cannot stat '%.255s' (in '%.255s')"), cdep
->d_name
, dir
);
176 if (S_ISREG(stab
.st_mode
)) {
177 int exec_mark
= (S_IXUSR
& stab
.st_mode
) ? '*' : ' ';
181 cc
= fopen(controlfile
.buf
, "r");
183 ohshite(_("cannot open '%.255s' (in '%.255s')"), cdep
->d_name
, dir
);
185 interpreter
= info_interpreter(cc
, &lines
);
188 ohshite(_("failed to read '%.255s' (in '%.255s')"), cdep
->d_name
, dir
);
190 if (str_is_set(interpreter
))
191 printf(_(" %7jd bytes, %5d lines %c %-20.127s %.127s\n"),
192 (intmax_t)stab
.st_size
, lines
, exec_mark
, cdep
->d_name
,
195 printf(_(" %7jd bytes, %5d lines %c %.127s\n"),
196 (intmax_t)stab
.st_size
, lines
, exec_mark
, cdep
->d_name
);
200 printf(_(" not a plain file %.255s\n"), cdep
->d_name
);
206 varbuf_reset(&controlfile
);
207 varbuf_printf(&controlfile
, "%s/%s", dir
, CONTROLFILE
);
208 cc
= fopen(controlfile
.buf
, "r");
211 ohshite(_("failed to read '%.255s' (in '%.255s')"), CONTROLFILE
, dir
);
212 warning(_("no 'control' file in control archive!"));
217 while ((c
= getc(cc
))!= EOF
) {
227 ohshite(_("failed to read '%.255s' (in '%.255s')"), CONTROLFILE
, dir
);
231 m_output(stdout
, _("<standard output>"));
232 varbuf_destroy(&controlfile
);
236 info_field(const char *debar
, const char *dir
, const char *const *fields
,
237 enum fwriteflags fieldflags
)
240 struct varbuf str
= VARBUF_INIT
;
244 controlfile
= str_fmt("%s/%s", dir
, CONTROLFILE
);
245 parsedb(controlfile
, pdb_parse_binary
| pdb_ignore_archives
, &pkg
);
248 for (i
= 0; fields
[i
]; i
++) {
249 const struct fieldinfo
*field
;
252 field
= find_field_info(fieldinfos
, fields
[i
]);
254 field
->wcall(&str
, pkg
, &pkg
->available
, fieldflags
, field
);
256 const struct arbitraryfield
*arbfield
;
258 arbfield
= find_arbfield_info(pkg
->available
.arbs
, fields
[i
]);
260 varbuf_add_arbfield(&str
, arbfield
, fieldflags
);
262 varbuf_end_str(&str
);
264 if (fieldflags
& fw_printheader
)
265 printf("%s", str
.buf
);
267 printf("%s\n", str
.buf
);
270 m_output(stdout
, _("<standard output>"));
272 varbuf_destroy(&str
);
276 do_showinfo(const char *const *argv
)
278 const char *debar
, *dir
;
280 struct dpkg_error err
;
282 struct pkg_format_node
*fmt
;
284 fmt
= pkg_format_parse(opt_showformat
, &err
);
286 ohshit(_("error in show format: %s"), err
.str
);
288 info_prepare(&argv
, &debar
, &dir
, 1);
290 controlfile
= str_fmt("%s/%s", dir
, CONTROLFILE
);
291 parsedb(controlfile
, pdb_parse_binary
| pdb_ignore_archives
, &pkg
);
292 pkg_format_show(fmt
, pkg
, &pkg
->available
);
293 pkg_format_free(fmt
);
300 do_info(const char *const *argv
)
302 const char *debar
, *dir
;
304 if (*argv
&& argv
[1]) {
305 info_prepare(&argv
, &debar
, &dir
, 1);
306 info_spew(debar
, dir
, argv
);
308 info_prepare(&argv
, &debar
, &dir
, 2);
309 info_list(debar
, dir
);
316 do_field(const char *const *argv
)
318 const char *debar
, *dir
;
320 info_prepare(&argv
, &debar
, &dir
, 1);
322 info_field(debar
, dir
, argv
, argv
[1] != NULL
? fw_printheader
: 0);
324 static const char *const controlonly
[] = { CONTROLFILE
, NULL
};
325 info_spew(debar
, dir
, controlonly
);
332 do_contents(const char *const *argv
)
334 const char *debar
= *argv
++;
336 if (debar
== NULL
|| *argv
)
337 badusage(_("--%s takes exactly one argument"), cipaction
->olong
);
338 extracthalf(debar
, NULL
, DPKG_TAR_LIST
, 0);