~noskcaj/ubuntu/vivid/thunar/1.6.4

« back to all changes in this revision

Viewing changes to thunar-vfs/thunar-vfs-io-jobs.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-2007 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
 
#ifdef HAVE_FCNTL_H
36
 
#include <fcntl.h>
37
 
#endif
38
 
#ifdef HAVE_UNISTD_H
39
 
#include <unistd.h>
40
 
#endif
41
 
 
42
 
#include <thunar-vfs/thunar-vfs-enum-types.h>
43
 
#include <thunar-vfs/thunar-vfs-info.h>
44
 
#include <thunar-vfs/thunar-vfs-io-jobs.h>
45
 
#include <thunar-vfs/thunar-vfs-io-local.h>
46
 
#include <thunar-vfs/thunar-vfs-io-ops.h>
47
 
#include <thunar-vfs/thunar-vfs-io-scandir.h>
48
 
#include <thunar-vfs/thunar-vfs-io-trash.h>
49
 
#include <thunar-vfs/thunar-vfs-monitor-private.h>
50
 
#include <thunar-vfs/thunar-vfs-private.h>
51
 
#include <thunar-vfs/thunar-vfs-alias.h>
52
 
 
53
 
/* use g_chmod(), g_open() and g_stat() on win32 */
54
 
#if defined(G_OS_WIN32)
55
 
#include <glib/gstdio.h>
56
 
#else
57
 
#define g_chmod(path, mode) (chmod ((path), (mode)))
58
 
#define g_open(path, flags, mode) (open ((path), (flags), (mode)))
59
 
#define g_stat(path, statb) (stat ((path), (statb)))
60
 
#endif
61
 
 
62
 
 
63
 
 
64
 
static GList *tvij_collect_nofollow (ThunarVfsJob *job,
65
 
                                     GList        *base_path_list,
66
 
                                     GError      **error) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
67
 
 
68
 
 
69
 
 
70
 
static GList*
71
 
tvij_collect_nofollow (ThunarVfsJob *job,
72
 
                       GList        *base_path_list,
73
 
                       GError      **error)
74
 
{
75
 
  GError *err = NULL;
76
 
  GList  *child_path_list;
77
 
  GList  *path_list = NULL;
78
 
  GList  *lp;
79
 
 
80
 
  /* tell the user that we're preparing to unlink the files */
81
 
  _thunar_vfs_job_info_message (job, _("Preparing..."));
82
 
 
83
 
  /* recursively collect the paths */
84
 
  for (lp = base_path_list; err == NULL && lp != NULL && !thunar_vfs_job_cancelled (job); lp = lp->next)
85
 
    {
86
 
      /* try to scan the path as directory */
87
 
      child_path_list = _thunar_vfs_io_scandir (lp->data, &job->cancelled, THUNAR_VFS_IO_SCANDIR_RECURSIVE, &err);
88
 
      if (G_UNLIKELY (err != NULL))
89
 
        {
90
 
          /* we can safely ignore ENOENT/ENOTDIR errors here */
91
 
          if (err->domain == G_FILE_ERROR && (err->code == G_FILE_ERROR_NOENT || err->code == G_FILE_ERROR_NOTDIR))
92
 
            {
93
 
              /* reset the error */
94
 
              g_clear_error (&err);
95
 
            }
96
 
        }
97
 
 
98
 
      /* prepend the new paths to the existing list */
99
 
      path_list = thunar_vfs_path_list_prepend (path_list, lp->data);
100
 
      path_list = g_list_concat (child_path_list, path_list);
101
 
    }
102
 
 
103
 
  /* check if we failed */
104
 
  if (G_UNLIKELY (err != NULL))
105
 
    {
106
 
      /* release the collected paths */
107
 
      thunar_vfs_path_list_free (path_list);
108
 
 
109
 
      /* propagate the error */
110
 
      g_propagate_error (error, err);
111
 
      return NULL;
112
 
    }
113
 
 
114
 
  return path_list;
115
 
}
116
 
 
117
 
 
118
 
 
119
 
/**
120
 
 * _thunar_vfs_io_jobs_chmod:
121
 
 * @job            : a #ThunarVfsJob.
122
 
 * @param_values   : exactly six #GValue with the #GList of #ThunarVfsPath<!---->s
123
 
 *                   for the files whose mode to changed, the directory mode mask,
124
 
 *                   the directory mode, the file mask, the file mode and a boolean
125
 
 *                   flag telling whether to recursively process directories.
126
 
 * @n_param_values : the number of #GValue<!---->s in @param_values, must be exactly
127
 
 *                   six for this job.
128
 
 * @error          : return location for errors or %NULL.
129
 
 *
130
 
 * See thunar_vfs_change_mode() for details.
131
 
 *
132
 
 * The #ThunarVfsPath<!---->s in the first item of @param_values may only include local
133
 
 * paths with scheme %THUNAR_VFS_PATH_SCHEME_FILE.
134
 
 *
135
 
 * Return value: %TRUE if the changing of permissions succeed, %FALSE otherwise.
136
 
 **/
