~noskcaj/ubuntu/vivid/thunar/1.6.4

« back to all changes in this revision

Viewing changes to thunar-vfs/thunar-vfs-path.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_ERRNO_H
26
 
#include <errno.h>
27
 
#endif
28
 
#ifdef HAVE_MEMORY_H
29
 
#include <memory.h>
30
 
#endif
31
 
#ifdef HAVE_STRING_H
32
 
#include <string.h>
33
 
#endif
34
 
 
35
 
/* implement thunar-vfs-path's inline functions */
36
 
#define G_IMPLEMENT_INLINES 1
37
 
#define __THUNAR_VFS_PATH_C__
38
 
#include <thunar-vfs/thunar-vfs-path.h>
39
 
 
40
 
#include <thunar-vfs/thunar-vfs-io-trash.h>
41
 
#include <thunar-vfs/thunar-vfs-private.h>
42
 
#include <thunar-vfs/thunar-vfs-util.h>
43
 
#include <thunar-vfs/thunar-vfs-alias.h>
44
 
 
45
 
 
46
 
 
47
 
/* Masks to handle the 4-byte aligned path names */
48
 
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
49
 
#define THUNAR_VFS_PATH_MASK (((gsize) 0xffu) << ((sizeof (gsize) - 1) * 8))
50
 
#define THUNAR_VFS_PATH_ROOT (0x2fu)
51
 
#elif G_BYTE_ORDER == G_BIG_ENDIAN
52
 
#define THUNAR_VFS_PATH_MASK (0xffu)
53
 
#define THUNAR_VFS_PATH_ROOT (((gsize) 0x2fu) << ((sizeof (gsize) - 1) * 8))
54
 
#else
55
 
#error "Unsupported endianess"
56
 
#endif
57
 
 
58
 
 
59
 
 
60
 
static guint thunar_vfs_path_escape_uri_length  (const ThunarVfsPath *path);
61
 
static guint thunar_vfs_path_escape_uri         (const ThunarVfsPath *path,
62
 
                                                 gchar               *buffer);
63
 
 
64
 
 
65
 
 
66
 
/* A table of the ASCII chars from space (32) to DEL (127) */
67
 
static const guchar ACCEPTABLE_URI_CHARS[96] = {
68
 
  /*      !    "    #    $    %    &    '    (    )    *    +    ,    -    .    / */ 
69
 
  0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C,
70
 
  /* 0    1    2    3    4    5    6    7    8    9    :    ;    <    =    >    ? */
71
 
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20,
72
 
  /* @    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O */
73
 
  0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
74
 
  /* P    Q    R    S    T    U    V    W    X    Y    Z    [    \    ]    ^    _ */
75
 
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F,
76
 
  /* `    a    b    c    d    e    f    g    h    i    j    k    l    m    n    o */
77
 
  0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
78
 
  /* p    q    r    s    t    u    v    w    x    y    z    {    |    }    ~  DEL */
79
 
  0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20
80
 
};
81
 
 
82
 
/* List of hexadecimal characters */
83
 
static const gchar HEX_CHARS[16] = "0123456789ABCDEF";
84
 
 
85
 
#define ACCEPTABLE_URI_CHAR(c) ((c) >= 32 && (c) < 128 && (ACCEPTABLE_URI_CHARS[(c) - 32] & 0x08))
86
 
 
87
 
 
88
 
 
89
 
/* Debugging support in ThunarVfsPath */
90
 
#ifdef G_ENABLE_DEBUG
91
 
 
92
 
G_LOCK_DEFINE_STATIC (debug_paths);
93
 
static GList *debug_paths = NULL;
94
 
 
95
 
#define THUNAR_VFS_PATH_DEBUG_INSERT(path)                                                        \
96
 
G_STMT_START{                                                                                     \
97
 
  G_LOCK (debug_paths);                                                                           \
98
 
  debug_paths = g_list_prepend (debug_paths, (path));                                             \
99
 
  G_UNLOCK (debug_paths);                                                                         \
100
 
}G_STMT_END
101
 
 
102
 
#define THUNAR_VFS_PATH_DEBUG_REMOVE(path)                                                        \
103
 
G_STMT_START{                                                                                     \
104
 
  G_LOCK (debug_paths);                                                                           \
105
 
  debug_paths = g_list_remove (debug_paths, (path));                                              \
106
 
  G_UNLOCK (debug_paths);                                                                         \
107
 
}G_STMT_END
108
 
 
109
 
#define THUNAR_VFS_PATH_DEBUG_SHUTDOWN()                                                          \
110
 
G_STMT_START{                                                                                     \
111
 
  if (G_UNLIKELY (debug_paths != NULL))                                                           \
112
 
    {                                                                                             \
113
 
      GList *lp;                                                                                  \
114
 
      gchar *uri;                                                                                 \
115
 
      gint   n;                                                                                   \
116
 
      G_LOCK (debug_paths);                                                                       \
117
 
      g_print ("--- Leaked a total of %u ThunarVfsPath objects:\n", g_list_length (debug_paths)); \
118
 
      for (lp = debug_paths; lp != NULL; lp = lp->next)                                           \
119
 
        {                                                                                         \
120
 
          uri = thunar_vfs_path_dup_string (lp->data);                                            \
121
 
          n = ((ThunarVfsPath *) lp->data)->ref_count;                                            \
122
 
          g_print ("--> %s (%d)\n", uri, (n & ~THUNAR_VFS_PATH_SCHEME_MASK));                     \
123
 
          g_free (uri);                                                                           \
124
 
        }                                                                                         \
125
 
      G_UNLOCK (debug_paths);                                                                     \
126
 
    }                                                                                             \
127
 
}G_STMT_END
128
 
 
129
 
#else /* !G_ENABLE_DEBUG */
130
 
 
131
 
#define THUNAR_VFS_PATH_DEBUG_INSERT(path)  G_STMT_START{ (void)0; }G_STMT_END
132
 
#define THUNAR_VFS_PATH_DEBUG_REMOVE(path)  G_STMT_START{ (void)0; }G_STMT_END
133
 
#define THUNAR_VFS_PATH_DEBUG_SHUTDOWN()    G_STMT_START{ (void)0; }G_STMT_END
134
 
 
135
 
#endif /* !G_ENABLE_DEBUG */
136
 
 
137
 
 
138
 
 
139
 
/* components of the path to the users home folder */
140
 
static ThunarVfsPath **home_components;
141
 
static guint           n_home_components;
142
 
 
143
 
/**
144
 
 * _thunar_vfs_path_trash_root:
145
 
 *
146
 
 * The shared instance of the #ThunarVfsPath that points to the
147
 
 * trash root folder.
148
 
 **/
149
 
ThunarVfsPath *_thunar_vfs_path_trash_root = NULL;
150
 
 
151
 
 
152
 
 
153
 
static guint
154
 
thunar_vfs_path_escape_uri_length (const ThunarVfsPath *path)
155
 
{
156
 
  const guchar *s;
157
 
  guint         base_length;
158
 
  guint         length;
159
 
 
160
 
  /* determine the base length for the scheme (file:/// or trash:///) */
161
 
  length = base_length = _thunar_vfs_path_is_local (path) ? 8 : 9;
162
 
 
163
 
  /* determine the length for the path part */
164
 
  for (; path->parent != NULL; path = path->parent)
165
 
    {
166
 
      /* prepend a path separator */
167
 
      if (length > base_length)
168
 
        length += 1;
169
 
 
170
 
      for (s = (const guchar *) thunar_vfs_path_get_name (path); *s != '\0'; ++s)
171
 
        length += ACCEPTABLE_URI_CHAR (*s) ? 1 : 3;
172
 
    }
173
 
 
174
 
  return length;
175
 
}
176
 
 
177
 
 
178
 
 
179
 
static guint
180
 
