gdummyfile.c 17.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

#include <config.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
30
#ifdef HAVE_UNISTD_H
31
#include <unistd.h>
32
#endif
33
34
35
36
#include <stdlib.h>

#include "gdummyfile.h"

37
38
#include "gioalias.h"

Matthias Clasen's avatar
Matthias Clasen committed
39
static void g_dummy_file_file_iface_init (GFileIface *iface);
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

typedef struct {
  char *scheme;
  char *userinfo;
  char *host;
  int port; /* -1 => not in uri */
  char *path;
  char *query;
  char *fragment;
} GDecodedUri;

struct _GDummyFile
{
  GObject parent_instance;

  GDecodedUri *decoded_uri;
  char *text_uri;
};

Alexander Larsson's avatar
Alexander Larsson committed
59
#define g_dummy_file_get_type _g_dummy_file_get_type
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
G_DEFINE_TYPE_WITH_CODE (GDummyFile, g_dummy_file, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
						g_dummy_file_file_iface_init))

#define SUB_DELIM_CHARS  "!$&'()*+,;="

static char *       _g_encode_uri       (GDecodedUri *decoded);
static void         _g_decoded_uri_free (GDecodedUri *decoded);
static GDecodedUri *_g_decode_uri       (const char  *uri);
static GDecodedUri *_g_decoded_uri_new  (void);

static char * unescape_string (const gchar *escaped_string,
			       const gchar *escaped_string_end,
			       const gchar *illegal_characters);

Matthias Clasen's avatar
Matthias Clasen committed
75
76
static void g_string_append_encoded (GString    *string, 
                                     const char *encoded,
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
				     const char *reserved_chars_allowed);

static void
g_dummy_file_finalize (GObject *object)
{
  GDummyFile *dummy;

  dummy = G_DUMMY_FILE (object);

  if (dummy->decoded_uri)
    _g_decoded_uri_free (dummy->decoded_uri);
  
  g_free (dummy->text_uri);
  
  if (G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize)
    (*G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize) (object);
}

static void
g_dummy_file_class_init (GDummyFileClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = g_dummy_file_finalize;
}

static void
g_dummy_file_init (GDummyFile *dummy)
{
}

/**
 * g_dummy_file_new:
 * @uri: Universal Resource Identifier for the dummy file object.
 * 
 * Returns: a new #GFile. 
 **/
GFile *
Alexander Larsson's avatar
Alexander Larsson committed
115
_g_dummy_file_new (const char *uri)
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{
  GDummyFile *dummy;

  g_return_val_if_fail (uri != NULL, NULL);

  dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
  dummy->text_uri = g_strdup (uri);
  dummy->decoded_uri = _g_decode_uri (uri);
  
  return G_FILE (dummy);
}

static gboolean
g_dummy_file_is_native (GFile *file)
{
  return FALSE;
}

static char *
g_dummy_file_get_basename (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);
  
  if (dummy->decoded_uri)
    return g_path_get_basename (dummy->decoded_uri->path);
  return g_strdup (dummy->text_uri);
}

static char *
g_dummy_file_get_path (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);

  if (dummy->decoded_uri)
    return g_strdup (dummy->decoded_uri->path);
  return NULL;
}

static char *
g_dummy_file_get_uri (GFile *file)
{
  return g_strdup (G_DUMMY_FILE (file)->text_uri);
}

static char *
g_dummy_file_get_parse_name (GFile *file)
{
  return g_strdup (G_DUMMY_FILE (file)->text_uri);
}

static GFile *
g_dummy_file_get_parent (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);
  GFile *parent;
  char *dirname;
  char *uri;
  GDecodedUri new_decoded_uri;

175
176
  if (dummy->decoded_uri == NULL ||
      g_strcmp0 (dummy->decoded_uri->path, "/") == 0)
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    return NULL;

  dirname = g_path_get_dirname (dummy->decoded_uri->path);
  
  if (strcmp (dirname, ".") == 0)
    {
      g_free (dirname);
      return NULL;
    }
  
  new_decoded_uri = *dummy->decoded_uri;
  new_decoded_uri.path = dirname;
  uri = _g_encode_uri (&new_decoded_uri);
  g_free (dirname);
  
Alexander Larsson's avatar
Alexander Larsson committed
192
  parent = _g_dummy_file_new (uri);
193
194
195
196
197
198
199
200
201
202
  g_free (uri);
  
  return parent;
}

static GFile *
g_dummy_file_dup (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);

Alexander Larsson's avatar
Alexander Larsson committed
203
  return _g_dummy_file_new (dummy->text_uri);
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
}

static guint
g_dummy_file_hash (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);
  
  return g_str_hash (dummy->text_uri);
}