137
 
gboolean
138
 
_thunar_vfs_io_jobs_chmod (ThunarVfsJob *job,
139
 
                           const GValue *param_values,
140
 
                           guint         n_param_values,
141
 
                           GError      **error)
142
 
{
143
 
  ThunarVfsFileMode file_mask = g_value_get_flags (&param_values[3]);
144
 
  ThunarVfsFileMode file_mode = g_value_get_flags (&param_values[4]);
145
 
  ThunarVfsFileMode dir_mask = g_value_get_flags (&param_values[1]);
146
 
  ThunarVfsFileMode dir_mode = g_value_get_flags (&param_values[2]);
147
 
  ThunarVfsFileMode mask;
148
 
  ThunarVfsFileMode mode;
149
 
  struct stat       statb;
150
 
  gboolean          recursive = g_value_get_boolean (&param_values[5]);
151
 
  gboolean          retry;
152
 
  GError           *err = NULL;
153
 
  GList            *path_list = g_value_get_boxed (&param_values[0]);
154
 
  GList            *lp;
155
 
  gchar            *absolute_path;
156
 
  gchar            *display_name;
157
 
  gint              sverrno;
158
 
 
159
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[1], THUNAR_VFS_TYPE_VFS_FILE_MODE), FALSE);
160
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[2], THUNAR_VFS_TYPE_VFS_FILE_MODE), FALSE);
161
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[3], THUNAR_VFS_TYPE_VFS_FILE_MODE), FALSE);
162
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[4], THUNAR_VFS_TYPE_VFS_FILE_MODE), FALSE);
163
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[0], THUNAR_VFS_TYPE_PATH_LIST), FALSE);
164
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[5], G_TYPE_BOOLEAN), FALSE);
165
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
166
 
  _thunar_vfs_return_val_if_fail (THUNAR_VFS_IS_JOB (job), FALSE);
167
 
  _thunar_vfs_return_val_if_fail (n_param_values == 6, FALSE);
168
 
 
169
 
#ifdef G_ENABLE_DEBUG
170
 
  /* verify that we have only local paths */
171
 
  for (lp = path_list; lp != NULL; lp = lp->next)
172
 
    _thunar_vfs_assert (_thunar_vfs_path_is_local (lp->data));
173
 
#endif
174
 
 
175
 
  /* collect the paths for the chmod operation */
176
 
  path_list = recursive ? tvij_collect_nofollow (job, path_list, &err) : thunar_vfs_path_list_copy (path_list);
177
 
  if (G_UNLIKELY (err != NULL))
178
 
    {
179
 
      /* propagate the error */
180
 
      g_propagate_error (error, err);
181
 
      return FALSE;
182
 
    }
183
 
 
184
 
  /* we know the total list of paths to process now */
185
 
  _thunar_vfs_job_total_paths (job, path_list);
186
 
 
187
 
  /* change the permissions of all paths */
188
 
  for (lp = path_list; lp != NULL && !thunar_vfs_job_cancelled (job); lp = lp->next)