thunar_vfs_path_escape_uri (const ThunarVfsPath *path,
181
 
                            gchar               *buffer)
182
 
{
183
 
  typedef struct _ThunarVfsPathItem
184
 
  {
185
 
    const ThunarVfsPath       *path;
186
 
    struct _ThunarVfsPathItem *next;
187
 
  } ThunarVfsPathItem;
188
 
 
189
 
  ThunarVfsPathItem *item;
190
 
  ThunarVfsPathItem *root = NULL;
191
 
  const gchar       *s;
192
 
  guchar             c;
193
 
  gchar             *t;
194
 
 
195
 
  /* prepend 'trash:///' or 'file:///' string (using a simple optimization on i386/ppc) */
196
 
  if (G_LIKELY (thunar_vfs_path_get_scheme (path) == THUNAR_VFS_PATH_SCHEME_FILE))
197
 
    {
198
 
#if defined(__GNUC__) && (defined(__i386__) || defined(__ppc__))
199
 
      ((guint32 *) buffer)[0] = ((const guint32 *) "file:///")[0];
200
 
      ((guint32 *) buffer)[1] = ((const guint32 *) "file:///")[1];
201
 
#else
202
 
      /* hopefully the compiler will be able to optimize this */
203
 
      buffer[0] = 'f'; buffer[1] = 'i';
204
 
      buffer[2] = 'l'; buffer[3] = 'e';
205
 
      buffer[4] = ':'; buffer[5] = '/';
206
 
      buffer[6] = '/'; buffer[7] = '/';
207
 
#endif
208
 
      t = buffer + 8;
209
 
    }
210
 
  else
211
 
    {
212
 
#if defined(__GNUC__) && (defined(__i386__) || defined(__ppc__))
213
 
      ((guint32 *) buffer)[0] = ((const guint32 *) "trash://")[0];
214
 
      ((guint32 *) buffer)[1] = ((const guint32 *) "trash://")[1];
215
 
#else
216
 
      /* hopefully the compiler will be able to optimize this */
217
 
      buffer[0] = 't'; buffer[1] = 'r';
218
 
      buffer[2] = 'a'; buffer[3] = 's';
219
 
      buffer[4] = 'h'; buffer[5] = ':';
220
 
      buffer[6] = '/'; buffer[7] = '/';
221
 
#endif
222
 
      buffer[8] = '/';
223
 
      t = buffer + 9;
224
 
    }
225
 
 
226
 
  /* generate the path item list (reverse parent relation) */
227
 
  for (; path->parent != NULL; path = path->parent)
228
 
    {
229
 
      item = g_newa (ThunarVfsPathItem, 1);
230
 
      item->path = path;
231
 
      item->next = root;
232
 
      root = item;
233
 
    }
234
 
 
235
 
  /* generate the uri */
236
 
  for (item = root; item != NULL; item = item->next)
237
 
    {
238
 
      /* append a '/' character */
239
 
      if (G_LIKELY (item != root))
240
 
        *t++ = '/';
241
 
 
242
 
      /* copy the path component name */
243
 
      for (s = thunar_vfs_path_get_name (item->path); *s != '\0'; ++s)
244
 
        {
245
 
          c = *((const guchar *) s);
246
 
          if (G_UNLIKELY (!ACCEPTABLE_URI_CHAR (c)))
247
 
            {
248
 
              *t++ = '%';
249
 
              *t++ = HEX_CHARS[c >> 4];
250
 
              *t++ = HEX_CHARS[c & 15];
251
 
            }
252
 
          else
253
 
            {
254
 
              *t++ = *s;
255
 
            }
256
 
        }
257
 
    }
258
 
 
259
 
  /* zero-terminate the URI */
260
 
  *t = '\0';
261
 
 
262
 
  return (t - buffer) + 1;
263
 
}
264
 
 
265
 
 
266
 
 
267
 
GType
268
 
thunar_vfs_path_get_type (void)
269
 
{
270
 
  static GType type = G_TYPE_INVALID;
271
 
 
272
 
  if (G_UNLIKELY (type == G_TYPE_INVALID))
273
 
    {
274
 
      type = g_boxed_type_register_static (I_("ThunarVfsPath"),
275
 
                                           (GBoxedCopyFunc) thunar_vfs_path_ref,
276
 
                                           (GBoxedFreeFunc) thunar_vfs_path_unref);
277
 
    }
278
 
 
279
 
  return type;
280
 
}
281
 
 
282
 
 
283
 
 
284
 
GType
285
 
thunar_vfs_path_list_get_type (void)
286
 
{
287
 
  static GType type = G_TYPE_INVALID;
288
 
 
289
 
  if (G_UNLIKELY (type == G_TYPE_INVALID))
290
 
    {
291
 
      type = g_boxed_type_register_static (I_("ThunarVfsPathList"),
292
 
                                           (GBoxedCopyFunc) thunar_vfs_path_list_copy,
293
 
                                           (GBoxedFreeFunc) thunar_vfs_path_list_free);
294
 
    }
295
 
 
296
 
  return type;
297
 
}
298
 
 
299
 
 
300
 
 
301
 
/**
302
 
 * thunar_vfs_path_new:
303
 
 * @identifier : an URI identifier or an absolute path.
304
 
 * @error      : return location for errors or %NULL.
305
 
 *
306
 
 * Returns a #ThunarVfsPath that represents the given
307
 
 * @identifier or %NULL on error. In the latter case
308
 
 * @error will be set to point to an #GError describing
309
 
 * the problem.
310
 
 * 
311
 
 * The caller is responsible to free the returned
312
 
 * object using thunar_vfs_path_unref() when no
313
 
 * longer needed.
314
 
 *
315
 
 * Return value: the #ThunarVfsPath for @identifier
316
 
 *               or %NULL on error.
317
 
 **/
318
 
ThunarVfsPath*
319
 
thunar_vfs_path_new (const gchar *identifier,
320
 
                     GError     **error)
321
 
{
322
 
  ThunarVfsPath *path = home_components[0]; /* default to file system root */
323
 
  const gchar   *s;
324
 
  const gchar   *s1;
325
 
  const gchar   *s2;
326
 
  gchar         *filename;
327
 
  gchar         *t;
328
 
  guint          n;
329
 
 
330
 
  /* check if we have an absolute path or an URI */
331
 
  if (G_UNLIKELY (*identifier != G_DIR_SEPARATOR))
332
 
    {
333
 
      /* treat the identifier as URI */
334
 
      filename = g_filename_from_uri (identifier, NULL, NULL);
335
 
      if (G_UNLIKELY (filename == NULL))
336
 
        {
337
 
          /* hey, but maybe it's a trash:-URI */
338
 
          if (G_LIKELY (identifier[0] == 't' && identifier[1] == 'r' && identifier[2] == 'a'
339
 
                     && identifier[3] == 's' && identifier[4] == 'h' && identifier[5] == ':'))
340
 
            {
341
 
              /* skip slashes (yes, not dir separators) */
342
 
              for (s = identifier + 6; *s == '/'; ++s)
343
 
                ;
344
 
 
345
 
              /* start at the trash root folder */
346
 
              path = _thunar_vfs_path_trash_root;
347
 
 
348
 
              /* check if it's the trash root folder */
349
 
              if (G_LIKELY (*s == '\0'))
350
 
                return thunar_vfs_path_ref (path);
351
 
 
352
 
              /* try to interpret the file part */
353
 
              t = g_strconcat ("file:/", s, NULL);
354
 
              filename = g_filename_from_uri (t, NULL, NULL);
355
 
              g_free (t);
356
 
            }
357
 
        }
358
 
 
359
 
      /* check if the URI is invalid */
360
 
      if (G_UNLIKELY (filename == NULL))
361
 
        {
362
 
          g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_BAD_URI, _("The URI \"%s\" is invalid"), identifier);
363
 
          return NULL;
364
 
        }
365
 
    }
