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