189
 
    {
190
 
retry_chmod:
191
 
      /* reset the saved errno */
192
 
      sverrno = 0;
193
 
 
194
 
      /* update the progress information */
195
 
      _thunar_vfs_job_process_path (job, lp);
196
 
 
197
 
      /* try to stat the file */
198
 
      absolute_path = thunar_vfs_path_dup_string (lp->data);
199
 
      if (g_stat (absolute_path, &statb) == 0)
200
 
        {
201
 
          /* different actions depending on the type of the file */
202
 
          mask = S_ISDIR (statb.st_mode) ? dir_mask : file_mask;
203
 
          mode = S_ISDIR (statb.st_mode) ? dir_mode : file_mode;
204
 
 
205
 
          /* determine the new mode */
206
 
          mode = ((statb.st_mode & ~mask) | mode) & 07777;
207
 
 
208
 
          /* try to apply the new mode */
209
 
          if (g_chmod (absolute_path, mode) < 0)
210
 
            goto sverror;
211
 
 
212
 
          /* feed a change notification event */
213
 
          thunar_vfs_monitor_feed (_thunar_vfs_monitor, THUNAR_VFS_MONITOR_EVENT_CHANGED, lp->data);
214
 
        }
215
 
      else
216
 
        {
217
 
sverror:
218
 
          /* save the errno */
219
 
          sverrno = errno;
220
 
        }
221
 
      g_free (absolute_path);
222
 
 
223
 
      /* check if we failed (ignoring ENOENT) */
224
 
      if (G_UNLIKELY (sverrno != 0 && sverrno != ENOENT))
225
 
        {
226
 
          /* generate an error */
227
 
          display_name = _thunar_vfs_path_dup_display_name (lp->data);
228
 
          _thunar_vfs_set_g_error_from_errno2 (&err, sverrno, _("Failed to change permissions of \"%s\""), display_name);
229
 
          g_free (display_name);
230
 
 
231
 
          /* ask the user whether to skip this one */
232
 
          retry = (_thunar_vfs_job_ask_skip (job, "%s", err->message) == THUNAR_VFS_JOB_RESPONSE_RETRY);
233
 
 
234
 
          /* reset the error */
235
 
          g_clear_error (&err);
236
 
 
237
 
          /* check whether to retry */
238
 
          if (G_UNLIKELY (retry))
239
 
            goto retry_chmod;
240
 
        }
241
 
    }
242
 
 
243
 
  /* release the path list */
244
 
  thunar_vfs_path_list_free (path_list);
245
 
 
246
 
  return TRUE;
247
 
}
248
 
 
249
 
 
250
 
 
251
 
/**
252
 
 * _thunar_vfs_io_jobs_chown:
253
 
 * @job            : a #ThunarVfsJob.
254
 
 * @param_values   : exactly four #GValue with the #GList of #ThunarVfsPath<!---->s
255
 
 *                   for the files whose ownership to changed, the new user id or %-1,
256
 
 *                   the new group id or %-1, and a boolean flag telling whether to
257
 
 *                   recursively process directories.
258
 
 * @n_param_values : the number of #GValue<!---->s in @param_values, must be exactly
259
 
 *                   four for this job.
260
 
 * @error          : return location for errors or %NULL.
261
 
 *
262
 
 * See thunar_vfs_change_group() and thunar_vfs_change_owner() for details.
263
 
 *
264
 
 * The #ThunarVfsPath<!---->s in the first item of @param_values may only include local
265
 
 * paths with scheme %THUNAR_VFS_PATH_SCHEME_FILE.
266
 
 *
267
 
 * Return value: %TRUE if the changing of ownership succeed, %FALSE otherwise.
268
 
 **/
269
 
gboolean
270
 
_thunar_vfs_io_jobs_chown (ThunarVfsJob *job,
271
 
                           const GValue *param_values,
272
 
                           guint         n_param_values,
273
 
                           GError      **error)
274
 
{
275
 
  struct stat statb;
276
 
  gboolean    recursive = g_value_get_boolean (&param_values[3]);
277
 
  gboolean    retry;
278
 
  GError     *err = NULL;
279
 
  GList      *path_list = g_value_get_boxed (&param_values[0]);
280
 
  GList      *lp;
281
 
  gchar      *absolute_path;
282
 
  gchar      *display_name;
283
 
  gint        sverrno;
284
 
  gint        uid = g_value_get_int (&param_values[1]);
285
 
  gint        gid = g_value_get_int (&param_values[2]);
286
 
  gint        nuid;
287
 
  gint        ngid;
288
 
 
289
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[0], THUNAR_VFS_TYPE_PATH_LIST), FALSE);
290
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[3], G_TYPE_BOOLEAN), FALSE);
291
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[1], G_TYPE_INT), FALSE);
292
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[2], G_TYPE_INT), FALSE);
293
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
294
 
  _thunar_vfs_return_val_if_fail (THUNAR_VFS_IS_JOB (job), FALSE);
295
 
  _thunar_vfs_return_val_if_fail (uid >= 0 || gid >= 0, FALSE);
296
 
  _thunar_vfs_return_val_if_fail (n_param_values == 4, FALSE);
297
 
 
298
 
#ifdef G_ENABLE_DEBUG
299
 
  /* verify that we have only local paths */
300
 
  for (lp = path_list; lp != NULL; lp = lp->next)
301
 
    _thunar_vfs_assert (_thunar_vfs_path_is_local (lp->data));
302
 
#endif
303
 
 
304
 
  /* collect the paths for the chown operation */
305
 
  path_list = recursive ? tvij_collect_nofollow (job, path_list, &err) : thunar_vfs_path_list_copy (path_list);
306
 
  if (G_UNLIKELY (err != NULL))
