-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack.c
271 lines (225 loc) · 5 KB
/
pack.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/** fcom: Pack files into any supported archive type
2023, Simon Zolin */
static const char* pack_help()
{
return "\
Pack files into any supported archive type.\n\
Usage:\n\
fcom pack INPUT... [-C OUTPUT_DIR] -o OUTPUT\n\
";
}
#include <fcom.h>
#include <util/util.h>
#include <ffsys/path.h>
#include <ffsys/pipe.h>
const fcom_core *core;
struct pack {
fcom_cominfo cominfo;
uint state;
fcom_cominfo *cmd;
ffstr iname, base;
uint stop;
int result;
fffd pr, pw;
};
/** Find operation name by file extension */
static const char* op_find_ext(ffstr ext)
{
static const char ext_op[][2][4] = {
{"gz", "gz"},
{"iso", "iso"},
{"tar", "tar"},
{"zip", "zip"},
{"zipx","zip"},
{"zst", "zst"},
};
int r;
if (0 > (r = ffcharr_find_sorted_padding(ext_op, FF_COUNT(ext_op), 4, 4, ext.ptr, ext.len)))
return NULL;
return ext_op[r][1];
}
/** Get names of the operations we need to perform */
static const char* pack_detect(struct pack *p, const char **oper2, ffstr oname)
{
ffstr name, ext, ext2;
ffpath_splitpath_str(oname, NULL, &name);
ffpath_splitname_str(name, &name, &ext2);
if (ffpath_splitname_str(name, NULL, &ext) < 0) {
ext = ext2;
ext2.len = 0;
}
if (ffstr_eqz(&ext, "tgz")) {
ffstr_setz(&ext, "tar");
ffstr_setz(&ext2, "gz");
}
const char *opname;
if (!(opname = op_find_ext(ext))) {
fcom_errlog("unknown archive file extension .%S", &ext);
return NULL;
}
// tar | gz > file.tar.gz
if (ext2.len)
*oper2 = op_find_ext(ext2);
return opname;
}
static void pack_run(fcom_op *op);
static void pack_op_complete(void *param, int result)
{
uint level = (size_t)param & 1;
struct pack *p = (void*)((size_t)param & ~1);
if (level == 1) {
// tar process is complete: make gz input reader return 0
ffpipe_close(p->pw); p->pw = FFPIPE_NULL;
return;
}
p->result = result;
pack_run(p);
}
static void vec_str_dup(ffvec *dst, ffvec *src)
{
ffstr *it;
FFSLICE_WALK(src, it) {
ffstr *s = ffvec_zpushT(dst, ffstr);
ffstr_dupstr(s, it);
}
}
/** Exec a child operation */
static int pack_child(struct pack *p, const char *opname, uint level)
{
fcom_cominfo *c = core->com->create();
c->operation = ffsz_dup(opname);
c->overwrite = p->cmd->overwrite;
c->test = p->cmd->test;
c->on_complete = pack_op_complete;
c->opaque = p;
if (level == 0 || level == 1) {
vec_str_dup(&c->input, &p->cmd->input);
vec_str_dup(&c->include, &p->cmd->include);
vec_str_dup(&c->exclude, &p->cmd->exclude);
} else {
ffstr *s = ffvec_zpushT(&c->input, ffstr);
ffstr_dupz(s, "");
}
if (level == 1) {
c->stdout = 1;
c->fd_stdout = p->pw;
c->opaque = (void*)((size_t)p | 1);
} else {
ffstrz_dup_str0(&c->output, p->cmd->output);
ffstr_dup_str0(&c->chdir, p->cmd->chdir);
if (level == 2) {
c->stdin = 1;
c->fd_stdin = p->pr;
}
}
if (core->com->run(c))
return -1;
return 0;
}
static int pack_begin(struct pack *p, ffstr oname)
{
const char *opname, *opname2 = NULL;
if (!(opname = pack_detect(p, &opname2, oname)))
return -1;
if (opname2) {
if (ffpipe_create2(&p->pr, &p->pw, FFPIPE_NONBLOCK)) {
fcom_syserrlog("ffpipe_create");
return -1;
}
if (pack_child(p, opname, 1))
return -1;
if (pack_child(p, opname2, 2))
return -1;
} else {
if (pack_child(p, opname, 0))
return -1;
}
return 0;
}
#define O(member) FF_OFF(struct pack, member)
static int pack_args_parse(struct pack *p, fcom_cominfo *cmd)
{
static const struct ffarg args[] = {
{}
};
int r = core->com->args_parse(cmd, args, p, FCOM_COM_AP_INOUT);
if (r)
return r;
if (!p->cmd->output.len) {
fcom_errlog("Please specify output file name with `-o NAME`");
return -1;
}
return 0;
}
#undef O
static void pack_close(fcom_op *op)
{
struct pack *p = op;
ffpipe_close(p->pr);
ffpipe_close(p->pw);
ffmem_free(p);
}
static fcom_op* pack_create(fcom_cominfo *cmd)
{
struct pack *p = ffmem_new(struct pack);
p->cmd = cmd;
p->pr = p->pw = FFPIPE_NULL;
if (pack_args_parse(p, cmd))
goto end;
return p;
end:
pack_close(p);
return NULL;
}
static void pack_run(fcom_op *op)
{
struct pack *p = op;
int r, rc = 1;
enum { I_IN, I_COMPLETE };
while (!FFINT_READONCE(p->stop)) {
switch (p->state) {
case I_IN:
// if (0 > (r = core->com->input_next(p->cmd, &p->iname, &p->base, 0))) {
// if (r == FCOM_COM_RINPUT_NOMORE) {
// rc = 0;
// }
// goto end;
// }
r = pack_begin(p, p->cmd->output);
if (r) goto end;
p->state = I_COMPLETE;
return;
case I_COMPLETE:
if (p->result) {
rc = p->result;
goto end;
}
if (p->pw != FFPIPE_NULL) {
core->com->async(p->cmd);
return;
}
ffpipe_close(p->pr); p->pr = FFPIPE_NULL;
rc = 0;
goto end;
// p->state = I_IN;
// continue;
}
}
end:
{
fcom_cominfo *cmd = p->cmd;
pack_close(p);
core->com->complete(cmd, rc);
}
}
static void pack_signal(fcom_op *op, uint signal)
{
struct pack *p = op;
FFINT_WRITEONCE(p->stop, 1);
}
static const fcom_operation fcom_op_pack = {
pack_create, pack_close,
pack_run, pack_signal,
pack_help,
};
FCOM_MOD_DEFINE(pack, fcom_op_pack, core)