gdummyfile.c 16.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22 23 24 25 26 27 28 29 30

#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>

#include "gdummyfile.h"
31
#include "gfile.h"
32

33

Matthias Clasen's avatar
Matthias Clasen committed
34
static void g_dummy_file_file_iface_init (GFileIface *iface);
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

typedef struct {
  char *scheme;
  char *userinfo;
  char *host;
  int port; /* -1 => not in uri */
  char *path;
  char *query;
  char *fragment;
} GDecodedUri;

struct _GDummyFile
{
  GObject parent_instance;

  GDecodedUri *decoded_uri;
  char *text_uri;
};

54
#define g_dummy_file_get_type _g_dummy_file_get_type
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
G_DEFINE_TYPE_WITH_CODE (GDummyFile, g_dummy_file, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
						g_dummy_file_file_iface_init))

#define SUB_DELIM_CHARS  "!$&'()*+,;="

static char *       _g_encode_uri       (GDecodedUri *decoded);
static void         _g_decoded_uri_free (GDecodedUri *decoded);
static GDecodedUri *_g_decode_uri       (const char  *uri);
static GDecodedUri *_g_decoded_uri_new  (void);

static char * unescape_string (const gchar *escaped_string,
			       const gchar *escaped_string_end,
			       const gchar *illegal_characters);

Matthias Clasen's avatar
Matthias Clasen committed
70 71
static void g_string_append_encoded (GString    *string, 
                                     const char *encoded,
72 73 74 75 76 77 78 79 80 81 82 83 84
				     const char *reserved_chars_allowed);

static void
g_dummy_file_finalize (GObject *object)
{
  GDummyFile *dummy;

  dummy = G_DUMMY_FILE (object);

  if (dummy->decoded_uri)
    _g_decoded_uri_free (dummy->decoded_uri);
  
  g_free (dummy->text_uri);
85 86

  G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize (object);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

static void
g_dummy_file_class_init (GDummyFileClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = g_dummy_file_finalize;
}

static void
g_dummy_file_init (GDummyFile *dummy)
{
}

GFile *
103
_g_dummy_file_new (const char *uri)
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
{
  GDummyFile *dummy;

  g_return_val_if_fail (uri != NULL, NULL);

  dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
  dummy->text_uri = g_strdup (uri);
  dummy->decoded_uri = _g_decode_uri (uri);
  
  return G_FILE (dummy);
}

static gboolean
g_dummy_file_is_native (GFile *file)
{
  return FALSE;
}

static char *
g_dummy_file_get_basename (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);
  
  if (dummy->decoded_uri)
    return g_path_get_basename (dummy->decoded_uri->path);
  return g_strdup (dummy->text_uri);
}

static char *
g_dummy_file_get_path (GFile *file)
{
  return NULL;
}

static char *
g_dummy_file_get_uri (GFile *file)
{
  return g_strdup (G_DUMMY_FILE (file)->text_uri);
}

static char *
g_dummy_file_get_parse_name (GFile *file)
{
  return g_strdup (G_DUMMY_FILE (file)->text_uri);
}

static GFile *
g_dummy_file_get_parent (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);
  GFile *parent;
  char *dirname;
  char *uri;
  GDecodedUri new_decoded_uri;

159 160
  if (dummy->decoded_uri == NULL ||
      g_strcmp0 (dummy->decoded_uri->path, "/") == 0)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    return NULL;

  dirname = g_path_get_dirname (dummy->decoded_uri->path);
  
  if (strcmp (dirname, ".") == 0)
    {
      g_free (dirname);
      return NULL;
    }
  
  new_decoded_uri = *dummy->decoded_uri;
  new_decoded_uri.path = dirname;
  uri = _g_encode_uri (&new_decoded_uri);
  g_free (dirname);
  
176
  parent = _g_dummy_file_new (uri);
177 178 179 180 181 182 183 184 185 186
  g_free (uri);
  
  return parent;
}

static GFile *
g_dummy_file_dup (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);

187
  return _g_dummy_file_new (dummy->text_uri);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
}

static guint
g_dummy_file_hash (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);
  
  return g_str_hash (dummy->text_uri);
}

static gboolean
g_dummy_file_equal (GFile *file1,
		    GFile *file2)
{
  GDummyFile *dummy1 = G_DUMMY_FILE (file1);
  GDummyFile *dummy2 = G_DUMMY_FILE (file2);

  return g_str_equal (dummy1->text_uri, dummy2->text_uri);
}

