File: foundry-file.c

package info (click to toggle)
foundry 1.1~beta-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,552 kB
  • sloc: ansic: 167,487; xml: 417; makefile: 21; sh: 19; javascript: 10
file content (547 lines) | stat: -rw-r--r-- 16,536 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
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
412
413
414
415
416
417
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
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
/* foundry-file.c
 *
 * Copyright 2024 Christian Hergert <chergert@redhat.com>
 *
 * 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.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
 * 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 General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <errno.h>

#include <glib/gstdio.h>

#include "foundry-file.h"
#include "foundry-process-launcher.h"
#include "foundry-util-private.h"

static DexFuture *
foundry_file_find_in_ancestors_fiber (GFile      *file,
                                      const char *name)
{
  GFile *parent;

  g_assert (G_IS_FILE (file));
  g_assert (name != NULL);

  parent = g_file_get_parent (file);

  while (parent != NULL)
    {
      g_autoptr(GFile) child = g_file_get_child (parent, name);
      g_autoptr(GFile) old_parent = NULL;

      if (dex_await_boolean (dex_file_query_exists (child), NULL))
        return dex_future_new_true ();

      old_parent = g_steal_pointer (&parent);
      parent = g_file_get_parent (old_parent);
    }

  return dex_future_new_reject (G_IO_ERROR,
                                G_IO_ERROR_NOT_FOUND,
                                "Failed to locate \"%s\" within ancestors",
                                name);
}

/**
 * foundry_file_find_in_ancestors:
 * @file: a [iface@Gio.File]
 * @name: the name of the file to find in the ancestors such as ".gitignore"
 *
 * Locates @name within any of the ancestors of @file up to the root of
 * the filesystem.
 *
 * Returns: (transfer full): a [class@Dex.Future] that resolves to
 *   a [iface@Gio.File] or rejects with error,
 */
DexFuture *
foundry_file_find_in_ancestors (GFile      *file,
                                const char *name)
{
  dex_return_error_if_fail (G_IS_FILE (file));
  dex_return_error_if_fail (name != NULL);

  return foundry_scheduler_spawn (NULL, 0,
                                  G_CALLBACK (foundry_file_find_in_ancestors_fiber),
                                  2,
                                  G_TYPE_FILE, file,
                                  G_TYPE_STRING, name);
}

static gboolean
is_internally_ignored (const char *name)
{
  if (name == NULL)
    return TRUE;

  if (g_str_has_prefix (name, ".goutputstream-"))
    return TRUE;

  if (g_str_has_suffix (name, "~"))
    return TRUE;

  if (g_str_has_suffix (name, ".min.js") || strstr (name, ".min.js.") != NULL)
    return TRUE;

  return FALSE;
}

typedef struct _Find
{
  GFile        *file;
  GPatternSpec *spec;
  GRegex       *regex;
  guint         depth;
} Find;

static void
populate_descendants_matching (GFile        *file,
                               GCancellable *cancellable,
                               GPtrArray    *results,
                               const Find   *find,
                               guint         depth)
{
  g_autoptr(GFileEnumerator) enumerator = NULL;
  g_autoptr(GPtrArray) children = NULL;

  g_assert (G_IS_FILE (file));
  g_assert (results != NULL);
  g_assert (find != NULL);
  g_assert (find->regex || find->spec);
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  if (depth == 0)
    return;

  enumerator = g_file_enumerate_children (file,
                                          G_FILE_ATTRIBUTE_STANDARD_NAME","
                                          G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK","
                                          G_FILE_ATTRIBUTE_STANDARD_TYPE,
                                          G_FILE_QUERY_INFO_NONE,
                                          cancellable,
                                          NULL);

  if (enumerator == NULL)
    return;

  for (;;)
    {
      g_autoptr(GFileInfo) info = g_file_enumerator_next_file (enumerator, cancellable, NULL);
      const gchar *name;
      GFileType file_type;

      if (info == NULL)
        break;

      name = g_file_info_get_name (info);
      file_type = g_file_info_get_file_type (info);

      if (is_internally_ignored (name))
        continue;

      if ((find->spec && g_pattern_spec_match_string (find->spec, name)) ||
          (find->regex && g_regex_match (find->regex, name, 0, NULL)))
        g_ptr_array_add (results, g_file_enumerator_get_child (enumerator, info));

      if (!g_file_info_get_is_symlink (info) && file_type == G_FILE_TYPE_DIRECTORY)
        {
          /* Try to project ourselves a bit from common traps */
          if (g_strcmp0 (name, ".flatpak-builder") == 0 ||
              g_strcmp0 (name, ".cache") == 0)
            continue;

          if (children == NULL)
            children = g_ptr_array_new_with_free_func (g_object_unref);

          g_ptr_array_add (children, g_file_enumerator_get_child (enumerator, info));
        }
    }

  g_file_enumerator_close (enumerator, cancellable, NULL);

  if (children != NULL)
    {
      for (guint i = 0; i < children->len; i++)
        {
          GFile *child = g_ptr_array_index (children, i);

          populate_descendants_matching (child, cancellable, results, find, depth - 1);
        }
    }
}

