forked from gentoo/portage-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qxpak.c
170 lines (149 loc) · 3.92 KB
/
qxpak.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright 2005-2021 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
*
* Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
* Copyright 2005-2014 Mike Frysinger - <vapier@gentoo.org>
*/
#include "main.h"
#include "applets.h"
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "basename.h"
#include "safe_io.h"
#include "scandirat.h"
#include "xpak.h"
#define QXPAK_FLAGS "lxcd:O" COMMON_FLAGS
static struct option const qxpak_long_opts[] = {
{"list", no_argument, NULL, 'l'},
{"extract", no_argument, NULL, 'x'},
{"create", no_argument, NULL, 'c'},
{"dir", a_argument, NULL, 'd'},
{"stdout", no_argument, NULL, 'O'},
COMMON_LONG_OPTS
};
static const char * const qxpak_opts_help[] = {
"List the contents of an archive",
"Extract the contents of an archive",
"Create an archive of a directory/files",
"Change to specified directory",
"Write files to stdout",
COMMON_OPTS_HELP
};
#define qxpak_usage(ret) usage(ret, QXPAK_FLAGS, qxpak_long_opts, qxpak_opts_help, NULL, lookup_applet_idx("qxpak"))
static char xpak_stdout;
struct qxpak_cb {
int dir_fd;
int argc;
char **argv;
bool extract;
};
static void
_xpak_callback(
void *ctx,
char *pathname,
int pathname_len,
int data_offset,
int data_len,
char *data)
{
FILE *out;
struct qxpak_cb *xctx = (struct qxpak_cb *)ctx;
/* see if there is a match when there is a selection */
if (xctx->argc > 0) {
int i;
for (i = 0; i < xctx->argc; i++) {
if (xctx->argv[i] && !strcmp(pathname, xctx->argv[i])) {
xctx->argv[i] = NULL;
break;
}
}
if (i == xctx->argc)
return;
}
if (verbose == 0 + (xctx->extract ? 1 : 0))
printf("%.*s\n", pathname_len, pathname);
else if (verbose == 1 + (xctx->extract ? 1 : 0))
printf("%.*s: %d byte%s\n",
pathname_len, pathname,
data_len, (data_len > 1 ? "s" : ""));
else if (verbose != 0)
printf("%.*s: %d byte%s @ offset byte %d\n",
pathname_len, pathname,
data_len, (data_len > 1 ? "s" : ""), data_offset);
if (!xctx->extract)
return;
if (!xpak_stdout) {
int fd = openat(xctx->dir_fd, pathname,
O_WRONLY | O_CLOEXEC | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
return;
out = fdopen(fd, "w");
if (!out)
return;
} else
out = stdout;
fwrite(data + data_offset, 1, data_len, out);
if (!xpak_stdout)
fclose(out);
}
int qxpak_main(int argc, char **argv)
{
enum { XPAK_ACT_NONE, XPAK_ACT_LIST, XPAK_ACT_EXTRACT, XPAK_ACT_CREATE };
int i, ret;
char *xpak;
char action = XPAK_ACT_NONE;
struct qxpak_cb cbctx;
xpak_stdout = 0;
cbctx.dir_fd = AT_FDCWD;
cbctx.extract = false;
while ((i = GETOPT_LONG(QXPAK, qxpak, "")) != -1) {
switch (i) {
COMMON_GETOPTS_CASES(qxpak)
case 'l': action = XPAK_ACT_LIST; break;
case 'x': action = XPAK_ACT_EXTRACT; break;
case 'c': action = XPAK_ACT_CREATE; break;
case 'O': xpak_stdout = 1; break;
case 'd':
if (cbctx.dir_fd != AT_FDCWD)
err("Only use -d once");
cbctx.dir_fd = open(optarg, O_RDONLY|O_CLOEXEC|O_PATH);
if (cbctx.dir_fd < 0)
errp("Could not open directory %s", optarg);
break;
}
}
if (optind == argc || action == XPAK_ACT_NONE)
qxpak_usage(EXIT_FAILURE);
xpak = argv[optind++];
argc -= optind;
argv += optind;
cbctx.argc = argc;
cbctx.argv = argv;
switch (action) {
case XPAK_ACT_LIST:
ret = xpak_list(xpak, &cbctx, &_xpak_callback);
break;
case XPAK_ACT_EXTRACT:
cbctx.extract = true;
ret = xpak_extract(xpak, &cbctx, &_xpak_callback);
break;
case XPAK_ACT_CREATE:
ret = xpak_create(cbctx.dir_fd, xpak, argc, argv, 0, verbose);
break;
default:
ret = -1;
}
ret = ret < 0 ? EXIT_FAILURE : 0;
if (cbctx.dir_fd != AT_FDCWD)
close(cbctx.dir_fd);
/* warn about non-matched args */
if (argc > 0 && (action == XPAK_ACT_LIST || action == XPAK_ACT_EXTRACT))
for (i = 0; i < argc; ++i)
if (argv[i])
warn("Could not locate '%s' in archive", argv[i]);
return ret;
}