366
 
  else
367
 
    {
368
 
      /* canonicalize the absolute path, to remove additional slashes and dots */
369
 
      filename = thunar_vfs_canonicalize_filename (identifier);
370
 
    }
371
 
 
372
 
  /* parse the filename to a ThunarVfsPath */
373
 
  s = filename + 1;
374
 
 
375
 
  /* local paths may be relative to a home component */
376
 
  if (G_LIKELY (path == home_components[0]))
377
 
    {
378
 
      /* start at the root path */
379
 
      for (n = 1; n < n_home_components; ++n)
380
 
        {
381
 
          /* skip additional slashes */
382
 
          for (; G_UNLIKELY (*s == G_DIR_SEPARATOR); ++s)
383
 
            ;
384
 
 
385
 
          /* check if we have reached the end of the filename */
386
 
          if (G_UNLIKELY (*s == '\0'))
387
 
            break;
388
 
 
389
 
          /* check if the path component equals the next home path component */
390
 
          for (s1 = thunar_vfs_path_get_name (home_components[n]), s2 = s; *s1 != '\0' && *s1 == *s2; ++s1, ++s2)
391
 
            ;
392
 
          if (*s1 != '\0' || (*s2 != '\0' && *s2 != G_DIR_SEPARATOR))
393
 
            break;
394
 
 
395
 
          /* go on with the next home path component */
396
 
          path = home_components[n];
397
 
          s = s2;
398
 
        }
399
 
    }
400
 
 
401
 
  /* determine the subpath (takes appropriate references) */
402
 
  path = _thunar_vfs_path_new_relative (path, s);
403
 
 
404
 
  /* cleanup */
405
 
  g_free (filename);
406
 
 
407
 
  return path;
408
 
}
409
 
 
410
 
 
411
 
 
412
 
/**
413
 
 * thunar_vfs_path_get_for_home:
414
 
 *
415
 
 * Returns the #ThunarVfsPath that represents
416
 
 * the current users home directory.
417
 
 *
418
 
 * The caller is responsible to free the
419
 
 * returned object using thunar_vfs_path_unref()
420
 
 * when no longer needed.
421
 
 *
422
 
 * Return value: the #ThunarVfsPath for the 
423
 
 *               current users home directory.
424
 
 **/
425
 
ThunarVfsPath*
426
 
thunar_vfs_path_get_for_home (void)
427
 
{
428
 
  return thunar_vfs_path_ref (home_components[n_home_components - 1]);
429
 
}
430
 
 
431
 
 
432
 
 
433
 
/**
434
 
 * thunar_vfs_path_get_for_root:
435
 
 *
436
 
 * Returns the #ThunarVfsPath that represents the
437
 
 * file systems root folder.
438
 
 *
439
 
 * The caller is responsible to free the returned
440
 
 * object using thunar_vfs_path_unref() when no
441
 
 * longer needed.
442
 
 *
443
 
 * Return value: the #ThunarVfsPath for the file
444
 
 *               systems root directory.
445
 
 **/
446
 
ThunarVfsPath*
447
 
thunar_vfs_path_get_for_root (void)
448
 
{
449
 
  return thunar_vfs_path_ref (home_components[0]);
450
 
}
451
 
 
452
 
 
453
 
 
454
 
/**
455
 
 * thunar_vfs_path_get_for_trash:
456
 
 *
457
 
 * Returns the #ThunarVfsPath that represents the
458
 
 * trash root folder.
459
 
 *
460
 
 * The caller is responsible to free the returned
461
 
 * object using thunar_vfs_path_unref() when no
462
 
 * longer needed.
463
 
 *
464
 
 * Return value: the #ThunarVfsPath for the trash
465
 
 *               root folder.
466
 
 **/
467
 
ThunarVfsPath*
468
 
thunar_vfs_path_get_for_trash (void)
469
 
{
470
 
  return thunar_vfs_path_ref (_thunar_vfs_path_trash_root);
471
 
}
472
 
 
473
 
 
474
 
 
475
 
/**
476
 
 * thunar_vfs_path_unref:
477
 
 * @path : a #ThunarVfsPath.
478
 
 *
479
 
 * Decreases the reference count on @path and
480
 
 * frees the resources allocated for @path
481
 
 * once the reference count drops to zero.
482
 
 **/
483
 
void
484
 
thunar_vfs_path_unref (ThunarVfsPath *path)
485
 
{
486
 
  ThunarVfsPath *parent;
487
 
  const gsize   *p;
488
 
 
489
 
  while (path != NULL && (g_atomic_int_exchange_and_add (&path->ref_count, -1) & ~THUNAR_VFS_PATH_SCHEME_MASK) == 1)
490
 
    {
491
 
      /* verify that we don't free the paths for trash root or home components */
492
 
#ifdef G_ENABLE_DEBUG
493
 
      if (G_UNLIKELY (path == _thunar_vfs_path_trash_root))
494
 
        {
495
 
          /* the trash root path may not be freed */
496
 
          g_error (G_STRLOC ": Attempt to free the trash root path detected");
497
 
        }
498
 
      else
499
 
        {
500
 
          /* same for the home component paths */
501
 
          guint n;
502
 
          for (n = 0; n < n_home_components; ++n)
503
 
            if (G_UNLIKELY (path == home_components[n]))
504
 
              break;
505
 
 
506
 
          /* check if one of the home components matched */
507
 
          if (G_UNLIKELY (n < n_home_components))
508
 
            {
509
 
              /* none of the home component paths can be freed this way */
510
 
              g_error (G_STRLOC ": Attempt to free the home component path \"%s\" detected", thunar_vfs_path_get_name (path));
511
 
            }
512
 
        }
513
 
#endif
514
 
 
515
 
      /* remember the parent path */
516
 
      parent = path->parent;
517
 
 
518
 
      /* remove the path from the debug list */
519
 
      THUNAR_VFS_PATH_DEBUG_REMOVE (path);
520
 
 
521
 
      /* release the path resources (we need to determine the size for the slice allocator) */
522
 
      for (p = (const gsize *) thunar_vfs_path_get_name (path); (*p & THUNAR_VFS_PATH_MASK) != 0u; ++p)
523
 
        ;
524
 
      _thunar_vfs_slice_free1 (((const guint8 *) (p + 1)) - ((const guint8 *) path), path);
525
 
 
526
 
      /* continue with the parent */
527
 
      path = parent;
528
 
    }
529
 
}
530
 
 
531
 
 
532
 
 
533
 
/**
534
 
 * thunar_vfs_path_hash:
535
 
 * @path_ptr : a #ThunarVfsPath.
536
 
 *
537
 
 * Generates a hash value for the given @path_ptr.
538
 
 *
539
 
 * Return value: the hash value for @path_ptr.
540
 
 **/
541
 
guint
542
 
thunar_vfs_path_hash (gconstpointer path_ptr)
543
 
{
544
 
  const gchar *p = thunar_vfs_path_get_name (path_ptr);
545
 
  guint        h = *p + thunar_vfs_path_get_scheme (path_ptr);
546
 
 
547
 
  /* hash the last path component (which cannot be empty) */
548
 
  while (*++p != '\0')
549
 
    h = (h << 5) - h + *p;
550
 
 
551
 
  return h;
552
 
}
553
 
 
554
 
 
555
 
 
556
 
/**
557
 
 * thunar_vfs_path_equal:
558
 
 * @path_ptr_a : first #ThunarVfsPath.
559
 
 * @path_ptr_b : second #ThunarVfsPath.
560
 
 *
561
 
 * Checks whether @path_ptr_a and @path_ptr_b refer
562
 
 * to the same local path.
563
 
 *
564
 
 * Return value: %TRUE if @path_ptr_a and @path_ptr_b
565
 
 *               are equal.
566
 
 **/
567
 
gboolean
568
 