static gboolean
g_dummy_file_equal (GFile *file1,
		    GFile *file2)
{
  GDummyFile *dummy1 = G_DUMMY_FILE (file1);
  GDummyFile *dummy2 = G_DUMMY_FILE (file2);

  return g_str_equal (dummy1->text_uri, dummy2->text_uri);
}

static int
Matthias Clasen's avatar
Matthias Clasen committed
225
226
safe_strcmp (const char *a, 
             const char *b)
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
{
  if (a == NULL)
    a = "";
  if (b == NULL)
    b = "";

  return strcmp (a, b);
}

static gboolean
uri_same_except_path (GDecodedUri *a,
		      GDecodedUri *b)
{
  if (safe_strcmp (a->scheme, b->scheme) != 0)
    return FALSE;
  if (safe_strcmp (a->userinfo, b->userinfo) != 0)
    return FALSE;
  if (safe_strcmp (a->host, b->host) != 0)
    return FALSE;
  if (a->port != b->port)
    return FALSE;

  return TRUE;
}

static const char *
Matthias Clasen's avatar
Matthias Clasen committed
253
254
match_prefix (const char *path, 
              const char *prefix)
255
256
257
258
259
260
261
262
263
264
{
  int prefix_len;

  prefix_len = strlen (prefix);
  if (strncmp (path, prefix, prefix_len) != 0)
    return NULL;
  return path + prefix_len;
}

static gboolean
265
g_dummy_file_prefix_matches (GFile *parent, GFile *descendant)
266
267
268
269
270
271
272
273
274
{
  GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
  GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
  const char *remainder;

  if (parent_dummy->decoded_uri != NULL &&
      descendant_dummy->decoded_uri != NULL)
    {
      if (uri_same_except_path (parent_dummy->decoded_uri,
Matthias Clasen's avatar
Matthias Clasen committed
275
276
277
278
279
280
281
282
283
284
285
286
				descendant_dummy->decoded_uri)) 
        {
	  remainder = match_prefix (descendant_dummy->decoded_uri->path,
                                    parent_dummy->decoded_uri->path);
          if (remainder != NULL && *remainder == '/')
	    {
	      while (*remainder == '/')
	        remainder++;
	      if (*remainder != 0)
	        return TRUE;
	    }
        }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
    }
  else
    {
      remainder = match_prefix (descendant_dummy->text_uri,
				parent_dummy->text_uri);
      if (remainder != NULL && *remainder == '/')
	  {
	    while (*remainder == '/')
	      remainder++;
	    if (*remainder != 0)
	      return TRUE;
	  }
    }
  
  return FALSE;
}

static char *
g_dummy_file_get_relative_path (GFile *parent,
				GFile *descendant)
{
  GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
  GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
  const char *remainder;

  if (parent_dummy->decoded_uri != NULL &&
      descendant_dummy->decoded_uri != NULL)
    {
      if (uri_same_except_path (parent_dummy->decoded_uri,
Matthias Clasen's avatar
Matthias Clasen committed
316
317
318
319
320
321
322
323
324
325
326
327
				descendant_dummy->decoded_uri)) 
        {
          remainder = match_prefix (descendant_dummy->decoded_uri->path,
                                    parent_dummy->decoded_uri->path);
          if (remainder != NULL && *remainder == '/')
	    {
	      while (*remainder == '/')
	        remainder++;
	      if (*remainder != 0)
	        return g_strdup (remainder);
	    }
        }
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
    }
  else
    {
      remainder = match_prefix (descendant_dummy->text_uri,
				parent_dummy->text_uri);
      if (remainder != NULL && *remainder == '/')
	  {
	    while (*remainder == '/')
	      remainder++;
	    if (*remainder != 0)
	      return unescape_string (remainder, NULL, "/");
	  }
    }
  
  return NULL;
}


static GFile *
Matthias Clasen's avatar
Matthias Clasen committed
347
g_dummy_file_resolve_relative_path (GFile      *file,
348
349
350
351
352
353
354
355
356
357
358
359
360
				    const char *relative_path)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);
  GFile *child;
  char *uri;
  GDecodedUri new_decoded_uri;
  GString *str;

  if (dummy->decoded_uri == NULL)
    {
      str = g_string_new (dummy->text_uri);
      g_string_append (str, "/");
      g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
Alexander Larsson's avatar
Alexander Larsson committed
361
      child = _g_dummy_file_new (str->str);
362
363
364
365
366
367
368
369
370
371
372
373
374
375
      g_string_free (str, TRUE);
    }
  else
    {
      new_decoded_uri = *dummy->decoded_uri;
      
      if (g_path_is_absolute (relative_path))
	new_decoded_uri.path = g_strdup (relative_path);
      else
	new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
      
      uri = _g_encode_uri (&new_decoded_uri);
      g_free (new_decoded_uri.path);
      
Alexander Larsson's avatar
Alexander Larsson committed
376
      child = _g_dummy_file_new (uri);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
      g_free (uri);
    }

  return child;
}

