/
flatpak-dir.c
12577 lines (10441 loc) · 427 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
/*
* Copyright © 2014 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://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <alexl@redhat.com>
* Philip Withnall <withnall@endlessm.com>
*/
#include "config.h"
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <utime.h>
#include <glnx-console.h>
#include <glib/gi18n.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>
#include "libglnx/libglnx.h"
#include "flatpak-error.h"
#include <ostree.h>
#include "flatpak-dir-private.h"
#include "flatpak-utils-private.h"
#include "flatpak-oci-registry-private.h"
#include "flatpak-run-private.h"
#include "errno.h"
#define NO_SYSTEM_HELPER ((FlatpakSystemHelper *) (gpointer) 1)
#define SUMMARY_CACHE_TIMEOUT_SEC 5 *60
#define SYSCONF_INSTALLATIONS_DIR "installations.d"
#define SYSCONF_INSTALLATIONS_FILE_EXT ".conf"
#define SYSTEM_DIR_DEFAULT_ID "default"
#define SYSTEM_DIR_DEFAULT_DISPLAY_NAME "Default system directory"
#define SYSTEM_DIR_DEFAULT_STORAGE_TYPE FLATPAK_DIR_STORAGE_TYPE_DEFAULT
#define SYSTEM_DIR_DEFAULT_PRIORITY 0
static FlatpakOciRegistry *flatpak_dir_create_system_child_oci_registry (FlatpakDir *self,
GLnxLockFile *file_lock,
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_checksum,
GError **error);
static gboolean flatpak_dir_mirror_oci (FlatpakDir *self,
FlatpakOciRegistry *dst_registry,
FlatpakRemoteState *state,
const char *ref,
const char *skip_if_current_is,
OstreeAsyncProgress *progress,
GCancellable *cancellable,
GError **error);
static gboolean flatpak_dir_remote_fetch_summary (FlatpakDir *self,
const char *name,
GBytes **out_summary,
GBytes **out_summary_sig,
GCancellable *cancellable,
GError **error);
static GVariant * flatpak_create_deploy_data_from_old (GFile *deploy_dir,
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_fetch_remote_state_metadata_branch (FlatpakDir *self,
FlatpakRemoteState *state,
GCancellable *cancellable,
GError **error);
static void ensure_soup_session (FlatpakDir *self);
typedef struct
{
GBytes *bytes;
GBytes *bytes_sig;
char *remote;
char *url;
guint64 time;
} CachedSummary;
typedef struct
{
char *id;
char *display_name;
gint priority;
FlatpakDirStorageType storage_type;
} DirExtraData;
struct FlatpakDir
{
GObject parent;
gboolean user;
GFile *basedir;
DirExtraData *extra_data;
OstreeRepo *repo;
gboolean no_system_helper;
GDBusConnection *system_helper_bus;
GHashTable *summary_cache;
SoupSession *soup_session;
};
typedef struct
{
GObjectClass parent_class;
} FlatpakDirClass;
struct FlatpakDeploy
{
GObject parent;
GFile *dir;
GKeyFile *metadata;
FlatpakContext *system_overrides;
FlatpakContext *user_overrides;
FlatpakContext *system_app_overrides;
FlatpakContext *user_app_overrides;
};
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 FlatpakRemoteState *
flatpak_remote_state_new (void)
{
FlatpakRemoteState *state = g_new0 (FlatpakRemoteState, 1);
state->refcount = 1;
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->summary, g_variant_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->metadata, g_variant_unref);
g_clear_error (&remote_state->metadata_fetch_error);
g_free (remote_state);
}
}
gboolean
flatpak_remote_state_ensure_summary (FlatpakRemoteState *self,
GError **error)
{
if (self->summary == NULL)
return flatpak_fail (error, "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_metadata (FlatpakRemoteState *self,
GError **error)
{
if (self->metadata == NULL)
{
g_autofree char *error_msg = NULL;
/* If the collection ID is NULL the metadata comes from the summary */
if (self->metadata_fetch_error != NULL)
error_msg = g_strdup (self->metadata_fetch_error->message);
else if (self->collection_id == NULL && self->summary_fetch_error != NULL)
error_msg = g_strdup_printf ("summary fetch error: %s", self->summary_fetch_error->message);
return flatpak_fail (error, "Unable to load metadata from remote %s: %s", self->remote_name,
error_msg != NULL ? error_msg : "unknown error");
}
return TRUE;
}
/* Returns TRUE if the ref is found in the summary or cache. out_checksum and
* out_variant are not guaranteed to be set even when the ref is found. */
gboolean
flatpak_remote_state_lookup_ref (FlatpakRemoteState *self,
const char *ref,
char **out_checksum,
GVariant **out_variant,
GError **error)
{
if (self->collection_id == NULL || self->summary != NULL)
{
if (!flatpak_remote_state_ensure_summary (self, error))
return FALSE;
if (!flatpak_summary_lookup_ref (self->summary, self->collection_id, ref, out_checksum, out_variant))
{
if (self->collection_id != NULL)
flatpak_fail (error, "No such ref (%s, %s) in remote %s", self->collection_id, ref, self->remote_name);
else
flatpak_fail (error, "No such ref '%s' in remote %s", ref, self->remote_name);
return FALSE;
}
}
else
{
if (!flatpak_remote_state_ensure_metadata (self, error))
return FALSE;
if (!flatpak_remote_state_lookup_cache (self, ref, NULL, NULL, NULL, error))
return FALSE;
}
return TRUE;
}
char **
flatpak_remote_state_match_subrefs (FlatpakRemoteState *self,
const char *ref)
{
if (self->summary == NULL)
{
const char *empty[] = { NULL };
g_debug ("flatpak_remote_state_match_subrefs with no summary");
return g_strdupv ((char **) empty);
}
return flatpak_summary_match_subrefs (self->summary, self->collection_id, ref);
}
gboolean
flatpak_remote_state_lookup_repo_metadata (FlatpakRemoteState *self,
const char *key,
const char *format_string,
...)
{
g_autoptr(GVariant) value = NULL;
va_list args;
if (self->metadata == NULL)
return FALSE;
/* Extract the metadata from it, if set. */
value = g_variant_lookup_value (self->metadata, key, NULL);
if (value == NULL)
return FALSE;
if (!g_variant_check_format_string (value, format_string, FALSE))
return FALSE;
va_start (args, format_string);
g_variant_get_va (value, format_string, NULL, &args);
va_end (args);
return TRUE;
}
gboolean
flatpak_remote_state_lookup_cache (FlatpakRemoteState *self,
const char *ref,
guint64 *download_size,
guint64 *installed_size,
const char **metadata,
GError **error)
{
g_autoptr(GVariant) cache_v = NULL;
g_autoptr(GVariant) cache = NULL;
g_autoptr(GVariant) res = NULL;
g_autoptr(GVariant) refdata = NULL;
int pos;
if (!flatpak_remote_state_ensure_metadata (self, error))
return FALSE;
cache_v = g_variant_lookup_value (self->metadata, "xa.cache", NULL);
if (cache_v == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("No flatpak cache in remote '%s' summary"), self->remote_name);
return FALSE;
}
cache = g_variant_get_child_value (cache_v, 0);
if (!flatpak_variant_bsearch_str (cache, ref, &pos))
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("No entry for %s in remote '%s' summary flatpak cache "), ref, self->remote_name);
return FALSE;
}
refdata = g_variant_get_child_value (cache, pos);
res = g_variant_get_child_value (refdata, 1);
if (installed_size)
{
guint64 v;
g_variant_get_child (res, 0, "t", &v);
*installed_size = GUINT64_FROM_BE (v);
}
if (download_size)
{
guint64 v;
g_variant_get_child (res, 1, "t", &v);
*download_size = GUINT64_FROM_BE (v);
}
if (metadata)
g_variant_get_child (res, 2, "&s", metadata);
return TRUE;
}
GVariant *
flatpak_remote_state_lookup_sparse_cache (FlatpakRemoteState *self,
const char *ref,
GError **error)
{
g_autoptr(GVariant) cache = NULL;
int pos;
if (!flatpak_remote_state_ensure_metadata (self, error))
return FALSE;
cache = g_variant_lookup_value (self->metadata, "xa.sparse-cache", NULL);
if (cache != NULL && flatpak_variant_bsearch_str (cache, ref, &pos))
{
g_autoptr(GVariant) refdata = g_variant_get_child_value (cache, pos);
return g_variant_get_child_value (refdata, 1);
}
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("No entry for %s in remote summary flatpak sparse cache "), ref);
return FALSE;
}
static gboolean
flatpak_remote_state_save_summary (FlatpakRemoteState *self,
GFile *dir,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GFile) summary_file = g_file_get_child (dir, "summary");
g_autoptr(GBytes) summary_bytes = NULL;
/* For non-p2p case we always require a summary */
if (!flatpak_remote_state_ensure_summary (self, error))
return FALSE;
summary_bytes = g_variant_get_data_as_bytes (self->summary);
if (!g_file_replace_contents (summary_file,
g_bytes_get_data (summary_bytes, NULL),
g_bytes_get_size (summary_bytes),
NULL, FALSE, 0, NULL, cancellable, error))
return FALSE;
if (self->summary_sig_bytes != NULL)
{
g_autoptr(GFile) summary_sig_file = g_file_get_child (dir, "summary.sig");
if (!g_file_replace_contents (summary_sig_file,
g_bytes_get_data (self->summary_sig_bytes, NULL),
g_bytes_get_size (self->summary_sig_bytes),
NULL, FALSE, 0, NULL, cancellable, error))
return FALSE;
}
return TRUE;
}
static DirExtraData *
dir_extra_data_new (const char *id,
const char *display_name,
gint priority,
FlatpakDirStorageType type)
{
DirExtraData *dir_extra_data = g_new0 (DirExtraData, 1);
dir_extra_data->id = g_strdup (id);
dir_extra_data->display_name = g_strdup (display_name);
dir_extra_data->priority = priority;
dir_extra_data->storage_type = type;
return dir_extra_data;
}
static DirExtraData *
dir_extra_data_clone (DirExtraData *extra_data)
{
if (extra_data != NULL)
return dir_extra_data_new (extra_data->id,
extra_data->display_name,
extra_data->priority,
extra_data->storage_type);
return NULL;
}
static void
dir_extra_data_free (DirExtraData *dir_extra_data)
{
g_free (dir_extra_data->id);
g_free (dir_extra_data->display_name);
g_free (dir_extra_data);
}
static GVariant *
variant_new_ay_bytes (GBytes *bytes)
{
gsize size;
gconstpointer data;
data = g_bytes_get_data (bytes, &size);
g_bytes_ref (bytes);
return g_variant_ref_sink (g_variant_new_from_data (G_VARIANT_TYPE ("ay"), data, size,
TRUE, (GDestroyNotify) g_bytes_unref, bytes));
}
static void
flatpak_deploy_finalize (GObject *object)
{
FlatpakDeploy *self = FLATPAK_DEPLOY (object);
g_clear_object (&self->dir);
g_clear_pointer (&self->metadata, g_key_file_unref);
g_clear_pointer (&self->system_overrides, flatpak_context_free);
g_clear_pointer (&self->user_overrides, flatpak_context_free);
g_clear_pointer (&self->system_app_overrides, flatpak_context_free);
g_clear_pointer (&self->user_app_overrides, flatpak_context_free);
G_OBJECT_CLASS (flatpak_deploy_parent_class)->finalize (object);
}
static void
flatpak_deploy_class_init (FlatpakDeployClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = flatpak_deploy_finalize;
}
static void
flatpak_deploy_init (FlatpakDeploy *self)
{
}
GFile *
flatpak_deploy_get_dir (FlatpakDeploy *deploy)
{
return g_object_ref (deploy->dir);
}
GVariant *
flatpak_load_deploy_data (GFile *deploy_dir,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GFile) data_file = NULL;
g_autoptr(GError) my_error = NULL;
char *data = NULL;
gsize data_size;
data_file = g_file_get_child (deploy_dir, "deploy");
if (!g_file_load_contents (data_file, cancellable, &data, &data_size, NULL, &my_error))
{
if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
g_propagate_error (error, g_steal_pointer (&my_error));
return NULL;
}
return flatpak_create_deploy_data_from_old (deploy_dir,
cancellable, error);
}
return g_variant_ref_sink (g_variant_new_from_data (FLATPAK_DEPLOY_DATA_GVARIANT_FORMAT,
data, data_size,
FALSE, g_free, data));
}
GVariant *
flatpak_deploy_get_deploy_data (FlatpakDeploy *deploy,
GCancellable *cancellable,
GError **error)
{
return flatpak_load_deploy_data (deploy->dir,
cancellable,
error);
}
GFile *
flatpak_deploy_get_files (FlatpakDeploy *deploy)
{
return g_file_get_child (deploy->dir, "files");
}
FlatpakContext *
flatpak_deploy_get_overrides (FlatpakDeploy *deploy)
{
FlatpakContext *overrides = flatpak_context_new ();
if (deploy->system_overrides)
flatpak_context_merge (overrides, deploy->system_overrides);
if (deploy->system_app_overrides)
flatpak_context_merge (overrides, deploy->system_app_overrides);
if (deploy->user_overrides)
flatpak_context_merge (overrides, deploy->user_overrides);
if (deploy->user_app_overrides)
flatpak_context_merge (overrides, deploy->user_app_overrides);
return overrides;
}
GKeyFile *
flatpak_deploy_get_metadata (FlatpakDeploy *deploy)
{
return g_key_file_ref (deploy->metadata);
}
static FlatpakDeploy *
flatpak_deploy_new (GFile *dir, GKeyFile *metadata)
{
FlatpakDeploy *deploy;
deploy = g_object_new (FLATPAK_TYPE_DEPLOY, NULL);
deploy->dir = g_object_ref (dir);
deploy->metadata = g_key_file_ref (metadata);
return deploy;
}
GFile *
flatpak_get_system_default_base_dir_location (void)
{
static gsize path = 0;
if (g_once_init_enter (&path))
{
gsize setup_value = 0;
const char *system_dir = g_getenv ("FLATPAK_SYSTEM_DIR");
if (system_dir != NULL)
setup_value = (gsize) system_dir;
else
setup_value = (gsize) FLATPAK_SYSTEMDIR;
g_once_init_leave (&path, setup_value);
}
return g_file_new_for_path ((char *) path);
}
static FlatpakDirStorageType
parse_storage_type (const char *type_string)
{
if (type_string != NULL)
{
g_autofree char *type_low = NULL;
type_low = g_ascii_strdown (type_string, -1);
if (g_strcmp0 (type_low, "network") == 0)
return FLATPAK_DIR_STORAGE_TYPE_NETWORK;
if (g_strcmp0 (type_low, "mmc") == 0)
return FLATPAK_DIR_STORAGE_TYPE_MMC;
if (g_strcmp0 (type_low, "sdcard") == 0)
return FLATPAK_DIR_STORAGE_TYPE_SDCARD;
if (g_strcmp0 (type_low, "hardisk") == 0)
return FLATPAK_DIR_STORAGE_TYPE_HARD_DISK;
}
return FLATPAK_DIR_STORAGE_TYPE_DEFAULT;
}
static gboolean
has_system_location (GPtrArray *locations,
const char *id)
{
int i;
for (i = 0; i < locations->len; i++)
{
GFile *path = g_ptr_array_index (locations, i);
DirExtraData *extra_data = g_object_get_data (G_OBJECT (path), "extra-data");
if (extra_data != NULL && g_strcmp0 (extra_data->id, id) == 0)
return TRUE;
}
return FALSE;
}
static void
append_new_system_location (GPtrArray *locations,
GFile *location,
const char *id,
const char *display_name,
FlatpakDirStorageType storage_type,
gint priority)
{
DirExtraData *extra_data = NULL;
extra_data = dir_extra_data_new (id, display_name, priority, storage_type);
g_object_set_data_full (G_OBJECT (location), "extra-data", extra_data,
(GDestroyNotify) dir_extra_data_free);
g_ptr_array_add (locations, location);
}
static gboolean
append_locations_from_config_file (GPtrArray *locations,
const char *file_path,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GKeyFile) keyfile = NULL;
g_auto(GStrv) groups = NULL;
g_autoptr(GError) my_error = NULL;
gboolean ret = FALSE;
gsize n_groups;
int i;
keyfile = g_key_file_new ();
if (!g_key_file_load_from_file (keyfile, file_path, G_KEY_FILE_NONE, &my_error))
{
g_debug ("Could not get list of system installations: %s", my_error->message);
g_propagate_error (error, g_steal_pointer (&my_error));
goto out;
}
/* One configuration file might define more than one installation */
groups = g_key_file_get_groups (keyfile, &n_groups);
for (i = 0; i < n_groups; i++)
{
g_autofree char *id = NULL;
g_autofree char *path = NULL;
size_t len;
if (!g_str_has_prefix (groups[i], "Installation \""))
{
if (g_str_has_prefix (groups[i], "Installation "))
g_warning ("Installation without quotes (%s). Ignoring", groups[i]);
continue;
}
id = g_strdup (&groups[i][14]);
if (!g_str_has_suffix (id, "\""))
{
g_warning ("Installation without closing quote (%s). Ignoring", groups[i]);
continue;
}
len = strlen (id);
if (len > 0)
id[len - 1] = '\0';
if (has_system_location (locations, id))
{
g_warning ("Found duplicate flatpak installation (Id: %s). Ignoring", id);
continue;
}
path = g_key_file_get_string (keyfile, groups[i], "Path", &my_error);
if (path == NULL)
{
g_debug ("Unable to get path for installation '%s': %s", id, my_error->message);
g_propagate_error (error, g_steal_pointer (&my_error));
goto out;
}
else
{
GFile *location = NULL;
g_autofree char *display_name = NULL;
g_autofree char *priority = NULL;
g_autofree char *storage_type = NULL;
gint priority_val = 0;
display_name = g_key_file_get_string (keyfile, groups[i], "DisplayName", NULL);
priority = g_key_file_get_string (keyfile, groups[i], "Priority", NULL);
storage_type = g_key_file_get_string (keyfile, groups[i], "StorageType", NULL);
if (priority != NULL)
priority_val = g_ascii_strtoll (priority, NULL, 10);
location = g_file_new_for_path (path);
append_new_system_location (locations, location, id, display_name,
parse_storage_type (storage_type),
priority_val);
}
}
ret = TRUE;
out:
return ret;
}
static gint
system_locations_compare_func (gconstpointer location_a, gconstpointer location_b)
{
const GFile *location_object_a = *(const GFile **) location_a;
const GFile *location_object_b = *(const GFile **) location_b;
DirExtraData *extra_data_a = NULL;
DirExtraData *extra_data_b = NULL;
gint prio_a = 0;
gint prio_b = 0;
extra_data_a = g_object_get_data (G_OBJECT (location_object_a), "extra-data");
prio_a = (extra_data_a != NULL) ? extra_data_a->priority : 0;
extra_data_b = g_object_get_data (G_OBJECT (location_object_b), "extra-data");
prio_b = (extra_data_b != NULL) ? extra_data_b->priority : 0;
return prio_b - prio_a;
}
static GPtrArray *
system_locations_from_configuration (GCancellable *cancellable,
GError **error)
{
g_autoptr(GPtrArray) locations = NULL;
g_autoptr(GFile) conf_dir = NULL;
g_autoptr(GFileEnumerator) dir_enum = NULL;
g_autoptr(GError) my_error = NULL;
g_autofree char *config_dir = NULL;
locations = g_ptr_array_new_with_free_func (g_object_unref);
config_dir = g_strdup_printf ("%s/%s",
get_config_dir_location (),
SYSCONF_INSTALLATIONS_DIR);
if (!g_file_test (config_dir, G_FILE_TEST_IS_DIR))
{
g_debug ("No installations directory in %s. Skipping", config_dir);
goto out;
}
conf_dir = g_file_new_for_path (config_dir);
dir_enum = g_file_enumerate_children (conf_dir,
G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE,
G_FILE_QUERY_INFO_NONE,
cancellable, &my_error);
if (my_error != NULL)
{
g_debug ("Unexpected error retrieving extra installations in %s: %s",
config_dir, my_error->message);
g_propagate_error (error, g_steal_pointer (&my_error));
goto out;
}
while (TRUE)
{
GFileInfo *file_info;
GFile *path;
const char *name;
guint32 type;
if (!g_file_enumerator_iterate (dir_enum, &file_info, &path,
cancellable, &my_error))
{
g_debug ("Unexpected error reading file in %s: %s",
config_dir, my_error->message);
g_propagate_error (error, g_steal_pointer (&my_error));
goto out;
}
if (file_info == NULL)
break;
name = g_file_info_get_attribute_byte_string (file_info, "standard::name");
type = g_file_info_get_attribute_uint32 (file_info, "standard::type");
if (type == G_FILE_TYPE_REGULAR && g_str_has_suffix (name, SYSCONF_INSTALLATIONS_FILE_EXT))
{
g_autofree char *path_str = g_file_get_path (path);
if (!append_locations_from_config_file (locations, path_str, cancellable, error))
goto out;
}
}
out:
return g_steal_pointer (&locations);
}
static GPtrArray *
get_system_locations (GCancellable *cancellable,
GError **error)
{
g_autoptr(GPtrArray) locations = NULL;
/* This will always return a GPtrArray, being an empty one
* if no additional system installations have been configured.
*/
locations = system_locations_from_configuration (cancellable, error);
/* Only fill the details of the default directory if not overriden. */
if (!has_system_location (locations, SYSTEM_DIR_DEFAULT_ID))
{
append_new_system_location (locations,
flatpak_get_system_default_base_dir_location (),
SYSTEM_DIR_DEFAULT_ID,
SYSTEM_DIR_DEFAULT_DISPLAY_NAME,
SYSTEM_DIR_DEFAULT_STORAGE_TYPE,
SYSTEM_DIR_DEFAULT_PRIORITY);
}
/* Store the list of system locations sorted according to priorities */
g_ptr_array_sort (locations, system_locations_compare_func);
return g_steal_pointer (&locations);
}
GPtrArray *
flatpak_get_system_base_dir_locations (GCancellable *cancellable,
GError **error)
{
static gsize array = 0;
if (g_once_init_enter (&array))
{
gsize setup_value = 0;
setup_value = (gsize) get_system_locations (cancellable, error);
g_once_init_leave (&array, setup_value);
}
return (GPtrArray *) array;
}
GFile *
flatpak_get_user_base_dir_location (void)
{
static gsize file = 0;
if (g_once_init_enter (&file))
{
gsize setup_value = 0;
const char *path;
g_autofree char *free_me = NULL;
const char *user_dir = g_getenv ("FLATPAK_USER_DIR");
if (user_dir != NULL && *user_dir != 0)
path = user_dir;
else
path = free_me = g_build_filename (g_get_user_data_dir (), "flatpak", NULL);
setup_value = (gsize) g_file_new_for_path (path);
g_once_init_leave (&file, setup_value);
}
return g_object_ref ((GFile *) file);
}
/* This is a cache directory similar to ~/.cache/flatpak/system-cache,
* but in /var/tmp. This is useful for things like the system child
* repos, because it is more likely to be on the same filesystem as
* the system repo (thus increasing chances for e.g. reflink copying),
* and avoids filling the users homedirectory with temporary data.
*
* In order to re-use this between instances we create a symlink
* in /run to it and verify it before use.
*/
static GFile *
flatpak_ensure_system_user_cache_dir_location (GError **error)
{
g_autofree char *path = NULL;
g_autofree char *symlink_path = NULL;
struct stat st_buf;
const char *custom_path = g_getenv ("FLATPAK_SYSTEM_CACHE_DIR");
if (custom_path != NULL && *custom_path != 0)
{
if (g_mkdir_with_parents (custom_path, 0755) != 0)
{
glnx_set_error_from_errno (error);
return NULL;
}
return g_file_new_for_path (custom_path);
}
symlink_path = g_build_filename (g_get_user_runtime_dir (), ".flatpak-cache", NULL);
path = flatpak_readlink (symlink_path, NULL);
if (stat (path, &st_buf) == 0 &&
/* Must be owned by us */
st_buf.st_uid == getuid () &&
/* and not writeable by others */
(st_buf.st_mode & 0022) == 0)
return g_file_new_for_path (path);
path = g_strdup ("/var/tmp/flatpak-cache-XXXXXX");
if (g_mkdtemp_full (path, 0755) == NULL)
{
flatpak_fail (error, "Can't create temporary directory");
return NULL;
}
unlink (symlink_path);
if (symlink (path, symlink_path) != 0)
{
glnx_set_error_from_errno (error);
return NULL;
}
return g_file_new_for_path (path);
}
static GFile *