-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgz.c
287 lines (238 loc) · 5.7 KB
/
gz.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
/** fcom: pack file into .gz
2023, Simon Zolin */
static const char* gz_help()
{
return "\
Compress file into .gz.\n\
Usage:\n\
`fcom gz` INPUT [OPTIONS] [-o OUTPUT.gz]\n\
\n\
OPTIONS:\n\
`-l`, `--level` INT Compression level: 1..9; default:6\n\
";
}
#include <fcom.h>
#include <ffsys/globals.h>
#include <ffsys/path.h>
#include <ffpack/gzwrite.h>
const fcom_core *core;
struct gz {
fcom_cominfo cominfo;
uint st;
fcom_cominfo *cmd;
ffstr iname, basename;
char *oname;
fcom_file_obj *in, *out;
ffgzwrite gz;
ffstr data, zdata;
ffvec buf;
uint stop;
uint del_on_close :1;
uint64 in_total, out_total;
uint level;
};
#define O(member) (void*)FF_OFF(struct gz, member)
static int args_parse(struct gz *z, fcom_cominfo *cmd)
{
z->level = 6;
static const struct ffarg args[] = {
{ "--level", 'u', O(level) },
{ "-l", 'u', O(level) },
{}
};
int r = core->com->args_parse(cmd, args, z, FCOM_COM_AP_INOUT);
if (r != 0)
return r;
return 0;
}
#undef O
static void gz_close(fcom_op *op)
{
struct gz *z = op;
ffgzwrite_destroy(&z->gz);
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* gz_create(fcom_cominfo *cmd)
{
struct gz *z = ffmem_new(struct gz);
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:
gz_close(z);
return NULL;
}
/** Prepare output file name
idir/iname -> idir/iname.gz
idir/iname -> odir/iname.gz (--chdir) */
static char* out_name(struct gz *z, ffstr in, ffstr base)
{
char *ofn = NULL;
if (z->cmd->output.len != 0) {
ofn = ffsz_dup(z->cmd->output.ptr);
} else if (z->cmd->chdir.len != 0) {
ffstr name;
if (base.len == 0)
base = in;
ffpath_splitpath(base.ptr, base.len, NULL, &name);
if (name.len != 0)
ffstr_shift(&in, name.ptr - base.ptr);
ofn = ffsz_allocfmt("%S%c%S.gz"
, &z->cmd->chdir, FFPATH_SLASH, &in);
} else {
ofn = ffsz_allocfmt("%S.gz", &in);
}
fcom_dbglog("output file name: %s", ofn);
return ofn;
}
static int gzcomp(struct gz *z, ffstr *in, ffstr *out)
{
ffsize n = in->len;
int r = ffgzwrite_process(&z->gz, in, out);
z->in_total += n - in->len;
switch ((enum FFGZWRITE_R)r) {
case FFGZWRITE_DATA:
z->out_total += out->len;
return 'data';
case FFGZWRITE_DONE:
fcom_verblog("%U => %U (%u%%)"
, z->in_total, z->out_total, (uint)FFINT_DIVSAFE(z->out_total * 100, z->in_total));
return 'done';
case FFGZWRITE_MORE:
return 'more';
case FFGZWRITE_ERROR:
fcom_errlog("ffgzwrite_process: %s", ffgzwrite_error(&z->gz));
return 'erro';
}
return 'erro';
}
static void gz_run(fcom_op *op)
{
struct gz *z = op;
int r, rc = 1;
enum { I_IN, I_IN_OPEN, I_OUT_OPEN, I_READ, I_COMP, 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;
ffgzwrite_conf gzconf = {};
gzconf.deflate_level = z->level;
gzconf.deflate_mem = 256;
ffpath_splitpath_str(z->iname, NULL, &gzconf.name);
gzconf.mtime = fffileinfo_mtime(&fi).sec;
if (0 != ffgzwrite_init(&z->gz, &gzconf)) {
fcom_errlog("ffgzwrite_init: %s", ffgzwrite_error(&z->gz));
goto end;
}
if (!z->cmd->stdout) {
if (NULL == (z->oname = out_name(z, z->iname, z->basename)))
goto end;
}
z->st = I_OUT_OPEN;
}
// fallthrough
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;
z->st = I_READ;
}
// fallthrough
case I_READ:
r = core->file->read(z->in, &z->data, -1);
if (r == FCOM_FILE_ERR) goto end;
if (r == FCOM_FILE_ASYNC) {
core->com->async(z->cmd);
return;
}
if (r == FCOM_FILE_EOF)
ffgzwrite_finish(&z->gz);
z->st = I_COMP;
// fallthrough
case I_COMP:
r = gzcomp(z, &z->data, &z->zdata);
switch (r) {
case 'erro':
goto end;
case 'more':
z->st = I_READ;
continue;
case 'done':
z->del_on_close = 0;
z->st = I_IN;
continue;
}
z->st = I_WRITE;
// fallthrough
case I_WRITE:
r = core->file->write(z->out, z->zdata, -1);
if (r == FCOM_FILE_ERR) goto end;
if (r == FCOM_FILE_ASYNC) {
core->com->async(z->cmd);
return;
}
z->st = I_COMP;
continue;
}
}
end:
{
fcom_cominfo *cmd = z->cmd;
gz_close(z);
core->com->complete(cmd, rc);
}
}
static void gz_signal(fcom_op *op, uint signal)
{
struct gz *z = op;
FFINT_WRITEONCE(z->stop, 1);
}
static const fcom_operation fcom_op_gz = {
gz_create, gz_close,
gz_run, gz_signal,
gz_help,
};
static void gz_init(const fcom_core *_core) { core = _core; }
static void gz_destroy() {}
extern const fcom_operation fcom_op_ungz;
static const fcom_operation* gz_provide_op(const char *name)
{
if (ffsz_eq(name, "gz"))
return &fcom_op_gz;
else if (ffsz_eq(name, "ungz"))
return &fcom_op_ungz;
return NULL;
}
FF_EXPORT const struct fcom_module fcom_module = {
FCOM_VER, FCOM_CORE_VER,
gz_init, gz_destroy, gz_provide_op,
};