307
 
    {
308
 
      /* propagate the error */
309
 
      g_propagate_error (error, err);
310
 
      return FALSE;
311
 
    }
312
 
 
313
 
  /* we know the total list of paths to process now */
314
 
  _thunar_vfs_job_total_paths (job, path_list);
315
 
 
316
 
  /* change the ownership of all paths */
317
 
  for (lp = path_list; lp != NULL && !thunar_vfs_job_cancelled (job); lp = lp->next)
318
 
    {
319
 
retry_chown:
320
 
      /* reset the saved errno */
321
 
      sverrno = 0;
322
 
 
323
 
      /* update the progress information */
324
 
      _thunar_vfs_job_process_path (job, lp);
325
 
 
326
 
      /* try to stat the file */
327
 
      absolute_path = thunar_vfs_path_dup_string (lp->data);
328
 
      if (g_stat (absolute_path, &statb) == 0)
329
 
        {
330
 
          /* determine the new uid/gid */
331
 
          nuid = (uid < 0) ? (gint) statb.st_uid : uid;
332
 
          ngid = (gid < 0) ? (gint) statb.st_gid : gid;
333
 
 
334
 
          /* try to apply the new ownership */
335
 
          if (chown (absolute_path, nuid, ngid) < 0)
336
 
            goto sverror;
337
 
 
338
 
          /* feed a change notification event */
339
 
          thunar_vfs_monitor_feed (_thunar_vfs_monitor, THUNAR_VFS_MONITOR_EVENT_CHANGED, lp->data);
340
 
        }
341
 
      else
342
 
        {
343
 
sverror:
344
 
          /* save the errno */
345
 
          sverrno = errno;
346
 
        }
347
 
      g_free (absolute_path);
348
 
 
349
 
      /* check if we failed (ignoring ENOENT) */
350
 
      if (G_UNLIKELY (sverrno != 0 && sverrno != ENOENT))
351
 
        {
352
 
          /* generate an error */
353
 
          display_name = _thunar_vfs_path_dup_display_name (lp->data);
354
 
          _thunar_vfs_set_g_error_from_errno2 (&err, sverrno, G_LIKELY (uid >= 0)
355
 
                                               ? _("Failed to change file owner of \"%s\"")
356
 
                                               : _("Failed to change file group of \"%s\""),
357
 
                                               display_name);
358
 
          g_free (display_name);
359
 
 
360
 
          /* ask the user whether to skip/retry this one */
361
 
          retry = (_thunar_vfs_job_ask_skip (job, "%s", err->message) == THUNAR_VFS_JOB_RESPONSE_RETRY);
362
 
 
363
 
          /* reset the error */
364
 
          g_clear_error (&err);
365
 
 
366
 
          /* check whether to retry */
367
 
          if (G_UNLIKELY (retry))
368
 
            goto retry_chown;
369
 
        }
370
 
    }
371
 
 
372
 
  /* release the path list */
373
 
  thunar_vfs_path_list_free (path_list);
374
 
 
375
 
  return TRUE;
376
 
}
377
 
 
378
 
 
379
 
 
380
 
/**
381
 
 * _thunar_vfs_io_jobs_create:
382
 
 * @job            : a #ThunarVfsJob.
383
 
 * @param_values   : exactly one #GValue with the #GList of #ThunarVfsPath<!---->s
384
 
 *                   for which to create new files.
385
 
 * @n_param_values : the number of #GValue<!---->s in @param_values, must be exactly
386
 
 *                   one for this job.
387
 
 * @error          : return location for errors or %NULL.
388
 
 *
389
 
 * Creates empty files for all #ThunarVfsPath<!---->s specified in @param_values.
390
 
 *
391
 
 * The #ThunarVfsPath<!---->s in @param_values may only include local paths with scheme
392
 
 * %THUNAR_VFS_PATH_SCHEME_FILE.
393
 
 *
394
 
 * Return value: %TRUE if the creation of the files succeed, %FALSE otherwise.
395
 
 **/
396
 
gboolean
397
 
_thunar_vfs_io_jobs_create (ThunarVfsJob *job,
398
 
                            const GValue *param_values,
399
 
                            guint         n_param_values,
400
 
                            GError      **error)
401
 
{
402
 
  ThunarVfsJobResponse response;
403
 
  GError              *err = NULL;
404
 
  gchar               *absolute_path;
405
 
  gchar               *display_name;
406
 
  gchar               *message;
407
 
  GList               *path_list = g_value_get_boxed (&param_values[0]);
408
 
  GList               *lp;
409
 
  gint                 fd;
410
 
 
411
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[0], THUNAR_VFS_TYPE_PATH_LIST), FALSE);
412
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
413
 
  _thunar_vfs_return_val_if_fail (THUNAR_VFS_IS_JOB (job), FALSE);
