Forgotten entry
[glib.git] / gio / gdummyfile.c
blob6a5883fa3d4399bf08ced5ea28597a4b0698aae4
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"
36 #include "gfile.h"
38 #include "gioalias.h"
40 static void g_dummy_file_file_iface_init (GFileIface *iface);
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;
52 struct _GDummyFile
54 GObject parent_instance;
56 GDecodedUri *decoded_uri;
57 char *text_uri;
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))
65 #define SUB_DELIM_CHARS "!$&'()*+,;="
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);
72 static char * unescape_string (const gchar *escaped_string,
73 const gchar *escaped_string_end,
74 const gchar *illegal_characters);
76 static void g_string_append_encoded (GString *string,
77 const char *encoded,
78 const char *reserved_chars_allowed);
80 static void
81 g_dummy_file_finalize (GObject *object)
83 GDummyFile *dummy;
85 dummy = G_DUMMY_FILE (object);
87 if (dummy->decoded_uri)
88 _g_decoded_uri_free (dummy->decoded_uri);
90 g_free (dummy->text_uri);
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 g_strcmp0 (dummy->decoded_uri->path, "/") == 0)
177 return NULL;
179 dirname = g_path_get_dirname (dummy->decoded_uri->path);
181 if (strcmp (dirname, ".") == 0)
183 g_free (dirname);
184 return NULL;
187 new_decoded_uri = *dummy->decoded_uri;
188 new_decoded_uri.path = dirname;
189 uri = _g_encode_uri (&new_decoded_uri);
190 g_free (dirname);
192 parent = _g_dummy_file_new (uri);
193 g_free (uri);
195 return parent;
198 static GFile *
199 g_dummy_file_dup (GFile *file)
201 GDummyFile *dummy = G_DUMMY_FILE (file);
203 return _g_dummy_file_new (dummy->text_uri);
206 static guint
207 g_dummy_file_hash (GFile *file)
209 GDummyFile *dummy = G_DUMMY_FILE (file);
211 return g_str_hash (dummy->text_uri);
214 static gboolean
215 g_dummy_file_equal (GFile *file1,
216 GFile *file2)
218 GDummyFile *dummy1 = G_DUMMY_FILE (file1);
219 GDummyFile *dummy2 = G_DUMMY_FILE (file2);
221 return g_str_equal (dummy1->text_uri, dummy2->text_uri);
224 static int
225 safe_strcmp (const char *a,
226 const char *b)
228 if (a == NULL)
229 a = "";
230 if (b == NULL)
231 b = "";
233 return strcmp (a, b);
236 static gboolean
237 uri_same_except_path (GDecodedUri *a,
238 GDecodedUri *b)
240 if (safe_strcmp (a->scheme, b->scheme) != 0)
241 return FALSE;
242 if (safe_strcmp (a->userinfo, b->userinfo) != 0)
243 return FALSE;
244 if (safe_strcmp (a->host, b->host) != 0)
245 return FALSE;
246 if (a->port != b->port)
247 return FALSE;
249 return TRUE;
252 static const char *
253 match_prefix (const char *path,
254 const char *prefix)
256 int prefix_len;
258 prefix_len = strlen (prefix);
259 if (strncmp (path, prefix, prefix_len) != 0)
260 return NULL;
261 return path + prefix_len;
264 static gboolean
265 g_dummy_file_prefix_matches (GFile *parent, GFile *descendant)
267 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
268 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
269 const char *remainder;
271 if (parent_dummy->decoded_uri != NULL &&
272 descendant_dummy->decoded_uri != NULL)
274 if (uri_same_except_path (parent_dummy->decoded_uri,
275 descendant_dummy->decoded_uri))
277 remainder = match_prefix (descendant_dummy->decoded_uri->path,
278 parent_dummy->decoded_uri->path);
279 if (remainder != NULL && *remainder == '/')
281 while (*remainder == '/')
282 remainder++;
283 if (*remainder != 0)
284 return TRUE;
288 else
290 remainder = match_prefix (descendant_dummy->text_uri,
291 parent_dummy->text_uri);
292 if (remainder != NULL && *remainder == '/')
294 while (*remainder == '/')
295 remainder++;
296 if (*remainder != 0)
297 return TRUE;
301 return FALSE;
304 static char *
305 g_dummy_file_get_relative_path (GFile *parent,
306 GFile *descendant)
308 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
309 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
310 const char *remainder;
312 if (parent_dummy->decoded_uri != NULL &&
313 descendant_dummy->decoded_uri != NULL)
315 if (uri_same_except_path (parent_dummy->decoded_uri,
316 descendant_dummy->decoded_uri))
318 remainder = match_prefix (descendant_dummy->decoded_uri->path,
319 parent_dummy->decoded_uri->path);
320 if (remainder != NULL && *remainder == '/')
322 while (*remainder == '/')
323 remainder++;
324 if (*remainder != 0)
325 return g_strdup (remainder);
329 else
331 remainder = match_prefix (descendant_dummy->text_uri,
332 parent_dummy->text_uri);
333 if (remainder != NULL && *remainder == '/')
335 while (*remainder == '/')
336 remainder++;
337 if (*remainder != 0)
338 return unescape_string (remainder, NULL, "/");
342 return NULL;
346 static GFile *
347 g_dummy_file_resolve_relative_path (GFile *file,
348 const char *relative_path)
350 GDummyFile *dummy = G_DUMMY_FILE (file);
351 GFile *child;
352 char *uri;
353 GDecodedUri new_decoded_uri;
354 GString *str;
356 if (dummy->decoded_uri == NULL)
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);
364 else
366 new_decoded_uri = *dummy->decoded_uri;
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);
373 uri = _g_encode_uri (&new_decoded_uri);
374 g_free (new_decoded_uri.path);
376 child = _g_dummy_file_new (uri);
377 g_free (uri);
380 return child;
383 static GFile *
384 g_dummy_file_get_child_for_display_name (GFile *file,
385 const char *display_name,
386 GError **error)
388 return g_file_get_child (file, display_name);
391 static gboolean
392 g_dummy_file_has_uri_scheme (GFile *file,
393 const char *uri_scheme)
395 GDummyFile *dummy = G_DUMMY_FILE (file);
397 if (dummy->decoded_uri)
398 return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
399 return FALSE;
402 static char *
403 g_dummy_file_get_uri_scheme (GFile *file)
405 GDummyFile *dummy = G_DUMMY_FILE (file);
407 if (dummy->decoded_uri)
408 return g_strdup (dummy->decoded_uri->scheme);
410 return NULL;
414 static void
415 g_dummy_file_file_iface_init (GFileIface *iface)
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->prefix_matches = g_dummy_file_prefix_matches;
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;
434 /* Uri handling helper functions: */
436 static int
437 unescape_character (const char *scanner)
439 int first_digit;
440 int second_digit;
442 first_digit = g_ascii_xdigit_value (*scanner++);
443 if (first_digit < 0)
444 return -1;
446 second_digit = g_ascii_xdigit_value (*scanner++);
447 if (second_digit < 0)
448 return -1;
450 return (first_digit << 4) | second_digit;
453 static char *
454 unescape_string (const gchar *escaped_string,
455 const gchar *escaped_string_end,
456 const gchar *illegal_characters)
458 const gchar *in;
459 gchar *out, *result;
460 gint character;
462 if (escaped_string == NULL)
463 return NULL;
465 if (escaped_string_end == NULL)
466 escaped_string_end = escaped_string + strlen (escaped_string);
468 result = g_malloc (escaped_string_end - escaped_string + 1);
470 out = result;
471 for (in = escaped_string; in < escaped_string_end; in++)
473 character = *in;
474 if (*in == '%')
476 in++;
477 if (escaped_string_end - in < 2)
479 g_free (result);
480 return NULL;
483 character = unescape_character (in);
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))
490 g_free (result);
491 return NULL;
493 in++; /* The other char will be eaten in the loop header */
495 *out++ = (char)character;
498 *out = '\0';
499 g_warn_if_fail (out - result <= strlen (escaped_string));
500 return result;
503 void
504 _g_decoded_uri_free (GDecodedUri *decoded)
506 if (decoded == NULL)
507 return;
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);
518 GDecodedUri *
519 _g_decoded_uri_new (void)
521 GDecodedUri *uri;
523 uri = g_new0 (GDecodedUri, 1);
524 uri->port = -1;
526 return uri;
529 GDecodedUri *
530 _g_decode_uri (const char *uri)
532 GDecodedUri *decoded;
533 const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
534 char *out;
535 char c;
537 /* From RFC 3986 Decodes:
538 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
541 p = uri;
543 /* Decode scheme:
544 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
547 if (!g_ascii_isalpha (*p))
548 return NULL;
550 while (1)
552 c = *p++;
554 if (c == ':')
555 break;
557 if (!(g_ascii_isalnum(c) ||
558 c == '+' ||
559 c == '-' ||
560 c == '.'))
561 return NULL;
564 decoded = _g_decoded_uri_new ();
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;
572 hier_part_start = p;
574 query_start = strchr (p, '?');
575 if (query_start)
577 hier_part_end = query_start++;
578 fragment_start = strchr (query_start, '#');
579 if (fragment_start)
581 decoded->query = g_strndup (query_start, fragment_start - query_start);
582 decoded->fragment = g_strdup (fragment_start+1);
584 else
586 decoded->query = g_strdup (query_start);
587 decoded->fragment = NULL;
590 else
592 /* No query */
593 decoded->query = NULL;
594 fragment_start = strchr (p, '#');
595 if (fragment_start)
597 hier_part_end = fragment_start++;
598 decoded->fragment = g_strdup (fragment_start);
600 else
602 hier_part_end = p + strlen (p);
603 decoded->fragment = NULL;
607 /* 3:
608 hier-part = "//" authority path-abempty
609 / path-absolute
610 / path-rootless
611 / path-empty
615 if (hier_part_start[0] == '/' &&
616 hier_part_start[1] == '/')
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;
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;
629 /* 3.2:
630 authority = [ userinfo "@" ] host [ ":" port ]
633 userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
634 if (userinfo_end)
636 userinfo_start = authority_start;
637 decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
638 if (decoded->userinfo == NULL)
640 _g_decoded_uri_free (decoded);
641 return NULL;
643 host_start = userinfo_end + 1;
645 else
646 host_start = authority_start;
648 port_start = memchr (host_start, ':', authority_end - host_start);
649 if (port_start)
651 host_end = port_start++;
653 decoded->port = atoi(port_start);
655 else
657 host_end = authority_end;
658 decoded->port = -1;
661 decoded->host = g_strndup (host_start, host_end - host_start);
663 hier_part_start = authority_end;
666 decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
668 if (decoded->path == NULL)
670 _g_decoded_uri_free (decoded);
671 return NULL;
674 return decoded;
677 static gboolean
678 is_valid (char c, const char *reserved_chars_allowed)
680 if (g_ascii_isalnum (c) ||
681 c == '-' ||
682 c == '.' ||
683 c == '_' ||
684 c == '~')
685 return TRUE;
687 if (reserved_chars_allowed &&
688 strchr (reserved_chars_allowed, c) != NULL)
689 return TRUE;
691 return FALSE;
694 static void
695 g_string_append_encoded (GString *string,
696 const char *encoded,
697 const char *reserved_chars_allowed)
699 unsigned char c;
700 const char *end;
701 static const gchar hex[16] = "0123456789ABCDEF";
703 end = encoded + strlen (encoded);
705 while ((c = *encoded) != 0)
707 if (is_valid (c, reserved_chars_allowed))
709 g_string_append_c (string, c);
710 encoded++;
712 else
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++;
722 static char *
723 _g_encode_uri (GDecodedUri *decoded)
725 GString *uri;
727 uri = g_string_new (NULL);
729 g_string_append (uri, decoded->scheme);
730 g_string_append (uri, "://");
732 if (decoded->host != NULL)
734 if (decoded->userinfo)
736 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
737 g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
738 g_string_append_c (uri, '@');
741 g_string_append (uri, decoded->host);
743 if (decoded->port != -1)
745 g_string_append_c (uri, ':');
746 g_string_append_printf (uri, "%d", decoded->port);
750 g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
752 if (decoded->query)
754 g_string_append_c (uri, '?');
755 g_string_append (uri, decoded->query);
758 if (decoded->fragment)
760 g_string_append_c (uri, '#');
761 g_string_append (uri, decoded->fragment);
764 return g_string_free (uri, FALSE);