-
-
Notifications
You must be signed in to change notification settings - Fork 390
/
flatpak-dir.c
16947 lines (14198 loc) · 604 KB
/
flatpak-dir.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set et sw=2 sts=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e-s:
*
* Copyright © 2014-2019 Red Hat, Inc
* Copyright © 2017 Endless Mobile, Inc.
*
* This program 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 Lesser General Public
* License along with this library. If not, see <http:https://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <[email protected]>
* Philip Withnall <[email protected]>
* Matthew Leeds <[email protected]>
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <utime.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>
#include <ostree.h>
#ifdef USE_SYSTEM_HELPER
#include <polkit/polkit.h>
#endif
#include "flatpak-appdata-private.h"
#include "flatpak-dir-private.h"
#include "flatpak-error.h"
#include "flatpak-oci-registry-private.h"
#include "flatpak-ref.h"
#include "flatpak-run-private.h"
#include "flatpak-utils-base-private.h"
#include "flatpak-variant-private.h"
#include "flatpak-variant-impl-private.h"
#include "libglnx.h"
#include "system-helper/flatpak-system-helper.h"
#ifdef HAVE_LIBMALCONTENT
#include <libmalcontent/malcontent.h>
#include "flatpak-parental-controls-private.h"
#endif
#ifdef HAVE_LIBSYSTEMD
#define SD_JOURNAL_SUPPRESS_LOCATION
#include <systemd/sd-journal.h>
#endif
#define NO_SYSTEM_HELPER ((FlatpakSystemHelper *) (gpointer) 1)
#define SUMMARY_CACHE_TIMEOUT_SEC (60 * 5)
#define FILTER_MTIME_CHECK_TIMEOUT_MSEC 500
#define SYSCONF_INSTALLATIONS_DIR "installations.d"
#define SYSCONF_INSTALLATIONS_FILE_EXT ".conf"
#define SYSCONF_REMOTES_DIR "remotes.d"
#define SYSCONF_REMOTES_FILE_EXT ".flatpakrepo"
#define SIDELOAD_REPOS_DIR_NAME "sideload-repos"
#ifdef USE_SYSTEM_HELPER
/* This uses a weird Auto prefix to avoid conflicts with later added polkit types.
*/
typedef PolkitAuthority AutoPolkitAuthority;
typedef PolkitAuthorizationResult AutoPolkitAuthorizationResult;
typedef PolkitDetails AutoPolkitDetails;
typedef PolkitSubject AutoPolkitSubject;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (AutoPolkitAuthority, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (AutoPolkitAuthorizationResult, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (AutoPolkitDetails, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC (AutoPolkitSubject, g_object_unref)
#endif
static FlatpakOciRegistry *flatpak_dir_create_system_child_oci_registry (FlatpakDir *self,
GLnxLockFile *file_lock,
const char *token,
GError **error);
static OstreeRepo * flatpak_dir_create_child_repo (FlatpakDir *self,
GFile *cache_dir,
GLnxLockFile *file_lock,
const char *optional_commit,
GError **error);
static OstreeRepo * flatpak_dir_create_system_child_repo (FlatpakDir *self,
GLnxLockFile *file_lock,
const char *optional_commit,
GError **error);
static gboolean flatpak_dir_mirror_oci (FlatpakDir *self,
FlatpakOciRegistry *dst_registry,
FlatpakRemoteState *state,
const char *ref,
const char *opt_rev,
const char *skip_if_current_is,
const char *token,
FlatpakProgress *progress,
GCancellable *cancellable,
GError **error);
static gboolean flatpak_dir_remote_fetch_summary (FlatpakDir *self,
const char *name,
gboolean only_cached,
GBytes **out_summary,
GBytes **out_summary_sig,
GCancellable *cancellable,
GError **error);
static gboolean flatpak_dir_remote_fetch_summary_index (FlatpakDir *self,
const char *name_or_uri,
gboolean only_cached,
GBytes **out_index,
GBytes **out_index_sig,
GCancellable *cancellable,
GError **error);
static gboolean flatpak_dir_remote_fetch_indexed_summary (FlatpakDir *self,
const char *name_or_uri,
const char *arch,
GVariant *subsummary_info_v,
gboolean only_cached,
GBytes **out_summary,
GCancellable *cancellable,
GError **error);
static gboolean flatpak_dir_gc_cached_digested_summaries (FlatpakDir *self,
const char *remote_name,
const char *dont_prune_file,
GCancellable *cancellable,
GError **error);
static gboolean flatpak_dir_cleanup_remote_for_url_change (FlatpakDir *self,
const char *remote_name,
const char *url,
GCancellable *cancellable,
GError **error);
static gboolean flatpak_dir_lookup_remote_filter (FlatpakDir *self,
const char *name,
gboolean force_load,
char **checksum_out,
GRegex **allow_regex,
GRegex **deny_regex,
GError **error);
static void ensure_http_session (FlatpakDir *self);
static void flatpak_dir_log (FlatpakDir *self,
const char *file,
int line,
const char *func,
const char *source,
const char *change,
const char *remote,
const char *ref,
const char *commit,
const char *old_commit,
const char *url,
const char *format,
...) G_GNUC_PRINTF (12, 13);
#define flatpak_dir_log(self, change, remote, ref, commit, old_commit, url, format, ...) \
(flatpak_dir_log) (self, __FILE__, __LINE__, __FUNCTION__, \
NULL, change, remote, ref, commit, old_commit, url, format, __VA_ARGS__)
static GBytes *upgrade_deploy_data (GBytes *deploy_data,
GFile *deploy_dir,
FlatpakDecomposed *ref,
OstreeRepo *repo,
GCancellable *cancellable,
GError **error);
typedef struct
{
GBytes *bytes;
GBytes *bytes_sig;
char *name;
char *url;
guint64 time;
} CachedSummary;
typedef struct
{
char *id;
char *display_name;
gint priority;
FlatpakDirStorageType storage_type;
} DirExtraData;
typedef struct {
GFile *path;
GTimeVal mtime;
guint64 last_mtime_check;
char *checksum;
GRegex *allow;
GRegex *deny;
} RemoteFilter;
struct FlatpakDir
{
GObject parent;
gboolean user;
GFile *basedir;
DirExtraData *extra_data;
OstreeRepo *repo;
GFile *cache_dir;
gboolean no_system_helper;
gboolean no_interaction;
pid_t source_pid;
GDBusConnection *system_helper_bus;
GHashTable *summary_cache;
GHashTable *remote_filters;
/* Config cache, protected by config_cache lock */
GRegex *masked;
GRegex *pinned;
FlatpakHttpSession *http_session;
};
G_LOCK_DEFINE_STATIC (config_cache);
typedef struct
{
GObjectClass parent_class;
} FlatpakDirClass;
struct FlatpakDeploy
{
GObject parent;
FlatpakDecomposed *ref;
GFile *dir;
GKeyFile *metadata;
FlatpakContext *system_overrides;
FlatpakContext *user_overrides;
FlatpakContext *system_app_overrides;
FlatpakContext *user_app_overrides;
OstreeRepo *repo;
};
typedef struct
{
GObjectClass parent_class;
} FlatpakDeployClass;
G_DEFINE_TYPE (FlatpakDir, flatpak_dir, G_TYPE_OBJECT)
G_DEFINE_TYPE (FlatpakDeploy, flatpak_deploy, G_TYPE_OBJECT)
enum {
PROP_0,
PROP_USER,
PROP_PATH
};
#define OSTREE_GIO_FAST_QUERYINFO ("standard::name,standard::type,standard::size,standard::is-symlink,standard::symlink-target," \
"unix::device,unix::inode,unix::mode,unix::uid,unix::gid,unix::rdev")
static const char *
get_config_dir_location (void)
{
static gsize path = 0;
if (g_once_init_enter (&path))
{
gsize setup_value = 0;
const char *config_dir = g_getenv ("FLATPAK_CONFIG_DIR");
if (config_dir != NULL)
setup_value = (gsize) config_dir;
else
setup_value = (gsize) FLATPAK_CONFIGDIR;
g_once_init_leave (&path, setup_value);
}
return (const char *) path;
}
static const char *
get_run_dir_location (void)
{
static gsize path = 0;
if (g_once_init_enter (&path))
{
gsize setup_value = 0;
/* Note: $FLATPAK_RUN_DIR should only be set in the unit tests. At
* runtime, /run/flatpak is assumed by
* flatpak-create-sideload-symlinks.sh
*/
const char *config_dir = g_getenv ("FLATPAK_RUN_DIR");
if (config_dir != NULL)
setup_value = (gsize) config_dir;
else
setup_value = (gsize) "/run/flatpak";
g_once_init_leave (&path, setup_value);
}
return (const char *) path;
}
static void
flatpak_sideload_state_free (FlatpakSideloadState *sideload_state)
{
g_object_unref (sideload_state->repo);
g_variant_unref (sideload_state->summary);
g_free (sideload_state);
}
static void
variant_maybe_unref (GVariant *variant)
{
if (variant)
g_variant_unref (variant);
}
static FlatpakRemoteState *
flatpak_remote_state_new (void)
{
FlatpakRemoteState *state = g_new0 (FlatpakRemoteState, 1);
state->refcount = 1;
state->sideload_repos = g_ptr_array_new_with_free_func ((GDestroyNotify)flatpak_sideload_state_free);
state->subsummaries = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)variant_maybe_unref);
return state;
}
FlatpakRemoteState *
flatpak_remote_state_ref (FlatpakRemoteState *remote_state)
{
g_assert (remote_state->refcount > 0);
remote_state->refcount++;
return remote_state;
}
void
flatpak_remote_state_unref (FlatpakRemoteState *remote_state)
{
g_assert (remote_state->refcount > 0);
remote_state->refcount--;
if (remote_state->refcount == 0)
{
g_free (remote_state->remote_name);
g_free (remote_state->collection_id);
g_clear_pointer (&remote_state->index, g_variant_unref);
g_clear_pointer (&remote_state->index_ht, g_hash_table_unref);
g_clear_pointer (&remote_state->index_sig_bytes, g_bytes_unref);
g_clear_pointer (&remote_state->subsummaries, g_hash_table_unref);
g_clear_pointer (&remote_state->summary, g_variant_unref);
g_clear_pointer (&remote_state->summary_bytes, g_bytes_unref);
g_clear_pointer (&remote_state->summary_sig_bytes, g_bytes_unref);
g_clear_error (&remote_state->summary_fetch_error);
g_clear_pointer (&remote_state->allow_refs, g_regex_unref);
g_clear_pointer (&remote_state->deny_refs, g_regex_unref);
g_clear_pointer (&remote_state->sideload_repos, g_ptr_array_unref);
g_free (remote_state);
}
}
static gboolean
_validate_summary_for_collection_id (GVariant *summary_v,
const char *collection_id,
GError **error)
{
VarSummaryRef summary;
summary = var_summary_from_gvariant (summary_v);
if (!flatpak_summary_find_ref_map (summary, collection_id, NULL))
return flatpak_fail_error (error, FLATPAK_ERROR_INVALID_DATA,
_("Configured collection ID ‘%s’ not in summary file"), collection_id);
return TRUE;
}
static void
flatpak_remote_state_add_sideload_repo (FlatpakRemoteState *self,
GFile *dir)
{
g_autoptr(GFile) summary_path = NULL;
g_autoptr(GMappedFile) mfile = NULL;
g_autoptr(OstreeRepo) sideload_repo = NULL;
/* Sideloading only works if collection id is set */
if (self->collection_id == NULL)
return;
summary_path = g_file_get_child (dir, "summary");
sideload_repo = ostree_repo_new (dir);
mfile = g_mapped_file_new (flatpak_file_get_path_cached (summary_path), FALSE, NULL);
if (mfile != NULL && ostree_repo_open (sideload_repo, NULL, NULL))
{
g_autoptr(GError) local_error = NULL;
g_autoptr(GBytes) summary_bytes = g_mapped_file_get_bytes (mfile);
FlatpakSideloadState *ss = g_new0 (FlatpakSideloadState, 1);
ss->repo = g_steal_pointer (&sideload_repo);
ss->summary = g_variant_ref_sink (g_variant_new_from_bytes (OSTREE_SUMMARY_GVARIANT_FORMAT, summary_bytes, TRUE));
if (!_validate_summary_for_collection_id (ss->summary, self->collection_id, &local_error))
{
/* We expect to hit this code path when the repo is providing things
* from other remotes
*/
g_info ("Sideload repo at path %s not valid for remote %s: %s",
flatpak_file_get_path_cached (dir), self->remote_name, local_error->message);
flatpak_sideload_state_free (ss);
}
else
{
g_ptr_array_add (self->sideload_repos, ss);
g_info ("Using sideloaded repo %s for remote %s", flatpak_file_get_path_cached (dir), self->remote_name);
}
}
}
static void add_sideload_subdirs (GPtrArray *res,
GFile *parent,
gboolean recurse);
static void
add_sideload_create_usb_subdirs (GPtrArray *res,
GFile *parent)
{
g_autoptr(GFile) ostree_repo_subpath = NULL;
g_autoptr(GFile) dot_ostree_repo_subpath = NULL;
g_autoptr(GFile) dot_ostree_repo_d_subpath = NULL;
g_autoptr(OstreeRepo) ostree_repo_subpath_repo = NULL;
g_autoptr(OstreeRepo) dot_ostree_repo_subpath_repo = NULL;
/* This path is not used by "flatpak create-usb" but it's a standard location
* recognized by libostree; see the man page ostree create-usb(1)
*/
ostree_repo_subpath = g_file_resolve_relative_path (parent, "ostree/repo");
ostree_repo_subpath_repo = ostree_repo_new (ostree_repo_subpath);
if (ostree_repo_open (ostree_repo_subpath_repo, NULL, NULL))
g_ptr_array_add (res, g_object_ref (ostree_repo_subpath));
/* These paths are used by "flatpak create-usb" */
dot_ostree_repo_subpath = g_file_resolve_relative_path (parent, ".ostree/repo");
dot_ostree_repo_subpath_repo = ostree_repo_new (dot_ostree_repo_subpath);
if (ostree_repo_open (dot_ostree_repo_subpath_repo, NULL, NULL))
g_ptr_array_add (res, g_object_ref (dot_ostree_repo_subpath));
dot_ostree_repo_d_subpath = g_file_resolve_relative_path (parent, ".ostree/repos.d");
add_sideload_subdirs (res, dot_ostree_repo_d_subpath, FALSE);
}
static void
add_sideload_subdirs (GPtrArray *res,
GFile *parent,
gboolean recurse)
{
g_autoptr(GFileEnumerator) dir_enum = NULL;
dir_enum = g_file_enumerate_children (parent,
G_FILE_ATTRIBUTE_STANDARD_NAME ","
G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE,
NULL, NULL);
if (dir_enum == NULL)
return;
while (TRUE)
{
GFileInfo *info;
GFile *path;
if (!g_file_enumerator_iterate (dir_enum, &info, &path, NULL, NULL) ||
info == NULL)
break;
/* Here we support either a plain repo or, if @recurse is TRUE, the root
* directory of a USB created with "flatpak create-usb"
*/
if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
{
g_autoptr(OstreeRepo) repo = ostree_repo_new (path);
if (ostree_repo_open (repo, NULL, NULL))
g_ptr_array_add (res, g_object_ref (path));
else if (recurse)
add_sideload_create_usb_subdirs (res, path);
}
}
}
void
flatpak_remote_state_add_sideload_dir (FlatpakRemoteState *self,
GFile *dir)
{
g_autoptr(GPtrArray) sideload_paths = g_ptr_array_new_with_free_func (g_object_unref);
/* The directory could be a repo */
flatpak_remote_state_add_sideload_repo (self, dir);
/* Or it could be a directory with repos in well-known subdirectories */
add_sideload_create_usb_subdirs (sideload_paths, dir);
for (int i = 0; i < sideload_paths->len; i++)
flatpak_remote_state_add_sideload_repo (self, g_ptr_array_index (sideload_paths, i));
}
gboolean
flatpak_remote_state_ensure_summary (FlatpakRemoteState *self,
GError **error)
{
if (self->index == NULL && self->summary == NULL)
return flatpak_fail_error (error, FLATPAK_ERROR_INVALID_DATA, _("Unable to load summary from remote %s: %s"), self->remote_name,
self->summary_fetch_error != NULL ? self->summary_fetch_error->message : "unknown error");
return TRUE;
}
gboolean
flatpak_remote_state_ensure_subsummary (FlatpakRemoteState *self,
FlatpakDir *dir,
const char *arch,
gboolean only_cached,
GCancellable *cancellable,
GError **error)
{
GVariant *subsummary;
const char *alt_arch;
GVariant *subsummary_info_v;
g_autoptr(GBytes) bytes = NULL;
if (self->summary != NULL)
return TRUE; /* We have them all anyway */
if (self->index == NULL)
return TRUE; /* Don't fail unnecessarily in e.g. the sideload case */
if (g_hash_table_contains (self->subsummaries, arch))
return TRUE;
/* If i.e. we already loaded x86_64 subsummary (which has i386 refs),
* don't load i386 one */
alt_arch = flatpak_get_compat_arch_reverse (arch);
if (alt_arch != NULL &&
g_hash_table_contains (self->subsummaries, alt_arch))
return TRUE;
subsummary_info_v = g_hash_table_lookup (self->index_ht, arch);
if (subsummary_info_v == NULL)
return TRUE; /* No refs for this arch */
if (!flatpak_dir_remote_fetch_indexed_summary (dir, self->remote_name, arch, subsummary_info_v, only_cached,
&bytes, cancellable, error))
return FALSE;
subsummary = g_variant_ref_sink (g_variant_new_from_bytes (OSTREE_SUMMARY_GVARIANT_FORMAT, bytes, FALSE));
g_hash_table_insert (self->subsummaries, g_strdup (arch), subsummary);
return TRUE;
}
gboolean
flatpak_remote_state_ensure_subsummary_all_arches (FlatpakRemoteState *self,
FlatpakDir *dir,
gboolean only_cached,
GCancellable *cancellable,
GError **error)
{
if (self->index_ht == NULL)
return TRUE; /* No subsummaries, got all arches anyway */
GLNX_HASH_TABLE_FOREACH (self->index_ht, const char *, arch)
{
g_autoptr(GError) local_error = NULL;
if (!flatpak_remote_state_ensure_subsummary (self, dir, arch, only_cached, cancellable, &local_error))
{
/* Don't error on non-cached subsummaries */
if (only_cached && g_error_matches (local_error, FLATPAK_ERROR, FLATPAK_ERROR_NOT_CACHED))
continue;
g_propagate_error (error, g_steal_pointer (&local_error));
return FALSE;
}
}
return TRUE;
}
gboolean
flatpak_remote_state_allow_ref (FlatpakRemoteState *self,
const char *ref)
{
return flatpak_filters_allow_ref (self->allow_refs, self->deny_refs, ref);
}
static guint64
get_timestamp_from_ref_info (VarRefInfoRef info)
{
VarMetadataRef metadata = var_ref_info_get_metadata (info);
return GUINT64_FROM_BE(var_metadata_lookup_uint64 (metadata, OSTREE_COMMIT_TIMESTAMP, 0));
}
GFile *
flatpak_remote_state_lookup_sideload_checksum (FlatpakRemoteState *self,
char *checksum)
{
for (int i = 0; i < self->sideload_repos->len; i++)
{
FlatpakSideloadState *ss = g_ptr_array_index (self->sideload_repos, i);
OstreeRepoCommitState commit_state;
if (ostree_repo_load_commit (ss->repo, checksum, NULL, &commit_state, NULL) &&
commit_state == OSTREE_REPO_COMMIT_STATE_NORMAL)
return g_object_ref (ostree_repo_get_path (ss->repo));
}
return NULL;
}
static gboolean
flatpak_remote_state_resolve_sideloaded_ref (FlatpakRemoteState *self,
const char *ref,
char **out_checksum,
guint64 *out_timestamp,
VarRefInfoRef *out_info,
FlatpakSideloadState **out_sideload_state,
GError **error)
{
g_autofree char *latest_checksum = NULL;
guint64 latest_timestamp = 0;
FlatpakSideloadState *latest_ss = NULL;
VarRefInfoRef latest_sideload_info;
for (int i = 0; i < self->sideload_repos->len; i++)
{
FlatpakSideloadState *ss = g_ptr_array_index (self->sideload_repos, i);
g_autofree char *sideload_checksum = NULL;
VarRefInfoRef sideload_info;
if (flatpak_summary_lookup_ref (ss->summary, self->collection_id, ref, &sideload_checksum, &sideload_info))
{
guint64 timestamp = get_timestamp_from_ref_info (sideload_info);
if (latest_checksum == 0 || latest_timestamp < timestamp)
{
g_free (latest_checksum);
latest_checksum = g_steal_pointer (&sideload_checksum);
latest_timestamp = timestamp;
latest_sideload_info = sideload_info;
latest_ss = ss;
}
}
}
if (latest_checksum == NULL)
return flatpak_fail_error (error, FLATPAK_ERROR_REF_NOT_FOUND,
_("No such ref '%s' in remote %s"),
ref, self->remote_name);
if (out_checksum)
*out_checksum = g_steal_pointer (&latest_checksum);
if (out_timestamp)
*out_timestamp = latest_timestamp;
if (out_info)
*out_info = latest_sideload_info;
if (out_sideload_state)
*out_sideload_state = latest_ss;
return TRUE;
}
static GVariant *
get_summary_for_ref (FlatpakRemoteState *self,
const char *ref)
{
GVariant *summary = NULL;
if (self->index != NULL)
{
g_autofree char * arch = flatpak_get_arch_for_ref (ref);
if (arch != NULL)
summary = g_hash_table_lookup (self->subsummaries, arch);
if (summary == NULL && arch != NULL)
{
const char *non_compat_arch = flatpak_get_compat_arch_reverse (arch);
if (non_compat_arch != NULL)
summary = g_hash_table_lookup (self->subsummaries, non_compat_arch);
}
}
else
summary = self->summary;
return summary;
}
/* Returns TRUE if the ref is found in the summary or cache.
* out_checksum and out_variant are only set when the ref is found.
*/
gboolean
flatpak_remote_state_lookup_ref (FlatpakRemoteState *self,
const char *ref,
char **out_checksum,
guint64 *out_timestamp,
VarRefInfoRef *out_info,
GFile **out_sideload_path,
GError **error)
{
if (!flatpak_remote_state_allow_ref (self, ref))
{
return flatpak_fail_error (error, FLATPAK_ERROR_REF_NOT_FOUND,
_("No entry for %s in remote '%s' summary flatpak cache "),
ref, self->remote_name);
}
/* If there is a summary we use it for metadata and for latest. We may later install from a sideloaded source though */
if (self->summary != NULL || self->index != NULL)
{
VarRefInfoRef info;
g_autofree char *checksum = NULL;
GVariant *summary;
summary = get_summary_for_ref (self, ref);
if (summary == NULL ||
!flatpak_summary_lookup_ref (summary, NULL, ref, &checksum, &info))
return flatpak_fail_error (error, FLATPAK_ERROR_REF_NOT_FOUND,
_("No such ref '%s' in remote %s"),
ref, self->remote_name);
/* Even if its available in the summary we want to install it from a sideload repo if available */
if (out_sideload_path)
{
g_autoptr(GFile) found_sideload_path = NULL;
for (int i = 0; i < self->sideload_repos->len; i++)
{
FlatpakSideloadState *ss = g_ptr_array_index (self->sideload_repos, i);
OstreeRepoCommitState commit_state;
if (ostree_repo_load_commit (ss->repo, checksum, NULL, &commit_state, NULL) &&
commit_state == OSTREE_REPO_COMMIT_STATE_NORMAL)
{
found_sideload_path = g_object_ref (ostree_repo_get_path (ss->repo));
break;
}
}
*out_sideload_path = g_steal_pointer (&found_sideload_path);
}
if (out_info)
*out_info = info;
if (out_checksum)
*out_checksum = g_steal_pointer (&checksum);
if (out_timestamp)
*out_timestamp = get_timestamp_from_ref_info (info);
}
else
{
FlatpakSideloadState *ss = NULL;
if (!flatpak_remote_state_resolve_sideloaded_ref (self, ref, out_checksum, out_timestamp, out_info, &ss, error))
return FALSE;
if (out_sideload_path)
*out_sideload_path = g_object_ref (ostree_repo_get_path (ss->repo));
}
return TRUE;
}
GPtrArray *
flatpak_remote_state_match_subrefs (FlatpakRemoteState *self,
FlatpakDecomposed *ref)
{
GVariant *summary;
if (self->summary == NULL && self->index == NULL)
{
g_info ("flatpak_remote_state_match_subrefs with no summary");
return g_ptr_array_new_with_free_func ((GDestroyNotify)flatpak_decomposed_unref);
}
summary = get_summary_for_ref (self, flatpak_decomposed_get_ref (ref));
if (summary == NULL)
return g_ptr_array_new_with_free_func ((GDestroyNotify)flatpak_decomposed_unref);
return flatpak_summary_match_subrefs (summary, NULL, ref);
}
static VarMetadataRef
flatpak_remote_state_get_main_metadata (FlatpakRemoteState *self)
{
VarSummaryRef summary;
VarSummaryIndexRef index;
VarMetadataRef meta;
if (self->index)
{
index = var_summary_index_from_gvariant (self->index);
meta = var_summary_index_get_metadata (index);
}
else if (self->summary)
{
summary = var_summary_from_gvariant (self->summary);
meta = var_summary_get_metadata (summary);
}
else
g_assert_not_reached ();
return meta;
}
/* 0 if not specified */
static guint32
flatpak_remote_state_get_cache_version (FlatpakRemoteState *self)
{
VarMetadataRef meta;
if (!flatpak_remote_state_ensure_summary (self, NULL))
return 0;
meta = flatpak_remote_state_get_main_metadata (self);
return GUINT32_FROM_LE (var_metadata_lookup_uint32 (meta, "xa.cache-version", 0));
}
gboolean
flatpak_remote_state_lookup_cache (FlatpakRemoteState *self,
const char *ref,
guint64 *out_download_size,
guint64 *out_installed_size,
const char **out_metadata,
GError **error)
{
VarCacheDataRef cache_data;
VarMetadataRef meta;
VarSummaryRef summary;
guint32 summary_version;
GVariant *summary_v;
if (!flatpak_remote_state_ensure_summary (self, error))
return FALSE;
summary_v = get_summary_for_ref (self, ref);
if (summary_v == NULL)
return flatpak_fail_error (error, FLATPAK_ERROR_REF_NOT_FOUND,
_("No entry for %s in remote '%s' summary flatpak cache "),
ref, self->remote_name);
summary = var_summary_from_gvariant (summary_v);
meta = var_summary_get_metadata (summary);
summary_version = GUINT32_FROM_LE (var_metadata_lookup_uint32 (meta, "xa.summary-version", 0));
if (summary_version == 0)
{
VarCacheRef cache;
gsize pos;
VarVariantRef cache_vv;
VarVariantRef cache_v;
if (!var_metadata_lookup (meta, "xa.cache", NULL, &cache_vv))
{
flatpak_fail_error (error, FLATPAK_ERROR_INVALID_DATA, _("No summary or Flatpak cache available for remote %s"),
self->remote_name);
return FALSE;
}
/* For stupid historical reasons the xa.cache is double-wrapped in a variant */
cache_v = var_variant_from_variant (cache_vv);
cache = var_cache_from_variant (cache_v);
if (!var_cache_lookup (cache, ref, &pos, &cache_data))
return flatpak_fail_error (error, FLATPAK_ERROR_REF_NOT_FOUND,
_("No entry for %s in remote '%s' summary flatpak cache "),
ref, self->remote_name);
}
else if (summary_version == 1)
{
VarRefMapRef ref_map = var_summary_get_ref_map (summary);
VarRefInfoRef info;
VarMetadataRef commit_metadata;
VarVariantRef cache_data_v;
if (!flatpak_var_ref_map_lookup_ref (ref_map, ref, &info))
return flatpak_fail_error (error, FLATPAK_ERROR_REF_NOT_FOUND,
_("No entry for %s in remote '%s' summary cache "),
ref, self->remote_name);
commit_metadata = var_ref_info_get_metadata (info);
if (!var_metadata_lookup (commit_metadata, "xa.data", NULL, &cache_data_v))
return flatpak_fail_error (error, FLATPAK_ERROR_INVALID_DATA, _("Missing xa.data in summary for remote %s"),
self->remote_name);
cache_data = var_cache_data_from_variant (cache_data_v);
}
else
{
flatpak_fail_error (error, FLATPAK_ERROR_INVALID_DATA, _("Unsupported summary version %d for remote %s"),
summary_version, self->remote_name);
return FALSE;
}
if (out_installed_size)
*out_installed_size = var_cache_data_get_installed_size (cache_data);
if (out_download_size)
*out_download_size = var_cache_data_get_download_size (cache_data);
if (out_metadata)
*out_metadata = var_cache_data_get_metadata (cache_data);
return TRUE;
}
gboolean
flatpak_remote_state_load_data (FlatpakRemoteState *self,
const char *ref,
guint64 *out_download_size,
guint64 *out_installed_size,
char **out_metadata,
GError **error)
{
if (self->summary || self->index)
{
const char *metadata = NULL;
if (!flatpak_remote_state_lookup_cache (self, ref, out_download_size, out_installed_size, &metadata, error))
return FALSE;
if (out_metadata)
*out_metadata = g_strdup (metadata);
}
else
{
/* Look up from sideload */
g_autofree char *checksum = NULL;
guint64 timestamp;
VarRefInfoRef info;
FlatpakSideloadState *ss = NULL;
g_autoptr(GVariant) commit_data = NULL;
g_autoptr(GVariant) commit_metadata = NULL;
const char *xa_metadata = NULL;
guint64 download_size = 0;
guint64 installed_size = 0;
/* Use sideload refs if any */
if (!flatpak_remote_state_resolve_sideloaded_ref (self, ref, &checksum, ×tamp,
&info, &ss, error))
return FALSE;
if (!ostree_repo_load_commit (ss->repo, checksum, &commit_data, NULL, error))
return FALSE;
commit_metadata = g_variant_get_child_value (commit_data, 0);
g_variant_lookup (commit_metadata, "xa.metadata", "&s", &xa_metadata);
if (xa_metadata == NULL)
return flatpak_fail (error, "No xa.metadata in sideload commit %s ref %s", checksum, ref);
if (g_variant_lookup (commit_metadata, "xa.download-size", "t", &download_size))
download_size = GUINT64_FROM_BE (download_size);
if (g_variant_lookup (commit_metadata, "xa.installed-size", "t", &installed_size))
installed_size = GUINT64_FROM_BE (installed_size);
if (out_installed_size)
*out_installed_size = installed_size;