static GFile *
g_dummy_file_get_child_for_display_name (GFile        *file,
					 const char   *display_name,
					 GError      **error)
{
  return g_file_get_child (file, display_name);
}

static gboolean
g_dummy_file_has_uri_scheme (GFile *file,
			     const char *uri_scheme)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);
  
  if (dummy->decoded_uri)
    return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
  return FALSE;
}

static char *
g_dummy_file_get_uri_scheme (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);

  if (dummy->decoded_uri)
    return g_strdup (dummy->decoded_uri->scheme);
    
  return NULL;
}


static void
g_dummy_file_file_iface_init (GFileIface *iface)
{
  iface->dup = g_dummy_file_dup;
  iface->hash = g_dummy_file_hash;
  iface->equal = g_dummy_file_equal;
  iface->is_native = g_dummy_file_is_native;
  iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
  iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
  iface->get_basename = g_dummy_file_get_basename;
  iface->get_path = g_dummy_file_get_path;
  iface->get_uri = g_dummy_file_get_uri;
  iface->get_parse_name = g_dummy_file_get_parse_name;
  iface->get_parent = g_dummy_file_get_parent;
428
  iface->prefix_matches = g_dummy_file_prefix_matches;
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
  iface->get_relative_path = g_dummy_file_get_relative_path;
  iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
  iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
}

/* Uri handling helper functions: */

static int
unescape_character (const char *scanner)
{
  int first_digit;
  int second_digit;
  
  first_digit = g_ascii_xdigit_value (*scanner++);
  if (first_digit < 0)
    return -1;

  second_digit = g_ascii_xdigit_value (*scanner++);
  if (second_digit < 0)
    return -1;

  return (first_digit << 4) | second_digit;
}

static char *
unescape_string (const gchar *escaped_string,
		 const gchar *escaped_string_end,
		 const gchar *illegal_characters)
{
  const gchar *in;
  gchar *out, *result;
  gint character;
  
  if (escaped_string == NULL)
    return NULL;

  if (escaped_string_end == NULL)
    escaped_string_end = escaped_string + strlen (escaped_string);
  
  result = g_malloc (escaped_string_end - escaped_string + 1);
	
  out = result;
Matthias Clasen's avatar
Matthias Clasen committed
471
472
473
474
475
476
477
478
479
480
481
  for (in = escaped_string; in < escaped_string_end; in++) 
    {
      character = *in;
      if (*in == '%') 
        {
          in++;
          if (escaped_string_end - in < 2)
	    {
	      g_free (result);
	      return NULL;
	    }
482
      
Matthias Clasen's avatar
Matthias Clasen committed
483
          character = unescape_character (in);
484
      
Matthias Clasen's avatar
Matthias Clasen committed
485
486
487
488
489
490
491
492
493
494
495
          /* Check for an illegal character. We consider '\0' illegal here. */
          if (character <= 0 ||
	      (illegal_characters != NULL &&
	       strchr (illegal_characters, (char)character) != NULL))
	    {
	      g_free (result);
	      return NULL;
	    }
          in++; /* The other char will be eaten in the loop header */
        }
      *out++ = (char)character;
496
497
498
    }
  
  *out = '\0';
499
  g_warn_if_fail (out - result <= strlen (escaped_string));
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
  return result;
}

void
_g_decoded_uri_free (GDecodedUri *decoded)
{
  if (decoded == NULL)
    return;

  g_free (decoded->scheme);
  g_free (decoded->query);
  g_free (decoded->fragment);
  g_free (decoded->userinfo);
  g_free (decoded->host);
  g_free (decoded->path);
  g_free (decoded);
}

GDecodedUri *
_g_decoded_uri_new (void)
{
  GDecodedUri *uri;

  uri = g_new0 (GDecodedUri, 1);
  uri->port = -1;

  return uri;
}

