Coverage Report

Created: 2025-10-13 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rauc/subprojects/glib-2.76.5/gio/glocalvfs.c
Line
Count
Source
1
/* GIO - GLib Input, Output and Streaming Library
2
 * 
3
 * Copyright (C) 2006-2007 Red Hat, Inc.
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * Author: Alexander Larsson <alexl@redhat.com>
21
 */
22
23
#include "config.h"
24
#include "glocalvfs.h"
25
#include "glocalfile.h"
26
#include "giomodule.h"
27
#include "giomodule-priv.h"
28
#include "gvfs.h"
29
#include <gio/gdummyfile.h>
30
#include <sys/types.h>
31
#ifdef G_OS_UNIX
32
#include "glib-unix.h"
33
#include <pwd.h>
34
#endif
35
#include <string.h>
36
37
38
struct _GLocalVfs
39
{
40
  GVfs parent;
41
};
42
43
struct _GLocalVfsClass
44
{
45
  GVfsClass parent_class;
46
  
47
};
48
49
#define g_local_vfs_get_type _g_local_vfs_get_type
50
4
G_DEFINE_TYPE_WITH_CODE (GLocalVfs, g_local_vfs, G_TYPE_VFS,
51
4
       _g_io_modules_ensure_extension_points_registered ();
52
4
       g_io_extension_point_implement (G_VFS_EXTENSION_POINT_NAME,
53
4
               g_define_type_id,
54
4
               "local",
55
4
               0))
56
4
static void
57
4
g_local_vfs_finalize (GObject *object)
58
4
{
59
  /* must chain up */
60
0
  G_OBJECT_CLASS (g_local_vfs_parent_class)->finalize (object);
61
0
}
62
63
static void
64
g_local_vfs_init (GLocalVfs *vfs)
65
1
{
66
1
}
67
68
/**
69
 * g_local_vfs_new:
70
 *
71
 * Returns a new #GVfs handle for a local vfs.
72
 *
73
 * Returns: a new #GVfs handle.
74
 **/
75
GVfs *
76
_g_local_vfs_new (void)
77
0
{
78
0
  return g_object_new (G_TYPE_LOCAL_VFS, NULL);
79
0
}
80
81
static GFile *
82
g_local_vfs_get_file_for_path (GVfs       *vfs,
83
                               const char *path)
84
4.59k
{
85
4.59k
  if (*path == '\0')
86
0
    return _g_dummy_file_new (path);
87
4.59k
  else
88
4.59k
    return _g_local_file_new (path);
89
4.59k
}
90
91
static GFile *
92
g_local_vfs_get_file_for_uri (GVfs       *vfs,
93
                              const char *uri)
94
0
{
95
0
  char *path;
96
0
  GFile *file;
97
0
  char *stripped_uri, *hash;
98
  
99
0
  if (strchr (uri, '#') != NULL)
100
0
    {
101
0
      stripped_uri = g_strdup (uri);
102
0
      hash = strchr (stripped_uri, '#');
103
0
      *hash = 0;
104
0
    }
105
0
  else
106
0
    stripped_uri = (char *)uri;
107
      
108
0
  path = g_filename_from_uri (stripped_uri, NULL, NULL);
109
110
0
  if (stripped_uri != uri)
111
0
    g_free (stripped_uri);
112
  
113
0
  if (path != NULL)
114
0
    file = _g_local_file_new (path);
115
0
  else
116
0
    file = _g_dummy_file_new (uri);
117
118
0
  g_free (path);
119
120
0
  return file;
121
0
}
122
123
static const gchar * const *
124
g_local_vfs_get_supported_uri_schemes (GVfs *vfs)
125
0
{
126
0
  static const gchar * uri_schemes[] = { "file", NULL };
127
128
0
  return uri_schemes;
129
0
}
130
131
static GFile *
132
g_local_vfs_parse_name (GVfs       *vfs,
133
                        const char *parse_name)
134
0
{
135
0
  GFile *file;
136
0
  char *filename;
137
0
  char *user_prefix;
138
0
  const char *user_end;
139
0
  char *rest;
140
  
141
0
  g_return_val_if_fail (G_IS_VFS (vfs), NULL);
142
0
  g_return_val_if_fail (parse_name != NULL, NULL);
143
144
0
  if (g_ascii_strncasecmp ("file:", parse_name, 5) == 0)
145
0
    filename = g_filename_from_uri (parse_name, NULL, NULL);
146
0
  else
147
0
    {
148
0
      if (*parse_name == '~')
149
0
  {
150
0
#ifdef G_OS_UNIX
151
0
    const char *user_start;
152
0
    user_start = parse_name + 1;
153
0
#endif
154
0
    parse_name ++;
155
    
156
0
    while (*parse_name != 0 && *parse_name != '/')
157
0
      parse_name++;
158
    
159
0
    user_end = parse_name;
160
161
0
#ifdef G_OS_UNIX
162
0
    if (user_end == user_start)
163
0
      user_prefix = g_strdup (g_get_home_dir ());
164
0
    else
165
0
      {
166
0
              struct passwd *passwd_file_entry;
167
0
              char *user_name;
168
169
0
              user_name = g_strndup (user_start, user_end - user_start);
170
0
              passwd_file_entry = g_unix_get_passwd_entry (user_name, NULL);
171
0
              g_free (user_name);
172
173
0
              if (passwd_file_entry != NULL &&
174
0
                  passwd_file_entry->pw_dir != NULL)
175
0
                user_prefix = g_strdup (passwd_file_entry->pw_dir);
176
0
              else
177
0
                user_prefix = g_strdup (g_get_home_dir ());
178
179
0
              g_free (passwd_file_entry);
180
0
      }
181
#else
182
    user_prefix = g_strdup (g_get_home_dir ());
183
#endif
184
185
0
    rest = NULL;
186
0
    if (*user_end != 0)
187
0
      rest = g_filename_from_utf8 (user_end, -1, NULL, NULL, NULL);
188
    
189
0
    filename = g_build_filename (user_prefix, rest, NULL);
190
0
    g_free (rest);
191
0
    g_free (user_prefix);
192
0
  }
193
0
      else
194
0
  filename = g_filename_from_utf8 (parse_name, -1, NULL, NULL, NULL);
195
0
    }
196
  
197
0
  if (filename == NULL)
198
0
    filename = g_strdup (parse_name);
199
    
200
0
  file = _g_local_file_new (filename);
201
0
  g_free (filename);
202
203
0
  return file;
204
0
}
205
206
static gboolean
207
g_local_vfs_is_active (GVfs *vfs)
208
1
{
209
1
  return TRUE;
210
1
}
211
212
static void
213
g_local_vfs_class_init (GLocalVfsClass *class)
214
1
{
215
1
  GObjectClass *object_class;
216
1
  GVfsClass *vfs_class;
217
  
218
1
  object_class = (GObjectClass *) class;
219
220
1
  object_class->finalize = g_local_vfs_finalize;
221
222
1
  vfs_class = G_VFS_CLASS (class);
223
224
1
  vfs_class->is_active = g_local_vfs_is_active;
225
1
  vfs_class->get_file_for_path = g_local_vfs_get_file_for_path;
226
1
  vfs_class->get_file_for_uri = g_local_vfs_get_file_for_uri;
227
1
  vfs_class->get_supported_uri_schemes = g_local_vfs_get_supported_uri_schemes;
228
1
  vfs_class->parse_name = g_local_vfs_parse_name;
229
1
}