Debian Codegraph @

src/job/fm-dir-list-job.c

raw
1 /*
2 * fm-dir-list-job.c
3 *
4 * Copyright 2009 PCMan <[email protected]>
5 * Copyright 2013-2015 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 /**
25 * SECTION:fm-dir-list-job
26 * @short_description: Job to get listing of directory.
27 * @title: FmDirListJob
28 *
29 * @include: libfm/fm.h
30 *
31 * The #FmDirListJob can be used to gather list of #FmFileInfo that some
32 * directory contains.
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "fm-dir-list-job.h"
40 #include <glib/gi18n-lib.h>
41 #include <gio/gio.h>
42 #include <string.h>
43 #include <glib/gstdio.h>
44 #include "fm-mime-type.h"
45 #include "fm-file-info-job.h"
46 #include "glib-compat.h"
47
48 #include "fm-file-info.h"
49
50 enum {
51 FILES_FOUND,
52 N_SIGNALS
53 };
54
55 static void fm_dir_list_job_dispose (GObject *object);
56 G_DEFINE_TYPE(FmDirListJob, fm_dir_list_job, FM_TYPE_JOB);
57
58 static int signals[N_SIGNALS];
59
60 static gboolean fm_dir_list_job_run(FmJob *job);
61 static void fm_dir_list_job_finished(FmJob* job);
62
63 static gboolean emit_found_files(gpointer user_data);
64
65 static void fm_dir_list_job_class_init(FmDirListJobClass *klass)
66 {
67 GObjectClass *g_object_class;
68 FmJobClass* job_class = FM_JOB_CLASS(klass);
69 g_object_class = G_OBJECT_CLASS(klass);
70 g_object_class->dispose = fm_dir_list_job_dispose;
71 /* use finalize from parent class */
72
73 job_class->run = fm_dir_list_job_run;
74 job_class->finished = fm_dir_list_job_finished;
75
76 /**
77 * FmDirListJob::files-found
78 * @job: a job that emitted the signal
79 * @files: (element-type FmFileInfo): #GSList of found files
80 *
81 * The #FmDirListJob::files-found signal is emitted for every file
82 * found during directory listing. By default the signal is not
83 * emitted for performance reason. This can be turned on by calling
84 * fm_dir_list_job_set_incremental().
85 *
86 * Since: 1.0.2
87 */
88 signals[FILES_FOUND] =
89 g_signal_new("files-found",
90 G_TYPE_FROM_CLASS(klass),
91 G_SIGNAL_RUN_LAST,
92 G_STRUCT_OFFSET(FmDirListJobClass, files_found),
93 NULL, NULL,
94 g_cclosure_marshal_VOID__POINTER,
95 G_TYPE_NONE, 1, G_TYPE_POINTER);
96
97 }
98
99
100 static void fm_dir_list_job_init(FmDirListJob *job)
101 {
102 job->files = fm_file_info_list_new();
103 fm_job_init_cancellable(FM_JOB(job));
104 }
105
106 /**
107 * fm_dir_list_job_new
108 * @path: path to directory to get listing
109 * @dir_only: %TRUE to include only directories in the list
110 *
111 * Creates a new #FmDirListJob for directory listing. If @dir_only is
112 * %TRUE then objects other than directories will be omitted from the
113 * listing.
114 *
115 * Returns: (transfer full): a new #FmDirListJob object.
116 *
117 * Since: 0.1.0
118 *
119 * Deprecated: 1.2.0: Use fm_dir_list_job_new2() instead.
120 */
121 FmDirListJob* fm_dir_list_job_new(FmPath* path, gboolean dir_only)
122 {
123 FmDirListJob* job = (FmDirListJob*)g_object_new(FM_TYPE_DIR_LIST_JOB, NULL);
124 job->dir_path = fm_path_ref(path);
125 job->flags = dir_only ? FM_DIR_LIST_JOB_DIR_ONLY : FM_DIR_LIST_JOB_FAST;
126 return job;
127 }
128
129 /**
130 * fm_dir_list_job_new2
131 * @path: path to directory to get listing
132 * @flags: listing output mode for new job
133 *
134 * Creates a new #FmDirListJob for directory listing.
135 *
136 * Returns: (transfer full): a new #FmDirListJob object.
137 *
138 * Since: 1.2.0
139 */
140 FmDirListJob *fm_dir_list_job_new2(FmPath *path, FmDirListJobFlags flags)
141 {
142 FmDirListJob* job = (FmDirListJob*)g_object_new(FM_TYPE_DIR_LIST_JOB, NULL);
143 job->dir_path = fm_path_ref(path);
144 job->flags = flags;
145 return job;
146 }
147
148 /**
149 * fm_dir_list_job_new_for_gfile
150 * @gf: descriptor of directory to get listing
151 *
152 * Creates a new #FmDirListJob for listing of directory @gf.
153 *
154 * Returns: (transfer full): a new #FmDirListJob object.
155 *
156 * Since: 0.1.0
157 */
158 FmDirListJob* fm_dir_list_job_new_for_gfile(GFile* gf)
159 {
160 /* FIXME: should we cache this with hash table? Or, the cache
161 * should be done at the level of FmFolder instead? */
162 FmDirListJob* job = (FmDirListJob*)g_object_new(FM_TYPE_DIR_LIST_JOB, NULL);
163 job->dir_path = fm_path_new_for_gfile(gf);
164 return job;
165 }
166
167 static void fm_dir_list_job_dispose(GObject *object)
168 {
169 FmDirListJob *job;
170
171 g_return_if_fail(object != NULL);
172 g_return_if_fail(FM_IS_DIR_LIST_JOB(object));
173
174 job = (FmDirListJob*)object;
175
176 if(job->dir_path)
177 {
178 fm_path_unref(job->dir_path);
179 job->dir_path = NULL;
180 }
181
182 if(job->dir_fi)
183 {
184 fm_file_info_unref(job->dir_fi);
185 job->dir_fi = NULL;
186 }
187
188 if(job->files)
189 {
190 fm_file_info_list_unref(job->files);
191 job->files = NULL;
192 }
193
194 if(job->delay_add_files_handler)
195 {
196 g_source_remove(job->delay_add_files_handler);
197 job->delay_add_files_handler = 0;
198 g_slist_free_full(job->files_to_add, (GDestroyNotify)fm_file_info_unref);
199 job->files_to_add = NULL;
200 }
201
202 if (G_OBJECT_CLASS(fm_dir_list_job_parent_class)->dispose)
203 (* G_OBJECT_CLASS(fm_dir_list_job_parent_class)->dispose)(object);
204 }
205
206 static inline FmFileInfo *_new_info_for_native_file(FmDirListJob* job, FmPath* path, const char* path_str, GError** err)
207 {
208 FmFileInfo *fi;
209
210 if (fm_job_is_cancelled(FM_JOB(job)))
211 return NULL;
212 if (!(job->flags & FM_DIR_LIST_JOB_DETAILED))
213 return fm_file_info_new_from_native_file(path, path_str, err);
214 fi = fm_file_info_new();
215 fm_file_info_set_path(fi, path);
216 if (fm_file_info_set_from_native_file(fi, path_str, err))
217 return fi;
218 fm_file_info_unref(fi);
219 return NULL;
220 }
221
222 static gboolean fm_dir_list_job_run_posix(FmDirListJob* job)
223 {
224 FmJob* fmjob = FM_JOB(job);
225 FmFileInfo* fi;
226 GError *err = NULL;
227 char* path_str;
228 GDir* dir;
229
230 path_str = fm_path_to_str(job->dir_path);
231
232 fi = _new_info_for_native_file(job, job->dir_path, path_str, NULL);
233 if(fi)
234 {
235 if(! fm_file_info_is_dir(fi))
236 {
237 err = g_error_new(G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
238 _("The specified directory '%s' is not valid"),
239 path_str);
240 fm_file_info_unref(fi);
241 fm_job_emit_error(fmjob, err, FM_JOB_ERROR_CRITICAL);
242 g_error_free(err);
243 g_free(path_str);
244 return FALSE;
245 }
246 job->dir_fi = fi;
247 }
248 else
249 {
250 err = g_error_new(G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
251 _("The specified directory '%s' is not valid"),
252 path_str);
253 fm_job_emit_error(fmjob, err, FM_JOB_ERROR_CRITICAL);
254 g_error_free(err);
255 g_free(path_str);
256 return FALSE;
257 }
258
259 dir = g_dir_open(path_str, 0, &err);
260 if( dir )
261 {
262 const char* name;
263 GString* fpath = g_string_sized_new(4096);
264 int dir_len = strlen(path_str);
265 g_string_append_len(fpath, path_str, dir_len);
266 if(fpath->str[dir_len-1] != '/')
267 {
268 g_string_append_c(fpath, '/');
269 ++dir_len;
270 }
271 while( ! fm_job_is_cancelled(fmjob) && (name = g_dir_read_name(dir)) )
272 {
273 FmPath* new_path;
274 g_string_truncate(fpath, dir_len);
275 g_string_append(fpath, name);
276
277 if(job->flags & FM_DIR_LIST_JOB_DIR_ONLY) /* if we only want directories */
278 {
279 struct stat st;
280 /* FIXME: this results in an additional stat() call, which is inefficient */
281 if(stat(fpath->str, &st) == -1 || !S_ISDIR(st.st_mode))
282 continue;
283 }
284
285 new_path = fm_path_new_child(job->dir_path, name);
286
287 _retry:
288 fi = _new_info_for_native_file(job, new_path, fpath->str, &err);
289 if (fi == NULL && !fm_job_is_cancelled(fmjob)) /* we got a damaged file */
290 {
291 FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD);
292 GFile *gf;
293 GFileInfo *inf;
294 gchar *disp_basename;
295
296 g_error_free(err);
297 err = NULL;
298 if(act == FM_JOB_RETRY)
299 goto _retry;
300 /* bug #3615271: Damaged mountpoint isn't shown
301 let make a simple file info then */
302 inf = g_file_info_new();
303 gf = fm_path_to_gfile(new_path);
304 g_file_info_set_file_type(inf, G_FILE_TYPE_UNKNOWN);
305 g_file_info_set_name(inf, name);
306 disp_basename = g_filename_display_basename(fpath->str);
307 g_file_info_set_display_name(inf, disp_basename);
308 g_free(disp_basename);
309 g_file_info_set_content_type(inf, "inode/x-corrupted");
310 fi = fm_file_info_new_from_g_file_data(gf, inf, new_path);
311 g_object_unref(inf);
312 g_object_unref(gf);
313 }
314 fm_dir_list_job_add_found_file(job, fi);
315 fm_file_info_unref(fi);
316 fm_path_unref(new_path);
317 }
318 g_string_free(fpath, TRUE);
319 g_dir_close(dir);
320 }
321 else
322 {
323 fm_job_emit_error(fmjob, err, FM_JOB_ERROR_CRITICAL);
324 g_error_free(err);
325 }
326 g_free(path_str);
327 return TRUE;
328 }
329
330 static gboolean fm_dir_list_job_run_gio(FmDirListJob* job)
331 {
332 GFileEnumerator *enu;
333 GFileInfo *inf;
334 FmFileInfo* fi;
335 GError *err = NULL;
336 FmJob* fmjob = FM_JOB(job);
337 GFile* gf;
338 const char* query;
339
340 gf = fm_path_to_gfile(job->dir_path);
341 _retry:
342 inf = g_file_query_info(gf, gfile_info_query_attribs, 0, fm_job_get_cancellable(fmjob), &err);
343 if(!inf )
344 {
345 FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MODERATE);
346 g_error_free(err);
347 if( act == FM_JOB_RETRY )
348 {
349 err = NULL;
350 goto _retry;
351 }
352 else
353 {
354 g_object_unref(gf);
355 return FALSE;
356 }
357 }
358
359 if( g_file_info_get_file_type(inf) != G_FILE_TYPE_DIRECTORY)
360 {
361 char *path_str = fm_path_to_str(job->dir_path);
362 err = g_error_new(G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
363 _("The specified directory '%s' is not valid"),
364 path_str);
365 fm_job_emit_error(fmjob, err, FM_JOB_ERROR_CRITICAL);
366 g_free(path_str);
367 g_error_free(err);
368 g_object_unref(gf);
369 g_object_unref(inf);
370 return FALSE;
371 }
372
373 /* check if FS is R/O and set attr. into inf */
374 _fm_file_info_job_update_fs_readonly(gf, inf, NULL, NULL);
375
376 job->dir_fi = fm_file_info_new_from_g_file_data(gf, inf, job->dir_path);
377 g_object_unref(inf);
378
379 if(G_UNLIKELY(job->flags & FM_DIR_LIST_JOB_DIR_ONLY))
380 {
381 query = G_FILE_ATTRIBUTE_STANDARD_TYPE","G_FILE_ATTRIBUTE_STANDARD_NAME","
382 G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN","G_FILE_ATTRIBUTE_STANDARD_IS_BACKUP","
383 G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK","G_FILE_ATTRIBUTE_STANDARD_IS_VIRTUAL","
384 G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME","
385 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME","G_FILE_ATTRIBUTE_STANDARD_ICON","
386 G_FILE_ATTRIBUTE_STANDARD_SIZE","G_FILE_ATTRIBUTE_STANDARD_TARGET_URI","
387 "unix::*,time::*,access::*,id::filesystem";
388 }
389 else
390 query = gfile_info_query_attribs;
391
392 enu = g_file_enumerate_children (gf, query, 0, fm_job_get_cancellable(fmjob), &err);
393 g_object_unref(gf);
394 if(enu)
395 {
396 while( ! fm_job_is_cancelled(fmjob) )
397 {
398 inf = g_file_enumerator_next_file(enu, fm_job_get_cancellable(fmjob), &err);
399 if(inf)
400 {
401 FmPath *dir, *sub;
402 GFile *child;
403 if(G_UNLIKELY(job->flags & FM_DIR_LIST_JOB_DIR_ONLY))
404 {
405 /* FIXME: handle symlinks */
406 if(g_file_info_get_file_type(inf) != G_FILE_TYPE_DIRECTORY)
407 {
408 g_object_unref(inf);
409 continue;
410 }
411 }
412
413 /* virtual folders may return children not within them */
414 dir = fm_path_new_for_gfile(g_file_enumerator_get_container(enu));
415 if (fm_path_equal(job->dir_path, dir))
416 sub = fm_path_new_child(job->dir_path, g_file_info_get_name(inf));
417 else
418 sub = fm_path_new_child(dir, g_file_info_get_name(inf));
419 child = g_file_get_child(g_file_enumerator_get_container(enu),
420 g_file_info_get_name(inf));
421 if (g_file_info_get_file_type(inf) == G_FILE_TYPE_DIRECTORY)
422 /* for dir: check if its FS is R/O and set attr. into inf */
423 _fm_file_info_job_update_fs_readonly(child, inf, NULL, NULL);
424 fi = fm_file_info_new_from_g_file_data(child, inf, sub);
425 fm_path_unref(sub);
426 fm_path_unref(dir);
427 g_object_unref(child);
428 fm_dir_list_job_add_found_file(job, fi);
429 fm_file_info_unref(fi);
430 }
431 else
432 {
433 if(err)
434 {
435 FmJobErrorAction act = fm_job_emit_error(fmjob, err, FM_JOB_ERROR_MILD);
436 g_error_free(err);
437 /* FM_JOB_RETRY is not supported. */
438 if(act == FM_JOB_ABORT)
439 fm_job_cancel(fmjob);
440 }
441 /* otherwise it's EOL */
442 break;
443 }
444 g_object_unref(inf);
445 }
446 g_file_enumerator_close(enu, NULL, &err);
447 g_object_unref(enu);
448 }
449 else
450 {
451 fm_job_emit_error(fmjob, err, FM_JOB_ERROR_CRITICAL);
452 g_error_free(err);
453 return FALSE;
454 }
455 return TRUE;
456 }
457
458 static gboolean fm_dir_list_job_run(FmJob* fmjob)
459 {
460 gboolean ret;
461 FmDirListJob* job = FM_DIR_LIST_JOB(fmjob);
462 g_return_val_if_fail(job->dir_path != NULL, FALSE);
463 if(fm_path_is_native(job->dir_path)) /* if this is a native file on real file system */
464 ret = fm_dir_list_job_run_posix(job);
465 else /* this is a virtual path or remote file system path */
466 ret = fm_dir_list_job_run_gio(job);
467 return ret;
468 }
469
470 static void fm_dir_list_job_finished(FmJob* job)
471 {
472 FmDirListJob* dirlist_job = FM_DIR_LIST_JOB(job);
473 FmJobClass* job_class = FM_JOB_CLASS(fm_dir_list_job_parent_class);
474
475 if(dirlist_job->emit_files_found)
476 {
477 if(dirlist_job->delay_add_files_handler)
478 {
479 g_source_remove(dirlist_job->delay_add_files_handler);
480 emit_found_files(dirlist_job);
481 }
482 }
483 if(job_class->finished)
484 job_class->finished(job);
485 }
486
487 #if 0
488 /**
489 * fm_dir_list_job_get_dir_path
490 * @job: the job that collected listing
491 *
492 * Retrieves the path of the directory being listed.
493 *
494 * Returns: (transfer none): FmPath for the directory.
495 *
496 * Since: 1.0.2
497 */
498 FmPath* fm_dir_list_job_get_dir_path(FmDirListJob* job)
499 {
500 return job->dir_path;
501 }
502
503 /**
504 * fm_dir_list_job_get_dir_info
505 * @job: the job that collected listing
506 *
507 * Retrieves the information of the directory being listed.
508 *
509 * Returns: (transfer none): FmFileInfo for the directory.
510 *
511 * Since: 1.0.2
512 */
513 FmFileInfo* fm_dir_list_job_get_dir_info(FmDirListJob* job)
514 {
515 return job->dir_fi;
516 }
517 #endif
518
519 /**
520 * fm_dir_list_job_get_files
521 * @job: the job that collected listing
522 *
523 * Retrieves gathered listing from the @job. This function may be called
524 * only from #FmJob::finished signal handler. Returned data is owned by
525 * the @job and should be not freed by caller.
526 *
527 * Before 1.0.1 this call had name fm_dir_dist_job_get_files due to typo.
528 *
529 * Returns: (transfer none): list of gathered data.
530 *
531 * Since: 0.1.0
532 */
533 FmFileInfoList* fm_dir_list_job_get_files(FmDirListJob* job)
534 {
535 return job->files;
536 }
537
538 #ifndef FM_DISABLE_DEPRECATED
539 /**
540 * fm_dir_dist_job_get_files
541 * @job: the job that collected listing
542 *
543 * There is a typo in the function name. It should have been
544 * fm_dir_list_job_get_files(). The one with typo is kept here for backward
545 * compatibility and will be removed later.
546 *
547 * Since: 0.1.0
548 *
549 * Deprecated: 1.0.1: Use fm_dir_list_job_get_files() instead.
550 */
551 FmFileInfoList* fm_dir_dist_job_get_files(FmDirListJob* job)
552 {
553 return fm_dir_list_job_get_files(job);
554 }
555 #endif /* FM_DISABLE_DEPRECATED */
556
557 static gboolean emit_found_files(gpointer user_data)
558 {
559 /* this callback is called from the main thread */
560 FmDirListJob* job = FM_DIR_LIST_JOB(user_data);
561 /* g_print("emit_found_files: %d\n", g_slist_length(job->files_to_add)); */
562
563 if(g_source_is_destroyed(g_main_current_source()))
564 return FALSE;
565 g_signal_emit(job, signals[FILES_FOUND], 0, job->files_to_add);
566 g_slist_free_full(job->files_to_add, (GDestroyNotify)fm_file_info_unref);
567 job->files_to_add = NULL;
568 job->delay_add_files_handler = 0;
569 return FALSE;
570 }
571
572 static gpointer queue_add_file(FmJob* fmjob, gpointer user_data)
573 {
574 FmDirListJob* job = FM_DIR_LIST_JOB(fmjob);
575 FmFileInfo* file = FM_FILE_INFO(user_data);
576 /* this callback is called from the main thread */
577 /* g_print("queue_add_file: %s\n", fm_file_info_get_disp_name(file)); */
578 job->files_to_add = g_slist_prepend(job->files_to_add, fm_file_info_ref(file));
579 if(job->delay_add_files_handler == 0)
580 job->delay_add_files_handler = g_timeout_add_seconds_full(G_PRIORITY_LOW,
581 1, emit_found_files, g_object_ref(job), g_object_unref);
582 return NULL;
583 }
584
585 /**
586 * fm_dir_list_job_add_found_file
587 * @job: the job that collected listing
588 * @file: a FmFileInfo of the newly found file
589 *
590 * This API may be called by the classes derived of FmDirListJob only.
591 * Application developers should not use this API.
592 * When a new file is found in the dir being listed, implementations
593 * of FmDirListJob should call this API with the info of the newly found
594 * file. The FmFileInfo will be added to the found file list.
595 *
596 * If emission of the #FmDirListJob::files-found signal is turned on by
597 * fm_dir_list_job_set_incremental(), the signal will be emitted
598 * for the newly found files after several new files are added.
599 * See the document for the signal for more detail.
600 *
601 * Since: 1.0.2
602 */
603 void fm_dir_list_job_add_found_file(FmDirListJob* job, FmFileInfo* file)
604 {
605 fm_file_info_list_push_tail(job->files, file);
606 if(G_UNLIKELY(job->emit_files_found))
607 fm_job_call_main_thread(FM_JOB(job), queue_add_file, file);
608 }
609
610 #if 0
611 /**
612 * fm_dir_list_job_set_dir_path
613 * @job: the job that collected listing
614 * @path: a FmPath of the directory being loaded.
615 *
616 * This API is called by the implementation of FmDirListJob only.
617 * Application developers should not use this API most of the time.
618 *
619 * Since: 1.0.2
620 */
621 void fm_dir_list_job_set_dir_path(FmDirListJob* job, FmPath* path)
622 {
623 if(job->dir_path)
624 fm_path_unref(job->dir_path);
625 job->dir_path = fm_path_ref(path);
626 }
627
628 /**
629 * fm_dir_list_job_set_dir_info
630 * @job: the job that collected listing
631 * @info: a FmFileInfo of the directory being loaded.
632 *
633 * This API is called by the implementation of FmDirListJob only.
634 * Application developers should not use this API most of the time.
635 *
636 * Since: 1.0.2
637 */
638 void fm_dir_list_job_set_dir_info(FmDirListJob* job, FmFileInfo* info)
639 {
640 if(job->dir_fi)
641 fm_file_info_unref(job->dir_fi);
642 job->dir_fi = fm_file_info_ref(info);
643 }
644 #endif
645
646 /**
647 * fm_dir_list_job_set_incremental
648 * @job: the job descriptor
649 * @set: %TRUE if job should send the #FmDirListJob::files-found signal
650 *
651 * Sets whether @job should send the #FmDirListJob::files-found signal
652 * on found files before the @job is finished or not.
653 * This should only be called before the @job is launched.
654 *
655 * Since: 1.0.2
656 */
657 void fm_dir_list_job_set_incremental(FmDirListJob* job, gboolean set)
658 {
659 job->emit_files_found = set;
660 }