~ubuntu-branches/debian/sid/file-roller/sid

« back to all changes in this revision

Viewing changes to src/gio-utils.c

  • Committer: Package Import Robot
  • Author(s): Michael Biebl, Josselin Mouette, Michael Biebl
  • Date: 2011-10-13 22:43:53 UTC
  • mfrom: (5.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20111013224353-7fub412oa8jwkcgt
Tags: 3.0.2-2
[ Josselin Mouette ]
* file-roller.mime: dropped. We don’t do necromancy anymore.
* Drop desktop-check-mime-types call too.

[ Michael Biebl ]
* Upload to unstable.
* debian/control.in:
  - Set pkg-gnome-maintainers@lists.alioth.debian.org as Maintainer.
* debian/watch:
  - Switch to .xz tarballs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 *  GNU General Public License for more details.
17
17
 *
18
18
 *  You should have received a copy of the GNU General Public License
19
 
 *  along with this program; if not, write to the Free Software
20
 
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
20
 */
22
21
 
23
22
#include <string.h>
127
126
 
128
127
 
129
128
typedef struct {
130
 
        char                 *base_directory;
 
129
        GFile                *base_directory;
131
130
        gboolean              recursive;
132
131
        gboolean              follow_links;
133
132
        StartDirCallback      start_dir_func;
153
152
        if (fec == NULL)
154
153
                return;
155
154
 
156
 
        g_free (fec->base_directory);
 
155
        if (fec->base_directory != NULL)
 
156
                g_object_unref (fec->base_directory);
157
157
        if (fec->current != NULL)
158
158
                g_object_unref (fec->current);
159
159
        if (fec->already_visited)
172
172
        g_source_remove (fec->source_id);
173
173
        if (fec->current != NULL) {
174
174
                g_object_unref (fec->current);
175
 
                 fec->current = NULL;
 
175
                fec->current = NULL;
176
176
        }
177
177
        if (fec->done_func)
178
178
                fec->done_func (fec->error, fec->user_data);
212
212
 
213
213
 
214
214
static void
 
215
for_each_child_set_current_uri (ForEachChildData *fec,
 
216
                                const char       *directory)
 
217
{
 
218
        if (fec->current != NULL)
 
219
                g_object_unref (fec->current);
 
220
        fec->current = g_file_new_for_uri (directory);
 
221
}
 
222
 
 
223
 
 
224
static void
215
225
for_each_child_set_current (ForEachChildData *fec,
216
 
                            const char       *directory)
 
226
                            GFile            *directory)
217
227
{
218
228
        if (fec->current != NULL)
219
229
                g_object_unref (fec->current);
220
 
        fec->current = g_file_new_for_uri (directory);
 
230
        fec->current = g_file_dup (directory);
221
231
}
222
232
 
223
 
 
224
233
static void
225
234
for_each_child_start_next_sub_directory (ForEachChildData *fec)
226
235
{
236
245
        }
237
246
 
238
247
        if (sub_directory != NULL) {
239
 
                for_each_child_set_current (fec, sub_directory);
 
248
                for_each_child_set_current_uri (fec, sub_directory);
240
249
                for_each_child_start (fec);
241
250
        }
242
251
        else
276
285
{
277
286
        ForEachChildData *fec = user_data;
278
287
        GList            *children, *scan;
279
 
        char             *current_directory;
280
288
 
281
289
        children = g_file_enumerator_next_files_finish (fec->enumerator,
282
290
                                                        result,
291
299
                return;
292
300
        }
293
301
 
294
 
        current_directory = g_file_get_uri (fec->current);
295
302
        for (scan = children; scan; scan = scan->next) {
296
303
                GFileInfo *child_info = scan->data;
297
 
                char      *name, *uri;
 
304
                GFile     *f;
 
305
                char      *uri;
298
306
 
299
 
                name = g_uri_escape_string (g_file_info_get_name (child_info), G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT, FALSE);
300
 
                uri = g_strconcat (current_directory, "/", name, NULL);
 
307
                f = g_file_get_child (fec->current, g_file_info_get_name (child_info));
 
308
                uri = g_file_get_uri (f);
301
309
 
302
310
                if (g_file_info_get_file_type (child_info) == G_FILE_TYPE_DIRECTORY) {
303
 
                        /* avoid to visit a directory more than ones */
 
311
                        /* avoid to visit a directory more than once */
304
312
 
305
313
                        if (g_hash_table_lookup (fec->already_visited, uri) == NULL) {
306
314
                                char *sub_directory;
314
322
                fec->for_each_file_func (uri, child_info, fec->user_data);
315
323
 
316
324
                g_free (uri);
317
 
                g_free (name);
 
325
                g_object_unref (f);
318
326
        }
319
 
        g_free (current_directory);
320
327
 
321
328
        g_file_enumerator_next_files_async (fec->enumerator,
322
329
                                            N_FILES_PER_REQUEST,
404
411
 * Each callback uses the same @user_data additional parameter.
405
412
 */
406
413
void
407
 
g_directory_foreach_child (const char           *directory,
 
414
g_directory_foreach_child (GFile                *directory,
408
415
                           gboolean              recursive,
409
416
                           gboolean              follow_links,
410
417
                           GCancellable         *cancellable,
419
426
 
420
427
        fec = g_new0 (ForEachChildData, 1);
421
428
 
422
 
        fec->base_directory = g_strdup (directory);
 
429
        fec->base_directory = g_object_ref (directory);
423
430
        fec->recursive = recursive;
424
431
        fec->follow_links = follow_links;
425
432
        fec->cancellable = cancellable;
443
450
typedef struct {
444
451
        GList             *files;
445
452
        GList             *dirs;
446
 
        char              *directory;
447
 
        char              *base_dir;
 
453
        GFile             *directory;
 
454
        GFile             *base_dir;
448
455
        GCancellable      *cancellable;
449
456
        ListReadyCallback  done_func;
450
457
        gpointer           done_data;
469
476
        path_list_free (gfl->files);
470
477
        path_list_free (gfl->dirs);
471
478
        path_list_free (gfl->to_visit);
472
 
        g_free (gfl->directory);
473
 
        g_free (gfl->base_dir);
 
479
        if (gfl->directory != NULL)
 
480
                g_object_unref (gfl->directory);
 
481
        if (gfl->base_dir != NULL)
 
482
                g_object_unref (gfl->base_dir);
474
483
        g_free (gfl);
475
484
}
476
485
 
479
488
 
480
489
 
481
490
static GList*
482
 
get_relative_file_list (GList      *rel_list,
483
 
                        GList      *file_list,
484
 
                        const char *base_dir)
 
491
get_relative_file_list (GList *rel_list,
 
492
                        GList *file_list,
 
493
                        GFile *base_dir)
485
494
{
486
495
        GList *scan;
487
 
        int    base_len;
488
496
 
489
497
        if (base_dir == NULL)
490
498
                return NULL;
491
499
 
492
 
        base_len = 0;
493
 
        if (strcmp (base_dir, "/") != 0)
494
 
                base_len = strlen (base_dir);
495
 
 
496
500
        for (scan = file_list; scan; scan = scan->next) {
497
 
                char *full_path = scan->data;
 
501
                char  *full_path = scan->data;
 
502
                GFile *f;
 
503
                char  *relative_path;
498
504
 
499
 
                if (path_in_path (base_dir, full_path)) {
500
 
                        char *rel_path = g_uri_unescape_string (full_path + base_len + 1, NULL);
501
 
                        rel_list = g_list_prepend (rel_list, rel_path);
502
 
                }
 
505
                f = g_file_new_for_commandline_arg (full_path);
 
506
                relative_path = g_file_get_relative_path (base_dir, f);
 
507
                if (relative_path != NULL)
 
508
                        rel_list = g_list_prepend (rel_list, relative_path);
 
509
                g_object_unref (f);
503
510
        }
504
511
 
505
512
        return rel_list;
565
572
        GetFileListData *gfl = user_data;
566
573
        GHashTable      *h_dirs;
567
574
        GList           *scan;
 
575
        char            *uri;
568
576
 
569
577
        gfl->files = g_list_reverse (gfl->files);
570
578
        gfl->dirs = g_list_reverse (gfl->dirs);
582
590
        if (gfl->base_dir != NULL) {
583
591
                char *dir;
584
592
 
585
 
                dir = g_strdup (gfl->base_dir);
 
593
                dir = g_file_get_uri (gfl->base_dir);
586
594
                gfl->dirs = g_list_prepend (gfl->dirs, dir);
587
595
                g_hash_table_insert (h_dirs, dir, GINT_TO_POINTER (1));
588
596
        }
594
602
        for (scan = gfl->dirs; scan; scan = scan->next)
595
603
                g_hash_table_insert (h_dirs, (char*)scan->data, GINT_TO_POINTER (1));
596
604
 
597
 
        gfl->dirs = g_list_concat (gfl->dirs, get_dir_list_from_file_list (h_dirs, gfl->base_dir, gfl->files, FALSE));
 
605
        uri = g_file_get_uri (gfl->base_dir);
 
606
        gfl->dirs = g_list_concat (gfl->dirs, get_dir_list_from_file_list (h_dirs, uri, gfl->files, FALSE));
598
607
 
599
608
        if (filter_empty (gfl->include_filter))
600
 
                gfl->dirs = g_list_concat (gfl->dirs, get_dir_list_from_file_list (h_dirs, gfl->base_dir, gfl->dirs, TRUE));
 
609
                gfl->dirs = g_list_concat (gfl->dirs, get_dir_list_from_file_list (h_dirs, uri, gfl->dirs, TRUE));
601
610
 
 
611
        g_free (uri);
602
612
        /**/
603
613
 
604
614
        if (error == NULL) {
680
690
        FilterOptions    filter_options;
681
691
 
682
692
        gfl = g_new0 (GetFileListData, 1);
683
 
        gfl->directory = g_strdup (directory);
684
 
        gfl->base_dir = g_strdup (base_dir);
 
693
        gfl->directory = g_file_new_for_commandline_arg (directory);
 
694
        gfl->base_dir = g_file_new_for_commandline_arg (base_dir);
685
695
        gfl->done_func = done_func;
686
696
        gfl->done_data = done_data;
687
697
 
696
706
        gfl->exclude_filter = filter_new (exclude_files, ignorecase ? FILTER_IGNORECASE : FILTER_DEFAULT);
697
707
        gfl->exclude_folders_filter = filter_new (exclude_folders, ignorecase ? FILTER_IGNORECASE : FILTER_DEFAULT);
698
708
 
699
 
        g_directory_foreach_child (directory,
 
709
        g_directory_foreach_child (gfl->directory,
700
710
                                   recursive,
701
711
                                   follow_links,
702
712
                                   cancellable,
755
765
static void
756
766
get_items_for_current_dir (GetFileListData *gfl)
757
767
{
758
 
        const char *directory_name;
759
 
        char       *directory_uri;
 
768
        GFile *current_dir;
 
769
        char  *directory_name;
 
770
        GFile *directory_file;
 
771
        char  *directory_uri;
 
772
        char  *base_dir_uri;
760
773
 
761
774
        if (gfl->current_dir == NULL) {
762
775
                if (gfl->done_func) {
769
782
                return;
770
783
        }
771
784
 
772
 
        directory_name = file_name_from_path ((char*) gfl->current_dir->data);
773
 
        if (strcmp (gfl->base_dir, "/") == 0)
774
 
                directory_uri = g_strconcat (gfl->base_dir, directory_name, NULL);
775
 
        else
776
 
                directory_uri = g_strconcat (gfl->base_dir, "/", directory_name, NULL);
 
785
        current_dir = g_file_new_for_uri ((char*) gfl->current_dir->data);
 
786
        directory_name = g_file_get_basename (current_dir);
 
787
        directory_file = g_file_get_child (gfl->base_dir, directory_name);
 
788
        directory_uri = g_file_get_uri (directory_file);
 
789
        base_dir_uri = g_file_get_uri (gfl->base_dir);
777
790
 
778
791
        g_directory_list_all_async (directory_uri,
779
 
                                    gfl->base_dir,
 
792
                                    base_dir_uri,
780
793
                                    TRUE,
781
794
                                    gfl->cancellable,
782
 
                                    get_items_for_current_dir_done,
783
 
                                    gfl);
 
795
                                    get_items_for_current_dir_done,
 
796
                                    gfl);
784
797
 
 
798
        g_free (base_dir_uri);
785
799
        g_free (directory_uri);
 
800
        g_object_unref (directory_file);
 
801
        g_free (directory_name);
 
802
        g_object_unref (current_dir);
786
803
}
787
804
 
788
805
 
800
817
        g_return_if_fail (base_dir != NULL);
801
818
 
802
819
        gfl = g_new0 (GetFileListData, 1);
803
 
        gfl->base_dir = g_strdup (base_dir);
 
820
        gfl->base_dir = g_file_new_for_commandline_arg (base_dir);
804
821
        gfl->cancellable = cancellable;
805
822
        gfl->done_func = done_func;
806
823
        gfl->done_data = done_data;
916
933
        GError        *error = NULL;
917
934
 
918
935
        if (! g_file_copy_finish (source, result, &error)) {
 
936
                /* source and target are directories, ignore the error */
 
937
                if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE))
 
938
                        g_clear_error (&error);
 
939
                /* source is directory, create target directory */
 
940
                if (g_error_matches (error, G_IO_ERROR,  G_IO_ERROR_WOULD_RECURSE)) {
 
941
                        g_clear_error (&error);
 
942
                        g_file_make_directory ((GFile*) cfd->destination->data,
 
943
                                               cfd->cancellable,
 
944
                                               &error);
 
945
                }
 
946
        }
 
947
 
 
948
        if (error) {
919
949
                if (cfd->callback)
920
950
                        cfd->callback (error, cfd->user_data);
921
951
                g_clear_error (&error);
1123
1153
 
1124
1154
 
1125
1155
typedef struct {
1126
 
        char                  *source;
1127
 
        char                  *destination;
 
1156
        GFile                 *source;
 
1157
        GFile                 *destination;
1128
1158
        GFileCopyFlags         flags;
1129
1159
        int                    io_priority;
1130
1160
        GCancellable          *cancellable;
1149
1179
        if (dcd == NULL)
1150
1180
                return;
1151
1181
 
1152
 
        g_free (dcd->source);
1153
 
        g_free (dcd->destination);
 
1182
        if (dcd->source != NULL)
 
1183
                g_object_unref (dcd->source);
 
1184
        if (dcd->destination != NULL)
 
1185
                g_object_unref (dcd->destination);
1154
1186
        if (dcd->current_source != NULL) {
1155
1187
                g_object_unref (dcd->current_source);
1156
1188
                dcd->current_source = NULL;
1161
1193
        }
1162
1194
        g_list_foreach (dcd->to_copy, (GFunc) child_data_free, NULL);
1163
1195
        g_list_free (dcd->to_copy);
1164
 
        g_object_unref (dcd->cancellable);
1165
1196
        g_free (dcd);
1166
1197
}
1167
1198
 
1187
1218
get_destination_for_uri (DirectoryCopyData *dcd,
1188
1219
                         const char        *uri)
1189
1220
{
1190
 
        char  *destination_uri;
 
1221
        GFile *f_uri;
1191
1222
        GFile *destination_file;
1192
 
 
1193
 
        if (strlen (uri) <=  strlen (dcd->source))
1194
 
                return NULL;
1195
 
 
1196
 
        destination_uri = g_strconcat (dcd->destination, "/", uri + strlen (dcd->source) + 1, NULL);
1197
 
        destination_file = g_file_new_for_uri (destination_uri);
1198
 
        g_free (destination_uri);
 
1223
        char  *relative_path;
 
1224
 
 
1225
        f_uri = g_file_new_for_uri (uri);
 
1226
        relative_path = g_file_get_relative_path (dcd->source, f_uri);
 
1227
        if (relative_path != NULL)
 
1228
                destination_file = g_file_resolve_relative_path (dcd->destination, relative_path);
 
1229
        else
 
1230
                destination_file = g_file_dup (dcd->destination);
 
1231
 
 
1232
        g_free (relative_path);
 
1233
        g_object_unref (f_uri);
1199
1234
 
1200
1235
        return destination_file;
1201
1236
}
1417
1452
{
1418
1453
        DirectoryCopyData *dcd;
1419
1454
 
 
1455
        /* Creating GFile objects here will save us lot of effort in path construction */
1420
1456
        dcd = g_new0 (DirectoryCopyData, 1);
1421
 
        dcd->source = g_strdup (source);
1422
 
        dcd->destination = g_strdup (destination);
 
1457
        dcd->source = g_file_new_for_commandline_arg (source);
 
1458
        dcd->destination = g_file_new_for_commandline_arg (destination);
1423
1459
        dcd->flags = flags;
1424
1460
        dcd->io_priority = io_priority;
1425
1461
        dcd->cancellable = cancellable;