gkdbus: Fix underflow and unreachable code bug
[platform/upstream/glib.git] / gio / glocalvfs.c
index 8e195c4..00fec8e 100644 (file)
@@ -2,10 +2,12 @@
  * 
  * Copyright (C) 2006-2007 Red Hat, Inc.
  *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
  * 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.
+ * version 2.1 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Alexander Larsson <alexl@redhat.com>
  */
 
-#include <config.h>
+#include "config.h"
 #include "glocalvfs.h"
 #include "glocalfile.h"
+#include "giomodule.h"
+#include "giomodule-priv.h"
+#include "gvfs.h"
 #include <gio/gdummyfile.h>
 #include <sys/types.h>
+#ifdef G_OS_UNIX
+#include "glib-unix.h"
 #include <pwd.h>
+#endif
+#include <string.h>
 
-#include "gioalias.h"
 
 struct _GLocalVfs
 {
@@ -41,8 +47,12 @@ struct _GLocalVfsClass
 };
 
 #define g_local_vfs_get_type _g_local_vfs_get_type
-G_DEFINE_TYPE (GLocalVfs, g_local_vfs, G_TYPE_VFS)
+G_DEFINE_TYPE_WITH_CODE (GLocalVfs, g_local_vfs, G_TYPE_VFS,
+                        _g_io_modules_ensure_extension_points_registered ();
+                        g_io_extension_point_implement (G_VFS_EXTENSION_POINT_NAME,
+                                                        g_define_type_id,
+                                                        "local",
+                                                        0))
 static void
 g_local_vfs_finalize (GObject *object)
 {
@@ -72,7 +82,10 @@ static GFile *
 g_local_vfs_get_file_for_path (GVfs       *vfs,
                                const char *path)
 {
-  return _g_local_file_new (path);
+  if (*path == '\0')
+    return _g_dummy_file_new (path);
+  else
+    return _g_local_file_new (path);
 }
 
 static GFile *
@@ -81,9 +94,32 @@ g_local_vfs_get_file_for_uri (GVfs       *vfs,
 {
   char *path;
   GFile *file;
+  char *stripped_uri, *hash, *question_mark;
 
-  path = g_filename_from_uri (uri, NULL, NULL);
+  /* As per https://url.spec.whatwg.org/#file-state, file: URIs can contain
+   * query and fragment sections. We ignore them in order to get only the file
+   * path. Compliance to this part of the WhatWG spec doesn’t necessarily mean
+   * we comply with the entire spec. */
+  if (strchr (uri, '#') != NULL)
+    {
+      stripped_uri = g_strdup (uri);
+      hash = strchr (stripped_uri, '#');
+      *hash = 0;
+    }
+  else if (strchr (uri, '?') != NULL)
+    {
+      stripped_uri = g_strdup (uri);
+      question_mark = strchr (stripped_uri, '?');
+      *question_mark = 0;
+    }
+  else
+    stripped_uri = (char *)uri;
+      
+  path = g_filename_from_uri (stripped_uri, NULL, NULL);
 
+  if (stripped_uri != uri)
+    g_free (stripped_uri);
+  
   if (path != NULL)
     file = _g_local_file_new (path);
   else
@@ -108,11 +144,9 @@ g_local_vfs_parse_name (GVfs       *vfs,
 {
   GFile *file;
   char *filename;
-  char *user_name;
   char *user_prefix;
-  const char *user_start, *user_end;
+  const char *user_end;
   char *rest;
-  struct passwd *passwd_file_entry;
   
   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
   g_return_val_if_fail (parse_name != NULL, NULL);
@@ -123,28 +157,40 @@ g_local_vfs_parse_name (GVfs       *vfs,
     {
       if (*parse_name == '~')
        {
+#ifdef G_OS_UNIX
+         const char *user_start;
+         user_start = parse_name + 1;
+#endif
          parse_name ++;
-         user_start = parse_name;
          
          while (*parse_name != 0 && *parse_name != '/')
            parse_name++;
          
          user_end = parse_name;
 
+#ifdef G_OS_UNIX
          if (user_end == user_start)
-           user_prefix = g_strdup (g_get_home_dir());
+           user_prefix = g_strdup (g_get_home_dir ());
          else
            {
-             user_name = g_strndup (user_start, user_end - user_start);
-             passwd_file_entry = getpwnam (user_name);
-             g_free (user_name);
-             
-             if (passwd_file_entry != NULL &&
-                 passwd_file_entry->pw_dir != NULL)
-               user_prefix = g_strdup (passwd_file_entry->pw_dir);
-             else
-               user_prefix = g_strdup (g_get_home_dir ());
+              struct passwd *passwd_file_entry;
+              char *user_name;
+
+              user_name = g_strndup (user_start, user_end - user_start);
+              passwd_file_entry = g_unix_get_passwd_entry (user_name, NULL);
+              g_free (user_name);
+
+              if (passwd_file_entry != NULL &&
+                  passwd_file_entry->pw_dir != NULL)
+                user_prefix = g_strdup (passwd_file_entry->pw_dir);
+              else
+                user_prefix = g_strdup (g_get_home_dir ());
+
+              g_free (passwd_file_entry);
            }
+#else
+         user_prefix = g_strdup (g_get_home_dir ());
+#endif
 
          rest = NULL;
          if (*user_end != 0)
@@ -185,9 +231,6 @@ g_local_vfs_class_init (GLocalVfsClass *class)
 
   vfs_class = G_VFS_CLASS (class);
 
-  vfs_class->name = "local";
-  vfs_class->priority = 0;
-  
   vfs_class->is_active = g_local_vfs_is_active;
   vfs_class->get_file_for_path = g_local_vfs_get_file_for_path;
   vfs_class->get_file_for_uri = g_local_vfs_get_file_for_uri;