-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunzst.c
265 lines (219 loc) · 5.24 KB
/
unzst.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
/** fcom: unpack file from .zst
2023, Simon Zolin */
static const char* unzst_help()
{
return "\
Decompress file from .zst.\n\
Usage:\n\
`fcom unzst` INPUT [OPTIONS] [-o OUTPUT]\n\
";
}
#include <fcom.h>
#include <ffsys/path.h>
#include <zstd/zstd-ff.h>
extern const fcom_core *core;
struct unzst {
fcom_cominfo cominfo;
uint st;
fcom_cominfo *cmd;
ffstr iname, basename;
char *oname;
fcom_file_obj *in, *out;
zstd_decoder *zst;
ffstr data, zdata;
ffvec buf;
uint stop;
uint out_opened :1;
uint del_on_close :1;
uint64 in_total, out_total;
};
static int args_parse(struct unzst *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 unzst_close(fcom_op *op)
{
struct unzst *z = op;
zstd_decode_free(z->zst);
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);
ffvec_free(&z->buf);
ffmem_free(z);
}
static fcom_op* unzst_create(fcom_cominfo *cmd)
{
struct unzst *z = ffmem_new(struct unzst);
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);
ffsize cap = (cmd->buffer_size != 0) ? cmd->buffer_size : 64*1024;
ffvec_alloc(&z->buf, cap, 1);
return z;
end:
unzst_close(z);
return NULL;
}
/** Prepare output file name
idir/iname.zst -> odir/iname */
static char* out_name(struct unzst *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, "zst"))
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 zst_read(struct unzst *z, ffstr *input, ffstr *output)
{
zstd_buf in, out;
zstd_buf_set(&in, input->ptr, input->len);
zstd_buf_set(&out, z->buf.ptr, z->buf.cap);
int r = zstd_decode(z->zst, &in, &out);
ffstr_shift(input, in.pos);
ffstr_set(output, z->buf.ptr, out.pos);
z->in_total += in.pos;
z->out_total += out.pos;
fcom_dbglog("zstd_decode: %L (%U) -> %L (%U)"
, in.pos, z->in_total, out.pos, z->out_total);
if (r < 0) {
fcom_errlog("zstd_decode: %s", zstd_error(r));
return 'erro';
} else if (out.pos == 0) {
return 'more';
}
return 'data';
}
static void unzst_run(fcom_op *op)
{
struct unzst *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;
z->st = I_READ;
}
// fallthrough
case I_READ:
r = core->file->read(z->in, &z->zdata, -1);
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;
}
fcom_verblog("%s: %U => %U (%u%%)"
, z->oname, z->in_total, z->out_total, (uint)FFINT_DIVSAFE(z->out_total * 100, z->in_total));
zstd_decode_free(z->zst); z->zst = NULL;
z->in_total = z->out_total = 0;
core->file->close(z->out);
z->out_opened = 0;
z->del_on_close = 0;
z->st = I_IN;
continue;
}
if (!z->out_opened) {
z->out_opened = 1;
zstd_dec_conf zc = {};
zstd_decode_init(&z->zst, &zc);
if (!z->cmd->stdout)
z->oname = out_name(z, z->iname, z->basename);
z->st = I_OUT_OPEN;
continue;
}
z->st = I_DECOMP;
// fallthrough
case I_DECOMP:
r = zst_read(z, &z->zdata, &z->data);
switch (r) {
case 'more':
z->st = I_READ;
continue;
case 'erro':
goto end;
}
z->st = I_WRITE;
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_DECOMP;
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;
unzst_close(z);
core->com->complete(cmd, rc);
}
}
static void unzst_signal(fcom_op *op, uint signal)
{
struct unzst *z = op;
FFINT_WRITEONCE(z->stop, 1);
}
const fcom_operation fcom_op_unzst = {
unzst_create, unzst_close,
unzst_run, unzst_signal,
unzst_help,
};