thunar_vfs_path_equal (gconstpointer path_ptr_a,
569
 
                       gconstpointer path_ptr_b)
570
 
{
571
 
  const ThunarVfsPath *path_a = path_ptr_a;
572
 
  const ThunarVfsPath *path_b = path_ptr_b;
573
 
  const gsize         *a;
574
 
  const gsize         *b;
575
 
 
576
 
  /* compare the schemes */
577
 
  if (thunar_vfs_path_get_scheme (path_a) != thunar_vfs_path_get_scheme (path_b))
578
 
    return FALSE;
579
 
 
580
 
again:
581
 
  /* check if the paths are the same object */
582
 
  if (G_UNLIKELY (path_a == path_b))
583
 
    return TRUE;
584
 
 
585
 
  /* compare the last path component */
586
 
  a = (const gsize *) thunar_vfs_path_get_name (path_a);
587
 
  b = (const gsize *) thunar_vfs_path_get_name (path_b);
588
 
  for (;;)
589
 
    {
590
 
      if (*a != *b)
591
 
        return FALSE;
592
 
      else if ((*a & THUNAR_VFS_PATH_MASK) == 0u)
593
 
        break;
594
 
 
595
 
      ++a;
596
 
      ++b;
597
 
    }
598
 
 
599
 
  /* compare the parent path components */
600
 
  if (G_LIKELY (path_a->parent != NULL && path_b->parent != NULL))
601
 
    {
602
 
      path_a = path_a->parent;
603
 
      path_b = path_b->parent;
604
 
      goto again;
605
 
    }
606
 
 
607
 
  /* verify that both paths have no parents then */
608
 
  return (path_a->parent == NULL && path_b->parent == NULL);
609
 
}
610
 
 
611
 
 
612
 
 
613
 
/**
614
 
 * thunar_vfs_path_relative:
615
 
 * @parent : a #ThunarVfsPath.
616
 
 * @name   : a valid filename in the local file system encoding.
617
 
 *
618
 
 * Returns a #ThunarVfsPath for the file @name relative to
619
 
 * @parent. @name must be a valid filename in the local file
620
 
 * system encoding and it may not contain any slashes.
621
 
 *
622
 
 * The caller is responsible to free the returned object
623
 
 * using thunar_vfs_path_unref() when no longer needed.
624
 
 *
625
 
 * Return value: the path to @name relative to @parent.
626
 
 **/
627
 
ThunarVfsPath*
628
 
thunar_vfs_path_relative (ThunarVfsPath *parent,
629
 
                          const gchar   *name)
630
 
{
631
 
  g_return_val_if_fail (parent != NULL, NULL);
632
 
  g_return_val_if_fail (name != NULL, NULL);
633
 
  g_return_val_if_fail (*name != '\0', NULL);
634
 
  g_return_val_if_fail (strchr (name, '/') == NULL, NULL);
635
 
 
636
 
  /* let _thunar_vfs_path_child() do it's work */
637
 
  return _thunar_vfs_path_child (parent, name);
638
 
}
639
 
 
640
 
 
641
 
 
642
 
/**
643
 
 * thunar_vfs_path_is_ancestor:
644
 
 * @path     : a #ThunarVfsPath.
645
 
 * @ancestor : another #ThunarVfsPath.
646
 
 *
647
 
 * Determines whether @path is somewhere below @ancestor,
648
 
 * possible with intermediate folders.
649
 
 *
650
 
 * Return value: %TRUE if @ancestor contains @path as a
651
 
 *               child, grandchild, great grandchild, etc.
652
 
 **/
653
 
gboolean
654
 
thunar_vfs_path_is_ancestor (const ThunarVfsPath *path,
655
 
                             const ThunarVfsPath *ancestor)
656
 
{
657
 
  g_return_val_if_fail (path != NULL, FALSE);
658
 
  g_return_val_if_fail (ancestor != NULL, FALSE);
659
 
 
660
 
  for (path = path->parent; path != NULL; path = path->parent)
661
 
    if (thunar_vfs_path_equal (path, ancestor))
662
 
      return TRUE;
663
 
 
664
 
  return FALSE;
665
 
}
666
 
 
667
 
 
668
 
 
669
 
/**
670
 
 * thunar_vfs_path_is_home:
671
 
 * @path : a #ThunarVfsPath.
672
 
 *
673
 
 * Checks whether @path refers to the users home
674
 
 * directory.
675
 
 *
676
 
 * Return value: %TRUE if @path refers to the users
677
 
 *               home directory.
678
 
 **/
679
 
gboolean
680
 
thunar_vfs_path_is_home (const ThunarVfsPath *path)
681
 
{
682
 
  g_return_val_if_fail (path != NULL, FALSE);
683
 
  return (path == home_components[n_home_components - 1]);
684
 
}
685
 
 
686
 
 
687
 
 
688
 
/**
689
 
 * thunar_vfs_path_dup_string:
690
 
 * @path : a #ThunarVfsPath.
691
 
 *
692
 
 * Like thunar_vfs_path_to_string(), this function transform
693
 
 * the @path to its string representation, but unlike
694
 
 * thunar_vfs_path_to_string(), this function automatically
695
 
 * allocates the required amount of memory from the heap.
696
 
 * The returned string must be freed by the caller when
697
 
 * no longer needed.
698
 
 *
699
 
 * Return value: the string representation of @path.
700
 
 **/
701
 
gchar*
702
 
thunar_vfs_path_dup_string (const ThunarVfsPath *path)
703
 
{
704
 
  const ThunarVfsPath *p;
705
 
  gchar               *s;
706
 
  guint                n;
707
 
 
708
 
  /* determine the number of bytes required to
709
 
   * store the path's string representation.
710
 
   */
711
 
  for (n = 0, p = path; p != NULL; p = p->parent)
712
 
    n += strlen (thunar_vfs_path_get_name (p)) + 2;
713
 
 
714
 
  /* allocate the buffer to store the string */
715
 
  s = g_malloc (n);
716
 
 
717
 
  /* store the path string to the buffer */
718
 
  thunar_vfs_path_to_string (path, s, n, NULL);
719
 
 
720
 
  /* return the string buffer */
721
 
  return s;
722
 
}
723
 
 
724
 
 
725
 
 
726
 
/**
727
 
 * thunar_vfs_path_to_string:
728
 
 * @path    : a #ThunarVfsPath.
729
 
 * @buffer  : the buffer to store the path string to.
730
 
 * @bufsize : the size of @buffer in bytes.
731
 
 * @error   : return location for errors or %NULL.
732
 
 *
733
 
 * Stores the @path into the string pointed to by @buffer,
734
 
 * so it can be used for system path operations. Returns
735
 
 * the number of bytes stored to @buffer or a negative
736
 
 * value if @bufsize is too small to store the whole @path.
737
 
 * In the latter case @error will be set to point to an
738
 
 * error describing the problem.
739
 
 *
740
 
 * If @buffer is allocated on the stack, it is suggested
741
 
 * to use #THUNAR_VFS_PATH_MAXSTRLEN for the buffer size
742
 
 * in most cases. The stack should never be used in recursive
743
 
 * functions; use thunar_vfs_path_dup_string() instead there.
744
 
 *
745
 
 * Return value: the number of bytes (including the null
746
 
 *               byte) stored to @buffer or a negative
747
 
 *               value if @buffer cannot hold the whole
748
 
 *               @path.
749
 
 **/
750
 
gssize
751
 
thunar_vfs_path_to_string (const ThunarVfsPath *path,
752
 
                           gchar               *buffer,
753
 
                           gsize                bufsize,
754
 
                           GError             **error)
755
 
