Coverage Report

Created: 2025-11-16 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rauc/subprojects/glib-2.76.5/gio/glocalfileenumerator.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
25
#include <glib.h>
26
#include <glocalfileenumerator.h>
27
#include <glocalfileinfo.h>
28
#include <glocalfile.h>
29
#include <gioerror.h>
30
#include <string.h>
31
#include <stdlib.h>
32
#include "glibintl.h"
33
34
35
0
#define CHUNK_SIZE 1000
36
37
#ifdef G_OS_WIN32
38
#define USE_GDIR
39
#endif
40
41
#ifndef USE_GDIR
42
43
#include <sys/types.h>
44
#include <dirent.h>
45
#include <errno.h>
46
47
typedef struct {
48
  char *name;
49
  long inode;
50
  GFileType type;
51
} DirEntry;
52
53
#endif
54
55
struct _GLocalFileEnumerator
56
{
57
  GFileEnumerator parent;
58
59
  GFileAttributeMatcher *matcher;
60
  GFileAttributeMatcher *reduced_matcher;
61
  char *filename;
62
  char *attributes;
63
  GFileQueryInfoFlags flags;
64
65
  gboolean got_parent_info;
66
  GLocalParentFileInfo parent_info;
67
  
68
#ifdef USE_GDIR
69
  GDir *dir;
70
#else
71
  DIR *dir;
72
  DirEntry *entries;
73
  int entries_pos;
74
  gboolean at_end;
75
#endif
76
  
77
  gboolean follow_symlinks;
78
};
79
80
#define g_local_file_enumerator_get_type _g_local_file_enumerator_get_type
81
0
G_DEFINE_TYPE (GLocalFileEnumerator, g_local_file_enumerator, G_TYPE_FILE_ENUMERATOR)
82
0
83
0
static GFileInfo *g_local_file_enumerator_next_file (GFileEnumerator  *enumerator,
84
0
                 GCancellable     *cancellable,
85
0
                 GError          **error);
86
0
static gboolean   g_local_file_enumerator_close     (GFileEnumerator  *enumerator,
87
0
                 GCancellable     *cancellable,
88
0
                 GError          **error);
89
0
90
0
91
0
static void
92
0
free_entries (GLocalFileEnumerator *local)
93
0
{
94
0
#ifndef USE_GDIR
95
0
  int i;
96
97
0
  if (local->entries != NULL)
98
0
    {
99
0
      for (i = 0; local->entries[i].name != NULL; i++)
100
0
  g_free (local->entries[i].name);
101
      
102
0
      g_free (local->entries);
103
0
    }
104
0
#endif
105
0
}
106
107
static void
108
g_local_file_enumerator_finalize (GObject *object)
109
0
{
110
0
  GLocalFileEnumerator *local;
111
112
0
  local = G_LOCAL_FILE_ENUMERATOR (object);
113
114
0
  if (local->got_parent_info)
115
0
    _g_local_file_info_free_parent_info (&local->parent_info);
116
0
  g_free (local->filename);
117
0
  g_file_attribute_matcher_unref (local->matcher);
118
0
  g_file_attribute_matcher_unref (local->reduced_matcher);
119
0
  if (local->dir)
120
0
    {
121
#ifdef USE_GDIR
122
      g_dir_close (local->dir);
123
#else
124
0
      closedir (local->dir);
125
0
#endif      
126
0
      local->dir = NULL;
127
0
    }
128
129
0
  free_entries (local);
130
131
0
  G_OBJECT_CLASS (g_local_file_enumerator_parent_class)->finalize (object);
132
0
}
133
134
135
static void
136
g_local_file_enumerator_class_init (GLocalFileEnumeratorClass *klass)
137
0
{
138
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139
0
  GFileEnumeratorClass *enumerator_class = G_FILE_ENUMERATOR_CLASS (klass);
140
  
141
0
  gobject_class->finalize = g_local_file_enumerator_finalize;
142
143
0
  enumerator_class->next_file = g_local_file_enumerator_next_file;
144
0
  enumerator_class->close_fn = g_local_file_enumerator_close;
145
0
}
146
147
static void
148
g_local_file_enumerator_init (GLocalFileEnumerator *local)
149
0
{
150
0
}
151
152
#ifdef USE_GDIR
153
static void
154
convert_file_to_io_error (GError **error,
155
        GError  *file_error)
