summaryrefslogtreecommitdiff
path: root/app/flatpak-builtins-build-import-bundle.c
blob: 8072f083eefb7f955b34e5ac9d98ef9479564b4d (plain)
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
/*
 * Copyright © 2015 Red Hat, 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 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>
 */

#include "config.h"

#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "libgsystem.h"
#include "libglnx/libglnx.h"

#include "flatpak-builtins.h"
#include "flatpak-utils.h"

static char *opt_ref;
static gboolean opt_oci = FALSE;

static GOptionEntry options[] = {
  { "ref", 0, 0, G_OPTION_ARG_STRING, &opt_ref, "Override the ref used for the imported bundle", "REF" },
  { "oci", 0, 0, G_OPTION_ARG_NONE, &opt_oci, "Import oci image instead of flatpak bundle"},
  { NULL }
};

static gboolean
import_oci (OstreeRepo *repo, GFile *file,
            GCancellable *cancellable, GError **error)
{
#if !defined(HAVE_OSTREE_EXPORT_PATH_PREFIX)
  /* This code actually doesn't user path_prefix, but it need the support
     for reading commits from the transaction that was added at the same time. */
  g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
               "This version of ostree is to old to support OCI exports");
  return FALSE;
#elif !defined(HAVE_LIBARCHIVE)
  g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
               "This version of flatpak is not compiled with libarchive support");
  return FALSE;
#else
  g_autoptr(OstreeMutableTree) archive_mtree = NULL;
  g_autoptr(OstreeMutableTree) mtree = NULL;
  g_autoptr(OstreeMutableTree) files_mtree = NULL;
  g_autoptr(OstreeMutableTree) export_mtree = NULL;
  g_autoptr(GFile) archive_root = NULL;
  g_autoptr(GFile) root = NULL;
  g_autoptr(GFile) files = NULL;
  g_autoptr(GFile) export = NULL;
  g_autoptr(GFile) ref = NULL;
  g_autoptr(GFile) commit = NULL;
  g_autoptr(GFile) commitmeta = NULL;
  g_autoptr(GFile) metadata = NULL;
  g_autofree char *commit_checksum = NULL;
  g_autofree char *ref_data = NULL;
  g_autofree char *commit_data = NULL;
  gsize commit_size;
  g_autofree char *commitmeta_data = NULL;
  g_autofree char *parent = NULL;
  const char *subject;
  const char *body;
  const char *target_ref;
  const char *files_source;
  gsize commitmeta_size;
  g_autoptr(GVariant) commitv = NULL;
  g_autoptr(GVariant) commitv_metadata = NULL;
  g_autoptr(GVariant) commitmetav = NULL;

  if (!ostree_repo_prepare_transaction (repo, NULL, cancellable, error))
    return FALSE;

  /* There is no way to write a subset of the archive to a mtree, so instead
     we write all of it and then build a new mtree with the subset */
  archive_mtree = ostree_mutable_tree_new ();
  if (!ostree_repo_write_archive_to_mtree (repo, file, archive_mtree, NULL,
                                           TRUE,
                                           cancellable, error))
    return FALSE;

  if (!ostree_repo_write_mtree (repo, archive_mtree, &archive_root, cancellable, error))
    return FALSE;

  if (!ostree_repo_file_ensure_resolved ((OstreeRepoFile *) archive_root, error))
    return FALSE;

  ref = g_file_resolve_relative_path (archive_root, "rootfs/ref");
  metadata = g_file_resolve_relative_path (archive_root, "rootfs/metadata");
  commit = g_file_resolve_relative_path (archive_root, "rootfs/commit");
  commitmeta = g_file_resolve_relative_path (archive_root, "rootfs/commitmeta");

  if (!g_file_query_exists (ref, NULL))
    return flatpak_fail (error, "Required file ref not in tarfile");
  if (!g_file_query_exists (metadata, NULL))
    return flatpak_fail (error, "Required file metadata not in tarfile");
  if (!g_file_query_exists (commit, NULL))
    return flatpak_fail (error, "Required file commit not in tarfile");

  if (!g_file_load_contents (ref, cancellable,
                             &ref_data, NULL,
                             NULL, error))
    return FALSE;

  if (g_str_has_prefix (ref_data, "app/"))
    files_source = "rootfs/app";
  else
    files_source = "rootfs/usr";

  files = g_file_resolve_relative_path (archive_root, files_source);
  if (!g_file_query_exists (files, NULL))
    return flatpak_fail (error, "Required directory %s not in tarfile", files_source);

  export = g_file_resolve_relative_path (archive_root, "rootfs/export");

  if (!g_file_load_contents (commit, cancellable,
                             &commit_data, &commit_size,
                             NULL, error))
    return FALSE;

  commitv = g_variant_new_from_data (OSTREE_COMMIT_GVARIANT_FORMAT,
                                     g_steal_pointer (&commit_data), commit_size,
                                     FALSE, g_free, commit_data);
  if (!ostree_validate_structureof_commit (commitv, error))
    return FALSE;

  if (g_file_query_exists (commitmeta, NULL) &&
      !g_file_load_contents (commitmeta, cancellable,
                             &commitmeta_data, &commitmeta_size,
                             NULL, error))
    return FALSE;

  if (commitmeta_data != NULL)
    commitmetav = g_variant_new_from_data (G_VARIANT_TYPE ("a{sv}"),
                                           g_steal_pointer (&commitmeta_data), commitmeta_size,
                                           FALSE, g_free, commitmeta_data);

  mtree = ostree_mutable_tree_new ();

  if (!flatpak_mtree_create_root (repo, mtree, cancellable, error))
    return FALSE;

  if (!ostree_mutable_tree_ensure_dir (mtree, "files", &files_mtree, error))
    return FALSE;

  if (!ostree_repo_write_directory_to_mtree (repo, files, files_mtree, NULL,
                                             cancellable, error))
    return FALSE;

  if (g_file_query_exists (export, NULL))
    {
      if (!ostree_mutable_tree_ensure_dir (mtree, "export", &export_mtree, error))
        return FALSE;

      if (!ostree_repo_write_directory_to_mtree (repo, export, export_mtree, NULL,
                                                 cancellable, error))
        return FALSE;
    }

  if (!ostree_mutable_tree_replace_file (mtree, "metadata",
                                         ostree_repo_file_get_checksum ((OstreeRepoFile *) metadata),
                                         error))
    return FALSE;

  if (!ostree_repo_write_mtree (repo, mtree, &root, cancellable, error))
    return FALSE;

  /* Verify that we created the same contents */
  {
    g_autoptr(GVariant) tree_contents_bytes = NULL;
    g_autofree char *tree_contents_checksum = NULL;
    g_autoptr(GVariant) tree_metadata_bytes = NULL;
    g_autofree char *tree_metadata_checksum = NULL;
    tree_contents_bytes = g_variant_get_child_value (commitv, 6);
    tree_contents_checksum = ostree_checksum_from_bytes_v (tree_contents_bytes);
    tree_metadata_bytes = g_variant_get_child_value (commitv, 7);
    tree_metadata_checksum = ostree_checksum_from_bytes_v (tree_metadata_bytes);

    if (strcmp (tree_contents_checksum, ostree_repo_file_tree_get_contents_checksum ((OstreeRepoFile *) root)))
      return flatpak_fail (error, "Imported content checksum (%s) does not match original checksum (%s)",
                           tree_contents_checksum, ostree_repo_file_tree_get_contents_checksum ((OstreeRepoFile *) root));

    if (strcmp (tree_metadata_checksum, ostree_repo_file_tree_get_metadata_checksum ((OstreeRepoFile *) root)))
      return flatpak_fail (error, "Imported metadata checksum (%s) does not match original checksum (%s)",
                           tree_metadata_checksum, ostree_repo_file_tree_get_metadata_checksum ((OstreeRepoFile *) root));
  }

  commitv_metadata = g_variant_get_child_value (commitv, 0);
  parent = ostree_commit_get_parent (commitv);
  g_variant_get_child (commitv, 3, "s", &subject);
  g_variant_get_child (commitv, 4, "s", &body);

  if (!ostree_repo_write_commit_with_time (repo,
                                           parent,
                                           subject,
                                           body,
                                           commitv_metadata,
                                           OSTREE_REPO_FILE (root),
                                           ostree_commit_get_timestamp (commitv),
                                           &commit_checksum,
                                           cancellable, error))
    return FALSE;

  if (commitmetav != NULL &&
      !ostree_repo_write_commit_detached_metadata (repo, commit_checksum,
                                                   commitmetav, cancellable, error))
    return FALSE;

  if (opt_ref != NULL)
    target_ref = opt_ref;
  else
    target_ref = ref_data;

  ostree_repo_transaction_set_ref (repo, NULL, target_ref, commit_checksum);

  if (!ostree_repo_commit_transaction (repo, NULL, cancellable, error))
    return FALSE;

  g_print ("Importing %s (%s)\n", target_ref, commit_checksum);

  return TRUE;