{
756
 
  typedef struct _ThunarVfsPathItem
757
 
  {
758
 
    const gchar               *name;
759
 
    struct _ThunarVfsPathItem *next;
760
 
  } ThunarVfsPathItem;
761
 
 
762
 
  ThunarVfsPathItem *items = NULL;
763
 
  ThunarVfsPathItem *item;
764
 
  const gchar       *name;
765
 
  gchar             *bp;
766
 
  guint              n;
767
 
 
768
 
  g_return_val_if_fail (buffer != NULL, -1);
769
 
  g_return_val_if_fail (bufsize > 0, -1);
770
 
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
771
 
 
772
 
  /* the root element is a special case to ease the processing */
773
 
  if (G_UNLIKELY (path->parent == NULL))
774
 
    {
775
 
      if (G_UNLIKELY (bufsize < 2))
776
 
        goto error;
777
 
      buffer[0] = G_DIR_SEPARATOR;
778
 
      buffer[1] = '\0';
779
 
      return 2;
780
 
    }
781
 
 
782
 
  /* determine the buffer size required for the path buffer */
783
 
  for (n = 1; path->parent != NULL; path = path->parent)
784
 
    {
785
 
      /* add the path to the item list */
786
 
      item = g_newa (ThunarVfsPathItem, 1);
787
 
      item->name = thunar_vfs_path_get_name (path);
788
 
      item->next = items;
789
 
      items = item;
790
 
 
791
 
      /* add the size constraint (including the '/') */
792
 
      n += strlen (item->name) + 1;
793
 
    }
794
 
 
795
 
  /* verify the buffer size */
796
 
  if (G_UNLIKELY (bufsize < n))
797
 
    {
798
 
error:
799
 
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NAMETOOLONG,
800
 
                   _("Path too long to fit into buffer"));
801
 
      return -1;
802
 
    }
803
 
 
804
 
  /* generate the path string */
805
 
  for (bp = buffer, item = items; item != NULL; item = item->next)
806
 
    {
807
 
      /* prepend the path separator */
808
 
      *bp++ = G_DIR_SEPARATOR;
809
 
 
810
 
      /* append the component name */
811
 
      for (name = item->name; *name != '\0'; )
812
 
        *bp++ = *name++;
813
 
    }
814
 
 
815
 
  /* append the string terminator */
816
 
  *bp = '\0';
817
 
 
818
 
   /* return the number of bytes written to the buffer */
819
 
  return n;
820
 
}
821
 
 
822
 
 
823
 
 
824
 
/**
825
 
 * thunar_vfs_path_dup_uri:
826
 
 * @path : a #ThunarVfsPath.
827
 
 *
828
 
 * Similar to thunar_vfs_path_to_uri(), but automatically
829
 
 * allocates memory on the heap instead of using a user
830
 
 * supplied buffer for the URI.
831
 
 *
832
 
 * The caller is responsible to free the returned string
833
 
 * using g_free() when no longer needed.
834
 
 *
835
 
 * Return value: the escaped URI for @path.
836
 
 **/
837
 
gchar*
838
 
thunar_vfs_path_dup_uri (const ThunarVfsPath *path)
839
 
{
840
 
  gchar *s;
841
 
  guint  m;
842
 
  guint  n;
843
 
 
844
 
  g_return_val_if_fail (path != NULL, NULL);
845
 
 
846
 
  /* calculate the length of the uri string */
847
 
  n = thunar_vfs_path_escape_uri_length (path) + 1;
848
 
 
849
 
  /* escape the path to an uri string */
850
 
  s = g_malloc (sizeof (gchar) * n);
851
 
  m = thunar_vfs_path_escape_uri (path, s);
852
 
 
853
 
  /* verify the result */
854
 
  _thunar_vfs_assert (strlen (s) == m - 1);
855
 
  _thunar_vfs_assert (m == n);
856
 
 
857
 
  return s;
858
 
}
859
 
 
860
 
 
861
 
 
862
 
/**
863
 
 * thunar_vfs_path_to_uri:
864
 
 * @path    : a #ThunarVfsPath.
865
 
 * @buffer  : the buffer to store the URI string to.
866
 
 * @bufsize : the size of @buffer in bytes.
867
 
 * @error   : return location for errors or %NULL.
868
 
 *
869
 
 * Escapes @path according to the rules of the file URI
870
 
 * specification and stores the escaped URI to @buffer.
871
 
 * Returns the number of bytes stored to @buffer or a
872
 
 * negative value if @bufsize is too small to store the
873
 
 * escaped URI. In the latter case @error will be set to
874
 
 * point to an #GError describing the problem.
875
 
 *
876
 
 * When using the stack for @buffer, it is suggested to
877
 
 * use #THUNAR_VFS_PATH_MAXURILEN for the buffer size in
878
 
 * most cases. The stack should never be used in recursive
879
 
 * functions; use thunar_vfs_path_dup_uri() instead there.
880
 
 *
881
 
 * Return value: the number of bytes (including the null
882
 
 *               byte) stored to @buffer or a negative
883
 
 *               value if @buffer cannot hold the URI.
884
 
 **/
885
 
gssize
886
 
thunar_vfs_path_to_uri (const ThunarVfsPath *path,
887
 
                        gchar               *buffer,
888
 
                        gsize                bufsize,
889
 
                        GError             **error)
890
 
{
891
 
  guint n;
892
 
  guint m;
893
 
 
894
 
  g_return_val_if_fail (path != NULL, -1);
895
 
  g_return_val_if_fail (buffer != NULL, -1);
896
 
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
897
 
 
898
 
  /* verify that the buffer is large enough */
899
 
  n = thunar_vfs_path_escape_uri_length (path) + 1;
900
 
  if (G_UNLIKELY (bufsize < n))
901
 
    {
902
 
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NAMETOOLONG,
903
 
                   _("URI too long to fit into buffer"));
904
 
      return -1;
905
 
    }
906
 
 
907
 
  /* copy the URI to the buffer */
908
 
  m = thunar_vfs_path_escape_uri (path, buffer);
909
 
 
910
 
  /* verify the result */
911
 
  _thunar_vfs_assert (strlen (buffer) == m - 1);
912
 
  _thunar_vfs_assert (m == n);
913
 
 
914
 
  return n;
915
 
}
916
 
 
917
 
 
918
 
 
919
 
/**
920
 
 * thunar_vfs_path_list_from_string:
921
 
 * @uri_string : a string representation of an URI list.
922
 
 * @error      : return location for errors.
923
 
 *
924
 
 * Splits an URI list conforming to the text/uri-list
925
 
 * mime type defined in RFC 2483 into individual URIs,
926
 
 * discarding any comments and whitespace.
927
 
 *
928
 
 * If all URIs were successfully parsed into #ThunarVfsPath
929
 
 * objects, the list of parsed URIs will be returned, and
930
 
 * you'll need to call thunar_vfs_path_list_free() to
931
 
 * release the list resources. Else if the parsing fails
932
 
 * at some point, %NULL will be returned and @error will
933
 
 * be set to describe the cause.
934
 
 *
935
 
 * Note, that if @string contains no URIs, this function
936
 
 * will also return %NULL, but @error won't be set. So
937
 
 * take care when checking for an error condition!
938
 
 *
939
 
 * Return value: the list of #ThunarVfsPath's or %NULL.
940
 
 **/
941
 
GList*
942
 
thunar_vfs_path_list_from_string (const gchar *uri_string,
943
 
                                  GError     **error)
944
 