156
{
157
  int new_code;
158
159
  if (file_error == NULL)
160
    return;
161
  
162
  new_code = G_IO_ERROR_FAILED;
163
  
164
  if (file_error->domain == G_FILE_ERROR) 
165
    {
166
      switch (file_error->code) 
167
        {
168
        case G_FILE_ERROR_NOENT:
169
          new_code = G_IO_ERROR_NOT_FOUND;
170
          break;
171
        case G_FILE_ERROR_ACCES:
172
          new_code = G_IO_ERROR_PERMISSION_DENIED;
173
          break;
174
        case G_FILE_ERROR_NOTDIR:
175
          new_code = G_IO_ERROR_NOT_DIRECTORY;
176
          break;
177
        case G_FILE_ERROR_MFILE:
178
          new_code = G_IO_ERROR_TOO_MANY_OPEN_FILES;
179
          break;
180
        default:
181
          break;
182
        }
183
    }
184
  
185
  g_set_error_literal (error, G_IO_ERROR,
186
                       new_code,
187
                       file_error->message);
188
}
189
#else
190
static GFileAttributeMatcher *
191
g_file_attribute_matcher_subtract_attributes (GFileAttributeMatcher *matcher,
192
                                              const char *           attributes)
193
0
{
194
0
  GFileAttributeMatcher *result, *tmp;
195
196
0
  tmp = g_file_attribute_matcher_new (attributes);
197
0
  result = g_file_attribute_matcher_subtract (matcher, tmp);
198
0
  g_file_attribute_matcher_unref (tmp);
199
200
0
  return result;
201
0
}
202
#endif
203
204
GFileEnumerator *
205
_g_local_file_enumerator_new (GLocalFile *file,
206
            const char           *attributes,
207
            GFileQueryInfoFlags   flags,
208
            GCancellable         *cancellable,
209
            GError              **error)
210
0
{
211
0
  GLocalFileEnumerator *local;
212
0
  char *filename = g_file_get_path (G_FILE (file));
213
214
#ifdef USE_GDIR
215
  GError *dir_error;
216
  GDir *dir;
217
  
218
  dir_error = NULL;
219
  dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
220
  if (dir == NULL) 
221
    {
222
      if (error != NULL)
223
  {
224
    convert_file_to_io_error (error, dir_error);
225
    g_error_free (dir_error);
226
  }
227
      g_free (filename);
228
      return NULL;
229
    }
230
#else
231
0
  DIR *dir;
232
0
  int errsv;
233
234
0
  dir = opendir (filename);
235
0
  if (dir == NULL)
236
0
    {
237
0
      gchar *utf8_filename;
238
0
      errsv = errno;
239
240
0
      utf8_filename = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
241
0
      g_set_error (error, G_IO_ERROR,
242
0
                   g_io_error_from_errno (errsv),
243
0
                   "Error opening directory '%s': %s",
244
0
                   utf8_filename, g_strerror (errsv));
245
0
      g_free (utf8_filename);
246
0
      g_free (filename);
247
0
      return NULL;
248
0
    }
249
250
0
#endif
251
  
252
0
  local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR,
253
0
                        "container", file,
254
0
                        NULL);
255
256
0
  local->dir = dir;
257
0
  local->filename = filename;
258
0
  local->matcher = g_file_attribute_matcher_new (attributes);
259
0
#ifndef USE_GDIR
260
0
  local->reduced_matcher = g_file_attribute_matcher_subtract_attributes (local->matcher,
261
0
                                                                         G_LOCAL_FILE_INFO_NOSTAT_ATTRIBUTES","
262
0
                                                                         "standard::type");
263
0
#endif
264
0
  local->flags = flags;
265
  
266
0
  return G_FILE_ENUMERATOR (local);