static int
Matthias Clasen's avatar
Matthias Clasen committed
209 210
safe_strcmp (const char *a, 
             const char *b)
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
{
  if (a == NULL)
    a = "";
  if (b == NULL)
    b = "";

  return strcmp (a, b);
}

static gboolean
uri_same_except_path (GDecodedUri *a,
		      GDecodedUri *b)
{
  if (safe_strcmp (a->scheme, b->scheme) != 0)
    return FALSE;
  if (safe_strcmp (a->userinfo, b->userinfo) != 0)
    return FALSE;
  if (safe_strcmp (a->host, b->host) != 0)
    return FALSE;
  if (a->port != b->port)
    return FALSE;

  return TRUE;
}

static const char *
Matthias Clasen's avatar
Matthias Clasen committed
237 238
match_prefix (const char *path, 
              const char *prefix)
239 240 241 242 243 244 245 246 247 248
{
  int prefix_len;

  prefix_len = strlen (prefix);
  if (strncmp (path, prefix, prefix_len) != 0)
    return NULL;
  return path + prefix_len;
}

static gboolean
249
g_dummy_file_prefix_matches (GFile *parent, GFile *descendant)
250 251 252 253 254 255 256 257 258
{
  GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
  GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
  const char *remainder;

  if (parent_dummy->decoded_uri != NULL &&
      descendant_dummy->decoded_uri != NULL)
    {
      if (uri_same_except_path (parent_dummy->decoded_uri,
259 260 261 262 263 264 265 266 267 268 269 270
				descendant_dummy->decoded_uri)) 
        {
	  remainder = match_prefix (descendant_dummy->decoded_uri->path,
                                    parent_dummy->decoded_uri->path);
          if (remainder != NULL && *remainder == '/')
	    {
	      while (*remainder == '/')
	        remainder++;
	      if (*remainder != 0)
	        return TRUE;
	    }
        }
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    }
  else
    {
      remainder = match_prefix (descendant_dummy->text_uri,
				parent_dummy->text_uri);
      if (remainder != NULL && *remainder == '/')
	  {
	    while (*remainder == '/')
	      remainder++;
	    if (*remainder != 0)
	      return TRUE;
	  }
    }
  
  return FALSE;
}

static char *
g_dummy_file_get_relative_path (GFile *parent,
				GFile *descendant)
{
  GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
  GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
  const char *remainder;

  if (parent_dummy->decoded_uri != NULL &&
      descendant_dummy->decoded_uri != NULL)
    {
      if (uri_same_except_path (parent_dummy->decoded_uri,
300 301 302 303 304 305 306 307 308 309 310 311
				descendant_dummy->decoded_uri)) 
        {
          remainder = match_prefix (descendant_dummy->decoded_uri->path,
                                    parent_dummy->decoded_uri->path);
          if (remainder != NULL && *remainder == '/')
	    {
	      while (*remainder == '/')
	        remainder++;
	      if (*remainder != 0)
	        return g_strdup (remainder);
	    }
        }
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    }
  else
    {
      remainder = match_prefix (descendant_dummy->text_uri,
				parent_dummy->text_uri);
      if (remainder != NULL && *remainder == '/')
	  {
	    while (*remainder == '/')
	      remainder++;
	    if (*remainder != 0)
	      return unescape_string (remainder, NULL, "/");
	  }
    }
  
  return NULL;
}


static GFile *
331
g_dummy_file_resolve_relative_path (GFile      *file,
332 333 334 335 336 337 338 339 340 341 342 343 344
				    const char *relative_path)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);
  GFile *child;
  char *uri;
  GDecodedUri new_decoded_uri;
  GString *str;

  if (dummy->decoded_uri == NULL)
    {
      str = g_string_new (dummy->text_uri);
      g_string_append (str, "/");
      g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
345
      child = _g_dummy_file_new (str->str);
346 347 348 349 350 351 352 353 354 355 356 357 358 359
      g_string_free (str, TRUE);
    }
  else
    {
      new_decoded_uri = *dummy->decoded_uri;
      
      if (g_path_is_absolute (relative_path))
	new_decoded_uri.path = g_strdup (relative_path);
      else
	new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
      
      uri = _g_encode_uri (&new_decoded_uri);
      g_free (new_decoded_uri.path);
      
360
      child = _g_dummy_file_new (uri);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
      g_free (uri);
    }

  return child;
}

static GFile *
g_dummy_file_get_child_for_display_name (GFile        *file,
					 const char   *display_name,
					 GError      **error)
{
  return g_file_get_child (file, display_name);
}