{
945
 
  ThunarVfsPath *path;
946
 
  const gchar   *s;
947
 
  const gchar   *t;
948
 
  GList         *path_list = NULL;
949
 
  gchar         *identifier;
950
 
 
951
 
  for (s = uri_string; s != NULL; )
952
 
    {
953
 
      if (*s != '#')
954
 
        {
955
 
          while (g_ascii_isspace (*s))
956
 
            ++s;
957
 
 
958
 
          for (t = s; *t != '\0' && *t != '\n' && *t != '\r'; ++t)
959
 
            ;
960
 
 
961
 
          if (t > s)
962
 
            {
963
 
              for (t--; t > s && g_ascii_isspace (*t); t--)
964
 
                ;
965
 
 
966
 
              if (t > s)
967
 
                {
968
 
                  /* try to parse the URI */
969
 
                  identifier = g_strndup (s, t - s + 1);
970
 
                  path = thunar_vfs_path_new (identifier, error);
971
 
                  g_free (identifier);
972
 
 
973
 
                  /* check if we succeed */
974
 
                  if (G_UNLIKELY (path == NULL))
975
 
                    {
976
 
                      thunar_vfs_path_list_free (path_list);
977
 
                      return NULL;
978
 
                    }
979
 
                  else
980
 
                    {
981
 
                      /* append the newly parsed path */
982
 
                      path_list = g_list_append (path_list, path);
983
 
                    }
984
 
                }
985
 
            }
986
 
        }
987
 
 
988
 
      for (; *s != '\0' && *s != '\n'; ++s)
989
 
        ;
990
 
 
991
 
      if (*s++ == '\0')
992
 
        break;
993
 
    }
994
 
 
995
 
  return path_list;
996
 
}
997
 
 
998
 
 
999
 
 
1000
 
/**
1001
 
 * thunar_vfs_path_list_to_string:
1002
 
 * @path_list : a list of #ThunarVfsPath<!---->s.
1003
 
 *
1004
 
 * Free the returned value using g_free() when you
1005
 
 * are done with it.
1006
 
 *
1007
 
 * Return value: the string representation of @path_list conforming to the
1008
 
 *               text/uri-list mime type defined in RFC 2483.
1009
 
 **/
1010
 
gchar*
1011
 
thunar_vfs_path_list_to_string (GList *path_list)
1012
 
{
1013
 
  gchar *buffer;
1014
 
  gsize  bufsize = 512;
1015
 
  gsize  bufpos = 0;
1016
 
  GList *lp;
1017
 
  guint  n;
1018
 
 
1019
 
  /* allocate initial buffer */
1020
 
  buffer = g_malloc (bufsize + 1);
1021
 
 
1022
 
  for (lp = path_list; lp != NULL; lp = lp->next)
1023
 
    {
1024
 
      for (;;)
1025
 
        {
1026
 
          /* determine the size required to store the URI and the line break */
1027
 
          n = thunar_vfs_path_escape_uri_length (lp->data) + 2;
1028
 
          if (n > (bufsize - bufpos))
1029
 
            {
1030
 
              /* automatically increase the buffer */
1031
 
              bufsize += 512;
1032
 
              buffer = g_realloc (buffer, bufsize + 1);
1033
 
              continue;
1034
 
            }
1035
 
 
1036
 
          /* append the URI to the buffer */
1037
 
          n = thunar_vfs_path_escape_uri (lp->data, buffer + bufpos);
1038
 
 
1039
 
          /* shift the buffer position */
1040
 
          bufpos += (n - 1);
1041
 
 
1042
 
          /* append a line break */
1043
 
          buffer[bufpos++] = '\r';
1044
 
          buffer[bufpos++] = '\n';
1045
 
 
1046
 
          /* sanity checks */
1047
 
          _thunar_vfs_assert (bufpos <= bufsize);
1048
 
          break;
1049
 
        }
1050
 
    }
1051
 
 
1052
 
  /* zero terminate the string */
1053
 
  buffer[bufpos] = '\0';
1054
 
 
1055
 
  return buffer;
1056
 
}
1057
 
 
1058
 
 
1059
 
 
1060
 
/**
1061
 
 * thunar_vfs_path_list_copy:
1062
 
 * @path_list : a list of #ThunarVfsPath<!---->s.
1063
 
 *
1064
 
 * Takes a deep copy of @path_list and returns the
1065
 
 * result. The caller is responsible to free the
1066
 
 * returned list using thunar_vfs_path_list_free().
1067
 
 *
1068
 
 * Return value: a deep copy of @path_list.
1069
 
 **/
1070
 
GList*
1071
 
thunar_vfs_path_list_copy (GList *path_list)
1072
 
{
1073
 
  GList *list;
1074
 
  GList *lp;
1075
 
 
1076
 
  for (list = NULL, lp = g_list_last (path_list); lp != NULL; lp = lp->prev)
1077
 
    list = g_list_prepend (list, thunar_vfs_path_ref (lp->data));
1078
 
 
1079
 
  return list;
1080
 
}
1081
 
 
1082
 
 
1083
 
 
1084
 
/**
1085
 
 * thunar_vfs_path_list_free:
1086
 
 * @path_list : a list of #ThunarVfsPath<!---->s.
1087
 
 *
1088
 
 * Frees the #ThunarVfsPath<!---->s in @path_list and
1089
 
 * the @path_list itself.
1090
 
 **/
1091
 
void
1092
 
thunar_vfs_path_list_free (GList *path_list)
1093
 
{
1094
 
  GList *lp;
1095
 
  for (lp = path_list; lp != NULL; lp = lp->next)
1096
 
    thunar_vfs_path_unref (lp->data);
1097
 
  g_list_free (path_list);
1098
 
}
1099
 
 
1100
 
 
1101
 
 
1102
 
/**
1103
 
 * _thunar_vfs_path_init:
1104
 
 *
1105
 
 * Intialize the #ThunarVfsPath module.
1106
 
 **/
1107
 
void
1108
 
_thunar_vfs_path_init (void)
1109
 
{
1110
 
  ThunarVfsPath *path;
1111
 
  const gchar   *s;
1112
 
  gchar         *offset;
1113
 
  gchar        **components;
1114
 
  gchar        **component;
1115
 
  guint          n_bytes;
1116
 
  guint          n = 0;
1117
 
  gchar         *t;
1118
 
 
1119
 
  _thunar_vfs_return_if_fail (home_components == NULL);
1120
 
  _thunar_vfs_return_if_fail (n_home_components == 0);
1121
 
 
1122
 
  /* include the root element */
1123
 
  n_bytes = sizeof (ThunarVfsPath) + sizeof (gsize);
1124
 
  n_home_components = 1;
1125
 
 
1126
 
  /* split the home path into its components */
1127
 
  components = g_strsplit (g_get_home_dir (), "/", -1);
1128
 
  for (component = components; *component != NULL; ++component)
1129
 
    if (G_LIKELY (**component != '\0'))
1130
 
      {
1131
 
        n_bytes += sizeof (ThunarVfsPath) + ((strlen (*component) + sizeof (gsize)) / sizeof (gsize)) * sizeof (gsize);
1132
 
        n_home_components += 1;
1133
 
      }
1134
 
 
1135
 
  /* allocate the memory (including the pointer table overhead) */
1136
 
  home_components = g_malloc (n_bytes + n_home_components * sizeof (ThunarVfsPath *));
1137
 
  offset = ((gchar *) home_components) + n_home_components * sizeof (ThunarVfsPath *);
1138
 
 
1139
 
  /* add the root node */
1140
 
  path = (gpointer) offset;
1141
 
  path->ref_count = 1;
1142
 
  path->parent = NULL;
1143
 
  home_components[0] = path;
1144
 
  *((gsize *) thunar_vfs_path_get_name (path)) = THUNAR_VFS_PATH_ROOT;
1145
 
  offset += sizeof (ThunarVfsPath) + sizeof (gsize);
1146
 
 
1147
 
  /* add the remaining path components */
1148
 
  for (component = components; *component != NULL; ++component)
1149
 
    if (G_LIKELY (**component != '\0'))
1150
 
      {
1151
 
        /* setup the path basics */
1152
 
        path = (gpointer) offset;
1153
 
        path->ref_count = 1;
1154
 
        path->parent = home_components[n];
1155
 
        home_components[++n] = path;
1156
 
 
1157
 
        /* calculate the offset for the next home path component */
1158
 
        offset += sizeof (ThunarVfsPath) + ((strlen (*component) + sizeof (gsize)) / sizeof (gsize)) * sizeof (gsize);
1159
 
 
1160
 
        /* copy the path */
1161
 
        for (s = *component, t = (gchar *) thunar_vfs_path_get_name (path); *s != '\0'; )
1162
 
          *t++ = *s++;
1163
 
 
1164
 
        /* fill the path with zeros */
1165
 
        while (t < offset)
1166
 
          *t++ = '\0';
1167
 
      }
1168
 
 
1169
 
  /* verify state */
1170
 
  g_assert (n_home_components == n + 1);
1171
 
 
1172
 
  /* allocate the trash root path */
1173
 
  _thunar_vfs_path_trash_root = g_malloc (sizeof (ThunarVfsPath) + sizeof (gsize));
1174
 
  _thunar_vfs_path_trash_root->ref_count = 1 | THUNAR_VFS_PATH_SCHEME_TRASH;
1175
 
  _thunar_vfs_path_trash_root->parent = NULL;
1176
 
  *((gsize *) thunar_vfs_path_get_name (_thunar_vfs_path_trash_root)) = THUNAR_VFS_PATH_ROOT;
1177
 
 
1178
 
  /* cleanup */
1179
 
  g_strfreev (components);
1180
 
}
1181
 
 
1182
 
 
1183
 
 
1184
 