414
 
  _thunar_vfs_return_val_if_fail (n_param_values == 1, FALSE);
415
 
 
416
 
#ifdef G_ENABLE_DEBUG
417
 
  /* verify that we have only local paths */
418
 
  for (lp = path_list; lp != NULL; lp = lp->next)
419
 
    _thunar_vfs_assert (_thunar_vfs_path_is_local (lp->data));
420
 
#endif
421
 
 
422
 
  /* we know the total paths to be processed */
423
 
  _thunar_vfs_job_total_paths (job, path_list);
424
 
 
425
 
  /* process all paths */
426
 
  for (lp = path_list; err == NULL && lp != NULL && !thunar_vfs_job_cancelled (job); lp = lp->next)
427
 
    {
428
 
      /* update the progress information */
429
 
      _thunar_vfs_job_process_path (job, lp);
430
 
 
431
 
      /* determine the absolute path to the path object */
432
 
      absolute_path = thunar_vfs_path_dup_string (lp->data);
433
 
 
434
 
again:
435
 
      /* Try to create the file at the given path.
436
 
       *
437
 
       * Note that despite the 0666 mask, we won't really create a world-writable
438
 
       * file unless the user's umask permits it (ie the umask is 0000).
439
 
       */
440
 
      fd = g_open (absolute_path, O_CREAT | O_EXCL | O_WRONLY, 0666);
441
 
      if (G_UNLIKELY (fd < 0))
442
 
        {
443
 
          /* check if the file already exists */
444
 
          if (G_UNLIKELY (errno == EEXIST))
445
 
            {
446
 
              /* ask the user whether to override this path */
447
 
              display_name = _thunar_vfs_path_dup_display_name (lp->data);
448
 
              response = _thunar_vfs_job_ask_overwrite (job, _("The file \"%s\" already exists"), display_name);
449
 
              g_free (display_name);
450
 
 
451
 
              /* check if we should overwrite */
452
 
              if (G_UNLIKELY (response == THUNAR_VFS_JOB_RESPONSE_YES))
453
 
                {
454
 
                  /* try to remove the file (fail if not possible) */
455
 
                  if (_thunar_vfs_io_ops_remove (lp->data, THUNAR_VFS_IO_OPS_IGNORE_ENOENT, &err))
456
 
                    {
457
 
                      /* try again */
458
 
                      goto again;
459
 
                    }
460
 
                }
461
 
            }
462
 
          else
463
 
            {
464
 
              /* ask the user whether to skip/retry this path (cancels the job if not) */
465
 
              display_name = _thunar_vfs_path_dup_display_name (lp->data);
466
 
              message = g_strdup_printf (_("Failed to create empty file \"%s\""), display_name);
467
 
              response = _thunar_vfs_job_ask_skip (job, "%s: %s", message, g_strerror (errno));
468
 
              g_free (display_name);
469
 
              g_free (message);
470
 
 
471
 
              /* check whether to retry the create operation */
472
 
              if (response == THUNAR_VFS_JOB_RESPONSE_RETRY)
473
 
                goto again;
474
 
            }
475
 
        }
476
 
      else
477
 
        {
478
 
          /* feed a "created" event for the new file */
479
 
          thunar_vfs_monitor_feed (_thunar_vfs_monitor, THUNAR_VFS_MONITOR_EVENT_CREATED, lp->data);
480
 
 
481
 
          /* close the file */
482
 
          close (fd);
483
 
        }
484
 
 
485
 
      /* cleanup */
486
 
      g_free (absolute_path);
487
 
    }
488
 
 
489
 
  /* check if we failed */
490
 
  if (G_UNLIKELY (err != NULL))
491
 
    {
492
 
      /* propagate the error */
493
 
      g_propagate_error (error, err);
494
 
      return FALSE;
495
 
    }
496
 
 
497
 
  /* emit the "new-files" signal with the given path list */
498
 
  _thunar_vfs_job_new_files (job, path_list);
499
 
 
500
 
  return TRUE;
501
 
}
502
 
 
503
 
 
504
 
 
505
 