#endif
}

static gboolean
import_bundle (OstreeRepo *repo, GFile *file,
               GCancellable *cancellable, GError **error)
{
  g_autoptr(GVariant) metadata = NULL;
  g_autofree char *bundle_ref = NULL;
  g_autofree char *to_checksum = NULL;
  const char *ref;

  metadata = flatpak_bundle_load (file, &to_checksum,
                                  &bundle_ref,
                                  NULL,
                                  NULL,
                                  NULL,
                                  error);
  if (metadata == NULL)
    return FALSE;

  if (opt_ref != NULL)
    ref = opt_ref;
  else
    ref = bundle_ref;

  g_print ("Importing %s (%s)\n", ref, to_checksum);
  if (!flatpak_pull_from_bundle (repo, file,
                                 NULL, ref, FALSE,
                                 cancellable,
                                 error))
    return FALSE;

  return TRUE;
}

gboolean
flatpak_builtin_build_import (int argc, char **argv, GCancellable *cancellable, GError **error)
{
  g_autoptr(GOptionContext) context = NULL;
  g_autoptr(GFile) file = NULL;
  g_autoptr(GFile) repofile = NULL;
  g_autoptr(OstreeRepo) repo = NULL;
  const char *location;
  const char *filename;

  context = g_option_context_new ("LOCATION FILENAME - Import a file bundle into a local repository");

  if (!flatpak_option_context_parse (context, options, &argc, &argv, FLATPAK_BUILTIN_FLAG_NO_DIR, NULL, cancellable, error))
    return FALSE;

  if (argc < 3)
    return usage_error (context, "LOCATION and FILENAME must be specified", error);

  location = argv[1];
  filename = argv[2];

  repofile = g_file_new_for_commandline_arg (location);
  repo = ostree_repo_new (repofile);

  if (!g_file_query_exists (repofile, cancellable))
    return flatpak_fail (error, "'%s' is not a valid repository", location);

  file = g_file_new_for_commandline_arg (filename);

  if (!ostree_repo_open (repo, cancellable, error))
    return FALSE;

if (opt_oci)
    {
      if (!import_oci (repo, file, cancellable, error))
        return FALSE;
    }
  else
    {
      if (!import_bundle (repo, file, cancellable, error))
        return FALSE;
    }

  return TRUE;
}