/**
1185
 
 * _thunar_vfs_path_shutdown:
1186
 
 *
1187
 
 * Shutdown the #ThunarVfsPath module.
1188
 
 **/
1189
 
void
1190
 
_thunar_vfs_path_shutdown (void)
1191
 
{
1192
 
  guint n;
1193
 
 
1194
 
  _thunar_vfs_return_if_fail (home_components != NULL);
1195
 
  _thunar_vfs_return_if_fail (n_home_components != 0);
1196
 
 
1197
 
  /* print out the list of leaked paths */
1198
 
  THUNAR_VFS_PATH_DEBUG_SHUTDOWN ();
1199
 
 
1200
 
  for (n = 0; n < n_home_components; ++n)
1201
 
    g_assert (home_components[n]->ref_count == 1);
1202
 
 
1203
 
  g_free (home_components);
1204
 
  home_components = NULL;
1205
 
  n_home_components = 0;
1206
 
 
1207
 
  g_assert (_thunar_vfs_path_trash_root->ref_count == (1 | THUNAR_VFS_PATH_SCHEME_TRASH));
1208
 
  g_free (_thunar_vfs_path_trash_root);
1209
 
  _thunar_vfs_path_trash_root = NULL;
1210
 
}
1211
 
 
1212
 
 
1213
 
 
1214
 
/**
1215
 
 * _thunar_vfs_path_new_relative:
1216
 
 * @parent        : the parent path.
1217
 
 * @relative_path : a relative path, or the empty string.
1218
 
 *
1219
 
 * Creates a new #ThunarVfsPath, which represents the subpath of @parent,
1220
 
 * identified by the @relative_path. If @relative_path is the empty string,
1221
 
 * a new reference to @parent will be returned.
1222
 
 *
1223
 
 * The caller is responsible to free the returned #ThunarVfsPath object
1224
 
 * using thunar_vfs_path_unref() when no longer needed.
1225
 
 *
1226
 
 * Return value: the #ThunarVfsPath object to the subpath of @parent
1227
 
 *               identified by @relative_path.
1228
 
 **/
1229
 
ThunarVfsPath*
1230
 
_thunar_vfs_path_new_relative (ThunarVfsPath *parent,
1231
 
                               const gchar   *relative_path)
1232
 
{
1233
 
  ThunarVfsPath *path = parent;
1234
 
  const gchar   *s1;
1235
 
  const gchar   *s = relative_path;
1236
 
  gchar         *t;
1237
 
  guint          n;
1238
 
 
1239
 
  _thunar_vfs_return_val_if_fail (relative_path != NULL, NULL);
1240
 
  _thunar_vfs_return_val_if_fail (parent != NULL, NULL);
1241
 
 
1242
 
  /* skip additional slashes */
1243
 
  for (; G_UNLIKELY (*s == G_DIR_SEPARATOR); ++s)
1244
 
    ;
1245
 
 
1246
 
  /* generate the additional path components (if any) */
1247
 
  while (*s != '\0')
1248
 
    {
1249
 
      /* remember the current path as parent path */
1250
 
      parent = path;
1251
 
 
1252
 
      /* determine the length of the path component in bytes */
1253
 
      for (s1 = s + 1; *s1 != '\0' && *s1 != G_DIR_SEPARATOR; ++s1)
1254
 
        ;
1255
 
      n = (((s1 - s) + sizeof (gsize)) / sizeof (gsize)) * sizeof (gsize)
1256
 
        + sizeof (ThunarVfsPath);
1257
 
 
1258
 
      /* allocate memory for the new path component */
1259
 
      path = _thunar_vfs_slice_alloc (n);
1260
 
      path->ref_count = thunar_vfs_path_get_scheme (parent);
1261
 
      path->parent = thunar_vfs_path_ref (parent);
1262
 
 
1263
 
      /* insert the path into the debug list */
1264
 
      THUNAR_VFS_PATH_DEBUG_INSERT (path);
1265
 
 
1266
 
      /* zero out the last word to have the name zero-terminated */
1267
 
      *(((gsize *) (((gchar *) path) + n)) - 1) = 0;
1268
 
 
1269
 
      /* copy the path component name */
1270
 
      for (t = (gchar *) thunar_vfs_path_get_name (path); *s != '\0' && *s != G_DIR_SEPARATOR; )
1271
 
        *t++ = *s++;
1272
 
 
1273
 
      /* skip additional slashes */
1274
 
      for (; G_UNLIKELY (*s == G_DIR_SEPARATOR); ++s)
1275
 
        ;
1276
 
    }
1277
 
 
1278
 
  /* return a reference to the path */
1279
 
  return thunar_vfs_path_ref (path);
1280
 
}
1281
 
 
1282
 
 
1283
 
 
1284
 
/**
1285
 
 * _thunar_vfs_path_child:
1286
 
 * @parent : a #ThunarVfsPath.
1287
 
 * @name   : a valid filename in the local file system encoding.
1288
 
 *
1289
 
 * Internal implementation of thunar_vfs_path_relative(), that performs
1290
 
 * no external sanity checking of it's parameters.
1291
 
 *
1292
 
 * Returns a #ThunarVfsPath for the file @name relative to
1293
 
 * @parent. @name must be a valid filename in the local file
1294
 
 * system encoding and it may not contain any slashes.
1295
 
 *
1296
 
 * The caller is responsible to free the returned object
1297
 
 * using thunar_vfs_path_unref() when no longer needed.
1298
 
 *
1299
 
 * Return value: the child path to @name relative to @parent.
1300
 
 **/
1301
 
ThunarVfsPath*
1302
 
_thunar_vfs_path_child (ThunarVfsPath *parent,
1303
 
                        const gchar   *name)
1304
 