/**
506
 
 * _thunar_vfs_io_jobs_link:
507
 
 * @job            : a #ThunarVfsJob.
508
 
 * @param_values   : exactly two #GValue<!---->s with the #GList of #ThunarVfsPath<!---->s
509
 
 *                   for the source paths and the target paths.
510
 
 * @n_param_values : the number of #GValue<!---->s in @param_values, must be exactly
511
 
 *                   two for this job.
512
 
 * @error          : return location for errors or %NULL.
513
 
 *
514
 
 * Creates symbolic links from all #ThunarVfsPath<!---->s in the first parameter of @param_values
515
 
 * to the corresponding #ThunarVfsPath<!---->s in the second parameter of @param_values.
516
 
 *
517
 
 * The #ThunarVfsPath<!---->s in both @param_values may only include local paths with scheme
518
 
 * %THUNAR_VFS_PATH_SCHEME_FILE.
519
 
 *
520
 
 * Return value: %TRUE if the link were created successfully, %FALSE otherwise.
521
 
 **/
522
 
gboolean
523
 
_thunar_vfs_io_jobs_link (ThunarVfsJob *job,
524
 
                          const GValue *param_values,
525
 
                          guint         n_param_values,
526
 
                          GError      **error)
527
 
{
528
 
  ThunarVfsJobResponse overwrite;
529
 
  ThunarVfsPath       *target_path;
530
 
  GError              *err = NULL;
531
 
  GList               *source_path_list = g_value_get_boxed (&param_values[0]);
532
 
  GList               *target_path_list = g_value_get_boxed (&param_values[1]);
533
 
  GList               *sp;
534
 
  GList               *tp;
535
 
 
536
 
  _thunar_vfs_return_val_if_fail (g_list_length (source_path_list) == g_list_length (target_path_list), FALSE);
537
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[0], THUNAR_VFS_TYPE_PATH_LIST), FALSE);
538
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[1], THUNAR_VFS_TYPE_PATH_LIST), FALSE);
539
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
540
 
  _thunar_vfs_return_val_if_fail (THUNAR_VFS_IS_JOB (job), FALSE);
541
 
  _thunar_vfs_return_val_if_fail (n_param_values == 2, FALSE);
542
 
 
543
 
  /* we know the total list of paths to process */
544
 
  _thunar_vfs_job_total_paths (job, source_path_list);
545
 
 
546
 
  /* process all files */
547
 
  for (sp = source_path_list, tp = target_path_list; err == NULL && sp != NULL; sp = sp->next, tp = tp->next)
548
 
    {
549
 
      /* check if the job was cancelled by the user */
550
 
      if (G_UNLIKELY (thunar_vfs_job_cancelled (job)))
551
 
        break;
552
 
 
553
 
      /* update the progress information */
554
 
      _thunar_vfs_job_process_path (job, sp);
555
 
 
556
 
again:
557
 
      /* try to perform the symlink operation */
558
 
      if (_thunar_vfs_io_ops_link_file (sp->data, tp->data, &target_path, &err))
559
 
        {
560
 
          /* replace the path on the target path list */
561
 
          thunar_vfs_path_unref (tp->data);
562
 
          tp->data = target_path;
563
 
        }
564
 
      else if (G_LIKELY (err->domain == G_FILE_ERROR && err->code == G_FILE_ERROR_EXIST))
565
 
        {
566
 
          /* ask the user whether we should remove the target first */
567
 
          overwrite = _thunar_vfs_job_ask_overwrite (job, "%s", err->message);
568
 
 
569
 
          /* release the error */
570
 
          g_clear_error (&err);
571
 
 
572
 
          /* check if we should overwrite the existing file */
573
 
          if (G_LIKELY (overwrite == THUNAR_VFS_JOB_RESPONSE_YES))
574
 
            {
575
 
              /* try to remove the target file (fail if not possible) */
576
 
              if (_thunar_vfs_io_ops_remove (tp->data, THUNAR_VFS_IO_OPS_IGNORE_ENOENT, &err))
577
 
                {
578
 
                  /* try again... */
579
 
                  goto again;
580
 
                }
581
 
            }
582
 
        }
583
 
    }
584
 
 
585
 
  /* check if we failed */
586
 
  if (G_UNLIKELY (err != NULL))
587
 
    {
588
 
      /* propagate the error */
589
 
      g_propagate_error (error, err);
590
 
      return FALSE;
591
 
    }
592
 
 
593
 
  /* emit the "new-files" signal for the target paths */
594
 
  _thunar_vfs_job_new_files (job, target_path_list);
595
 
 
596
 
  return TRUE;
597
 
}
598
 
 
599
 
 
600
 
 
601
 
/**
602
 
 * _thunar_vfs_io_jobs_listdir:
603
 
 * @job            : a #ThunarVfsJob.
604
 
 * @param_values   : exactly one #GValue with the #ThunarVfsPath of the folder whose
605
 
 *                   contents to list.
606
 
 * @n_param_values : the number of #GValue<!---->s in @param_values, must be exactly
607
 
 *                   one for this job.
608
 
 * @error          : return location for errors or %NULL.
609
 
 *
610
 
 * Lists the contents of the folder at the #ThunarVfsPath in @param_values.
611
 
 *
612
 
 * Return value: %TRUE if the folder contents were successfully listed, %FALSE otherwise.
613
 
 **/