static DexFuture *
find_matching_fiber (gpointer user_data)
{
  Find *find = user_data;
  g_autoptr(GPtrArray) ar = NULL;

  g_assert (find != NULL);
  g_assert (G_IS_FILE (find->file));
  g_assert (find->spec || find->regex);
  g_assert (find->depth > 0);

  ar = g_ptr_array_new_with_free_func (g_object_unref);

  populate_descendants_matching (find->file, NULL, ar, find, find->depth);

  return dex_future_new_take_boxed (G_TYPE_PTR_ARRAY, g_steal_pointer (&ar));
}

static void
find_free (Find *find)
{
  g_clear_object (&find->file);
  g_clear_pointer (&find->spec, g_pattern_spec_free);
  g_clear_pointer (&find->regex, g_regex_unref);
  g_free (find);
}

static DexFuture *
find_matching (GFile        *file,
               GPatternSpec *spec,
               guint         depth)
{
  Find *state;

  g_assert (G_IS_FILE (file));
  g_assert (spec != NULL);
  g_assert (depth > 0);

  state = g_new0 (Find, 1);
  state->file = g_object_ref (file);
  state->spec = g_pattern_spec_copy (spec);
  state->depth = depth;

  return dex_scheduler_spawn (dex_thread_pool_scheduler_get_default (), 0,
                              find_matching_fiber,
                              state,
                              (GDestroyNotify) find_free);

}

/**
 * foundry_file_find_with_depth:
 * @file: an [iface@Gio.File]
 * @pattern: the pattern to find
 * @max_depth: the max depth to recurse
 *
 * Locates files starting from @file matching @pattern.
 *
 * Returns: (transfer full): a [class@Dex.Future] that resolves
 *   to a [struct@GLib.PtrArray] of [iface@Gio.File].
 */
DexFuture *
foundry_file_find_with_depth (GFile       *file,
                              const gchar *pattern,
                              guint        max_depth)
{
  g_autoptr(GPatternSpec) spec = NULL;

  dex_return_error_if_fail (G_IS_FILE (file));
  dex_return_error_if_fail (pattern != NULL);

  if (!(spec = g_pattern_spec_new (pattern)))
    return dex_future_new_reject (G_IO_ERROR,
                                  G_IO_ERROR_INVAL,
                                  "Invalid pattern");

  if (max_depth == 0)
    max_depth = G_MAXUINT;

  return find_matching (file, spec, max_depth);
}

/**
 * foundry_file_find_regex_with_depth:
 * @file: an [iface@Gio.File]
 * @regex: A regex to match filenames within descendants
 * @max_depth: the max depth to recurse
 *
 * Locates files starting from @file matching @regex.
 *
 * The regex will be passed the name within the parent directory, not the enter
 * path from @file.
 *
 * Returns: (transfer full): a [class@Dex.Future]
 */
DexFuture *
foundry_file_find_regex_with_depth (GFile  *file,
                                    GRegex *regex,
                                    guint   max_depth)
{
  g_autoptr(GPatternSpec) spec = NULL;
  Find *state;

  dex_return_error_if_fail (G_IS_FILE (file));
  dex_return_error_if_fail (regex != NULL);

  if (max_depth == 0)
    max_depth = G_MAXUINT;

  state = g_new0 (Find, 1);
  state->file = g_object_ref (file);
  state->spec = NULL;
  state->regex = g_regex_ref (regex);
  state->depth = max_depth;

  return dex_scheduler_spawn (dex_thread_pool_scheduler_get_default (), 0,
                              find_matching_fiber,
                              state,
                              (GDestroyNotify) find_free);
}

