-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrash.c
367 lines (310 loc) · 7 KB
/
trash.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/** fcom: move files to user's trash directory
2022, Simon Zolin */
static const char* trash_help()
{
return "\
Move files to user's trash directory, plus obfuscation.\n\
Usage:\n\
`fcom trash` INPUT...\n\
\n\
OPTIONS:\n\
`-w`, `--wipe` Overwrite data to hide file content (files-only).\n\
Additionally, set file modification time to 2000-01-01.\n\
`-n`, `--rename` Rename to \"00000000.0000\" before deleting (files-only)\n\
`-f` Delete from disk if moving to Trash has failed (files-only)\n\
";
}
#ifdef _WIN32
#include <util/windows-shell.h>
#endif
#include <fcom.h>
#include <util/unix-shell.h>
#include <ffsys/path.h>
static const fcom_core *core;
struct trash {
fcom_cominfo cominfo;
uint st;
fcom_cominfo *cmd;
fcom_file_obj *in;
ffvec buf;
uint stop;
ffvec names; //char*[]
uint n_trashed, n_deleted;
byte wipe;
byte rename;
};
#define O(member) (void*)FF_OFF(struct trash, member)
static int args_parse(struct trash *t, fcom_cominfo *cmd)
{
static const struct ffarg args[] = {
{ "--rename", '1', O(rename) },
{ "--wipe", '1', O(wipe) },
{ "-n", '1', O(rename) },
{ "-w", '1', O(wipe) },
{}
};
return core->com->args_parse(cmd, args, t, FCOM_COM_AP_INOUT);
}
static void trash_close(fcom_op *op);
static fcom_op* trash_create(fcom_cominfo *cmd)
{
struct trash *t = ffmem_new(struct trash);
t->cmd = cmd;
if (0 != args_parse(t, cmd))
goto end;
struct fcom_file_conf fc = {};
fc.buffer_size = cmd->buffer_size;
t->in = core->file->create(&fc);
return t;
end:
trash_close(t);
return NULL;
}
static void trash_close(fcom_op *op)
{
struct trash *t = op;
core->file->destroy(t->in);
char **p;
FFSLICE_WALK(&t->names, p) {
ffmem_free(*p);
}
ffvec_free(&t->names);
ffvec_free(&t->buf);
ffmem_free(t);
}
/** Fill buffer with pseudo-random data */
static void data_rnd(ffvec *d, uint chunk)
{
FF_ASSERT((chunk & 3) == 0);
char *p = d->ptr;
d->len = ffint_align_ceil(d->len, chunk);
const char *end = d->ptr + d->len;
while (p < end) {
uint rnd = core->random();
*(uint*)p = rnd;
p += 4;
}
}
static void f_wipe_mtime(struct trash *t, fcom_file_obj *f)
{
fftime mtime;
ffdatetime dt = {
.year = 2000,
.month = 1,
.day = 1,
0,
};
fftime_join1(&mtime, &dt);
core->file->mtime_set(f, mtime);
}
static int f_wipe(struct trash *t, const char *fn)
{
int rc = -1;
ffvec d = {};
struct fcom_file_conf conf = {};
fcom_file_obj *f = core->file->create(&conf);
uint flags = FCOM_FILE_WRITE | FCOM_FILE_NO_PREALLOC;
if (t->cmd->directio)
flags |= FCOM_FILE_DIRECTIO;
int r = core->file->open(f, fn, flags);
if (r == FCOM_FILE_ERR) goto end;
fffileinfo fi = {};
r = core->file->info(f, &fi);
if (r == FCOM_FILE_ERR) goto end;
uint64 fsize = fffileinfo_size(&fi);
if (t->cmd->buffer_size == 0)
t->cmd->buffer_size = 64*1024;
t->cmd->buffer_size = ffmax(t->cmd->buffer_size, 4096);
ffvec_alloc(&d, t->cmd->buffer_size, 1);
while ((int64)fsize > 0) {
d.len = ffmin(fsize, d.cap);
data_rnd(&d, 4096);
fsize -= d.len;
if (FFINT_READONCE(t->stop))
goto end;
r = core->file->write(f, *(ffstr*)&d, -1);
if (r == FCOM_FILE_ERR) goto end;
}
f_wipe_mtime(t, f);
core->file->close(f);
fcom_verblog("%s: wiped data", fn);
rc = 0;
end:
ffvec_free(&d);
core->file->destroy(f);
return rc;
}
/** Move files to Trash */
static int f_trash(struct trash *t, const char **names, ffsize names_n)
{
#ifdef FF_LINUX
int e = 0;
for (ffsize i = 0; i != names_n; i++) {
const char *err;
if (0 != ffui_glib_trash(names[i], &err)) {
if (!t->cmd->overwrite) {
fcom_fatlog("%s: can't move file to trash: %s"
, names[i], err);
}
e = 1;
}
}
if (e)
return -1;
#else
if (0 != ffui_file_del(names, names_n, FFUI_FILE_TRASH)) {
if (!t->cmd->overwrite)
fcom_sysfatlog("can't move files to trash");
return -1;
}
#endif
return 0;
}
/** Delete files from disk
Return N of files deleted */
static int f_del(ffvec names)
{
uint n = 0;
const char **namez;
FFSLICE_WALK(&names, namez) {
if (0 != core->file->del(*namez, 0))
n++;
}
if (n != 0) {
fcom_fatlog("%u files were not deleted", n);
}
return names.len - n;
}
/** /path/old -> /path/new */
static ffstr f_new_name(struct trash *t, ffstr old, const char *_new)
{
ffstr path;
if (ffpath_splitpath_str(old, &path, NULL) >= 0)
path.len++;
t->buf.len = 0;
ffvec_addfmt(&t->buf, "%S%s%Z", &path, _new);
t->buf.len--;
return *(ffstr*)&t->buf;
}
static int trash_all(struct trash *t)
{
int r;
if ((r = f_trash(t, t->names.ptr, t->names.len))) {
if (t->cmd->overwrite && !t->cmd->test) {
fcom_verblog("can't move files to trash: error code %d. Trying to delete them.", r);
if (0 == (t->n_deleted = f_del(t->names)))
return -1;
} else {
return -1;
}
} else {
t->n_trashed += t->names.len;
}
if (core->verbose) {
const char **namez;
FFSLICE_WALK(&t->names, namez) {
fcom_verblog("trash: %s", *namez);
}
}
return 0;
}
static int trash_process(struct trash *t)
{
const char **namez;
FFSLICE_WALK(&t->names, namez) {
const char *fn = *namez;
if (t->wipe) {
if (0 != f_wipe(t, fn)) {
return -1;
}
}
if (t->rename) {
ffstr old = FFSTR_INITZ(fn);
ffstr _new = f_new_name(t, old, "00000000.0000");
if (0 != core->file->move(old, _new, FCOM_FILE_MOVE_SAFE)) {
return -1;
}
fn = _new.ptr;
}
if (0 != f_trash(t, &fn, 1)) {
if (t->cmd->overwrite && !t->cmd->test) {
fcom_verblog("%s: can't move to trash. Deleting."
, fn);
if (0 == core->file->del(fn, 0)) {
t->n_deleted++;
}
}
} else {
fcom_verblog("trash: %s", fn);
t->n_trashed++;
}
}
return 0;
}
static void trash_done(struct trash *t)
{
if (core->verbose) {
fcom_verblog("%u files were moved to Trash", t->n_trashed);
if (t->n_deleted != 0) {
fcom_verblog("%u files were deleted", t->n_deleted);
}
}
uint n = t->names.len - (t->n_trashed + t->n_deleted);
if (n != 0) {
fcom_errlog("%u files were skipped", n);
}
}
static void trash_run(fcom_op *op)
{
enum { I_IN, I_ALL, I_OBO, I_DONE };
struct trash *t = op;
int r, rc = 1;
ffstr name;
while (!FFINT_READONCE(t->stop)) {
switch (t->st) {
case 0: {
if (0 > (r = core->com->input_next(t->cmd, &name, NULL, 0))) {
if (r == FCOM_COM_RINPUT_NOMORE) {
t->st = I_ALL;
if (t->wipe || t->rename)
t->st = I_OBO;
continue;
}
goto end;
}
char **p = ffvec_pushT(&t->names, char*);
*p = ffsz_dupstr(&name);
continue;
}
case I_ALL:
if (trash_all(t)) goto end;
t->st = I_DONE;
continue;
case I_OBO:
if (trash_process(t)) goto end;
t->st = I_DONE;
continue;
case I_DONE:
trash_done(t);
rc = 0;
goto end;
}
}
end:
{
fcom_cominfo *cmd = t->cmd;
trash_close(t);
core->com->complete(cmd, rc);
}
}
static void trash_signal(fcom_op *op, uint signal)
{
struct trash *t = op;
FFINT_WRITEONCE(t->stop, 1);
}
static const fcom_operation fcom_op_trash = {
trash_create, trash_close,
trash_run, trash_signal,
trash_help,
};
FCOM_MOD_DEFINE(trash, fcom_op_trash, core)