614
 
gboolean
615
 
_thunar_vfs_io_jobs_listdir (ThunarVfsJob *job,
616
 
                             const GValue *param_values,
617
 
                             guint         n_param_values,
618
 
                             GError      **error)
619
 
{
620
 
  ThunarVfsPath *path = g_value_get_boxed (&param_values[0]);
621
 
  GError        *err = NULL;
622
 
  GList         *info_list;
623
 
 
624
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[0], THUNAR_VFS_TYPE_PATH), FALSE);
625
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
626
 
  _thunar_vfs_return_val_if_fail (THUNAR_VFS_IS_JOB (job), FALSE);
627
 
  _thunar_vfs_return_val_if_fail (n_param_values == 1, FALSE);
628
 
 
629
 
  /* determine the type of directory to scan */
630
 
  switch (thunar_vfs_path_get_scheme (path))
631
 
    {
632
 
    case THUNAR_VFS_PATH_SCHEME_FILE:
633
 
      info_list = _thunar_vfs_io_local_listdir (path, &err);
634
 
      break;
635
 
 
636
 
    case THUNAR_VFS_PATH_SCHEME_TRASH:
637
 
      info_list = _thunar_vfs_io_trash_listdir (path, &err);
638
 
      break;
639
 
 
640
 
    default:
641
 
      _thunar_vfs_set_g_error_not_supported (error);
642
 
      return FALSE;
643
 
    }
644
 
 
645
 
  /* check if we have any files to report */
646
 
  if (G_LIKELY (info_list != NULL))
647
 
    {
648
 
      /* emit the "infos-ready" signal with the given list */
649
 
      if (!_thunar_vfs_job_infos_ready (job, info_list))
650
 
        {
651
 
          /* if none of the handlers took over ownership, we still
652
 
           * own it and we are thereby responsible to release it.
653
 
           */
654
 
          thunar_vfs_info_list_free (info_list);
655
 
        }
656
 
    }
657
 
  else if (err != NULL)
658
 
    {
659
 
      /* propagate the error to the caller */
660
 
      g_propagate_error (error, err);
661
 
      return FALSE;
662
 
    }
663
 
 
664
 
  return TRUE;
665
 
}
666
 
 
667
 
 
668
 
 
669
 
/**
670
 
 * _thunar_vfs_io_jobs_mkdir:
671
 
 * @job            : a #ThunarVfsJob.
672
 
 * @param_values   : exactly one #GValue with the #GList of #ThunarVfsPath<!---->s
673
 
 *                   for which to create new directories.
674
 
 * @n_param_values : the number of #GValue<!---->s in @param_values, must be exactly
675
 
 *                   one for this job.
676
 
 * @error          : return location for errors or %NULL.
677
 
 *
678
 
 * Creates new directories for all #ThunarVfsPath<!---->s specified in @param_values.
679
 
 *
680
 
 * The #ThunarVfsPath<!---->s in @param_values may only include local paths with scheme
681
 
 * %THUNAR_VFS_PATH_SCHEME_FILE.
682
 
 *
683
 
 * Return value: %TRUE if the creation of the folders succeed, %FALSE otherwise.
684
 
 **/
685
 
gboolean
686
 
_thunar_vfs_io_jobs_mkdir (ThunarVfsJob *job,
687
 
                           const GValue *param_values,
688
 
                           guint         n_param_values,
689
 
                           GError      **error)
690
 
{
691
 
  GList *path_list = g_value_get_boxed (&param_values[0]);
692
 
  GList *lp;
693
 
 
694
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[0], THUNAR_VFS_TYPE_PATH_LIST), FALSE);
695
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
696
 
  _thunar_vfs_return_val_if_fail (THUNAR_VFS_IS_JOB (job), FALSE);
697
 
  _thunar_vfs_return_val_if_fail (n_param_values == 1, FALSE);
698
 
 
699
 
#ifdef G_ENABLE_DEBUG
700
 
  /* verify that we have only local paths */
701
 
  for (lp = path_list; lp != NULL; lp = lp->next)
702
 
    _thunar_vfs_assert (_thunar_vfs_path_is_local (lp->data));
703
 
#endif
704
 
 
705
 
  /* we know the total list of paths to process */
706
 
  _thunar_vfs_job_total_paths (job, path_list);