GDecodedUri *
_g_decode_uri (const char *uri)
{
  GDecodedUri *decoded;
  const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
  char *out;
  char c;

  /* From RFC 3986 Decodes:
   * URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
   */ 

  p = uri;
  
  /* Decode scheme:
     scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  */

  if (!g_ascii_isalpha (*p))
    return NULL;

  while (1)
    {
      c = *p++;

      if (c == ':')
	break;
      
      if (!(g_ascii_isalnum(c) ||
	    c == '+' ||
	    c == '-' ||
	    c == '.'))
	return NULL;
    }

  decoded = _g_decoded_uri_new ();
  
  decoded->scheme = g_malloc (p - uri);
  out = decoded->scheme;
  for (in = uri; in < p - 1; in++)
    *out++ = g_ascii_tolower (*in);
  *out = 0;

  hier_part_start = p;

  query_start = strchr (p, '?');
  if (query_start)
    {
      hier_part_end = query_start++;
      fragment_start = strchr (query_start, '#');
      if (fragment_start)
	{
	  decoded->query = g_strndup (query_start, fragment_start - query_start);
	  decoded->fragment = g_strdup (fragment_start+1);
	}
      else
	{
	  decoded->query = g_strdup (query_start);
	  decoded->fragment = NULL;
	}
    }
  else
    {
      /* No query */
      decoded->query = NULL;
      fragment_start = strchr (p, '#');
      if (fragment_start)
	{
	  hier_part_end = fragment_start++;
	  decoded->fragment = g_strdup (fragment_start);
	}
      else
	{
	  hier_part_end = p + strlen (p);
	  decoded->fragment = NULL;
	}
    }

  /*  3:
      hier-part   = "//" authority path-abempty
                  / path-absolute
                  / path-rootless
                  / path-empty

  */

  if (hier_part_start[0] == '/' &&
      hier_part_start[1] == '/')
    {
      const char *authority_start, *authority_end;
      const char *userinfo_start, *userinfo_end;
      const char *host_start, *host_end;
      const char *port_start;
      
      authority_start = hier_part_start + 2;
      /* authority is always followed by / or nothing */
      authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
      if (authority_end == NULL)
	authority_end = hier_part_end;

      /* 3.2:
	      authority   = [ userinfo "@" ] host [ ":" port ]
      */

      userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
      if (userinfo_end)
	{
	  userinfo_start = authority_start;
	  decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
	  if (decoded->userinfo == NULL)
	    {
	      _g_decoded_uri_free (decoded);
	      return NULL;
	    }
	  host_start = userinfo_end + 1;
	}
      else
	host_start = authority_start;

      port_start = memchr (host_start, ':', authority_end - host_start);
      if (port_start)
	{
	  host_end = port_start++;

	  decoded->port = atoi(port_start);
	}
      else
	{
	  host_end = authority_end;
	  decoded->port = -1;
	}

      decoded->host = g_strndup (host_start, host_end - host_start);

      hier_part_start = authority_end;
    }

  decoded->path = unescape_string (hier_part_start, hier_part_end, "/");

  if (decoded->path == NULL)
    {
      _g_decoded_uri_free (decoded);
      return NULL;
    }
  
  return decoded;
}

static gboolean
is_valid (char c, const char *reserved_chars_allowed)
{
  if (g_ascii_isalnum (c) ||
      c == '-' ||
      c == '.' ||
      c == '_' ||
      c == '~')
    return TRUE;

  if (reserved_chars_allowed &&
      strchr (reserved_chars_allowed, c) != NULL)
    return TRUE;
  
  return FALSE;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
695
696
g_string_append_encoded (GString    *string, 
                         const char *encoded,
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
			 const char *reserved_chars_allowed)
{
  unsigned char c;
  const char *end;
  static const gchar hex[16] = "0123456789ABCDEF";

  end = encoded + strlen (encoded);
  
  while ((c = *encoded) != 0)
    {
      if (is_valid (c, reserved_chars_allowed))
	{
	  g_string_append_c (string, c);
	  encoded++;
	}
      else
	{
	  g_string_append_c (string, '%');
	  g_string_append_c (string, hex[((guchar)c) >> 4]);
	  g_string_append_c (string, hex[((guchar)c) & 0xf]);
	  encoded++;
	}
    }
}

static char *
_g_encode_uri (GDecodedUri *decoded)
{
  GString *uri;

  uri = g_string_new (NULL);

  g_string_append (uri, decoded->scheme);
  g_string_append (uri, "://");

  if (decoded->host != NULL)
    {
      if (decoded->userinfo)
	{
	  /* userinfo    = *( unreserved / pct-encoded / sub-delims / ":" ) */
	  g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
	  g_string_append_c (uri, '@');
	}
      
      g_string_append (uri, decoded->host);
      
      if (decoded->port != -1)
	{
	  g_string_append_c (uri, ':');
	  g_string_append_printf (uri, "%d", decoded->port);
	}
    }

  g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
  
  if (decoded->query)
    {
      g_string_append_c (uri, '?');
      g_string_append (uri, decoded->query);
    }
    
  if (decoded->fragment)
    {
      g_string_append_c (uri, '#');
      g_string_append (uri, decoded->fragment);
    }

  return g_string_free (uri, FALSE);
}