/**
 * foundry_file_query_exists_nofollow:
 * @file: a [iface@Gio.File]
 *
 * Resolves to true if @file exists.
 *
 * Does not follow symlink.
 *
 * Returns: (transfer full): a future that resolves to a boolean
 */
DexFuture *
foundry_file_query_exists_nofollow (GFile *file)
{
  dex_return_error_if_fail (G_IS_FILE (file));

  return dex_future_then (dex_file_query_info (file,
                                               G_FILE_ATTRIBUTE_STANDARD_TYPE,
                                               G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                               G_PRIORITY_DEFAULT),
                          foundry_future_return_true,
                          NULL, NULL);
}

/* NOTE: This requires that file exists */
/**
 * foundry_file_canonicalize:
 * @file: a [iface@Gio.File]
 *
 * Returns: (transfer full):
 */
GFile *
foundry_file_canonicalize (GFile   *file,
                           GError **error)
{
  char *canonical_path = NULL;

  if ((canonical_path = realpath (g_file_peek_path (file), NULL)))
    {
      GFile *ret = g_file_new_for_path (canonical_path);
      free (canonical_path);
      return ret;
    }

  return (gpointer)foundry_set_error_from_errno (error);
}

/* NOTE: This requires both files to exist */
gboolean
foundry_file_is_in (GFile *file,
                    GFile *toplevel)
{
  g_autoptr(GFile) canonical_file = NULL;
  g_autoptr(GFile) canonical_toplevel = NULL;

  if (!(canonical_toplevel = foundry_file_canonicalize (toplevel, NULL)))
    return FALSE;

  if (!(canonical_file = foundry_file_canonicalize (file, NULL)))
    return FALSE;

  return g_file_equal (canonical_file, canonical_toplevel) ||
         g_file_has_prefix (canonical_file, canonical_toplevel);
}

typedef struct _ListChildrenTyped
{
  GFile     *file;
  char      *attributes;
  GFileType  file_type;
} ListChildrenTyped;

static void
list_children_typed_free (gpointer data)
{
  ListChildrenTyped *state = data;

  g_clear_object (&state->file);
  g_clear_pointer (&state->attributes, g_free);
  g_free (state);
}

static DexFuture *
foundry_list_children_typed_fiber (gpointer user_data)
{
  ListChildrenTyped *state = user_data;
  g_autoptr(GFileEnumerator) enumerator = NULL;
  g_autoptr(GPtrArray) ar = NULL;
  g_autoptr(GError) error = NULL;
  GList *list;

  g_assert (state != NULL);
  g_assert (G_IS_FILE (state->file));

  if (!(enumerator = dex_await_object (dex_file_enumerate_children (state->file,
                                                                    state->attributes,
                                                                    G_FILE_QUERY_INFO_NONE,
                                                                    G_PRIORITY_DEFAULT),
                                       &error)))
    return dex_future_new_for_error (g_steal_pointer (&error));

  ar = g_ptr_array_new_with_free_func (g_object_unref);

  while ((list = dex_await_boxed (dex_file_enumerator_next_files (enumerator,
                                                                  100,
                                                                  G_PRIORITY_DEFAULT),
                                  &error)))
    {
      for (const GList *iter = list; iter; iter = iter->next)
        {
          GFileInfo *info = iter->data;
          GFileType file_type = g_file_info_get_file_type (info);

          if (file_type == state->file_type)
            g_ptr_array_add (ar, iter->data);
          else
            g_object_unref (iter->data);
        }

      g_list_free (list);
    }

  if (error != NULL)
    return dex_future_new_for_error (g_steal_pointer (&error));

  return dex_future_new_take_boxed (G_TYPE_PTR_ARRAY, g_steal_pointer (&ar));
}

