-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunxz.c
303 lines (256 loc) · 6 KB
/
unxz.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/** fcom: unpack file from .xz
2023, Simon Zolin */
static const char* unxz_help()
{
return "\
Decompress file from .xz.\n\
Usage:\n\
`fcom unxz` INPUT [OPTIONS] [-o OUTPUT]\n\
";
}
#include <fcom.h>
#include <ffsys/path.h>
#include <ffpack/xzread.h>
static const fcom_core *core;
struct unxz {
fcom_cominfo cominfo;
uint st;
fcom_cominfo *cmd;
ffstr iname, basename;
char *oname;
fcom_file_obj *in, *out;
uint64 in_off;
ffxzread unxz;
uint64 isize;
ffstr data, zdata;
uint stop;
uint unxz_opened :1;
uint next_chunk_begin :1;
uint out_opened :1;
uint del_on_close :1;
uint64 in_total, out_total;
};
static int args_parse(struct unxz *z, fcom_cominfo *cmd)
{
static const struct ffarg args[] = {
{}
};
int r = core->com->args_parse(cmd, args, z, FCOM_COM_AP_INOUT);
if (r != 0)
return r;
if (cmd->chdir.len == 0)
ffstr_dupz(&cmd->chdir, ".");
return 0;
}
static void unxz_close(fcom_op *op)
{
struct unxz *z = op;
ffxzread_close(&z->unxz);
core->file->destroy(z->in);
if (z->del_on_close)
core->file->del(z->oname, 0);
core->file->destroy(z->out);
ffmem_free(z->oname);
ffmem_free(z);
}
static fcom_op* unxz_create(fcom_cominfo *cmd)
{
struct unxz *z = ffmem_new(struct unxz);
z->cmd = cmd;
if (0 != args_parse(z, cmd))
goto end;
struct fcom_file_conf fc = {};
fcom_cmd_file_conf(&fc, cmd);
z->in = core->file->create(&fc);
z->out = core->file->create(&fc);
return z;
end:
unxz_close(z);
return NULL;
}
/** Prepare output file name
idir/iname.xz -> odir/iname */
static char* out_name(struct unxz *z, ffstr in, ffstr base)
{
char *ofn = NULL;
if (z->cmd->output.len != 0) {
ofn = ffsz_dup(z->cmd->output.ptr);
} else {
ffpath_splitpath_str(in, NULL, &in);
ffstr name, ext;
ffpath_splitname_str(in, &name, &ext);
if (ffstr_eqz(&ext, "xz"))
in = name;
ofn = ffsz_allocfmt("%S%c%S"
, &z->cmd->chdir, FFPATH_SLASH, &in);
}
fcom_dbglog("output file name: %s", ofn);
return ofn;
}
static int xzread(struct unxz *z, ffstr *in, ffstr *out)
{
if (!z->unxz_opened) {
z->unxz_opened = 1;
if (0 != ffxzread_open(&z->unxz, z->isize)) {
fcom_errlog("ffxzread_open");
return 'erro';
}
}
ffsize n = in->len;
int r = ffxzread_process(&z->unxz, in, out);
z->in_total += n - in->len;
switch ((enum FFXZREAD_R)r) {
case FFXZREAD_INFO: {
ffxzread_info *info = ffxzread_getinfo(&z->unxz);
fcom_dbglog("info: osize:%U", info->uncompressed_size);
return 'info';
}
case FFXZREAD_DATA:
z->out_total += out->len;
return 'data';
case FFXZREAD_DONE:
fcom_verblog("%s: %U => %U (%u%%)"
, z->oname, z->in_total, z->out_total, (uint)FFINT_DIVSAFE(z->out_total * 100, z->in_total));
ffxzread_close(&z->unxz);
z->unxz_opened = 0;
return 'done';
case FFXZREAD_MORE:
return 'more';
case FFXZREAD_SEEK:
z->in_off = ffxzread_offset(&z->unxz);
return 'more';
case FFXZREAD_ERROR:
fcom_errlog("ffxzread_process: %s offset:0x%xU", ffxzread_error(&z->unxz), z->in_off);
return 'erro';
}
return 'erro';
}
static void unxz_run(fcom_op *op)
{
struct unxz *z = op;
int r, rc = 1;
enum { I_IN, I_IN_OPEN, I_OUT_OPEN, I_READ, I_DECOMP, I_WRITE, };
while (!FFINT_READONCE(z->stop)) {
switch (z->st) {
case I_IN:
if (0 > (r = core->com->input_next(z->cmd, &z->iname, &z->basename, 0))) {
if (r == FCOM_COM_RINPUT_NOMORE) {
rc = 0;
}
goto end;
}
z->st = I_IN_OPEN;
// fallthrough
case I_IN_OPEN: {
uint flags = fcom_file_cominfo_flags_i(z->cmd);
flags |= FCOM_FILE_READ;
r = core->file->open(z->in, z->iname.ptr, flags);
if (r == FCOM_FILE_ERR) goto end;
fffileinfo fi = {};
r = core->file->info(z->in, &fi);
if (r == FCOM_FILE_ERR) goto end;
z->isize = fffileinfo_size(&fi);
z->st = I_READ;
}
// fallthrough
case I_READ:
r = core->file->read(z->in, &z->zdata, z->in_off);
if (r == FCOM_FILE_ERR) goto end;
if (r == FCOM_FILE_ASYNC) {
core->com->async(z->cmd);
return;
}
if (r == FCOM_FILE_EOF) {
r = core->file->flush(z->out, 0);
if (r == FCOM_FILE_ASYNC) {
core->com->async(z->cmd);
return;
}
if (!z->next_chunk_begin) {
fcom_warnlog("file incomplete");
goto end;
}
z->st = I_IN;
continue;
}
z->in_off += z->zdata.len;
z->next_chunk_begin = 0;
z->st = I_DECOMP;
// fallthrough
case I_DECOMP:
r = xzread(z, &z->zdata, &z->data);
switch (r) {
case 0:
case 'info':
continue;
case 'done':
z->next_chunk_begin = 1;
z->del_on_close = 0;
// fallthrough
case 'more':
z->st = I_READ;
continue;
case 'erro':
goto end;
}
z->st = I_WRITE;
if (!z->out_opened) {
z->out_opened = 1;
if (!z->cmd->stdout)
z->oname = out_name(z, z->iname, z->basename);
z->st = I_OUT_OPEN;
}
continue;
case I_OUT_OPEN: {
uint flags = FCOM_FILE_WRITE;
flags |= fcom_file_cominfo_flags_o(z->cmd);
r = core->file->open(z->out, z->oname, flags);
if (r == FCOM_FILE_ERR) goto end;
z->del_on_close = !z->cmd->stdout && !z->cmd->test;
fffileinfo fi = {};
r = core->file->info(z->in, &fi);
if (r == FCOM_FILE_ERR) goto end;
core->file->mtime_set(z->out, fffileinfo_mtime1(&fi));
z->st = I_WRITE;
continue;
}
case I_WRITE:
r = core->file->write(z->out, z->data, -1);
if (r == FCOM_FILE_ERR) goto end;
if (r == FCOM_FILE_ASYNC) {
core->com->async(z->cmd);
return;
}
z->st = I_DECOMP;
continue;
}
}
end:
{
fcom_cominfo *cmd = z->cmd;
unxz_close(z);
core->com->complete(cmd, rc);
}
}
static void unxz_signal(fcom_op *op, uint signal)
{
struct unxz *z = op;
FFINT_WRITEONCE(z->stop, 1);
}
static const fcom_operation fcom_op_unxz = {
unxz_create, unxz_close,
unxz_run, unxz_signal,
unxz_help,
};
static void xz_init(const fcom_core *_core) { core = _core; }
static void xz_destroy() {}
static const fcom_operation* xz_provide_op(const char *name)
{
if (ffsz_eq(name, "unxz"))
return &fcom_op_unxz;
return NULL;
}
FF_EXPORT const struct fcom_module fcom_module = {
FCOM_VER, FCOM_CORE_VER,
xz_init, xz_destroy, xz_provide_op,
};