-
Notifications
You must be signed in to change notification settings - Fork 45
/
fm-file-ops-job-delete.c
543 lines (500 loc) · 18.3 KB
/
fm-file-ops-job-delete.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
* fm-file-ops-job-delete.c
*
* Copyright 2009 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
* Copyright 2012-2016 Andriy Grytsenko (LStranger) <andrej@rep.kiev.ua>
*
* This file is a part of the Libfm library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "fm-file-ops-job-delete.h"
#include "fm-file-ops-job-xfer.h"
#include "fm-config.h"
#include "fm-file.h"
#include <glib/gi18n-lib.h>
static const char query[] = G_FILE_ATTRIBUTE_STANDARD_TYPE","
G_FILE_ATTRIBUTE_STANDARD_NAME","
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME;
gboolean _fm_file_ops_job_delete_file(FmJob* job, GFile* gf, GFileInfo* inf,
FmFolder *folder, gboolean only_empty)
{
GError* err = NULL;
FmFileOpsJob* fjob = FM_FILE_OPS_JOB(job);
gboolean is_dir, is_trash_root = FALSE, ok;
GFileInfo* _inf = NULL;
FmPath *path;
FmJobErrorAction act;
while(!inf)
{
_inf = inf = g_file_query_info(gf, query,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
fm_job_get_cancellable(job), &err);
if(_inf)
break;
act = fm_job_emit_error(job, err, FM_JOB_ERROR_MODERATE);
g_error_free(err);
err = NULL;
if(act == FM_JOB_ABORT)
return FALSE;
if(act != FM_JOB_RETRY)
break;
}
if(!inf)
{
/* use basename of GFile as display name. */
char* basename = g_file_get_basename(gf);
char* disp = basename ? g_filename_display_name(basename) : NULL;
g_free(basename);
/* FIXME: translate it */
fm_file_ops_job_emit_cur_file(fjob, disp ? disp : "(invalid file)");
g_free(disp);
++fjob->finished;
return FALSE;
}
/* currently processed file. */
fm_file_ops_job_emit_cur_file(fjob, g_file_info_get_display_name(inf));
/* show progress */
++fjob->finished;
fm_file_ops_job_emit_percent(fjob);
is_dir = (g_file_info_get_file_type(inf)==G_FILE_TYPE_DIRECTORY);
if(_inf)
g_object_unref(_inf);
if( fm_job_is_cancelled(job) )
return FALSE;
if(is_dir)
{
/* special handling for trash:/// */
if(!g_file_is_native(gf))
{
char* scheme = g_file_get_uri_scheme(gf);
if(g_strcmp0(scheme, "trash") == 0)
{
/* little trick: basename of trash root is /. */
char* basename = g_file_get_basename(gf);
if(basename && basename[0] == G_DIR_SEPARATOR)
is_trash_root = TRUE;
g_free(basename);
}
g_free(scheme);
}
}
while(!fm_job_is_cancelled(job))
{
if(g_file_delete(gf, fm_job_get_cancellable(job), &err))
{
if (folder)
{
path = fm_path_new_for_gfile(gf);
_fm_folder_event_file_deleted(folder, path);
fm_path_unref(path);
}
return TRUE;
}
if(err)
{
/* if it's non-empty dir then descent into it then try again */
/* trash root gives G_IO_ERROR_PERMISSION_DENIED */
if(is_trash_root || /* FIXME: need to refactor this! */
(is_dir && !only_empty &&
err->domain == G_IO_ERROR && err->code == G_IO_ERROR_NOT_EMPTY))
{
GFileEnumerator* enu;
FmFolder *sub_folder;
g_error_free(err);
err = NULL;
enu = g_file_enumerate_children(gf, query,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
fm_job_get_cancellable(job), &err);
if(!enu)
{
fm_job_emit_error(job, err, FM_JOB_ERROR_MODERATE);
g_error_free(err);
return FALSE;
}
path = fm_path_new_for_gfile(gf);
sub_folder = fm_folder_find_by_path(path);
fm_path_unref(path);
while( ! fm_job_is_cancelled(job) )
{
inf = g_file_enumerator_next_file(enu, fm_job_get_cancellable(job), &err);
if(inf)
{
GFile* sub = g_file_get_child(gf, g_file_info_get_name(inf));
ok = _fm_file_ops_job_delete_file(job, sub, inf, sub_folder, FALSE);
g_object_unref(sub);
g_object_unref(inf);
if (!ok) /* stop the job if error happened */
goto _failed;
}
else
{
if(err)
{
fm_job_emit_error(job, err, FM_JOB_ERROR_MODERATE);
/* FM_JOB_RETRY is not supported here */
g_error_free(err);
_failed:
g_object_unref(enu);
if (sub_folder)
g_object_unref(sub_folder);
return FALSE;
}
else /* EOF */
break;
}
}
g_object_unref(enu);
if (sub_folder)
g_object_unref(sub_folder);
is_trash_root = FALSE; /* don't go here again! */
is_dir = FALSE;
continue;
}
else if (is_dir && only_empty)
{
/* special case: trying delete directory with skipped files
see _fm_file_ops_job_copy_file() for details */
g_error_free(err);
return TRUE;
}
if(err->domain == G_IO_ERROR && err->code == G_IO_ERROR_PERMISSION_DENIED)
{
/* special case for trash:/// */
/* FIXME: is there any better way to handle this? */
char* scheme = g_file_get_uri_scheme(gf);
if(g_strcmp0(scheme, "trash") == 0)
{
g_free(scheme);
g_error_free(err);
return TRUE;
}
g_free(scheme);
}
act = fm_job_emit_error(job, err, FM_JOB_ERROR_MODERATE);
g_error_free(err);
err = NULL;
if(act != FM_JOB_RETRY)
return FALSE;
}
else
return FALSE;
}
return FALSE;
}
gboolean _fm_file_ops_job_delete_run(FmFileOpsJob* job)
{
GList* l;
gboolean ret = TRUE;
/* prepare the job, count total work needed with FmDeepCountJob */
FmDeepCountJob* dc = fm_deep_count_job_new(job->srcs, FM_DC_JOB_PREPARE_DELETE);
FmJob* fmjob = FM_JOB(job);
FmPath *path, *parent = NULL;
FmFolder *parent_folder = NULL;
/* let the deep count job share the same cancellable */
fm_job_set_cancellable(FM_JOB(dc), fm_job_get_cancellable(fmjob));
fm_job_run_sync(FM_JOB(dc));
job->total = dc->count;
g_object_unref(dc);
if(fm_job_is_cancelled(fmjob))
{
g_debug("delete job is cancelled");
return FALSE;
}
g_debug("total number of files to delete: %llu", (long long unsigned int)job->total);
fm_file_ops_job_emit_prepared(job);
l = fm_path_list_peek_head_link(job->srcs);
for(; ! fm_job_is_cancelled(fmjob) && l;l=l->next)
{
GFile* src;
path = FM_PATH(l->data);
if (fm_path_get_parent(path) != parent && fm_path_get_parent(path) != NULL)
{
FmFolder *pf = fm_folder_find_by_path(fm_path_get_parent(path));
if (pf != parent_folder)
{
if (parent_folder)
{
fm_folder_unblock_updates(parent_folder);
g_object_unref(parent_folder);
}
if (pf)
fm_folder_block_updates(pf);
parent_folder = pf;
}
else if (pf)
g_object_unref(pf);
}
parent = fm_path_get_parent(path);
src = fm_path_to_gfile(path);
ret = _fm_file_ops_job_delete_file(fmjob, src, NULL, parent_folder, FALSE);
g_object_unref(src);
}
if (parent_folder)
{
fm_folder_unblock_updates(parent_folder);
g_object_unref(parent_folder);
}
return ret;
}
gboolean _fm_file_ops_job_trash_run(FmFileOpsJob* job)
{
gboolean ret = TRUE;
GList* l;
FmPathList* unsupported = fm_path_list_new();
GError* err = NULL;
FmJob* fmjob = FM_JOB(job);
FmPath *path, *parent = NULL;
FmFolder *parent_folder = NULL;
g_debug("total number of files to delete: %u", fm_path_list_get_length(job->srcs));
job->total = fm_path_list_get_length(job->srcs);
fm_file_ops_job_emit_prepared(job);
/* FIXME: we shouldn't trash a file already in trash:/// */
l = fm_path_list_peek_head_link(job->srcs);
for(; !fm_job_is_cancelled(fmjob) && l;l=l->next)
{
GFile* gf = fm_path_to_gfile(FM_PATH(l->data));
GFileInfo* inf;
path = FM_PATH(l->data);
if (fm_path_get_parent(path) != parent && fm_path_get_parent(path) != NULL)
{
FmFolder *pf = fm_folder_find_by_path(fm_path_get_parent(path));
if (pf != parent_folder)
{
if (parent_folder)
{
fm_folder_unblock_updates(parent_folder);
g_object_unref(parent_folder);
}
if (pf)
fm_folder_block_updates(pf);
parent_folder = pf;
}
else if (pf)
g_object_unref(pf);
}
parent = fm_path_get_parent(path);
_retry_trash:
inf = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0,
fm_job_get_cancellable(fmjob), &err);
if(inf)
{
/* currently processed file. */
fm_file_ops_job_emit_cur_file(job, g_file_info_get_display_name(inf));
g_object_unref(inf);
}
else
{
char* basename = g_file_get_basename(gf);
char* disp = basename ? g_filename_display_name(basename) : NULL;
g_free(basename);
ret = FALSE;
/* FIXME: translate it */
fm_file_ops_job_emit_cur_file(job, disp ? disp : "(invalid file)");
g_free(disp);
goto _on_error;
}
ret = FALSE;
if(fm_config->no_usb_trash)
{
GMount *mnt = g_file_find_enclosing_mount(gf, NULL, &err);
if(mnt)
{
ret = g_mount_can_unmount(mnt); /* TRUE if it's removable media */
g_object_unref(mnt);
if(ret)
fm_path_list_push_tail(unsupported, FM_PATH(l->data));
}
else
{
g_error_free(err);
err = NULL;
}
}
if(!ret)
{
ret = g_file_trash(gf, fm_job_get_cancellable(fmjob), &err);
if (ret && parent_folder)
_fm_folder_event_file_deleted(parent_folder, path);
/* FIXME: signal trash:/// that file added there */
}
if(!ret)
{
_on_error:
/* if trashing is not supported by the file system */
if( err->domain == G_IO_ERROR && err->code == G_IO_ERROR_NOT_SUPPORTED)
fm_path_list_push_tail(unsupported, FM_PATH(l->data));
else
{
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MODERATE);
g_error_free(err);
err = NULL;
if(act == FM_JOB_RETRY)
goto _retry_trash;
else if(act == FM_JOB_ABORT)
{
g_object_unref(gf);
fm_path_list_unref(unsupported);
return FALSE;
}
}
g_error_free(err);
err = NULL;
}
g_object_unref(gf);
++job->finished;
fm_file_ops_job_emit_percent(job);
}
if (parent_folder)
{
fm_folder_unblock_updates(parent_folder);
g_object_unref(parent_folder);
}
/* these files cannot be trashed due to lack of support from
* underlying file systems. */
if(fm_path_list_is_empty(unsupported))
fm_path_list_unref(unsupported);
else
{
/* FIXME: this is a dirty hack to fallback to delete if trash is not available.
* The API must be re-designed later. */
g_object_set_data_full(G_OBJECT(job), "trash-unsupported", unsupported, (GDestroyNotify)fm_path_list_unref);
}
return TRUE;
}
static const char trash_query[]=
G_FILE_ATTRIBUTE_STANDARD_TYPE","
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME","
G_FILE_ATTRIBUTE_STANDARD_NAME","
G_FILE_ATTRIBUTE_STANDARD_IS_VIRTUAL","
G_FILE_ATTRIBUTE_STANDARD_SIZE","
G_FILE_ATTRIBUTE_UNIX_BLOCKS","
G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE","
G_FILE_ATTRIBUTE_ID_FILESYSTEM","
"trash::orig-path";
static gboolean ensure_parent_dir(FmJob* job, GFile* orig_path)
{
GFile* parent = g_file_get_parent(orig_path);
gboolean ret = g_file_query_exists(parent, fm_job_get_cancellable(job));
if(!ret)
{
GError* err = NULL;
_retry_mkdir:
if(!g_file_make_directory_with_parents(parent, fm_job_get_cancellable(job), &err))
{
if(!fm_job_is_cancelled(job))
{
FmJobErrorAction act = fm_job_emit_error(job, err, FM_JOB_ERROR_MODERATE);
g_error_free(err);
err = NULL;
if(act == FM_JOB_RETRY)
goto _retry_mkdir;
}
}
else
ret = TRUE;
}
g_object_unref(parent);
return ret;
}
gboolean _fm_file_ops_job_untrash_run(FmFileOpsJob* job)
{
gboolean ret = TRUE;
GList* l;
GError* err = NULL;
FmJob* fmjob = FM_JOB(job);
job->total = fm_path_list_get_length(job->srcs);
fm_file_ops_job_emit_prepared(job);
l = fm_path_list_peek_head_link(job->srcs);
for(; !fm_job_is_cancelled(fmjob) && l;l=l->next)
{
GFile* gf;
GFileInfo* inf;
FmPath* path = FM_PATH(l->data);
if(!fm_path_is_trash(path))
continue;
gf = fm_path_to_gfile(path);
_retry_get_orig_path:
inf = g_file_query_info(gf, trash_query, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, fm_job_get_cancellable(fmjob), &err);
if(inf)
{
const char* orig_path_str = g_file_info_get_attribute_byte_string(inf, "trash::orig-path");
fm_file_ops_job_emit_cur_file(job, g_file_info_get_display_name(inf));
if(orig_path_str)
{
/* FIXME: what if orig_path_str is a relative path?
* This is actually allowed by the horrible trash spec. */
GFile* orig_path = fm_file_new_for_commandline_arg(orig_path_str);
FmFolder *src_folder = fm_folder_find_by_path(fm_path_get_parent(path));
FmPath *orig_fm_path = fm_path_new_for_gfile(orig_path);
FmFolder *dst_folder = fm_folder_find_by_path(fm_path_get_parent(orig_fm_path));
fm_path_unref(orig_fm_path);
/* ensure the existence of parent folder. */
if(ensure_parent_dir(fmjob, orig_path))
ret = _fm_file_ops_job_move_file(job, gf, inf, orig_path, path, src_folder, dst_folder);
if (src_folder)
g_object_unref(src_folder);
if (dst_folder)
g_object_unref(dst_folder);
g_object_unref(orig_path);
}
else
{
FmJobErrorAction act;
g_set_error(&err, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Cannot untrash file '%s': original path not known"),
g_file_info_get_display_name(inf));
act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MODERATE);
g_clear_error(&err);
if(act == FM_JOB_ABORT)
{
g_object_unref(inf);
g_object_unref(gf);
return FALSE;
}
}
g_object_unref(inf);
}
else
{
char* basename = g_file_get_basename(gf);
char* disp = basename ? g_filename_display_name(basename) : NULL;
g_free(basename);
/* FIXME: translate it */
fm_file_ops_job_emit_cur_file(job, disp ? disp : "(invalid file)");
g_free(disp);
if(err)
{
FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MODERATE);
g_error_free(err);
err = NULL;
if(act == FM_JOB_RETRY)
goto _retry_get_orig_path;
else if(act == FM_JOB_ABORT)
{
g_object_unref(gf);
return FALSE;
}
}
}
g_object_unref(gf);
++job->finished;
fm_file_ops_job_emit_percent(job);
}
return ret;
}