Fix the build
[glib.git] / gio / gdummyfile.c
blob2287674db4d2cb4f52518512d47f77a61b631b51
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include <config.h>
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>
35 #include "gdummyfile.h"
37 #include "gioalias.h"
39 static void g_dummy_file_file_iface_init (GFileIface *iface);
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;
51 struct _GDummyFile
53 GObject parent_instance;
55 GDecodedUri *decoded_uri;
56 char *text_uri;
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))
64 #define SUB_DELIM_CHARS "!$&'()*+,;="
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);
71 static char * unescape_string (const gchar *escaped_string,
72 const gchar *escaped_string_end,
73 const gchar *illegal_characters);
75 static void g_string_append_encoded (GString *string,
76 const char *encoded,
77 const char *reserved_chars_allowed);
79 static void
80 g_dummy_file_finalize (GObject *object)
82 GDummyFile *dummy;
84 dummy = G_DUMMY_FILE (object);
86 if (dummy->decoded_uri)
87 _g_decoded_uri_free (dummy->decoded_uri);
89 g_free (dummy->text_uri);
91 if (G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize)
92 (*G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize) (object);
95 static void
96 g_dummy_file_class_init (GDummyFileClass *klass)
98 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
100 gobject_class->finalize = g_dummy_file_finalize;
103 static void
104 g_dummy_file_init (GDummyFile *dummy)
109 * g_dummy_file_new:
110 * @uri: Universal Resource Identifier for the dummy file object.
112 * Returns: a new #GFile.
114 GFile *
115 _g_dummy_file_new (const char *uri)
117 GDummyFile *dummy;
119 g_return_val_if_fail (uri != NULL, NULL);
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);
125 return G_FILE (dummy);
128 static gboolean
129 g_dummy_file_is_native (GFile *file)
131 return FALSE;
134 static char *
135 g_dummy_file_get_basename (GFile *file)
137 GDummyFile *dummy = G_DUMMY_FILE (file);
139 if (dummy->decoded_uri)
140 return g_path_get_basename (dummy->decoded_uri->path);
141 return g_strdup (dummy->text_uri);
144 static char *
145 g_dummy_file_get_path (GFile *file)
147 GDummyFile *dummy = G_DUMMY_FILE (file);
149 if (dummy->decoded_uri)
150 return g_strdup (dummy->decoded_uri->path);
151 return NULL;
154 static char *
155 g_dummy_file_get_uri (GFile *file)
157 return g_strdup (G_DUMMY_FILE (file)->text_uri);
160 static char *
161 g_dummy_file_get_parse_name (GFile *file)
163 return g_strdup (G_DUMMY_FILE (file)->text_uri);
166 static GFile *
167 g_dummy_file_get_parent (GFile *file)
169 GDummyFile *dummy = G_DUMMY_FILE (file);
170 GFile *parent;
171 char *dirname;
172 char *uri;
173 GDecodedUri new_decoded_uri;
175 if (dummy->decoded_uri == NULL)
176 return NULL;
178 dirname = g_path_get_dirname (dummy->decoded_uri->path);
180 if (strcmp (dirname, ".") == 0)
182 g_free (dirname);
183 return NULL;
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);
191 parent = _g_dummy_file_new (uri);
192 g_free (uri);
194 return parent;
197 static GFile *
198 g_dummy_file_dup (GFile *file)
200 GDummyFile *dummy = G_DUMMY_FILE (file);
202 return _g_dummy_file_new (dummy->text_uri);
205 static guint
206 g_dummy_file_hash (GFile *file)
208 GDummyFile *dummy = G_DUMMY_FILE (file);
210 return g_str_hash (dummy->text_uri);
213 static gboolean
214 g_dummy_file_equal (GFile *file1,
215 GFile *file2)
217 GDummyFile *dummy1 = G_DUMMY_FILE (file1);
218 GDummyFile *dummy2 = G_DUMMY_FILE (file2);
220 return g_str_equal (dummy1->text_uri, dummy2->text_uri);
223 static int
224 safe_strcmp (const char *a,
225 const char *b)
227 if (a == NULL)
228 a = "";
229 if (b == NULL)
230 b = "";
232 return strcmp (a, b);
235 static gboolean
236 uri_same_except_path (GDecodedUri *a,
237 GDecodedUri *b)
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;
248 return TRUE;
251 static const char *
252 match_prefix (const char *path,
253 const char *prefix)
255 int prefix_len;
257 prefix_len = strlen (prefix);
258 if (strncmp (path, prefix, prefix_len) != 0)
259 return NULL;
260 return path + prefix_len;
263 static gboolean
264 g_dummy_file_prefix_matches (GFile *parent, GFile *descendant)
266 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
267 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
268 const char *remainder;
270 if (parent_dummy->decoded_uri != NULL &&
271 descendant_dummy->decoded_uri != NULL)
273 if (uri_same_except_path (parent_dummy->decoded_uri,
274 descendant_dummy->decoded_uri))
276 remainder = match_prefix (descendant_dummy->decoded_uri->path,
277 parent_dummy->decoded_uri->path);
278 if (remainder != NULL && *remainder == '/')
280 while (*remainder == '/')
281 remainder++;
282 if (*remainder != 0)
283 return TRUE;
287 else
289 remainder = match_prefix (descendant_dummy->text_uri,
290 parent_dummy->text_uri);
291 if (remainder != NULL && *remainder == '/')
293 while (*remainder == '/')
294 remainder++;
295 if (*remainder != 0)
296 return TRUE;
300 return FALSE;
303 static char *
304 g_dummy_file_get_relative_path (GFile *parent,
305 GFile *descendant)
307 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
308 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
309 const char *remainder;
311 if (parent_dummy->decoded_uri != NULL &&
312 descendant_dummy->decoded_uri != NULL)
314 if (uri_same_except_path (parent_dummy->decoded_uri,
315 descendant_dummy->decoded_uri))
317 remainder = match_prefix (descendant_dummy->decoded_uri->path,
318 parent_dummy->decoded_uri->path);
319 if (remainder != NULL && *remainder == '/')
321 while (*remainder == '/')
322 remainder++;
323 if (*remainder != 0)
324 return g_strdup (remainder);
328 else
330 remainder = match_prefix (descendant_dummy->text_uri,
331 parent_dummy->text_uri);
332 if (remainder != NULL && *remainder == '/')
334 while (*remainder == '/')
335 remainder++;
336 if (*remainder != 0)
337 return unescape_string (remainder, NULL, "/");
341 return NULL;
345 static GFile *
346 g_dummy_file_resolve_relative_path (GFile *file,
347 const char *relative_path)
349 GDummyFile *dummy = G_DUMMY_FILE (file);
350 GFile *child;
351 char *uri;
352 GDecodedUri new_decoded_uri;
353 GString *str;
355 if (dummy->decoded_uri == NULL)
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);
363 else
365 new_decoded_uri = *dummy->decoded_uri;
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);
372 uri = _g_encode_uri (&new_decoded_uri);
373 g_free (new_decoded_uri.path);
375 child = _g_dummy_file_new (uri);
376 g_free (uri);
379 return child;
382 static GFile *
383 g_dummy_file_get_child_for_display_name (GFile *file,
384 const char *display_name,
385 GError **error)
387 return g_file_get_child (file, display_name);
390 static gboolean
391 g_dummy_file_has_uri_scheme (GFile *file,
392 const char *uri_scheme)
394 GDummyFile *dummy = G_DUMMY_FILE (file);
396 if (dummy->decoded_uri)
397 return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
398 return FALSE;
401 static char *
402 g_dummy_file_get_uri_scheme (GFile *file)
404 GDummyFile *dummy = G_DUMMY_FILE (file);
406 if (dummy->decoded_uri)
407 return g_strdup (dummy->decoded_uri->scheme);
409 return NULL;
413 static void
414 g_dummy_file_file_iface_init (GFileIface *iface)
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;
433 /* Uri handling helper functions: */
435 static int
436 unescape_character (const char *scanner)
438 int first_digit;
439 int second_digit;
441 first_digit = g_ascii_xdigit_value (*scanner++);
442 if (first_digit < 0)
443 return -1;
445 second_digit = g_ascii_xdigit_value (*scanner++);
446 if (second_digit < 0)
447 return -1;
449 return (first_digit << 4) | second_digit;
452 static char *
453 unescape_string (const gchar *escaped_string,
454 const gchar *escaped_string_end,
455 const gchar *illegal_characters)
457 const gchar *in;
458 gchar *out, *result;
459 gint character;
461 if (escaped_string == NULL)
462 return NULL;
464 if (escaped_string_end == NULL)
465 escaped_string_end = escaped_string + strlen (escaped_string);
467 result = g_malloc (escaped_string_end - escaped_string + 1);
469 out = result;
470 for (in = escaped_string; in < escaped_string_end; in++)
472 character = *in;
473 if (*in == '%')
475 in++;
476 if (escaped_string_end - in < 2)
478 g_free (result);
479 return NULL;
482 character = unescape_character (in);
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))
489 g_free (result);
490 return NULL;
492 in++; /* The other char will be eaten in the loop header */
494 *out++ = (char)character;
497 *out = '\0';
498 g_warn_if_fail (out - result <= strlen (escaped_string));
499 return result;
502 void
503 _g_decoded_uri_free (GDecodedUri *decoded)
505 if (decoded == NULL)
506 return;
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);
517 GDecodedUri *
518 _g_decoded_uri_new (void)
520 GDecodedUri *uri;
522 uri = g_new0 (GDecodedUri, 1);
523 uri->port = -1;
525 return uri;
528 GDecodedUri *
529 _g_decode_uri (const char *uri)
531 GDecodedUri *decoded;
532 const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
533 char *out;
534 char c;
536 /* From RFC 3986 Decodes:
537 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
540 p = uri;
542 /* Decode scheme:
543 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
546 if (!g_ascii_isalpha (*p))
547 return NULL;
549 while (1)
551 c = *p++;
553 if (c == ':')
554 break;
556 if (!(g_ascii_isalnum(c) ||
557 c == '+' ||
558 c == '-' ||
559 c == '.'))
560 return NULL;
563 decoded = _g_decoded_uri_new ();
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;
571 hier_part_start = p;
573 query_start = strchr (p, '?');
574 if (query_start)
576 hier_part_end = query_start++;
577 fragment_start = strchr (query_start, '#');
578 if (fragment_start)
580 decoded->query = g_strndup (query_start, fragment_start - query_start);
581 decoded->fragment = g_strdup (fragment_start+1);
583 else
585 decoded->query = g_strdup (query_start);
586 decoded->fragment = NULL;
589 else
591 /* No query */
592 decoded->query = NULL;
593 fragment_start = strchr (p, '#');
594 if (fragment_start)
596 hier_part_end = fragment_start++;
597 decoded->fragment = g_strdup (fragment_start);
599 else
601 hier_part_end = p + strlen (p);
602 decoded->fragment = NULL;
606 /* 3:
607 hier-part = "//" authority path-abempty
608 / path-absolute
609 / path-rootless
610 / path-empty
614 if (hier_part_start[0] == '/' &&
615 hier_part_start[1] == '/')
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;
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;
628 /* 3.2:
629 authority = [ userinfo "@" ] host [ ":" port ]
632 userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
633 if (userinfo_end)
635 userinfo_start = authority_start;
636 decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
637 if (decoded->userinfo == NULL)
639 _g_decoded_uri_free (decoded);
640 return NULL;
642 host_start = userinfo_end + 1;
644 else
645 host_start = authority_start;
647 port_start = memchr (host_start, ':', authority_end - host_start);
648 if (port_start)
650 host_end = port_start++;
652 decoded->port = atoi(port_start);
654 else
656 host_end = authority_end;
657 decoded->port = -1;
660 decoded->host = g_strndup (host_start, host_end - host_start);
662 hier_part_start = authority_end;
665 decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
667 if (decoded->path == NULL)
669 _g_decoded_uri_free (decoded);
670 return NULL;
673 return decoded;
676 static gboolean
677 is_valid (char c, const char *reserved_chars_allowed)
679 if (g_ascii_isalnum (c) ||
680 c == '-' ||
681 c == '.' ||
682 c == '_' ||
683 c == '~')
684 return TRUE;
686 if (reserved_chars_allowed &&
687 strchr (reserved_chars_allowed, c) != NULL)
688 return TRUE;
690 return FALSE;
693 static void
694 g_string_append_encoded (GString *string,
695 const char *encoded,
696 const char *reserved_chars_allowed)
698 unsigned char c;
699 const char *end;
700 static const gchar hex[16] = "0123456789ABCDEF";
702 end = encoded + strlen (encoded);
704 while ((c = *encoded) != 0)
706 if (is_valid (c, reserved_chars_allowed))
708 g_string_append_c (string, c);
709 encoded++;
711 else
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++;
721 static char *
722 _g_encode_uri (GDecodedUri *decoded)
724 GString *uri;
726 uri = g_string_new (NULL);
728 g_string_append (uri, decoded->scheme);
729 g_string_append (uri, "://");
731 if (decoded->host != NULL)
733 if (decoded->userinfo)
735 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
736 g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
737 g_string_append_c (uri, '@');
740 g_string_append (uri, decoded->host);
742 if (decoded->port != -1)
744 g_string_append_c (uri, ':');
745 g_string_append_printf (uri, "%d", decoded->port);
749 g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
751 if (decoded->query)
753 g_string_append_c (uri, '?');
754 g_string_append (uri, decoded->query);
757 if (decoded->fragment)
759 g_string_append_c (uri, '#');
760 g_string_append (uri, decoded->fragment);
763 return g_string_free (uri, FALSE);