/**
 * foundry_file_list_children_typed:
 * @file: a [iface@Gio.File]
 * @file_type:
 * @attributes:
 *
 * Returns: (transfer full): a [class@Dex.Future] that resolves to a
 *   [struct@GLib.PtrArray] of [iface@Gio.File] or rejects with error.
 */
DexFuture *
foundry_file_list_children_typed (GFile      *file,
                                  GFileType   file_type,
                                  const char *attributes)
{
  ListChildrenTyped *state;

  g_return_val_if_fail (G_IS_FILE (file), NULL);

  state = g_new0 (ListChildrenTyped, 1);
  state->file = g_object_ref (file);
  state->file_type = file_type;

  if (attributes == NULL)
    state->attributes = g_strdup (G_FILE_ATTRIBUTE_STANDARD_NAME","G_FILE_ATTRIBUTE_STANDARD_TYPE);
  else
    state->attributes = g_strdup_printf ("%s,%s,%s",
                                         G_FILE_ATTRIBUTE_STANDARD_NAME,
                                         G_FILE_ATTRIBUTE_STANDARD_TYPE,
                                         attributes);

  return dex_scheduler_spawn (NULL, 0,
                              foundry_list_children_typed_fiber,
                              state,
                              list_children_typed_free);
}

static DexFuture *
foundry_host_file_get_contents_bytes_fiber (gpointer data)
{
  g_autoptr(FoundryProcessLauncher) launcher = NULL;
  g_autoptr(GSubprocess) subprocess = NULL;
  g_autoptr(GError) error = NULL;
  g_autoptr(GBytes) bytes = NULL;
  g_autoptr(GFile) tmpgfile = NULL;
  g_autofree char *tmpfile = NULL;
  const char *path = data;
  g_autofd int fd = -1;

  g_assert (path != NULL);
  g_assert (g_path_is_absolute (path));

  tmpfile = g_build_filename (g_get_tmp_dir (), ".foundry-host-file-XXXXXX", NULL);
  tmpgfile = g_file_new_for_path (tmpfile);

  /* We open a FD locally that we can write to and then pass that as our
   * stdout across the boundary so we can avoid incrementally reading
   * and instead do it once at the end.
   */
  if (-1 == (fd = g_mkstemp (tmpfile)))
    {
      int errsv = errno;
      return dex_future_new_reject (G_FILE_ERROR,
                                    g_file_error_from_errno (errsv),
                                    "%s", g_strerror (errsv));
    }

  launcher = foundry_process_launcher_new ();
  foundry_process_launcher_push_host (launcher);
  foundry_process_launcher_take_fd (launcher, g_steal_fd (&fd), STDOUT_FILENO);
  foundry_process_launcher_append_argv (launcher, "cat");
  foundry_process_launcher_append_argv (launcher, path);

  if ((subprocess = foundry_process_launcher_spawn_with_flags (launcher,
                                                               G_SUBPROCESS_FLAGS_STDERR_SILENCE,
                                                               &error)) &&
      dex_await (dex_subprocess_wait_check (subprocess), &error))
    bytes = dex_await_boxed (dex_file_load_contents_bytes (tmpgfile), &error);

  dex_await (dex_file_delete (tmpgfile, G_PRIORITY_DEFAULT), NULL);

  g_assert (bytes != NULL || error != NULL);

  if (error != NULL)
    return dex_future_new_for_error (g_steal_pointer (&error));

  return dex_future_new_take_boxed (G_TYPE_BYTES, g_steal_pointer (&bytes));
}

/**
 * foundry_host_file_get_contents_bytes:
 * @path: a path to a file on the host
 *
 * Returns: (transfer full): a [class@Dex.Future] that resolves to a
 *   [struct@GLib.Bytes] or rejects with error.
 */
DexFuture *
foundry_host_file_get_contents_bytes (const char *path)
{
  dex_return_error_if_fail (path != NULL);
  dex_return_error_if_fail (g_path_is_absolute (path));

  if (!_foundry_in_container ())
    {
      g_autoptr(GFile) file = g_file_new_for_path (path);

      return dex_file_load_contents_bytes (file);
    }

  return dex_scheduler_spawn (dex_thread_pool_scheduler_get_default (), 0,
                              foundry_host_file_get_contents_bytes_fiber,
                              g_strdup (path),
                              g_free);
}