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