~noskcaj/ubuntu/vivid/thunar/1.6.4

« back to all changes in this revision

Viewing changes to thunar-vfs/thunar-vfs-io-ops.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-12-04 16:46:20 UTC
  • mto: (2.1.3 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 69.
  • Revision ID: james.westby@ubuntu.com-20101204164620-h7p4t2e9z6hfhz6l
Tags: upstream-1.1.4
ImportĀ upstreamĀ versionĀ 1.1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id$ */
2
 
/*-
3
 
 * Copyright (c) 2005-2006 Benedikt Meurer <benny@xfce.org>
4
 
 *
5
 
 * This library is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU Library General Public
7
 
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
9
 
 *
10
 
 * This library is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
 * Library General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU Library General Public
16
 
 * License along with this library; if not, write to the
17
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
 
 * Boston, MA 02111-1307, USA.
19
 
 */
20
 
 
21
 
#ifdef HAVE_CONFIG_H
22
 
#include <config.h>
23
 
#endif
24
 
 
25
 
#ifdef HAVE_SYS_TYPES_H
26
 
#include <sys/types.h>
27
 
#endif
28
 
#ifdef HAVE_SYS_STAT_H
29
 
#include <sys/stat.h>
30
 
#endif
31
 
 
32
 
#ifdef HAVE_ERRNO_H
33
 
#include <errno.h>
34
 
#endif
35
 
#include <stdio.h>
36
 
#ifdef HAVE_UNISTD_H
37
 
#include <unistd.h>
38
 
#endif
39
 
 
40
 
#include <thunar-vfs/thunar-vfs-io-local.h>
41
 
#include <thunar-vfs/thunar-vfs-io-ops.h>
42
 
#include <thunar-vfs/thunar-vfs-io-trash.h>
43
 
#include <thunar-vfs/thunar-vfs-monitor-private.h>
44
 
#include <thunar-vfs/thunar-vfs-private.h>
45
 
#include <thunar-vfs/thunar-vfs-thumb-private.h>
46
 
#include <thunar-vfs/thunar-vfs-alias.h>
47
 
 
48
 
/* Use g_lstat(), g_mkdir() and g_remove() on win32 */
49
 
#if defined(G_OS_WIN32)
50
 
#include <glib/gstdio.h>
51
 
#else
52
 
#define g_lstat(path, statb) (lstat ((path), (statb)))
53
 
#define g_mkdir(path, mode) (mkdir ((path), (mode)))
54
 
#define g_remove(path) (remove ((path)))
55
 
#endif
56
 
 
57
 
 
58
 
 
59
 
static void tvio_set_g_error_with_paths (GError       **error,
60
 
                                         const gchar   *format,
61
 
                                         ThunarVfsPath *source_path,
62
 
                                         ThunarVfsPath *target_path,
63
 
                                         GError        *err);
64
 
 
65
 
 
66
 
 
67
 
static void
68
 
tvio_set_g_error_with_paths (GError       **error,
69
 
                             const gchar   *format,
70
 
                             ThunarVfsPath *source_path,
71
 
                             ThunarVfsPath *target_path,
72
 
                             GError        *err)
73
 
{
74
 
  gchar *source_display_name;
75
 
  gchar *target_display_name;
76
 
  gchar *s;
77
 
 
78
 
  /* determine a displayable variant of the source_path */
79
 
  s = _thunar_vfs_path_is_local (source_path) ? thunar_vfs_path_dup_string (source_path) : thunar_vfs_path_dup_uri (source_path);
80
 
  source_display_name = g_filename_display_name (s);
81
 
  g_free (s);
82
 
 
83
 
  /* determine a displayable variant of the target_path */
84
 
  s = _thunar_vfs_path_is_local (target_path) ? thunar_vfs_path_dup_string (target_path) : thunar_vfs_path_dup_uri (target_path);
85
 
  target_display_name = g_filename_display_name (s);
86
 
  g_free (s);
87
 
 
88
 
  /* generate a useful error message */
89
 
  s = g_strdup_printf (format, source_display_name, target_display_name);
90
 
  g_set_error (error, err->domain, err->code, "%s: %s", s, err->message);
91
 
  g_free (s);
92
 
 
93
 
  /* cleanup */
94
 
  g_free (target_display_name);
95
 
  g_free (source_display_name);
96
 
}
97
 
 
98
 
 
99
 
 
100
 
/**
101
 
 * _thunar_vfs_io_ops_get_file_size_and_type:
102
 
 * @path        : the #ThunarVfsPath to the file whose size and type
103
 
 *                to determine.
104
 
 * @size_return : return location for the file size or %NULL.
105
 
 * @type_return : return location for the file type or %NULL.
106
 
 * @error       : return location for errors or %NULL.
107
 
 *
108
 
 * Determines the size and type of the file at @path.
109
 
 *
110
 
 * Return value: %TRUE if the size and type was determined successfully,
111
 
 *               %FALSE otherwise.
112
 
 **/
113
 
gboolean
114
 
_thunar_vfs_io_ops_get_file_size_and_type (ThunarVfsPath       *path,
115
 
                                           ThunarVfsFileSize   *size_return,
116
 
                                           ThunarVfsFileType   *type_return,
117
 
                                           GError             **error)
118
 
{
119
 
  struct stat statb;
120
 
  gboolean    succeed = FALSE;
121
 
  gchar      *absolute_path;
122
 
  gchar      *uri;
123
 
 
124
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
125
 
 
126
 
  /* determine the absolute local path for the path object */
127
 
  absolute_path = _thunar_vfs_path_translate_dup_string (path, THUNAR_VFS_PATH_SCHEME_FILE, error);
128
 
  if (G_LIKELY (absolute_path != NULL))
129
 
    {
130
 
      /* determine the file info for the absolute path */
131
 
      succeed = (g_lstat (absolute_path, &statb) == 0);
132
 
      if (G_LIKELY (succeed))
133
 
        {
134
 
          /* return size and type if requested */
135
 
          if (G_LIKELY (size_return != NULL))
136
 
            *size_return = statb.st_size;
137
 
          if (G_LIKELY (type_return != NULL))
138
 
            *type_return = (statb.st_mode & S_IFMT) >> 12;
139
 
        }
140
 
      else
141
 
        {
142
 
          /* generate a useful error message */
143
 
          uri = thunar_vfs_path_dup_uri (path);
144
 
          _thunar_vfs_set_g_error_from_errno2 (error, errno, _("Failed to determine file info for \"%s\""), uri);
145
 
          g_free (uri);
146
 
        }
147
 
 
148
 
      /* cleanup */
149
 
      g_free (absolute_path);
150
 
    }
151
 
 
152
 
  return succeed;
153
 
}
154
 
 
155
 
 
156
 
 
157
 
/**
158
 
 * _thunar_vfs_io_ops_copy_file:
159
 
 * @source_path        : the #ThunarVfsPath to the source file.
160
 
 * @target_path        : the #ThunarVfsPath to the target location.
161
 
 * @target_path_return : the final #ThunarVfsPath of the target location, which may be
162
 
 *                       different than the @target_path.
163
 
 * @callback           : the progress callback, invoked whenever a new chunk of data is copied.
164
 
 * @callback_data      : additional data to pass to @callback.
165
 
 * @error              : return location for errors or %NULL.
166
 
 *
167
 
 * Copies the file at the @source_path to the location specified by the
168
 
 * @target_path. The final target location will be returned in @target_path_return.
169
 
 *
170
 
 * As a special case, if @callback returns %FALSE, the operation will be cancelled
171
 
 * and an @error with %G_FILE_ERROR and %G_FILE_ERROR_INTR will be returned.
172
 
 *
173
 
 * Return value: %TRUE if the file was successfully copied, %FALSE otherwise.
174
 
 **/
175
 
gboolean
176
 
_thunar_vfs_io_ops_copy_file (ThunarVfsPath                 *source_path,
177
 
                              ThunarVfsPath                 *target_path,
178
 
                              ThunarVfsPath                **target_path_return,
179
 
                              ThunarVfsIOOpsProgressCallback callback,
180
 
                              gpointer                       callback_data,
181
 
                              GError                       **error)
182
 
{
183
 
  gboolean succeed;
184
 
  GError  *err = NULL;
185
 
 
186
 
  _thunar_vfs_return_val_if_fail (!thunar_vfs_path_is_root (source_path), FALSE);
187
 
  _thunar_vfs_return_val_if_fail (!thunar_vfs_path_is_root (target_path), FALSE);
188
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
189
 
  _thunar_vfs_return_val_if_fail (callback != NULL, FALSE);
190
 
 
191
 
  /* check if either source or target is trash */
192
 
  if (_thunar_vfs_path_is_trash (source_path) || _thunar_vfs_path_is_trash (target_path))
193
 
    {
194
 
      /* copy to or from the trash may result in a new target path */
195
 
      succeed = _thunar_vfs_io_trash_copy_file (source_path, target_path, &target_path, callback, callback_data, &err);
196
 
    }
197
 
  else if (_thunar_vfs_path_is_local (source_path) && _thunar_vfs_path_is_local (target_path))
198
 
    {
199
 
      /* copying files in the file system */
200
 
      succeed = _thunar_vfs_io_local_copy_file (source_path, target_path, &target_path, callback, callback_data, &err);
201
 
    }
202
 
  else
203
 
    {
204
 
      _thunar_vfs_set_g_error_not_supported (error);
205
 
      return FALSE;
206
 
    }
207
 
 
208
 
  /* check if we succeed */
209
 
  if (G_LIKELY (succeed))
210
 
    {
211
 
      /* schedule a "created" event for the target path */
212
 
      thunar_vfs_monitor_feed (_thunar_vfs_monitor, THUNAR_VFS_MONITOR_EVENT_CREATED, target_path);
213
 
 
214
 
      /* return the new target path */
215
 
      if (G_LIKELY (target_path_return != NULL))
216
 
        *target_path_return = target_path;
217
 
      else
218
 
        thunar_vfs_path_unref (target_path);
219
 
    }
220
 
  else
221
 
    {
222
 
      /* generate a useful error message */
223
 
      tvio_set_g_error_with_paths (error, _("Failed to copy \"%s\" to \"%s\""), source_path, target_path, err);
224
 
      g_error_free (err);
225
 
    }
226
 
 
227
 
  return succeed;
228
 
}
229
 
 
230
 
 
231
 
 
232
 
/**
233
 
 * _thunar_vfs_io_ops_link_file:
234
 
 * @source_path        : the #ThunarVfsPath to the source file.
235
 
 * @target_path        : the #ThunarVfsPath to the target location.
236
 
 * @target_path_return : return location for the final target path or %NULL.
237
 
 * @error              : return location for errors or %NULL.
238
 
 *
239
 
 * Creates a symbolic link at @target_path, which points to @source_path. The
240
 
 * real target path may be different than the suggested @target_path, and will
241
 
 * be returned in @target_path_return if not %NULL.
242
 
 *
243
 
 * The caller is responsible to free the #ThunarVfsPath object returned in 
244
 
 * @target_path_return using thunar_vfs_path_unref() when no longer needed
245
 
 * if %TRUE is returned.
246
 
 *
247
 
 * Return value: %TRUE if the link was successfully created, %FALSE otherwise.
248
 
 **/
249
 
gboolean
250
 
_thunar_vfs_io_ops_link_file (ThunarVfsPath  *source_path,
251
 
                              ThunarVfsPath  *target_path,
252
 
                              ThunarVfsPath **target_path_return,
253
 
                              GError        **error)
254
 
{
255
 
  gboolean succeed;
256
 
  GError  *err = NULL;
257
 
 
258
 
  _thunar_vfs_return_val_if_fail (!thunar_vfs_path_is_root (source_path), FALSE);
259
 
  _thunar_vfs_return_val_if_fail (!thunar_vfs_path_is_root (target_path), FALSE);
260
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
261
 
 
262
 
  /* check if either source and target are local */
263
 
  if (_thunar_vfs_path_is_local (source_path) && _thunar_vfs_path_is_local (target_path))
264
 
    {
265
 
      /* copying files in the file system */
266
 
      succeed = _thunar_vfs_io_local_link_file (source_path, target_path, &target_path, &err);
267
 
    }
268
 
  else
269
 
    {
270
 
      /* impossible to perform the link operation */
271
 
      _thunar_vfs_set_g_error_not_supported (&err);
272
 
      succeed = FALSE;
273
 
    }
274
 
 
275
 
  /* check if we succeed */
276
 
  if (G_LIKELY (succeed))
277
 
    {
278
 
      /* schedule a "created" event for the target path */
279
 
      thunar_vfs_monitor_feed (_thunar_vfs_monitor, THUNAR_VFS_MONITOR_EVENT_CREATED, target_path);
280
 
 
281
 
      /* return the new target path */
282
 
      if (G_LIKELY (target_path_return != NULL))
283
 
        *target_path_return = target_path;
284
 
      else
285
 
        thunar_vfs_path_unref (target_path);
286
 
    }
287
 
  else
288
 
    {
289
 
      /* generate a useful error message */
290
 
      tvio_set_g_error_with_paths (error, _("Failed to link \"%s\" to \"%s\""), source_path, target_path, err);
291
 
      g_error_free (err);
292
 
    }
293
 
 
294
 
  return succeed;
295
 
}
296
 
 
297
 
 
298
 
 
299
 
/**
300
 
 * _thunar_vfs_io_ops_move_file:
301
 
 * @source_path        : the #ThunarVfsPath to the source file.
302
 
 * @target_path        : the #ThunarVfsPath to the target location.
303
 
 * @target_path_return : the final #ThunarVfsPath of the target location, which
304
 
 *                       may be different than the @target_path.
305
 
 * @error              : return location for errors or %NULL.
306
 
 *
307
 
 * Moves the file at the @source_path to the location specified by the
308
 
 * @target_path. The final target location is returned in @target_path_return.
309
 
 *
310
 
 * Return value: %TRUE if the file was successfully moved, %FALSE otherwise.
311
 
 **/
312
 
gboolean
313
 
_thunar_vfs_io_ops_move_file (ThunarVfsPath      *source_path,
314
 
                              ThunarVfsPath      *target_path,
315
 
                              ThunarVfsPath     **target_path_return,
316
 
                              GError            **error)
317
 
{
318
 
  gboolean succeed;
319
 
  GError  *err = NULL;
320
 
 
321
 
  _thunar_vfs_return_val_if_fail (!thunar_vfs_path_is_root (source_path), FALSE);
322
 
  _thunar_vfs_return_val_if_fail (!thunar_vfs_path_is_root (target_path), FALSE);
323
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
324
 
 
325
 
  /* check if either source or target is trash */
326
 
  if (_thunar_vfs_path_is_trash (source_path) || _thunar_vfs_path_is_trash (target_path))
327
 
    {
328
 
      /* move to or from the trash may result in a new target path */
329
 
      succeed = _thunar_vfs_io_trash_move_file (source_path, target_path, &target_path, &err);
330
 
    }
331
 
  else if (_thunar_vfs_path_is_local (source_path) && _thunar_vfs_path_is_local (target_path))
332
 
    {
333
 
      /* moving files in the file system */
334
 
      succeed = _thunar_vfs_io_local_move_file (source_path, target_path, &err);
335
 
 
336
 
      /* local move does not alter the target path */
337
 
      if (G_LIKELY (succeed))
338
 
        thunar_vfs_path_ref (target_path);
339
 
    }
340
 
  else
341
 
    {
342
 
      _thunar_vfs_set_g_error_not_supported (error);
343
 
      return FALSE;
344
 
    }
345
 
 
346
 
  /* check if we succeed */
347
 
  if (G_LIKELY (succeed))
348
 
    {
349
 
      /* drop the thumbnails for the source path */
350
 
      _thunar_vfs_thumbnail_remove_for_path (source_path);
351
 
 
352
 
      /* schedule a "created" event for the target path */
353
 
      thunar_vfs_monitor_feed (_thunar_vfs_monitor, THUNAR_VFS_MONITOR_EVENT_CREATED, target_path);
354
 
 
355
 
      /* schedule a "deleted" event for the source path */
356
 
      thunar_vfs_monitor_feed (_thunar_vfs_monitor, THUNAR_VFS_MONITOR_EVENT_DELETED, source_path);
357
 
 
358
 
      /* return the new target path */
359
 
      if (G_LIKELY (target_path_return != NULL))
360
 
        *target_path_return = target_path;
361
 
      else
362
 
        thunar_vfs_path_unref (target_path);
363
 
    }
364
 
  else
365
 
    {
366
 
      /* generate a useful error message */
367
 
      tvio_set_g_error_with_paths (error, _("Failed to move \"%s\" to \"%s\""), source_path, target_path, err);
368
 
      g_error_free (err);
369
 
    }
370
 
 
371
 
  return succeed;
372
 
}
373
 
 
374
 
 
375
 
 
376
 
/**
377
 
 * _thunar_vfs_io_ops_mkdir:
378
 
 * @path  : the #ThunarVfsPath to the directory to create.
379
 
 * @mode  : the new #ThunarVfsFileMode for the directory.
380
 
 * @flags : see #ThunarVfsIOOpsFlags.
381
 
 * @error : return location for errors or %NULL.
382
 
 *
383
 
 * Creates a new directory at the specified @path with the given @mode.
384
 
 *
385
 
 * Return value: %TRUE if the directory was created successfully or the
386
 
 *               directory exists and %THUNAR_VFS_IO_OPS_IGNORE_EEXIST
387
 
 *               was specified in @flags, %FALSE otherwise.
388
 
 **/
389
 
gboolean
390
 
_thunar_vfs_io_ops_mkdir (ThunarVfsPath      *path,
391
 
                          ThunarVfsFileMode   mode,
392
 
                          ThunarVfsIOOpsFlags flags,
393
 
                          GError            **error)
394
 
{
395
 
  gboolean succeed;
396
 
  gchar   *absolute_path;
397
 
  gchar   *display_name;
398
 
  gint     sverrno;
399
 
 
400
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
401
 
 
402
 
  /* translate the path object to an absolute path */
403
 
  absolute_path = _thunar_vfs_path_translate_dup_string (path, THUNAR_VFS_PATH_SCHEME_FILE, error);
404
 
  if (G_UNLIKELY (absolute_path == NULL))
405
 
    return FALSE;
406
 
 
407
 
  /* try to create the directory with the given mode */
408
 
  succeed = (g_mkdir (absolute_path, mode) == 0 || (errno == EEXIST && (flags & THUNAR_VFS_IO_OPS_IGNORE_EEXIST) != 0));
409
 
  if (G_UNLIKELY (!succeed))
410
 
    {
411
 
      /* save the errno value, may be modified */
412
 
      sverrno = errno;
413
 
 
414
 
      /* generate a useful error message */
415
 
      display_name = g_filename_display_name (absolute_path);
416
 
      _thunar_vfs_set_g_error_from_errno2 (error, sverrno, _("Failed to create directory \"%s\""), display_name);
417
 
      g_free (display_name);
418
 
    }
419
 
  else if (G_LIKELY (errno != EEXIST))
420
 
    {
421
 
      /* schedule a "created" event for the directory path */
422
 
      thunar_vfs_monitor_feed (_thunar_vfs_monitor, THUNAR_VFS_MONITOR_EVENT_CREATED, path);
423
 
    }
424
 
 
425
 
  /* cleanup */
426
 
  g_free (absolute_path);
427
 
 
428
 
  return succeed;
429
 
}
430
 
 
431
 
 
432
 
 
433
 
/**
434
 
 * _thunar_vfs_io_ops_remove:
435
 
 * @path  : the #ThunarVfsPath to the file to remove.
436
 
 * @flags : see #ThunarVfsIOOpsFlags.
437
 
 * @error : return location for errors or %NULL.
438
 
 *
439
 
 * Removes the file or folder at the specified @path.
440
 
 *
441
 
 * Return value: %TRUE if the file was successfully
442
 
 *               removed, %FALSE otherwise.
443
 
 **/
444
 
gboolean
445
 
_thunar_vfs_io_ops_remove (ThunarVfsPath      *path,
446
 
                           ThunarVfsIOOpsFlags flags,
447
 
                           GError            **error)
448
 
{
449
 
  gboolean succeed;
450
 
  GError  *err = NULL;
451
 
  gchar   *message;
452
 
  gchar   *display_name;
453
 
  gchar   *absolute_path;
454
 
 
455
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
456
 
 
457
 
  /* remove using the appropriate backend */
458
 
  switch (thunar_vfs_path_get_scheme (path))
459
 
    {
460
 
    case THUNAR_VFS_PATH_SCHEME_FILE:
461
 
      absolute_path = thunar_vfs_path_dup_string (path);
462
 
      succeed = (g_remove (absolute_path) == 0);
463
 
      if (G_UNLIKELY (!succeed))
464
 
        _thunar_vfs_set_g_error_from_errno3 (&err);
465
 
      g_free (absolute_path);
466
 
      break;
467
 
 
468
 
    case THUNAR_VFS_PATH_SCHEME_TRASH:
469
 
      succeed = _thunar_vfs_io_trash_remove (path, &err);
470
 
      break;
471
 
 
472
 
    default:
473
 
      _thunar_vfs_set_g_error_not_supported (error);
474
 
      return FALSE;
475
 
    }
476
 
 
477
 
  /* drop the thumbnails if the removal succeed */
478
 
  if (G_LIKELY (succeed))
479
 
    {
480
 
      /* drop the thumbnails for the removed file */
481
 
      _thunar_vfs_thumbnail_remove_for_path (path);
482
 
 
483
 
      /* tell the monitor about the removed file */
484
 
      thunar_vfs_monitor_feed (_thunar_vfs_monitor, THUNAR_VFS_MONITOR_EVENT_DELETED, path);
485
 
    }
486
 
  else if ((flags & THUNAR_VFS_IO_OPS_IGNORE_ENOENT) != 0 && err->domain == G_FILE_ERROR && err->code == G_FILE_ERROR_NOENT)
487
 
    {
488
 
      /* we should ignore ENOENT errors */
489
 
      g_error_free (err);
490
 
      succeed = TRUE;
491
 
    }
492
 
  else
493
 
    {
494
 
      /* generate a useful error message */
495
 
      display_name = _thunar_vfs_path_dup_display_name (path);
496
 
      message = g_strdup_printf (_("Failed to remove \"%s\""), display_name);
497
 
      g_set_error (error, err->domain, err->code, "%s: %s", message, err->message);
498
 
      g_free (display_name);
499
 
      g_error_free (err);
500
 
      g_free (message);
501
 
    }
502
 
 
503
 
  return succeed;
504
 
}
505
 
 
506
 
 
507
 
 
508
 
#define __THUNAR_VFS_IO_OPS_C__
509
 
#include <thunar-vfs/thunar-vfs-aliasdef.c>