Debian Codegraph @

src/job/fm-file-ops-job-delete.c

raw
1 /*
2 * fm-file-ops-job-delete.c
3 *
4 * Copyright 2009 Hong Jen Yee (PCMan) <[email protected]>
5 * Copyright 2012-2016 Andriy Grytsenko (LStranger) <[email protected]>
6 *
7 * This file is a part of the Libfm library.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "fm-file-ops-job-delete.h"
29 #include "fm-file-ops-job-xfer.h"
30 #include "fm-config.h"
31 #include "fm-file.h"
32 #include <glib/gi18n-lib.h>
33
34 static const char query[] = G_FILE_ATTRIBUTE_STANDARD_TYPE","
35 G_FILE_ATTRIBUTE_STANDARD_NAME","
36 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME;
37
38
39 gboolean _fm_file_ops_job_delete_file(FmJob* job, GFile* gf, GFileInfo* inf,
40 FmFolder *folder, gboolean only_empty)
41 {
42 GError* err = NULL;
43 FmFileOpsJob* fjob = FM_FILE_OPS_JOB(job);
44 gboolean is_dir, is_trash_root = FALSE, ok;
45 GFileInfo* _inf = NULL;
46 FmPath *path;
47 FmJobErrorAction act;
48
49 while(!inf)
50 {
51 _inf = inf = g_file_query_info(gf, query,
52 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
53 fm_job_get_cancellable(job), &err);
54 if(_inf)
55 break;
56 act = fm_job_emit_error(job, err, FM_JOB_ERROR_MODERATE);
57 g_error_free(err);
58 err = NULL;
59 if(act == FM_JOB_ABORT)
60 return FALSE;
61 if(act != FM_JOB_RETRY)
62 break;
63 }
64 if(!inf)
65 {
66 /* use basename of GFile as display name. */
67 char* basename = g_file_get_basename(gf);
68 char* disp = basename ? g_filename_display_name(basename) : NULL;
69 g_free(basename);
70 /* FIXME: translate it */
71 fm_file_ops_job_emit_cur_file(fjob, disp ? disp : "(invalid file)");
72 g_free(disp);
73 ++fjob->finished;
74 return FALSE;
75 }
76
77 /* currently processed file. */
78 fm_file_ops_job_emit_cur_file(fjob, g_file_info_get_display_name(inf));
79
80 /* show progress */
81 ++fjob->finished;
82 fm_file_ops_job_emit_percent(fjob);
83
84 is_dir = (g_file_info_get_file_type(inf)==G_FILE_TYPE_DIRECTORY);
85
86 if(_inf)
87 g_object_unref(_inf);
88
89 if( fm_job_is_cancelled(job) )
90 return FALSE;
91
92 if(is_dir)
93 {
94 /* special handling for trash:/// */
95 if(!g_file_is_native(gf))
96 {
97 char* scheme = g_file_get_uri_scheme(gf);
98 if(g_strcmp0(scheme, "trash") == 0)
99 {
100 /* little trick: basename of trash root is /. */
101 char* basename = g_file_get_basename(gf);
102 if(basename && basename[0] == G_DIR_SEPARATOR)
103 is_trash_root = TRUE;
104 g_free(basename);
105 }
106 g_free(scheme);
107 }
108 }
109
110 while(!fm_job_is_cancelled(job))
111 {
112 if(g_file_delete(gf, fm_job_get_cancellable(job), &err))
113 {
114 if (folder)
115 {
116 path = fm_path_new_for_gfile(gf);
117 _fm_folder_event_file_deleted(folder, path);
118 fm_path_unref(path);
119 }
120 return TRUE;
121 }
122 if(err)
123 {
124 /* if it's non-empty dir then descent into it then try again */
125 /* trash root gives G_IO_ERROR_PERMISSION_DENIED */
126 if(is_trash_root || /* FIXME: need to refactor this! */
127 (is_dir && !only_empty &&
128 err->domain == G_IO_ERROR && err->code == G_IO_ERROR_NOT_EMPTY))
129 {
130 GFileEnumerator* enu;
131 FmFolder *sub_folder;
132
133 g_error_free(err);
134 err = NULL;
135 enu = g_file_enumerate_children(gf, query,
136 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
137 fm_job_get_cancellable(job), &err);
138 if(!enu)
139 {
140 fm_job_emit_error(job, err, FM_JOB_ERROR_MODERATE);
141 g_error_free(err);
142 return FALSE;
143 }
144
145 path = fm_path_new_for_gfile(gf);
146 sub_folder = fm_folder_find_by_path(path);
147 fm_path_unref(path);
148 while( ! fm_job_is_cancelled(job) )
149 {
150 inf = g_file_enumerator_next_file(enu, fm_job_get_cancellable(job), &err);
151 if(inf)
152 {
153 GFile* sub = g_file_get_child(gf, g_file_info_get_name(inf));
154 ok = _fm_file_ops_job_delete_file(job, sub, inf, sub_folder, FALSE);
155 g_object_unref(sub);
156 g_object_unref(inf);
157 if (!ok) /* stop the job if error happened */
158 goto _failed;
159 }
160 else
161 {
162 if(err)
163 {
164 fm_job_emit_error(job, err, FM_JOB_ERROR_MODERATE);
165 /* FM_JOB_RETRY is not supported here */
166 g_error_free(err);
167 _failed:
168 g_object_unref(enu);
169 if (sub_folder)
170 g_object_unref(sub_folder);
171 return FALSE;
172 }
173 else /* EOF */
174 break;
175 }
176 }
177 g_object_unref(enu);
178 if (sub_folder)
179 g_object_unref(sub_folder);
180
181 is_trash_root = FALSE; /* don't go here again! */
182 is_dir = FALSE;
183 continue;
184 }
185 else if (is_dir && only_empty)
186 {
187 /* special case: trying delete directory with skipped files
188 see _fm_file_ops_job_copy_file() for details */
189 g_error_free(err);
190 return TRUE;
191 }
192 if(err->domain == G_IO_ERROR && err->code == G_IO_ERROR_PERMISSION_DENIED)
193 {
194 /* special case for trash:/// */
195 /* FIXME: is there any better way to handle this? */
196 char* scheme = g_file_get_uri_scheme(gf);
197 if(g_strcmp0(scheme, "trash") == 0)
198 {
199 g_free(scheme);
200 g_error_free(err);
201 return TRUE;
202 }
203 g_free(scheme);
204 }
205 act = fm_job_emit_error(job, err, FM_JOB_ERROR_MODERATE);
206 g_error_free(err);
207 err = NULL;
208 if(act != FM_JOB_RETRY)
209 return FALSE;
210 }
211 else
212 return FALSE;
213 }
214
215 return FALSE;
216 }
217
218
219 gboolean _fm_file_ops_job_delete_run(FmFileOpsJob* job)
220 {
221 GList* l;
222 gboolean ret = TRUE;
223 /* prepare the job, count total work needed with FmDeepCountJob */
224 FmDeepCountJob* dc = fm_deep_count_job_new(job->srcs, FM_DC_JOB_PREPARE_DELETE);
225 FmJob* fmjob = FM_JOB(job);
226 FmPath *path, *parent = NULL;
227 FmFolder *parent_folder = NULL;
228
229 /* let the deep count job share the same cancellable */
230 fm_job_set_cancellable(FM_JOB(dc), fm_job_get_cancellable(fmjob));
231 fm_job_run_sync(FM_JOB(dc));
232 job->total = dc->count;
233 g_object_unref(dc);
234
235 if(fm_job_is_cancelled(fmjob))
236 {
237 g_debug("delete job is cancelled");
238 return FALSE;
239 }
240
241 g_debug("total number of files to delete: %llu", (long long unsigned int)job->total);
242
243 fm_file_ops_job_emit_prepared(job);
244
245 l = fm_path_list_peek_head_link(job->srcs);
246 for(; ! fm_job_is_cancelled(fmjob) && l;l=l->next)
247 {
248 GFile* src;
249
250 path = FM_PATH(l->data);
251 if (fm_path_get_parent(path) != parent && fm_path_get_parent(path) != NULL)
252 {
253 FmFolder *pf = fm_folder_find_by_path(fm_path_get_parent(path));
254 if (pf != parent_folder)
255 {
256 if (parent_folder)
257 {
258 fm_folder_unblock_updates(parent_folder);
259 g_object_unref(parent_folder);
260 }
261 if (pf)
262 fm_folder_block_updates(pf);
263 parent_folder = pf;
264 }
265 else if (pf)
266 g_object_unref(pf);
267 }
268 parent = fm_path_get_parent(path);
269 src = fm_path_to_gfile(path);
270
271 ret = _fm_file_ops_job_delete_file(fmjob, src, NULL, parent_folder, FALSE);
272 g_object_unref(src);
273 }
274 if (parent_folder)
275 {
276 fm_folder_unblock_updates(parent_folder);
277 g_object_unref(parent_folder);
278 }
279 return ret;
280 }
281
282 gboolean _fm_file_ops_job_trash_run(FmFileOpsJob* job)
283 {
284 gboolean ret = TRUE;
285 GList* l;
286 FmPathList* unsupported = fm_path_list_new();
287 GError* err = NULL;
288 FmJob* fmjob = FM_JOB(job);
289 FmPath *path, *parent = NULL;
290 FmFolder *parent_folder = NULL;
291
292 g_debug("total number of files to delete: %u", fm_path_list_get_length(job->srcs));
293 job->total = fm_path_list_get_length(job->srcs);
294
295 fm_file_ops_job_emit_prepared(job);
296
297 /* FIXME: we shouldn't trash a file already in trash:/// */
298
299 l = fm_path_list_peek_head_link(job->srcs);
300 for(; !fm_job_is_cancelled(fmjob) && l;l=l->next)
301 {
302 GFile* gf = fm_path_to_gfile(FM_PATH(l->data));
303 GFileInfo* inf;
304
305 path = FM_PATH(l->data);
306 if (fm_path_get_parent(path) != parent && fm_path_get_parent(path) != NULL)
307 {
308 FmFolder *pf = fm_folder_find_by_path(fm_path_get_parent(path));
309 if (pf != parent_folder)
310 {
311 if (parent_folder)
312 {
313 fm_folder_unblock_updates(parent_folder);
314 g_object_unref(parent_folder);
315 }
316 if (pf)
317 fm_folder_block_updates(pf);
318 parent_folder = pf;
319 }
320 else if (pf)
321 g_object_unref(pf);
322 }
323 parent = fm_path_get_parent(path);
324 _retry_trash:
325 inf = g_file_query_info(gf, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0,
326 fm_job_get_cancellable(fmjob), &err);
327 if(inf)
328 {
329 /* currently processed file. */
330 fm_file_ops_job_emit_cur_file(job, g_file_info_get_display_name(inf));
331 g_object_unref(inf);
332 }
333 else
334 {
335 char* basename = g_file_get_basename(gf);
336 char* disp = basename ? g_filename_display_name(basename) : NULL;
337 g_free(basename);
338 ret = FALSE;
339 /* FIXME: translate it */
340 fm_file_ops_job_emit_cur_file(job, disp ? disp : "(invalid file)");
341 g_free(disp);
342 goto _on_error;
343 }
344 ret = FALSE;
345 if(fm_config->no_usb_trash)
346 {
347 GMount *mnt = g_file_find_enclosing_mount(gf, NULL, &err);
348
349 if(mnt)
350 {
351 ret = g_mount_can_unmount(mnt); /* TRUE if it's removable media */
352 g_object_unref(mnt);
353 if(ret)
354 fm_path_list_push_tail(unsupported, FM_PATH(l->data));
355 }
356 else
357 {
358 g_error_free(err);
359 err = NULL;
360 }
361 }
362
363 if(!ret)
364 {
365 ret = g_file_trash(gf, fm_job_get_cancellable(fmjob), &err);
366 if (ret && parent_folder)
367 _fm_folder_event_file_deleted(parent_folder, path);
368 /* FIXME: signal trash:/// that file added there */
369 }
370 if(!ret)
371 {
372 _on_error:
373 /* if trashing is not supported by the file system */
374 if( err->domain == G_IO_ERROR && err->code == G_IO_ERROR_NOT_SUPPORTED)
375 fm_path_list_push_tail(unsupported, FM_PATH(l->data));
376 else
377 {
378 FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MODERATE);
379 g_error_free(err);
380 err = NULL;
381 if(act == FM_JOB_RETRY)
382 goto _retry_trash;
383 else if(act == FM_JOB_ABORT)
384 {
385 g_object_unref(gf);
386 fm_path_list_unref(unsupported);
387 return FALSE;
388 }
389 }
390 g_error_free(err);
391 err = NULL;
392 }
393 g_object_unref(gf);
394 ++job->finished;
395 fm_file_ops_job_emit_percent(job);
396 }
397 if (parent_folder)
398 {
399 fm_folder_unblock_updates(parent_folder);
400 g_object_unref(parent_folder);
401 }
402
403 /* these files cannot be trashed due to lack of support from
404 * underlying file systems. */
405 if(fm_path_list_is_empty(unsupported))
406 fm_path_list_unref(unsupported);
407 else
408 {
409 /* FIXME: this is a dirty hack to fallback to delete if trash is not available.
410 * The API must be re-designed later. */
411 g_object_set_data_full(G_OBJECT(job), "trash-unsupported", unsupported, (GDestroyNotify)fm_path_list_unref);
412 }
413 return TRUE;
414 }
415
416 static const char trash_query[]=
417 G_FILE_ATTRIBUTE_STANDARD_TYPE","
418 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME","
419 G_FILE_ATTRIBUTE_STANDARD_NAME","
420 G_FILE_ATTRIBUTE_STANDARD_IS_VIRTUAL","
421 G_FILE_ATTRIBUTE_STANDARD_SIZE","
422 G_FILE_ATTRIBUTE_UNIX_BLOCKS","
423 G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE","
424 G_FILE_ATTRIBUTE_ID_FILESYSTEM","
425 "trash::orig-path";
426
427 static gboolean ensure_parent_dir(FmJob* job, GFile* orig_path)
428 {
429 GFile* parent = g_file_get_parent(orig_path);
430 gboolean ret = g_file_query_exists(parent, fm_job_get_cancellable(job));
431 if(!ret)
432 {
433 GError* err = NULL;
434 _retry_mkdir:
435 if(!g_file_make_directory_with_parents(parent, fm_job_get_cancellable(job), &err))
436 {
437 if(!fm_job_is_cancelled(job))
438 {
439 FmJobErrorAction act = fm_job_emit_error(job, err, FM_JOB_ERROR_MODERATE);
440 g_error_free(err);
441 err = NULL;
442 if(act == FM_JOB_RETRY)
443 goto _retry_mkdir;
444 }
445 }
446 else
447 ret = TRUE;
448 }
449 g_object_unref(parent);
450 return ret;
451 }
452
453 gboolean _fm_file_ops_job_untrash_run(FmFileOpsJob* job)
454 {
455 gboolean ret = TRUE;
456 GList* l;
457 GError* err = NULL;
458 FmJob* fmjob = FM_JOB(job);
459 job->total = fm_path_list_get_length(job->srcs);
460 fm_file_ops_job_emit_prepared(job);
461
462 l = fm_path_list_peek_head_link(job->srcs);
463 for(; !fm_job_is_cancelled(fmjob) && l;l=l->next)
464 {
465 GFile* gf;
466 GFileInfo* inf;
467 FmPath* path = FM_PATH(l->data);
468 if(!fm_path_is_trash(path))
469 continue;
470 gf = fm_path_to_gfile(path);
471 _retry_get_orig_path:
472 inf = g_file_query_info(gf, trash_query, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, fm_job_get_cancellable(fmjob), &err);
473 if(inf)
474 {
475 const char* orig_path_str = g_file_info_get_attribute_byte_string(inf, "trash::orig-path");
476 fm_file_ops_job_emit_cur_file(job, g_file_info_get_display_name(inf));
477
478 if(orig_path_str)
479 {
480 /* FIXME: what if orig_path_str is a relative path?
481 * This is actually allowed by the horrible trash spec. */
482 GFile* orig_path = fm_file_new_for_commandline_arg(orig_path_str);
483 FmFolder *src_folder = fm_folder_find_by_path(fm_path_get_parent(path));
484 FmPath *orig_fm_path = fm_path_new_for_gfile(orig_path);
485 FmFolder *dst_folder = fm_folder_find_by_path(fm_path_get_parent(orig_fm_path));
486 fm_path_unref(orig_fm_path);
487 /* ensure the existence of parent folder. */
488 if(ensure_parent_dir(fmjob, orig_path))
489 ret = _fm_file_ops_job_move_file(job, gf, inf, orig_path, path, src_folder, dst_folder);
490 if (src_folder)
491 g_object_unref(src_folder);
492 if (dst_folder)
493 g_object_unref(dst_folder);
494 g_object_unref(orig_path);
495 }
496 else
497 {
498 FmJobErrorAction act;
499
500 g_set_error(&err, G_IO_ERROR, G_IO_ERROR_FAILED,
501 _("Cannot untrash file '%s': original path not known"),
502 g_file_info_get_display_name(inf));
503 act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MODERATE);
504 g_clear_error(&err);
505 if(act == FM_JOB_ABORT)
506 {
507 g_object_unref(inf);
508 g_object_unref(gf);
509 return FALSE;
510 }
511 }
512 g_object_unref(inf);
513 }
514 else
515 {
516 char* basename = g_file_get_basename(gf);
517 char* disp = basename ? g_filename_display_name(basename) : NULL;
518 g_free(basename);
519 /* FIXME: translate it */
520 fm_file_ops_job_emit_cur_file(job, disp ? disp : "(invalid file)");
521 g_free(disp);
522
523 if(err)
524 {
525 FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MODERATE);
526 g_error_free(err);
527 err = NULL;
528 if(act == FM_JOB_RETRY)
529 goto _retry_get_orig_path;
530 else if(act == FM_JOB_ABORT)
531 {
532 g_object_unref(gf);
533 return FALSE;
534 }
535 }
536 }
537 g_object_unref(gf);
538 ++job->finished;
539 fm_file_ops_job_emit_percent(job);
540 }
541
542 return ret;
543 }