Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gdummyfile.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public 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  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <stdlib.h>
34
35 #include "gdummyfile.h"
36 #include "gfile.h"
37
38 #include "gioalias.h"
39
40 static void g_dummy_file_file_iface_init (GFileIface *iface);
41
42 typedef struct {
43   char *scheme;
44   char *userinfo;
45   char *host;
46   int port; /* -1 => not in uri */
47   char *path;
48   char *query;
49   char *fragment;
50 } GDecodedUri;
51
52 struct _GDummyFile
53 {
54   GObject parent_instance;
55
56   GDecodedUri *decoded_uri;
57   char *text_uri;
58 };
59
60 #define g_dummy_file_get_type _g_dummy_file_get_type
61 G_DEFINE_TYPE_WITH_CODE (GDummyFile, g_dummy_file, G_TYPE_OBJECT,
62                          G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
63                                                 g_dummy_file_file_iface_init))
64
65 #define SUB_DELIM_CHARS  "!$&'()*+,;="
66
67 static char *       _g_encode_uri       (GDecodedUri *decoded);
68 static void         _g_decoded_uri_free (GDecodedUri *decoded);
69 static GDecodedUri *_g_decode_uri       (const char  *uri);
70 static GDecodedUri *_g_decoded_uri_new  (void);
71
72 static char * unescape_string (const gchar *escaped_string,
73                                const gchar *escaped_string_end,
74                                const gchar *illegal_characters);
75
76 static void g_string_append_encoded (GString    *string, 
77                                      const char *encoded,
78                                      const char *reserved_chars_allowed);
79
80 static void
81 g_dummy_file_finalize (GObject *object)
82 {
83   GDummyFile *dummy;
84
85   dummy = G_DUMMY_FILE (object);
86
87   if (dummy->decoded_uri)
88     _g_decoded_uri_free (dummy->decoded_uri);
89   
90   g_free (dummy->text_uri);
91
92   G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize (object);
93 }
94
95 static void
96 g_dummy_file_class_init (GDummyFileClass *klass)
97 {
98   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99
100   gobject_class->finalize = g_dummy_file_finalize;
101 }
102
103 static void
104 g_dummy_file_init (GDummyFile *dummy)
105 {
106 }
107
108 GFile *
109 _g_dummy_file_new (const char *uri)
110 {
111   GDummyFile *dummy;
112
113   g_return_val_if_fail (uri != NULL, NULL);
114
115   dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
116   dummy->text_uri = g_strdup (uri);
117   dummy->decoded_uri = _g_decode_uri (uri);
118   
119   return G_FILE (dummy);
120 }
121
122 static gboolean
123 g_dummy_file_is_native (GFile *file)
124 {
125   return FALSE;
126 }
127
128 static char *
129 g_dummy_file_get_basename (GFile *file)
130 {
131   GDummyFile *dummy = G_DUMMY_FILE (file);
132   
133   if (dummy->decoded_uri)
134     return g_path_get_basename (dummy->decoded_uri->path);
135   return g_strdup (dummy->text_uri);
136 }
137
138 static char *
139 g_dummy_file_get_path (GFile *file)
140 {
141   return NULL;
142 }
143
144 static char *
145 g_dummy_file_get_uri (GFile *file)
146 {
147   return g_strdup (G_DUMMY_FILE (file)->text_uri);
148 }
149
150 static char *
151 g_dummy_file_get_parse_name (GFile *file)
152 {
153   return g_strdup (G_DUMMY_FILE (file)->text_uri);
154 }
155
156 static GFile *
157 g_dummy_file_get_parent (GFile *file)
158 {
159   GDummyFile *dummy = G_DUMMY_FILE (file);
160   GFile *parent;
161   char *dirname;
162   char *uri;
163   GDecodedUri new_decoded_uri;
164
165   if (dummy->decoded_uri == NULL ||
166       g_strcmp0 (dummy->decoded_uri->path, "/") == 0)
167     return NULL;
168
169   dirname = g_path_get_dirname (dummy->decoded_uri->path);
170   
171   if (strcmp (dirname, ".") == 0)
172     {
173       g_free (dirname);
174       return NULL;
175     }
176   
177   new_decoded_uri = *dummy->decoded_uri;
178   new_decoded_uri.path = dirname;
179   uri = _g_encode_uri (&new_decoded_uri);
180   g_free (dirname);
181   
182   parent = _g_dummy_file_new (uri);
183   g_free (uri);
184   
185   return parent;
186 }
187
188 static GFile *
189 g_dummy_file_dup (GFile *file)
190 {
191   GDummyFile *dummy = G_DUMMY_FILE (file);
192
193   return _g_dummy_file_new (dummy->text_uri);
194 }
195
196 static guint
197 g_dummy_file_hash (GFile *file)
198 {
199   GDummyFile *dummy = G_DUMMY_FILE (file);
200   
201   return g_str_hash (dummy->text_uri);
202 }
203
204 static gboolean
205 g_dummy_file_equal (GFile *file1,
206                     GFile *file2)
207 {
208   GDummyFile *dummy1 = G_DUMMY_FILE (file1);
209   GDummyFile *dummy2 = G_DUMMY_FILE (file2);
210
211   return g_str_equal (dummy1->text_uri, dummy2->text_uri);
212 }
213
214 static int
215 safe_strcmp (const char *a, 
216              const char *b)
217 {
218   if (a == NULL)
219     a = "";
220   if (b == NULL)
221     b = "";
222
223   return strcmp (a, b);
224 }
225
226 static gboolean
227 uri_same_except_path (GDecodedUri *a,
228                       GDecodedUri *b)
229 {
230   if (safe_strcmp (a->scheme, b->scheme) != 0)
231     return FALSE;
232   if (safe_strcmp (a->userinfo, b->userinfo) != 0)
233     return FALSE;
234   if (safe_strcmp (a->host, b->host) != 0)
235     return FALSE;
236   if (a->port != b->port)
237     return FALSE;
238
239   return TRUE;
240 }
241
242 static const char *
243 match_prefix (const char *path, 
244               const char *prefix)
245 {
246   int prefix_len;
247
248   prefix_len = strlen (prefix);
249   if (strncmp (path, prefix, prefix_len) != 0)
250     return NULL;
251   return path + prefix_len;
252 }
253
254 static gboolean
255 g_dummy_file_prefix_matches (GFile *parent, GFile *descendant)
256 {
257   GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
258   GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
259   const char *remainder;
260
261   if (parent_dummy->decoded_uri != NULL &&
262       descendant_dummy->decoded_uri != NULL)
263     {
264       if (uri_same_except_path (parent_dummy->decoded_uri,
265                                 descendant_dummy->decoded_uri)) 
266         {
267           remainder = match_prefix (descendant_dummy->decoded_uri->path,
268                                     parent_dummy->decoded_uri->path);
269           if (remainder != NULL && *remainder == '/')
270             {
271               while (*remainder == '/')
272                 remainder++;
273               if (*remainder != 0)
274                 return TRUE;
275             }
276         }
277     }
278   else
279     {
280       remainder = match_prefix (descendant_dummy->text_uri,
281                                 parent_dummy->text_uri);
282       if (remainder != NULL && *remainder == '/')
283           {
284             while (*remainder == '/')
285               remainder++;
286             if (*remainder != 0)
287               return TRUE;
288           }
289     }
290   
291   return FALSE;
292 }
293
294 static char *
295 g_dummy_file_get_relative_path (GFile *parent,
296                                 GFile *descendant)
297 {
298   GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
299   GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
300   const char *remainder;
301
302   if (parent_dummy->decoded_uri != NULL &&
303       descendant_dummy->decoded_uri != NULL)
304     {
305       if (uri_same_except_path (parent_dummy->decoded_uri,
306                                 descendant_dummy->decoded_uri)) 
307         {
308           remainder = match_prefix (descendant_dummy->decoded_uri->path,
309                                     parent_dummy->decoded_uri->path);
310           if (remainder != NULL && *remainder == '/')
311             {
312               while (*remainder == '/')
313                 remainder++;
314               if (*remainder != 0)
315                 return g_strdup (remainder);
316             }
317         }
318     }
319   else
320     {
321       remainder = match_prefix (descendant_dummy->text_uri,
322                                 parent_dummy->text_uri);
323       if (remainder != NULL && *remainder == '/')
324           {
325             while (*remainder == '/')
326               remainder++;
327             if (*remainder != 0)
328               return unescape_string (remainder, NULL, "/");
329           }
330     }
331   
332   return NULL;
333 }
334
335
336 static GFile *
337 g_dummy_file_resolve_relative_path (GFile      *file,
338                                     const char *relative_path)
339 {
340   GDummyFile *dummy = G_DUMMY_FILE (file);
341   GFile *child;
342   char *uri;
343   GDecodedUri new_decoded_uri;
344   GString *str;
345
346   if (dummy->decoded_uri == NULL)
347     {
348       str = g_string_new (dummy->text_uri);
349       g_string_append (str, "/");
350       g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
351       child = _g_dummy_file_new (str->str);
352       g_string_free (str, TRUE);
353     }
354   else
355     {
356       new_decoded_uri = *dummy->decoded_uri;
357       
358       if (g_path_is_absolute (relative_path))
359         new_decoded_uri.path = g_strdup (relative_path);
360       else
361         new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
362       
363       uri = _g_encode_uri (&new_decoded_uri);
364       g_free (new_decoded_uri.path);
365       
366       child = _g_dummy_file_new (uri);
367       g_free (uri);
368     }
369
370   return child;
371 }
372
373 static GFile *
374 g_dummy_file_get_child_for_display_name (GFile        *file,
375                                          const char   *display_name,
376                                          GError      **error)
377 {
378   return g_file_get_child (file, display_name);
379 }
380
381 static gboolean
382 g_dummy_file_has_uri_scheme (GFile *file,
383                              const char *uri_scheme)
384 {
385   GDummyFile *dummy = G_DUMMY_FILE (file);
386   
387   if (dummy->decoded_uri)
388     return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
389   return FALSE;
390 }
391
392 static char *
393 g_dummy_file_get_uri_scheme (GFile *file)
394 {
395   GDummyFile *dummy = G_DUMMY_FILE (file);
396
397   if (dummy->decoded_uri)
398     return g_strdup (dummy->decoded_uri->scheme);
399     
400   return NULL;
401 }
402
403
404 static void
405 g_dummy_file_file_iface_init (GFileIface *iface)
406 {
407   iface->dup = g_dummy_file_dup;
408   iface->hash = g_dummy_file_hash;
409   iface->equal = g_dummy_file_equal;
410   iface->is_native = g_dummy_file_is_native;
411   iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
412   iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
413   iface->get_basename = g_dummy_file_get_basename;
414   iface->get_path = g_dummy_file_get_path;
415   iface->get_uri = g_dummy_file_get_uri;
416   iface->get_parse_name = g_dummy_file_get_parse_name;
417   iface->get_parent = g_dummy_file_get_parent;
418   iface->prefix_matches = g_dummy_file_prefix_matches;
419   iface->get_relative_path = g_dummy_file_get_relative_path;
420   iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
421   iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
422
423   iface->supports_thread_contexts = TRUE;
424 }
425
426 /* Uri handling helper functions: */
427
428 static int
429 unescape_character (const char *scanner)
430 {
431   int first_digit;
432   int second_digit;
433   
434   first_digit = g_ascii_xdigit_value (*scanner++);
435   if (first_digit < 0)
436     return -1;
437
438   second_digit = g_ascii_xdigit_value (*scanner++);
439   if (second_digit < 0)
440     return -1;
441
442   return (first_digit << 4) | second_digit;
443 }
444
445 static char *
446 unescape_string (const gchar *escaped_string,
447                  const gchar *escaped_string_end,
448                  const gchar *illegal_characters)
449 {
450   const gchar *in;
451   gchar *out, *result;
452   gint character;
453   
454   if (escaped_string == NULL)
455     return NULL;
456
457   if (escaped_string_end == NULL)
458     escaped_string_end = escaped_string + strlen (escaped_string);
459   
460   result = g_malloc (escaped_string_end - escaped_string + 1);
461         
462   out = result;
463   for (in = escaped_string; in < escaped_string_end; in++) 
464     {
465       character = *in;
466       if (*in == '%') 
467         {
468           in++;
469           if (escaped_string_end - in < 2)
470             {
471               g_free (result);
472               return NULL;
473             }
474       
475           character = unescape_character (in);
476       
477           /* Check for an illegal character. We consider '\0' illegal here. */
478           if (character <= 0 ||
479               (illegal_characters != NULL &&
480                strchr (illegal_characters, (char)character) != NULL))
481             {
482               g_free (result);
483               return NULL;
484             }
485           in++; /* The other char will be eaten in the loop header */
486         }
487       *out++ = (char)character;
488     }
489   
490   *out = '\0';
491   g_warn_if_fail (out - result <= strlen (escaped_string));
492   return result;
493 }
494
495 void
496 _g_decoded_uri_free (GDecodedUri *decoded)
497 {
498   if (decoded == NULL)
499     return;
500
501   g_free (decoded->scheme);
502   g_free (decoded->query);
503   g_free (decoded->fragment);
504   g_free (decoded->userinfo);
505   g_free (decoded->host);
506   g_free (decoded->path);
507   g_free (decoded);
508 }
509
510 GDecodedUri *
511 _g_decoded_uri_new (void)
512 {
513   GDecodedUri *uri;
514
515   uri = g_new0 (GDecodedUri, 1);
516   uri->port = -1;
517
518   return uri;
519 }
520
521 GDecodedUri *
522 _g_decode_uri (const char *uri)
523 {
524   GDecodedUri *decoded;
525   const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
526   char *out;
527   char c;
528
529   /* From RFC 3986 Decodes:
530    * URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
531    */ 
532
533   p = uri;
534   
535   /* Decode scheme:
536      scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
537   */
538
539   if (!g_ascii_isalpha (*p))
540     return NULL;
541
542   while (1)
543     {
544       c = *p++;
545
546       if (c == ':')
547         break;
548       
549       if (!(g_ascii_isalnum(c) ||
550             c == '+' ||
551             c == '-' ||
552             c == '.'))
553         return NULL;
554     }
555
556   decoded = _g_decoded_uri_new ();
557   
558   decoded->scheme = g_malloc (p - uri);
559   out = decoded->scheme;
560   for (in = uri; in < p - 1; in++)
561     *out++ = g_ascii_tolower (*in);
562   *out = 0;
563
564   hier_part_start = p;
565
566   query_start = strchr (p, '?');
567   if (query_start)
568     {
569       hier_part_end = query_start++;
570       fragment_start = strchr (query_start, '#');
571       if (fragment_start)
572         {
573           decoded->query = g_strndup (query_start, fragment_start - query_start);
574           decoded->fragment = g_strdup (fragment_start+1);
575         }
576       else
577         {
578           decoded->query = g_strdup (query_start);
579           decoded->fragment = NULL;
580         }
581     }
582   else
583     {
584       /* No query */
585       decoded->query = NULL;
586       fragment_start = strchr (p, '#');
587       if (fragment_start)
588         {
589           hier_part_end = fragment_start++;
590           decoded->fragment = g_strdup (fragment_start);
591         }
592       else
593         {
594           hier_part_end = p + strlen (p);
595           decoded->fragment = NULL;
596         }
597     }
598
599   /*  3:
600       hier-part   = "//" authority path-abempty
601                   / path-absolute
602                   / path-rootless
603                   / path-empty
604
605   */
606
607   if (hier_part_start[0] == '/' &&
608       hier_part_start[1] == '/')
609     {
610       const char *authority_start, *authority_end;
611       const char *userinfo_start, *userinfo_end;
612       const char *host_start, *host_end;
613       const char *port_start;
614       
615       authority_start = hier_part_start + 2;
616       /* authority is always followed by / or nothing */
617       authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
618       if (authority_end == NULL)
619         authority_end = hier_part_end;
620
621       /* 3.2:
622               authority   = [ userinfo "@" ] host [ ":" port ]
623       */
624
625       userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
626       if (userinfo_end)
627         {
628           userinfo_start = authority_start;
629           decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
630           if (decoded->userinfo == NULL)
631             {
632               _g_decoded_uri_free (decoded);
633               return NULL;
634             }
635           host_start = userinfo_end + 1;
636         }
637       else
638         host_start = authority_start;
639
640       port_start = memchr (host_start, ':', authority_end - host_start);
641       if (port_start)
642         {
643           host_end = port_start++;
644
645           decoded->port = atoi(port_start);
646         }
647       else
648         {
649           host_end = authority_end;
650           decoded->port = -1;
651         }
652
653       decoded->host = g_strndup (host_start, host_end - host_start);
654
655       hier_part_start = authority_end;
656     }
657
658   decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
659
660   if (decoded->path == NULL)
661     {
662       _g_decoded_uri_free (decoded);
663       return NULL;
664     }
665   
666   return decoded;
667 }
668
669 static gboolean
670 is_valid (char c, const char *reserved_chars_allowed)
671 {
672   if (g_ascii_isalnum (c) ||
673       c == '-' ||
674       c == '.' ||
675       c == '_' ||
676       c == '~')
677     return TRUE;
678
679   if (reserved_chars_allowed &&
680       strchr (reserved_chars_allowed, c) != NULL)
681     return TRUE;
682   
683   return FALSE;
684 }
685
686 static void
687 g_string_append_encoded (GString    *string,
688                          const char *encoded,
689                          const char *reserved_chars_allowed)
690 {
691   unsigned char c;
692   static const gchar hex[16] = "0123456789ABCDEF";
693
694   while ((c = *encoded) != 0)
695     {
696       if (is_valid (c, reserved_chars_allowed))
697         {
698           g_string_append_c (string, c);
699           encoded++;
700         }
701       else
702         {
703           g_string_append_c (string, '%');
704           g_string_append_c (string, hex[((guchar)c) >> 4]);
705           g_string_append_c (string, hex[((guchar)c) & 0xf]);
706           encoded++;
707         }
708     }
709 }
710
711 static char *
712 _g_encode_uri (GDecodedUri *decoded)
713 {
714   GString *uri;
715
716   uri = g_string_new (NULL);
717
718   g_string_append (uri, decoded->scheme);
719   g_string_append (uri, "://");
720
721   if (decoded->host != NULL)
722     {
723       if (decoded->userinfo)
724         {
725           /* userinfo    = *( unreserved / pct-encoded / sub-delims / ":" ) */
726           g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
727           g_string_append_c (uri, '@');
728         }
729       
730       g_string_append (uri, decoded->host);
731       
732       if (decoded->port != -1)
733         {
734           g_string_append_c (uri, ':');
735           g_string_append_printf (uri, "%d", decoded->port);
736         }
737     }
738
739   g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
740   
741   if (decoded->query)
742     {
743       g_string_append_c (uri, '?');
744       g_string_append (uri, decoded->query);
745     }
746     
747   if (decoded->fragment)
748     {
749       g_string_append_c (uri, '#');
750       g_string_append (uri, decoded->fragment);
751     }
752
753   return g_string_free (uri, FALSE);
754 }