forked from alexlarsson/xdg-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdg-app-utils.c
633 lines (527 loc) · 15.3 KB
/
xdg-app-utils.c
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
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
#include "config.h"
#include "xdg-app-utils.h"
#include "xdg-app-dir.h"
#include <glib.h>
#include "libgsystem.h"
#include <libsoup/soup.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
const char *
xdg_app_get_arch (void)
{
static struct utsname buf;
static char *arch = NULL;
if (arch == NULL)
{
if (uname (&buf))
arch = "unknown";
else
arch = buf.machine;
}
return arch;
}
static gboolean
is_valid_initial_name_character (gint c)
{
return
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c == '_');
}
static gboolean
is_valid_name_character (gint c)
{
return
is_valid_initial_name_character (c) ||
(c >= '0' && c <= '9');
}
/** xdg_app_is_name:
* @string: The string to check
*
* Checks if @string is a valid application name.
*
* App names are composed of 3 or more elements separated by a period
* ('.') character. All elements must contain at least one character.
*
* Each element must only contain the ASCII characters
* "[A-Z][a-z][0-9]_". Elements may not begin with a digit.
*
* App names must not begin with a '.' (period) character.
*
* App names must not exceed 255 characters in length.
*
* The above means that any app name is also a valid DBus well known
* bus name, but not all DBus names are valid app names. The difference are:
* 1) DBus name elements may contain '-'
* 2) DBus names require only two elements
*
* Returns: %TRUE if valid, %FALSE otherwise.
*
* Since: 2.26
*/
gboolean
xdg_app_is_valid_name (const char *string)
{
guint len;
gboolean ret;
const gchar *s;
const gchar *end;
int dot_count;
g_return_val_if_fail (string != NULL, FALSE);
ret = FALSE;
len = strlen (string);
if (G_UNLIKELY (len == 0 || len > 255))
goto out;
end = string + len;
s = string;
if (G_UNLIKELY (*s == '.'))
{
/* can't start with a . */
goto out;
}
else if (G_UNLIKELY (!is_valid_initial_name_character (*s)))
goto out;
s += 1;
dot_count = 0;
while (s != end)
{
if (*s == '.')
{
s += 1;
if (G_UNLIKELY (s == end || !is_valid_initial_name_character (*s)))
goto out;
dot_count++;
}
else if (G_UNLIKELY (!is_valid_name_character (*s)))
goto out;
s += 1;
}
if (G_UNLIKELY (dot_count < 2))
goto out;
ret = TRUE;
out:
return ret;
}
gboolean
xdg_app_has_name_prefix (const char *string,
const char *name)
{
const char *rest;
if (!g_str_has_prefix (string, name))
return FALSE;
rest = string + strlen (name);
return
*rest == 0 ||
*rest == '.' ||
!is_valid_name_character (*rest);
}
static gboolean
is_valid_initial_branch_character (gint c)
{
return
(c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c == '_') ||
(c == '-');
}
static gboolean
is_valid_branch_character (gint c)
{
return
is_valid_initial_branch_character (c) ||
(c == '.');
}
/** xdg_app_is_valid_branch:
* @string: The string to check
*
* Checks if @string is a valid branch name.
*
* Branch names must only contain the ASCII characters
* "[A-Z][a-z][0-9]_-.".
* Branch names may not begin with a digit.
* Branch names must contain at least one character.
*
* Returns: %TRUE if valid, %FALSE otherwise.
*
* Since: 2.26
*/
gboolean
xdg_app_is_valid_branch (const char *string)
{
guint len;
gboolean ret;
const gchar *s;
const gchar *end;
g_return_val_if_fail (string != NULL, FALSE);
ret = FALSE;
len = strlen (string);
if (G_UNLIKELY (len == 0))
goto out;
end = string + len;
s = string;
if (G_UNLIKELY (!is_valid_initial_branch_character (*s)))
goto out;
s += 1;
while (s != end)
{
if (G_UNLIKELY (!is_valid_branch_character (*s)))
goto out;
s += 1;
}
ret = TRUE;
out:
return ret;
}
char *
xdg_app_build_untyped_ref (const char *runtime,
const char *branch,
const char *arch)
{
if (arch == NULL)
arch = xdg_app_get_arch ();
return g_build_filename (runtime, arch, branch, NULL);
}
char *
xdg_app_build_runtime_ref (const char *runtime,
const char *branch,
const char *arch)
{
if (arch == NULL)
arch = xdg_app_get_arch ();
return g_build_filename ("runtime", runtime, arch, branch, NULL);
}
char *
xdg_app_build_app_ref (const char *app,
const char *branch,
const char *arch)
{
if (arch == NULL)
arch = xdg_app_get_arch ();
return g_build_filename ("app", app, arch, branch, NULL);
}
char **
xdg_app_list_deployed_refs (const char *type,
const char *name_prefix,
const char *branch,
const char *arch,
GCancellable *cancellable,
GError **error)
{
gchar **ret = NULL;
gs_unref_ptrarray GPtrArray *names = NULL;
gs_unref_hashtable GHashTable *hash = NULL;
gs_unref_object XdgAppDir *user_dir = NULL;
gs_unref_object XdgAppDir *system_dir = NULL;
const char *key;
GHashTableIter iter;
hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
user_dir = xdg_app_dir_get_user ();
system_dir = xdg_app_dir_get_system ();
if (!xdg_app_dir_collect_deployed_refs (user_dir, type, name_prefix,
branch, arch, hash, cancellable,
error))
goto out;
if (!xdg_app_dir_collect_deployed_refs (system_dir, type, name_prefix,
branch, arch, hash, cancellable,
error))
goto out;
names = g_ptr_array_new ();
g_hash_table_iter_init (&iter, hash);
while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL))
g_ptr_array_add (names, g_strdup (key));
g_ptr_array_sort (names, (GCompareFunc)g_strcmp0);
g_ptr_array_add (names, NULL);
ret = (char **)g_ptr_array_free (names, FALSE);
names = NULL;
out:
return ret;
}
GFile *
xdg_app_find_deploy_dir_for_ref (const char *ref,
GCancellable *cancellable,
GError **error)
{
gs_unref_object XdgAppDir *user_dir = NULL;
gs_unref_object XdgAppDir *system_dir = NULL;
GFile *deploy = NULL;
user_dir = xdg_app_dir_get_user ();
system_dir = xdg_app_dir_get_system ();
deploy = xdg_app_dir_get_if_deployed (user_dir, ref, NULL, cancellable);
if (deploy == NULL)
deploy = xdg_app_dir_get_if_deployed (system_dir, ref, NULL, cancellable);
if (deploy == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "%s not installed", ref);
return NULL;
}
return deploy;
}
static gboolean
overlay_symlink_tree_dir (int source_parent_fd,
const char *source_name,
const char *source_symlink_prefix,
int destination_parent_fd,
const char *destination_name,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
int res;
gs_dirfd_iterator_cleanup GSDirFdIterator source_iter;
gs_fd_close int destination_dfd = -1;
struct dirent *dent;
if (!gs_dirfd_iterator_init_at (source_parent_fd, source_name, FALSE, &source_iter, error))
goto out;
do
res = mkdirat (destination_parent_fd, destination_name, 0777);
while (G_UNLIKELY (res == -1 && errno == EINTR));
if (res == -1)
{
if (errno != EEXIST)
{
gs_set_error_from_errno (error, errno);
goto out;
}
}
if (!gs_file_open_dir_fd_at (destination_parent_fd, destination_name,
&destination_dfd,
cancellable, error))
goto out;
while (TRUE)
{
gboolean is_dir = FALSE;
if (!gs_dirfd_iterator_next_dent (&source_iter, &dent, cancellable, error))
goto out;
if (dent == NULL)
break;
if (dent->d_type == DT_DIR)
is_dir = TRUE;
else if (dent->d_type == DT_UNKNOWN)
{
struct stat stbuf;
if (fstatat (source_iter.fd, dent->d_name, &stbuf, AT_SYMLINK_NOFOLLOW) == -1)
{
int errsv = errno;
if (errsv == ENOENT)
continue;
else
{
gs_set_error_from_errno (error, errsv);
goto out;
}
}
is_dir = S_ISDIR (stbuf.st_mode);
}
if (is_dir)
{
gs_free gchar *target = g_build_filename ("..", source_symlink_prefix, dent->d_name, NULL);
if (!overlay_symlink_tree_dir (source_iter.fd, dent->d_name, target, destination_dfd, dent->d_name,
cancellable, error))
goto out;
}
else
{
gs_free gchar *target = g_build_filename (source_symlink_prefix, dent->d_name, NULL);
if (unlinkat (destination_dfd, dent->d_name, 0) != 0 && errno != ENOENT)
{
gs_set_error_from_errno (error, errno);
goto out;
}
if (symlinkat (target, destination_dfd, dent->d_name) != 0)
{
gs_set_error_from_errno (error, errno);
goto out;
}
}
}
ret = TRUE;
out:
return ret;
}
gboolean
xdg_app_overlay_symlink_tree (GFile *source,
GFile *destination,
const char *symlink_prefix,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
if (!gs_file_ensure_directory (destination, TRUE, cancellable, error))
goto out;
/* The fds are closed by this call */
if (!overlay_symlink_tree_dir (AT_FDCWD, gs_file_get_path_cached (source),
symlink_prefix,
AT_FDCWD, gs_file_get_path_cached (destination),
cancellable, error))
goto out;
ret = TRUE;
out:
return ret;
}
static gboolean
remove_dangling_symlinks (int parent_fd,
const char *name,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
struct dirent *dent;
GSDirFdIterator iter;
if (!gs_dirfd_iterator_init_at (parent_fd, name, FALSE, &iter, error))
goto out;
while (TRUE)
{
gboolean is_dir = FALSE;
gboolean is_link = FALSE;
if (!gs_dirfd_iterator_next_dent (&iter, &dent, cancellable, error))
goto out;
if (dent == NULL)
break;
if (dent->d_type == DT_DIR)
is_dir = TRUE;
else if (dent->d_type == DT_LNK)
is_link = TRUE;
else if (dent->d_type == DT_UNKNOWN)
{
struct stat stbuf;
if (fstatat (iter.fd, dent->d_name, &stbuf, AT_SYMLINK_NOFOLLOW) == -1)
{
int errsv = errno;
if (errsv == ENOENT)
continue;
else
{
gs_set_error_from_errno (error, errsv);
goto out;
}
}
is_dir = S_ISDIR (stbuf.st_mode);
is_link = S_ISLNK (stbuf.st_mode);
}
if (is_dir)
{
if (!remove_dangling_symlinks (iter.fd, dent->d_name, cancellable, error))
goto out;
}
else if (is_link)
{
struct stat stbuf;
if (fstatat (iter.fd, dent->d_name, &stbuf, 0) != 0 && errno == ENOENT)
{
if (unlinkat (iter.fd, dent->d_name, 0) != 0)
{
gs_set_error_from_errno (error, errno);
goto out;
}
}
}
}
ret = TRUE;
out:
return ret;
}
gboolean
xdg_app_remove_dangling_symlinks (GFile *dir,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
/* The fd is closed by this call */
if (!remove_dangling_symlinks (AT_FDCWD, gs_file_get_path_cached (dir),
cancellable, error))
goto out;
ret = TRUE;
out:
return ret;
}
static gboolean
load_contents (const char *uri, GBytes **contents, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
gs_free char *scheme = NULL;
scheme = g_uri_parse_scheme (uri);
if (strcmp (scheme, "file") == 0)
{
char *buffer;
gsize length;
gs_unref_object GFile *file = NULL;
g_debug ("Loading summary %s using GIO", uri);
file = g_file_new_for_uri (uri);
if (!g_file_load_contents (file, cancellable, &buffer, &length, NULL, NULL))
goto out;
*contents = g_bytes_new_take (buffer, length);
}
else
{
gs_unref_object SoupSession *session = NULL;
gs_unref_object SoupMessage *msg = NULL;
g_debug ("Loading summary %s using libsoup", uri);
session = soup_session_new ();
msg = soup_message_new ("GET", uri);
soup_session_send_message (session, msg);
if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
goto out;
*contents = g_bytes_new (msg->response_body->data, msg->response_body->length);
}
ret = TRUE;
g_debug ("Received %ld bytes", g_bytes_get_size (*contents));
out:
return ret;
}
gboolean
ostree_repo_load_summary (const char *repository_url,
GHashTable **refs,
gchar **title,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
gs_free char *summary_url = NULL;
gs_unref_bytes GBytes *bytes = NULL;
gs_unref_hashtable GHashTable *local_refs = NULL;
gs_free char *local_title = NULL;
summary_url = g_build_filename (repository_url, "summary", NULL);
if (load_contents (summary_url, &bytes, cancellable, NULL))
{
gs_unref_variant GVariant *summary;
gs_unref_variant GVariant *ref_list;
gs_unref_variant GVariant *extensions;
GVariantDict dict;
int i, n;
local_refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
summary = g_variant_new_from_bytes (OSTREE_SUMMARY_GVARIANT_FORMAT, bytes, FALSE);
ref_list = g_variant_get_child_value (summary, 0);
extensions = g_variant_get_child_value (summary, 1);
n = g_variant_n_children (ref_list);
g_debug ("Summary contains %d refs", n);
for (i = 0; i < n; i++)
{
gs_unref_variant GVariant *ref = NULL;
gs_unref_variant GVariant *csum_v = NULL;
char *refname;
char *checksum;
ref = g_variant_get_child_value (ref_list, i);
g_variant_get (ref, "(&s(t@aya{sv}))", &refname, NULL, &csum_v, NULL);
if (!ostree_validate_rev (refname, error))
goto out;
checksum = ostree_checksum_from_bytes_v (csum_v);
g_debug ("\t%s -> %s", refname, checksum);
g_hash_table_insert (local_refs, g_strdup (refname), checksum);
}
g_variant_dict_init (&dict, extensions);
g_variant_dict_lookup (&dict, "xa.title", "s", &local_title);
g_debug ("Summary title: %s", local_title);
g_variant_dict_end (&dict);
}
*refs = g_hash_table_ref (local_refs);
*title = g_strdup (local_title);
ret = TRUE;
out:
return ret;
}