[start of port to win32/msvc] HAVE_UNIST_H and _pipe() only include
[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_contains_file (GFile *parent,
265                             GFile *descendant)
266 {
267   GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
268   GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
269   const char *remainder;
270
271   if (parent_dummy->decoded_uri != NULL &&
272       descendant_dummy->decoded_uri != NULL)
273     {
274       if (uri_same_except_path (parent_dummy->decoded_uri,
275                                 descendant_dummy->decoded_uri)) 
276         {
277           remainder = match_prefix (descendant_dummy->decoded_uri->path,
278                                     parent_dummy->decoded_uri->path);
279           if (remainder != NULL && *remainder == '/')
280             {
281               while (*remainder == '/')
282                 remainder++;
283               if (*remainder != 0)
284                 return TRUE;
285             }
286         }
287     }
288   else
289     {
290       remainder = match_prefix (descendant_dummy->text_uri,
291                                 parent_dummy->text_uri);
292       if (remainder != NULL && *remainder == '/')
293           {
294             while (*remainder == '/')
295               remainder++;
296             if (*remainder != 0)
297               return TRUE;
298           }
299     }
300   
301   return FALSE;
302 }
303
304 static char *
305 g_dummy_file_get_relative_path (GFile *parent,
306                                 GFile *descendant)
307 {
308   GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
309   GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
310   const char *remainder;
311
312   if (parent_dummy->decoded_uri != NULL &&
313       descendant_dummy->decoded_uri != NULL)
314     {
315       if (uri_same_except_path (parent_dummy->decoded_uri,
316                                 descendant_dummy->decoded_uri)) 
317         {
318           remainder = match_prefix (descendant_dummy->decoded_uri->path,
319                                     parent_dummy->decoded_uri->path);
320           if (remainder != NULL && *remainder == '/')
321             {
322               while (*remainder == '/')
323                 remainder++;
324               if (*remainder != 0)
325                 return g_strdup (remainder);
326             }
327         }
328     }
329   else
330     {
331       remainder = match_prefix (descendant_dummy->text_uri,
332                                 parent_dummy->text_uri);
333       if (remainder != NULL && *remainder == '/')
334           {
335             while (*remainder == '/')
336               remainder++;
337             if (*remainder != 0)
338               return unescape_string (remainder, NULL, "/");
339           }
340     }
341   
342   return NULL;
343 }
344
345
346 static GFile *
347 g_dummy_file_resolve_relative_path (GFile      *file,
348                                     const char *relative_path)
349 {
350   GDummyFile *dummy = G_DUMMY_FILE (file);
351   GFile *child;
352   char *uri;
353   GDecodedUri new_decoded_uri;
354   GString *str;
355
356   if (dummy->decoded_uri == NULL)
357     {
358       str = g_string_new (dummy->text_uri);
359       g_string_append (str, "/");
360       g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
361       child = _g_dummy_file_new (str->str);
362       g_string_free (str, TRUE);
363     }
364   else
365     {
366       new_decoded_uri = *dummy->decoded_uri;
367       
368       if (g_path_is_absolute (relative_path))
369         new_decoded_uri.path = g_strdup (relative_path);
370       else
371         new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
372       
373       uri = _g_encode_uri (&new_decoded_uri);
374       g_free (new_decoded_uri.path);
375       
376       child = _g_dummy_file_new (uri);
377       g_free (uri);
378     }
379
380   return child;
381 }
382
383 static GFile *
384 g_dummy_file_get_child_for_display_name (GFile        *file,
385                                          const char   *display_name,
386                                          GError      **error)
387 {
388   return g_file_get_child (file, display_name);
389 }
390
391 static gboolean
392 g_dummy_file_has_uri_scheme (GFile *file,
393                              const char *uri_scheme)
394 {
395   GDummyFile *dummy = G_DUMMY_FILE (file);
396   
397   if (dummy->decoded_uri)
398     return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
399   return FALSE;
400 }
401
402 static char *
403 g_dummy_file_get_uri_scheme (GFile *file)
404 {
405   GDummyFile *dummy = G_DUMMY_FILE (file);
406
407   if (dummy->decoded_uri)
408     return g_strdup (dummy->decoded_uri->scheme);
409     
410   return NULL;
411 }
412
413
414 static void
415 g_dummy_file_file_iface_init (GFileIface *iface)
416 {
417   iface->dup = g_dummy_file_dup;
418   iface->hash = g_dummy_file_hash;
419   iface->equal = g_dummy_file_equal;
420   iface->is_native = g_dummy_file_is_native;
421   iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
422   iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
423   iface->get_basename = g_dummy_file_get_basename;
424   iface->get_path = g_dummy_file_get_path;
425   iface->get_uri = g_dummy_file_get_uri;
426   iface->get_parse_name = g_dummy_file_get_parse_name;
427   iface->get_parent = g_dummy_file_get_parent;
428   iface->contains_file = g_dummy_file_contains_file;
429   iface->get_relative_path = g_dummy_file_get_relative_path;
430   iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
431   iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
432 }
433
434 /* Uri handling helper functions: */
435
436 static int
437 unescape_character (const char *scanner)
438 {
439   int first_digit;
440   int second_digit;
441   
442   first_digit = g_ascii_xdigit_value (*scanner++);
443   if (first_digit < 0)
444     return -1;
445
446   second_digit = g_ascii_xdigit_value (*scanner++);
447   if (second_digit < 0)
448     return -1;
449
450   return (first_digit << 4) | second_digit;
451 }
452
453 static char *
454 unescape_string (const gchar *escaped_string,
455                  const gchar *escaped_string_end,
456                  const gchar *illegal_characters)
457 {
458   const gchar *in;
459   gchar *out, *result;
460   gint character;
461   
462   if (escaped_string == NULL)
463     return NULL;
464
465   if (escaped_string_end == NULL)
466     escaped_string_end = escaped_string + strlen (escaped_string);
467   
468   result = g_malloc (escaped_string_end - escaped_string + 1);
469         
470   out = result;
471   for (in = escaped_string; in < escaped_string_end; in++) 
472     {
473       character = *in;
474       if (*in == '%') 
475         {
476           in++;
477           if (escaped_string_end - in < 2)
478             {
479               g_free (result);
480               return NULL;
481             }
482       
483           character = unescape_character (in);
484       
485           /* Check for an illegal character. We consider '\0' illegal here. */
486           if (character <= 0 ||
487               (illegal_characters != NULL &&
488                strchr (illegal_characters, (char)character) != NULL))
489             {
490               g_free (result);
491               return NULL;
492             }
493           in++; /* The other char will be eaten in the loop header */
494         }
495       *out++ = (char)character;
496     }
497   
498   *out = '\0';
499   g_assert (out - result <= strlen (escaped_string));
500   return result;
501 }
502
503 void
504 _g_decoded_uri_free (GDecodedUri *decoded)
505 {
506   if (decoded == NULL)
507     return;
508
509   g_free (decoded->scheme);
510   g_free (decoded->query);
511   g_free (decoded->fragment);
512   g_free (decoded->userinfo);
513   g_free (decoded->host);
514   g_free (decoded->path);
515   g_free (decoded);
516 }
517
518 GDecodedUri *
519 _g_decoded_uri_new (void)
520 {
521   GDecodedUri *uri;
522
523   uri = g_new0 (GDecodedUri, 1);
524   uri->port = -1;
525
526   return uri;
527 }
528
529 GDecodedUri *
530 _g_decode_uri (const char *uri)
531 {
532   GDecodedUri *decoded;
533   const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
534   char *out;
535   char c;
536
537   /* From RFC 3986 Decodes:
538    * URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
539    */ 
540
541   p = uri;
542   
543   /* Decode scheme:
544      scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
545   */
546
547   if (!g_ascii_isalpha (*p))
548     return NULL;
549
550   while (1)
551     {
552       c = *p++;
553
554       if (c == ':')
555         break;
556       
557       if (!(g_ascii_isalnum(c) ||
558             c == '+' ||
559             c == '-' ||
560             c == '.'))
561         return NULL;
562     }
563
564   decoded = _g_decoded_uri_new ();
565   
566   decoded->scheme = g_malloc (p - uri);
567   out = decoded->scheme;
568   for (in = uri; in < p - 1; in++)
569     *out++ = g_ascii_tolower (*in);
570   *out = 0;
571
572   hier_part_start = p;
573
574   query_start = strchr (p, '?');
575   if (query_start)
576     {
577       hier_part_end = query_start++;
578       fragment_start = strchr (query_start, '#');
579       if (fragment_start)
580         {
581           decoded->query = g_strndup (query_start, fragment_start - query_start);
582           decoded->fragment = g_strdup (fragment_start+1);
583         }
584       else
585         {
586           decoded->query = g_strdup (query_start);
587           decoded->fragment = NULL;
588         }
589     }
590   else
591     {
592       /* No query */
593       decoded->query = NULL;
594       fragment_start = strchr (p, '#');
595       if (fragment_start)
596         {
597           hier_part_end = fragment_start++;
598           decoded->fragment = g_strdup (fragment_start);
599         }
600       else
601         {
602           hier_part_end = p + strlen (p);
603           decoded->fragment = NULL;
604         }
605     }
606
607   /*  3:
608       hier-part   = "//" authority path-abempty
609                   / path-absolute
610                   / path-rootless
611                   / path-empty
612
613   */
614
615   if (hier_part_start[0] == '/' &&
616       hier_part_start[1] == '/')
617     {
618       const char *authority_start, *authority_end;
619       const char *userinfo_start, *userinfo_end;
620       const char *host_start, *host_end;
621       const char *port_start;
622       
623       authority_start = hier_part_start + 2;
624       /* authority is always followed by / or nothing */
625       authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
626       if (authority_end == NULL)
627         authority_end = hier_part_end;
628
629       /* 3.2:
630               authority   = [ userinfo "@" ] host [ ":" port ]
631       */
632
633       userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
634       if (userinfo_end)
635         {
636           userinfo_start = authority_start;
637           decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
638           if (decoded->userinfo == NULL)
639             {
640               _g_decoded_uri_free (decoded);
641               return NULL;
642             }
643           host_start = userinfo_end + 1;
644         }
645       else
646         host_start = authority_start;
647
648       port_start = memchr (host_start, ':', authority_end - host_start);
649       if (port_start)
650         {
651           host_end = port_start++;
652
653           decoded->port = atoi(port_start);
654         }
655       else
656         {
657           host_end = authority_end;
658           decoded->port = -1;
659         }
660
661       decoded->host = g_strndup (host_start, host_end - host_start);
662
663       hier_part_start = authority_end;
664     }
665
666   decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
667
668   if (decoded->path == NULL)
669     {
670       _g_decoded_uri_free (decoded);
671       return NULL;
672     }
673   
674   return decoded;
675 }
676
677 static gboolean
678 is_valid (char c, const char *reserved_chars_allowed)
679 {
680   if (g_ascii_isalnum (c) ||
681       c == '-' ||
682       c == '.' ||
683       c == '_' ||
684       c == '~')
685     return TRUE;
686
687   if (reserved_chars_allowed &&
688       strchr (reserved_chars_allowed, c) != NULL)
689     return TRUE;
690   
691   return FALSE;
692 }
693
694 static void
695 g_string_append_encoded (GString    *string, 
696                          const char *encoded,
697                          const char *reserved_chars_allowed)
698 {
699   unsigned char c;
700   const char *end;
701   static const gchar hex[16] = "0123456789ABCDEF";
702
703   end = encoded + strlen (encoded);
704   
705   while ((c = *encoded) != 0)
706     {
707       if (is_valid (c, reserved_chars_allowed))
708         {
709           g_string_append_c (string, c);
710           encoded++;
711         }
712       else
713         {
714           g_string_append_c (string, '%');
715           g_string_append_c (string, hex[((guchar)c) >> 4]);
716           g_string_append_c (string, hex[((guchar)c) & 0xf]);
717           encoded++;
718         }
719     }
720 }
721
722 static char *
723 _g_encode_uri (GDecodedUri *decoded)
724 {
725   GString *uri;
726
727   uri = g_string_new (NULL);
728
729   g_string_append (uri, decoded->scheme);
730   g_string_append (uri, "://");
731
732   if (decoded->host != NULL)
733     {
734       if (decoded->userinfo)
735         {
736           /* userinfo    = *( unreserved / pct-encoded / sub-delims / ":" ) */
737           g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
738           g_string_append_c (uri, '@');
739         }
740       
741       g_string_append (uri, decoded->host);
742       
743       if (decoded->port != -1)
744         {
745           g_string_append_c (uri, ':');
746           g_string_append_printf (uri, "%d", decoded->port);
747         }
748     }
749
750   g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
751   
752   if (decoded->query)
753     {
754       g_string_append_c (uri, '?');
755       g_string_append (uri, decoded->query);
756     }
757     
758   if (decoded->fragment)
759     {
760       g_string_append_c (uri, '#');
761       g_string_append (uri, decoded->fragment);
762     }
763
764   return g_string_free (uri, FALSE);
765 }