static gboolean
g_dummy_file_has_uri_scheme (GFile *file,
			     const char *uri_scheme)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);
  
  if (dummy->decoded_uri)
    return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
  return FALSE;
}

static char *
g_dummy_file_get_uri_scheme (GFile *file)
{
  GDummyFile *dummy = G_DUMMY_FILE (file);

  if (dummy->decoded_uri)
    return g_strdup (dummy->decoded_uri->scheme);
    
  return NULL;
}


static void
g_dummy_file_file_iface_init (GFileIface *iface)
{
  iface->dup = g_dummy_file_dup;
  iface->hash = g_dummy_file_hash;
  iface->equal = g_dummy_file_equal;
  iface->is_native = g_dummy_file_is_native;
  iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
  iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
  iface->get_basename = g_dummy_file_get_basename;
  iface->get_path = g_dummy_file_get_path;
  iface->get_uri = g_dummy_file_get_uri;
  iface->get_parse_name = g_dummy_file_get_parse_name;
  iface->get_parent = g_dummy_file_get_parent;
412
  iface->prefix_matches = g_dummy_file_prefix_matches;
413 414 415
  iface->get_relative_path = g_dummy_file_get_relative_path;
  iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
  iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
416 417

  iface->supports_thread_contexts = TRUE;
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
}

/* Uri handling helper functions: */

static int
unescape_character (const char *scanner)
{
  int first_digit;
  int second_digit;
  
  first_digit = g_ascii_xdigit_value (*scanner++);
  if (first_digit < 0)
    return -1;

  second_digit = g_ascii_xdigit_value (*scanner++);
  if (second_digit < 0)
    return -1;

  return (first_digit << 4) | second_digit;
}

static char *
unescape_string (const gchar *escaped_string,
		 const gchar *escaped_string_end,
		 const gchar *illegal_characters)
{
  const gchar *in;
  gchar *out, *result;
  gint character;
  
  if (escaped_string == NULL)
    return NULL;

  if (escaped_string_end == NULL)
    escaped_string_end = escaped_string + strlen (escaped_string);
  
  result = g_malloc (escaped_string_end - escaped_string + 1);
	
  out = result;
457 458 459 460 461 462 463 464 465 466 467
  for (in = escaped_string; in < escaped_string_end; in++) 
    {
      character = *in;
      if (*in == '%') 
        {
          in++;
          if (escaped_string_end - in < 2)
	    {
	      g_free (result);
	      return NULL;
	    }
468
      
469
          character = unescape_character (in);
470
      
471 472 473 474 475 476 477 478 479 480 481
          /* Check for an illegal character. We consider '\0' illegal here. */
          if (character <= 0 ||
	      (illegal_characters != NULL &&
	       strchr (illegal_characters, (char)character) != NULL))
	    {
	      g_free (result);
	      return NULL;
	    }
          in++; /* The other char will be eaten in the loop header */
        }
      *out++ = (char)character;
482 483 484
    }
  
  *out = '\0';
485
  g_warn_if_fail (out - result <= strlen (escaped_string));
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
  return result;
}

void
_g_decoded_uri_free (GDecodedUri *decoded)
{
  if (decoded == NULL)
    return;

  g_free (decoded->scheme);
  g_free (decoded->query);
  g_free (decoded->fragment);
  g_free (decoded->userinfo);
  g_free (decoded->host);
  g_free (decoded->path);
  g_free (decoded);
}

GDecodedUri *
_g_decoded_uri_new (void)
{
  GDecodedUri *uri;

  uri = g_new0 (GDecodedUri, 1);
  uri->port = -1;

  return uri;
}