267
0
}
268
269
#ifndef USE_GDIR
270
static int
271
sort_by_inode (const void *_a, const void *_b)
272
0
{
273
0
  const DirEntry *a, *b;
274
275
0
  a = _a;
276
0
  b = _b;
277
0
  return a->inode - b->inode;
278
0
}
279
280
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
281
static GFileType
282
file_type_from_dirent (char d_type)
283
0
{
284
0
  switch (d_type)
285
0
    {
286
0
    case DT_BLK:
287
0
    case DT_CHR:
288
0
    case DT_FIFO:
289
0
    case DT_SOCK:
290
0
      return G_FILE_TYPE_SPECIAL;
291
0
    case DT_DIR:
292
0
      return G_FILE_TYPE_DIRECTORY;
293
0
    case DT_LNK:
294
0
      return G_FILE_TYPE_SYMBOLIC_LINK;
295
0
    case DT_REG:
296
0
      return G_FILE_TYPE_REGULAR;
297
0
    case DT_UNKNOWN:
298
0
    default:
299
0
      return G_FILE_TYPE_UNKNOWN;
300
0
    }
301
0
}
302
#endif
303
304
static const char *
305
next_file_helper (GLocalFileEnumerator *local, GFileType *file_type)
306
0
{
307
0
  struct dirent *entry;
308
0
  const char *filename;
309
0
  int i;
310
311
0
  if (local->at_end)
312
0
    return NULL;
313
  
314
0
  if (local->entries == NULL ||
315
0
      (local->entries[local->entries_pos].name == NULL))
316
0
    {
317
0
      if (local->entries == NULL)
318
0
  local->entries = g_new (DirEntry, CHUNK_SIZE + 1);
319
0
      else
320
0
  {
321
    /* Restart by clearing old names */
322
0
    for (i = 0; local->entries[i].name != NULL; i++)
323
0
      g_free (local->entries[i].name);
324
0
  }
325
      
326
0
      for (i = 0; i < CHUNK_SIZE; i++)
327
0
  {
328
0
    entry = readdir (local->dir);
329
0
    while (entry 
330
0
     && (0 == strcmp (entry->d_name, ".") ||
331
0
         0 == strcmp (entry->d_name, "..")))
332
0
      entry = readdir (local->dir);
333
334
0
    if (entry)
335
0
      {
336
0
        local->entries[i].name = g_strdup (entry->d_name);
337
0
        local->entries[i].inode = entry->d_ino;
338
0
#if HAVE_STRUCT_DIRENT_D_TYPE
339
0
              local->entries[i].type = file_type_from_dirent (entry->d_type);
340
#else
341
              local->entries[i].type = G_FILE_TYPE_UNKNOWN;
342
#endif
343
0
      }
344
0
    else
345
0
      break;
346
0
  }
347
0
      local->entries[i].name = NULL;
348
0
      local->entries_pos = 0;
349
      
350
0
      qsort (local->entries, i, sizeof (DirEntry), sort_by_inode);
351
0
    }
352
353
0
  filename = local->entries[local->entries_pos].name;
354
0
  if (filename == NULL)
355
0
    local->at_end = TRUE;
356
    
357
0
  *file_type = local->entries[local->entries_pos].type;
358
359
0
  local->entries_pos++;
360
361
0
  return filename;
362
0
}
363
364
#endif
365
366
static GFileInfo *
367
g_local_file_enumerator_next_file (GFileEnumerator  *enumerator,
368
           GCancellable     *cancellable,
369
           GError          **error)
370
0
{
371
0
  GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
372
0
  const char *filename;
373
0
  char *path;
374
0
  GFileInfo *info;
375
0
  GError *my_error;
376
0
  GFileType file_type;
377
378
0
  if (!local->got_parent_info)
379
0
    {
380
0
      _g_local_file_info_get_parent_info (local->filename, local->matcher, &local->parent_info);
381
0
      local->got_parent_info = TRUE;
382
0
    }
383
384
0
 next_file:
385
386
#ifdef USE_GDIR
387
  filename = g_dir_read_name (local->dir);
388
  file_type = G_FILE_TYPE_UNKNOWN;
389
#else
390
0
  filename = next_file_helper (local, &file_type);
391
0
#endif
392
393
0
  if (filename == NULL)
394
0
    return NULL;
395
396
0
  my_error = NULL;
397
0
  path = g_build_filename (local->filename, filename, NULL);
398
0
  if (file_type == G_FILE_TYPE_UNKNOWN ||
399
0
      (file_type == G_FILE_TYPE_SYMBOLIC_LINK && !(local->flags & G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS)))
400
0
    {
401
0
      info = _g_local_file_info_get (filename, path,
402
0
                                     local->matcher,
403
0
                                     local->flags,
404
0
                                     &local->parent_info,
405
0
                                     &my_error); 
406
0
    }
407
0
  else
408
0
    {
409
0
      info = _g_local_file_info_get (filename, path,
410
0
                                     local->reduced_matcher,
411
0
                                     local->flags,
412
0
                                     &local->parent_info,
413
0
                                     &my_error); 
414
0
      if (info)
415
0
        {
416
0
          _g_local_file_info_get_nostat (info, filename, path, local->matcher);
417
0
          g_file_info_set_file_type (info, file_type);
418
0
          if (file_type == G_FILE_TYPE_SYMBOLIC_LINK)
419
0
            g_file_info_set_is_symlink (info, TRUE);
420
0
        }
421
0
    }
422
0
  g_free (path);
423
424
0
  if (info == NULL)
425
0
    {
426
      /* Failed to get info */
427
      /* If the file does not exist there might have been a race where
428
       * the file was removed between the readdir and the stat, so we
429
       * ignore the file. */
430
0
      if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
431
0
  {
432
0
    g_error_free (my_error);
433
0
    goto next_file;
434
0
  }
435
0
      else
436
0
  g_propagate_error (error, my_error);
437
0
    }
438
439
0
  return info;
440
0
}
441
442
static gboolean
443
g_local_file_enumerator_close (GFileEnumerator  *enumerator,
444
             GCancellable     *cancellable,
445
             GError          **error)
446
0
{
447
0
  GLocalFileEnumerator *local = G_LOCAL_FILE_ENUMERATOR (enumerator);
448
449
0
  if (local->dir)
450
0
    {
451
#ifdef USE_GDIR
452
      g_dir_close (local->dir);
453
#else
454
0
      closedir (local->dir);
455
0
#endif
456
0
      local->dir = NULL;
457
0
    }
458
459
0
  return TRUE;
460
0
}