{
1305
 
  ThunarVfsPath *path;
1306
 
  const gchar   *s;
1307
 
  gchar         *t;
1308
 
  gint           n;
1309
 
 
1310
 
  _thunar_vfs_return_val_if_fail (parent != NULL, NULL);
1311
 
  _thunar_vfs_return_val_if_fail (name != NULL, NULL);
1312
 
  _thunar_vfs_return_val_if_fail (*name != '\0', NULL);
1313
 
  _thunar_vfs_return_val_if_fail (strchr (name, '/') == NULL, NULL);
1314
 
 
1315
 
  /* check if parent is one of the home path components */
1316
 
  for (n = n_home_components - 2; n >= 0; --n)
1317
 
    if (G_UNLIKELY (home_components[n] == parent))
1318
 
      {
1319
 
        /* check if the name equals the home path child component */
1320
 
        if (strcmp (name, thunar_vfs_path_get_name (home_components[n + 1])) == 0)
1321
 
          return thunar_vfs_path_ref (home_components[n + 1]);
1322
 
        break;
1323
 
      }
1324
 
 
1325
 
  /* determine the length of the name in bytes */
1326
 
  for (s = name + 1; *s != '\0'; ++s)
1327
 
    ;
1328
 
  n = (((s - name) + sizeof (gsize)) / sizeof (gsize)) * sizeof (gsize)
1329
 
    + sizeof (ThunarVfsPath);
1330
 
 
1331
 
  /* allocate memory for the new path component */
1332
 
  path = _thunar_vfs_slice_alloc (n);
1333
 
  path->ref_count = 1 | thunar_vfs_path_get_scheme (parent);
1334
 
  path->parent = thunar_vfs_path_ref (parent);
1335
 
 
1336
 
  /* insert the path into the debug list */
1337
 
  THUNAR_VFS_PATH_DEBUG_INSERT (path);
1338
 
 
1339
 
  /* zero out the last word to have the name zero-terminated */
1340
 
  *(((gsize *) (((gchar *) path) + n)) - 1) = 0;
1341
 
 
1342
 
  /* copy the path component name */
1343
 
  for (s = name, t = (gchar *) thunar_vfs_path_get_name (path); *s != '\0'; )
1344
 
    *t++ = *s++;
1345
 
 
1346
 
  return path;
1347
 
}
1348
 
 
1349
 
 
1350
 
 
1351
 
/**
1352
 
 * _thunar_vfs_path_dup_display_name:
1353
 
 * @path : a #ThunarVfsPath.
1354
 
 *
1355
 
 * Returns the display name for the @path<!---->s name. That said,
1356
 
 * the method is similar to thunar_vfs_path_get_name(), but the
1357
 
 * returned string is garantied to be valid UTF-8.
1358
 
 *
1359
 
 * The caller is responsible to free the returned string using
1360
 
 * g_free() when no longer needed.
1361
 
 *
1362
 
 * Return value: a displayable variant of the @path<!---->s name.
1363
 
 **/
1364
 
gchar*
1365
 
_thunar_vfs_path_dup_display_name (const ThunarVfsPath *path)
1366
 
{
1367
 
  return g_filename_display_name (thunar_vfs_path_get_name (path));
1368
 
}
1369
 
 
1370
 
 
1371
 
 
1372
 
/**
1373
 
 * _thunar_vfs_path_translate:
1374
 
 * @src_path   : the source #ThunarVfsPath.
1375
 
 * @dst_scheme : the destination #ThunarVfsPathScheme.
1376
 
 * @error      : return location for errors or %NULL.
1377
 
 *
1378
 
 * Translates the @src_path to the specified @dst_scheme if possible.
1379
 
 *
1380
 
 * If the @src_path is already in the @dst_scheme, a new reference
1381
 
 * on @src_path will be returned.
1382
 
 *
1383
 
 * The caller is responsible to free the returned #ThunarVfsPath using
1384
 
 * thunar_vfs_path_unref() when no longer needed.
1385
 
 *
1386
 
 * Return value: the #ThunarVfsPath that corresponds to @src_path in the
1387
 
 *               @dst_scheme, or %NULL on error.
1388
 
 **/
1389
 
ThunarVfsPath*
1390
 
_thunar_vfs_path_translate (ThunarVfsPath      *src_path,
1391
 
                            ThunarVfsPathScheme dst_scheme,
1392
 
                            GError            **error)
1393
 
{
1394
 
  ThunarVfsPathScheme src_scheme;
1395
 
  ThunarVfsPath      *dst_path = NULL;
1396
 
  gchar              *absolute_path;
1397
 
 
1398
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, NULL);
1399
 
 
1400
 
  /* check if the src_path is already in dst_scheme */
1401
 
  src_scheme = thunar_vfs_path_get_scheme (src_path);
1402
 
  if (G_LIKELY (dst_scheme == src_scheme))
1403
 
    return thunar_vfs_path_ref (src_path);
1404
 
 
1405
 
  /* we can translate trash:-URIs to file:-URIs */
1406
 
  if (src_scheme == THUNAR_VFS_PATH_SCHEME_TRASH && dst_scheme == THUNAR_VFS_PATH_SCHEME_FILE)
1407
 
    {
1408
 
      /* resolve the local path to the trash resource */
1409
 
      absolute_path = _thunar_vfs_io_trash_path_resolve (src_path, error);
1410
 
      if (G_LIKELY (absolute_path != NULL))
1411
 
        {
1412
 
          /* generate a file:-URI path for the trash resource */
1413
 
          dst_path = thunar_vfs_path_new (absolute_path, error);
1414
 
          g_free (absolute_path);
1415
 
        }
1416
 
    }
1417
 
  else
1418
 
    {
1419
 
      /* cannot perform the translation */
1420
 
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "%s", g_strerror (EINVAL));
1421
 
    }
1422
 
 
1423
 
  return dst_path;
1424
 
}
1425
 
 
1426
 
 
1427
 
 
1428
 
/**
1429
 
 * _thunar_vfs_path_translate_dup_string:
1430
 
 * @src_path   : the source #ThunarVfsPath.
1431
 
 * @dst_scheme : the destination #ThunarVfsPathScheme.
1432
 
 * @error      : return location for errors or %NULL.
1433
 
 *
1434
 
 * Uses _thunar_vfs_path_translate() and thunar_vfs_path_dup_string()
1435
 
 * to generate the string representation of the @src_path in the
1436
 
 * @dst_scheme.
1437
 
 *
1438
 
 * The caller is responsible to free the returned string using
1439
 
 * g_free() when no longer needed.
1440
 
 *
1441
 
 * Return value: the string representation of the @src_path in the
1442
 
 *               @dst_scheme, or %NULL in case of an error.
1443
 
 **/
1444
 
gchar*
1445
 
_thunar_vfs_path_translate_dup_string (ThunarVfsPath      *src_path,
1446
 
                                       ThunarVfsPathScheme dst_scheme,
1447
 
                                       GError            **error)
1448
 
{
1449
 
  ThunarVfsPath *dst_path;
1450
 
  gchar         *dst_string;
1451
 
 
1452
 
  _thunar_vfs_return_val_if_fail (error == NULL || *error == NULL, NULL);
1453
 
 
1454
 
  /* handle the common case first */
1455
 
  if (G_LIKELY (dst_scheme == THUNAR_VFS_PATH_SCHEME_FILE))
1456
 
    {
1457
 
      if (_thunar_vfs_path_is_local (src_path))
1458
 
        return thunar_vfs_path_dup_string (src_path);
1459
 
      else if (_thunar_vfs_path_is_trash (src_path))
1460
 
        return _thunar_vfs_io_trash_path_resolve (src_path, error);
1461
 
    }
1462
 
 
1463
 
  /* translate the source path to the destination scheme */
1464
 
  dst_path = _thunar_vfs_path_translate (src_path, dst_scheme, error);
1465
 
  if (G_LIKELY (dst_path != NULL))
1466
 
    {
1467
 
      /* determine the string representation */
1468
 
      dst_string = thunar_vfs_path_dup_string (dst_path);
1469
 
      thunar_vfs_path_unref (dst_path);
1470
 
    }
1471
 
  else
1472
 
    {
1473
 
      /* we failed to translate */
1474
 
      dst_string = NULL;
1475
 
    }
1476
 
 
1477
 
  return dst_string;
1478
 
}
1479
 
 
1480
 
 
1481
 
 
1482
 
#define __THUNAR_VFS_PATH_C__
1483
 
#include <thunar-vfs/thunar-vfs-aliasdef.c>