GDecodedUri *
_g_decode_uri (const char *uri)
{
  GDecodedUri *decoded;
  const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
  char *out;
  char c;

  /* From RFC 3986 Decodes:
   * URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
   */ 

  p = uri;
  
  /* Decode scheme:
     scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  */

  if (!g_ascii_isalpha (*p))
    return NULL;

  while (1)
    {
      c = *p++;

      if (c == ':')
	break;
      
      if (!(g_ascii_isalnum(c) ||
	    c == '+' ||
	    c == '-' ||
	    c == '.'))
	return NULL;
    }

  decoded = _g_decoded_uri_new ();
  
  decoded->scheme = g_malloc (p - uri);
  out = decoded->scheme;
  for (in = uri; in < p - 1; in++)
    *out++ = g_ascii_tolower (*in);
  *out = 0;

  hier_part_start = p;

  query_start = strchr (p, '?');
  if (query_start)
    {
      hier_part_end = query_start++;
      fragment_start = strchr (query_start, '#');
      if (fragment_start)
	{
	  decoded->query = g_strndup (query_start, fragment_start - query_start);
	  decoded->fragment = g_strdup (fragment_start+1);
	}
      else
	{
	  decoded->query = g_strdup (query_start);
	  decoded->fragment = NULL;
	}
    }
  else
    {
      /* No query */
      decoded->query = NULL;
      fragment_start = strchr (p, '#');
      if (fragment_start)
	{
	  hier_part_end = fragment_start++;
	  decoded->fragment = g_strdup (fragment_start);
	}
      else
	{
	  hier_part_end = p + strlen (p);
	  decoded->fragment = NULL;
	}
    }

  /*  3:
      hier-part   = "//" authority path-abempty
                  / path-absolute
                  / path-rootless
                  / path-empty

  */

  if (hier_part_start[0] == '/' &&
      hier_part_start[1] == '/')
    {
      const char *authority_start, *authority_end;
      const char *userinfo_start, *userinfo_end;
      const char *host_start, *host_end;
      const char *port_start;
      
      authority_start = hier_part_start + 2;
      /* authority is always followed by / or nothing */
      authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
      if (authority_end == NULL)
	authority_end = hier_part_end;

      /* 3.2:
	      authority   = [ userinfo "@" ] host [ ":" port ]
      */

      userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
      if (userinfo_end)
	{
	  userinfo_start = authority_start;
	  decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
	  if (decoded->userinfo == NULL)
	    {
	      _g_decoded_uri_free (decoded);
	      return NULL;
	    }
	  host_start = userinfo_end + 1;
	}
      else
	host_start = authority_start;

      port_start = memchr (host_start, ':', authority_end - host_start);
      if (port_start)
	{
	  host_end = port_start++;

	  decoded->port = atoi(port_start);
	}
      else
	{
	  host_end = authority_end;
	  decoded->port = -1;
	}

      decoded->host = g_strndup (host_start, host_end - host_start);

      hier_part_start = authority_end;
    }

  decoded->path = unescape_string (hier_part_start, hier_part_end, "/");

  if (decoded->path == NULL)
    {
      _g_decoded_uri_free (decoded);
      return NULL;
    }
  
  return decoded;
}

static gboolean
is_valid (char c, const char *reserved_chars_allowed)
{
  if (g_ascii_isalnum (c) ||
      c == '-' ||
      c == '.' ||
      c == '_' ||
      c == '~')
    return TRUE;

  if (reserved_chars_allowed &&
      strchr (reserved_chars_allowed, c) != NULL)
    return TRUE;
  
  return FALSE;
}

static void
681
g_string_append_encoded (GString    *string,
Matthias Clasen's avatar
Matthias Clasen committed
682
                         const char *encoded,
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
			 const char *reserved_chars_allowed)
{
  unsigned char c;
  static const gchar hex[16] = "0123456789ABCDEF";

  while ((c = *encoded) != 0)
    {
      if (is_valid (c, reserved_chars_allowed))
	{
	  g_string_append_c (string, c);
	  encoded++;
	}
      else
	{
	  g_string_append_c (string, '%');
	  g_string_append_c (string, hex[((guchar)c) >> 4]);
	  g_string_append_c (string, hex[((guchar)c) & 0xf]);
	  encoded++;
	}
    }
}

static char *
_g_encode_uri (GDecodedUri *decoded)
{
  GString *uri;

  uri = g_string_new (NULL);

  g_string_append (uri, decoded->scheme);
  g_string_append (uri, "://");

  if (decoded->host != NULL)
    {
      if (decoded->userinfo)
	{
	  /* userinfo    = *( unreserved / pct-encoded / sub-delims / ":" ) */
	  g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
	  g_string_append_c (uri, '@');
	}
      
      g_string_append (uri, decoded->host);
      
      if (decoded->port != -1)
	{
	  g_string_append_c (uri, ':');
	  g_string_append_printf (uri, "%d", decoded->port);
	}
    }

  g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
  
  if (decoded->query)
    {
      g_string_append_c (uri, '?');
      g_string_append (uri, decoded->query);
    }
    
  if (decoded->fragment)
    {
      g_string_append_c (uri, '#');
      g_string_append (uri, decoded->fragment);
    }

  return g_string_free (uri, FALSE);
}