707
 
 
708
 
  /* process all directories */
709
 
  for (lp = path_list; lp != NULL && !thunar_vfs_job_cancelled (job); lp = lp->next)
710
 
    {
711
 
      /* update the progress information */
712
 
      _thunar_vfs_job_process_path (job, lp);
713
 
 
714
 
      /* try to create the target directory
715
 
       *
716
 
       * Note that the mode specified here is limited by the user's umask, so we will not
717
 
       * actually be creating a world writable directory unless the user's umask permits
718
 
       * it.
719
 
       */
720
 
      if (!_thunar_vfs_io_ops_mkdir (lp->data, 0777, THUNAR_VFS_IO_OPS_NONE, error))
721
 
        return FALSE;
722
 
    }
723
 
 
724
 
  /* emit the "new-files" signal */
725
 
  _thunar_vfs_job_new_files (job, path_list);
726
 
  return TRUE;
727
 
}
728
 
 
729
 
 
730
 
 
731
 
/**
732
 
 * _thunar_vfs_io_jobs_unlink:
733
 
 * @job            : a #ThunarVfsJob.
734
 
 * @param_values   : exactly one #GValue with the #GList of #ThunarVfsPath<!---->s
735
 
 *                   for the files and folders which should be unlinked recursively.
736
 
 * @n_param_values : the number of #GValue<!---->s in @param_values, must be exactly
737
 
 *                   one for this job.
738
 
 * @error          : return location for errors or %NULL.
739
 
 *
740
 
 * Recursively unlinks all #ThunarVfsPath<!---->s specified in @param_values.
741
 
 *
742
 
 * Return value: %TRUE if the unlink operation succeed, %FALSE otherwise.
743
 
 **/
744
 
gboolean
745
 
_thunar_vfs_io_jobs_unlink (ThunarVfsJob *job,
746
 
                            const GValue *param_values,
747
 
                            guint         n_param_values,
748
 
                            GError      **error)
749
 
{
750
 
  gboolean retry;
751
 
  GError  *err = NULL;
752
 
  GList   *path_list;
753
 
  GList   *lp;
754
 
 
755
 
  _thunar_vfs_return_val_if_fail (G_VALUE_HOLDS (&param_values[0], THUNAR_VFS_TYPE_PATH_LIST), FALSE);
756
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, FALSE);
757
 
  _thunar_vfs_return_val_if_fail (THUNAR_VFS_IS_JOB (job), FALSE);
758
 
  _thunar_vfs_return_val_if_fail (n_param_values == 1, FALSE);
759
 
 
760
 
  /* collect the paths for the removal */
761
 
  path_list = tvij_collect_nofollow (job, g_value_get_boxed (&param_values[0]), &err);
762
 
  if (G_UNLIKELY (err != NULL))
763
 
    {
764
 
      /* propagate the error */
765
 
      g_propagate_error (error, err);
766
 
      return FALSE;
767
 
    }
768
 
 
769
 
  /* we know the total list of paths to process */
770
 
  _thunar_vfs_job_total_paths (job, path_list);
771
 
 
772
 
  /* perform the actual removal of the paths */
773
 
  for (lp = path_list; lp != NULL && !thunar_vfs_job_cancelled (job); lp = lp->next)
774
 
    {
775
 
      /* update the progress information */
776
 
      _thunar_vfs_job_process_path (job, lp);
777
 
 
778
 
      /* skip the root folders, which cannot be deleted anyway */
779
 
      if (G_LIKELY (!thunar_vfs_path_is_root (lp->data)))
780
 
        {
781
 
again:    /* remove the file for the current path */
782
 
          if (!_thunar_vfs_io_ops_remove (lp->data, THUNAR_VFS_IO_OPS_IGNORE_ENOENT, &err))
783
 
            {
784
 
              /* ask the user whether to skip the file */
785
 
              retry = (_thunar_vfs_job_ask_skip (job, "%s", err->message) == THUNAR_VFS_JOB_RESPONSE_RETRY);
786
 
 
787
 
              /* clear the error */
788
 
              g_clear_error (&err);
789
 
 
790
 
              /* check whether to retry */
791
 
              if (G_UNLIKELY (retry))
792
 
                goto again;
793
 
            }
794
 
        }
795
 
    }
796
 
 
797
 
  /* release the path list */
798
 
  thunar_vfs_path_list_free (path_list);
799
 
 
800
 
  return TRUE;
801
 
}
802
 
 
803
 
 
804
 
 
805
 
#define __THUNAR_VFS_IO_JOBS_C__
806
 
#include <thunar-vfs/thunar-vfs-aliasdef.c>