summaryrefslogtreecommitdiff
path: root/lib/flatpak-ref.c
blob: ba28b070a248daf831f0dd6c3a1e7643046cf5cb (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
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
/*
 * 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 "flatpak-utils.h"
#include "flatpak-ref.h"
#include "flatpak-enum-types.h"

/**
 * SECTION:flatpak-ref
 * @Title: FlatpakRef
 * @Short_description: Application reference
 *
 * Currently flatpak manages two types of binary artifacts: applications, and
 * runtimes. Applications contain a program that desktop users can run, while
 * runtimes contain only libraries and data. An FlatpakRef object (or short: ref)
 * can refer to either of these.
 *
 * Both applications and runtimes are identified by a 4-tuple of strings: kind,
 * name, arch and branch, e.g. app/org.gnome.evince/x86_64/master. The functions
 * flatpak_ref_parse() and flatpak_ref_format_ref() can be used to convert
 * FlatpakRef objects into this string representation and back.
 *
 * To uniquely identify a particular version of an application or runtime, you
 * need a commit.
 *
 * The subclasses #FlatpakInstalledRef and #FlatpakRemoteRef provide more information
 * for artifacts that are locally installed or available from a remote repository.
 */
typedef struct _FlatpakRefPrivate FlatpakRefPrivate;

struct _FlatpakRefPrivate
{
  char          *name;
  char          *arch;
  char          *branch;
  char          *commit;
  FlatpakRefKind kind;
};

G_DEFINE_TYPE_WITH_PRIVATE (FlatpakRef, flatpak_ref, G_TYPE_OBJECT)

enum {
  PROP_0,

  PROP_NAME,
  PROP_ARCH,
  PROP_BRANCH,
  PROP_COMMIT,
  PROP_KIND
};

static void
flatpak_ref_finalize (GObject *object)
{
  FlatpakRef *self = FLATPAK_REF (object);
  FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);

  g_free (priv->name);
  g_free (priv->arch);
  g_free (priv->branch);
  g_free (priv->commit);

  G_OBJECT_CLASS (flatpak_ref_parent_class)->finalize (object);
}

static void
flatpak_ref_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  FlatpakRef *self = FLATPAK_REF (object);
  FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);

  switch (prop_id)
    {
    case PROP_NAME:
      g_clear_pointer (&priv->name, g_free);
      priv->name = g_value_dup_string (value);
      break;

    case PROP_ARCH:
      g_clear_pointer (&priv->arch, g_free);
      priv->arch = g_value_dup_string (value);
      break;

    case PROP_BRANCH:
      g_clear_pointer (&priv->branch, g_free);
      priv->branch = g_value_dup_string (value);
      break;

    case PROP_COMMIT:
      g_clear_pointer (&priv->commit, g_free);
      priv->commit = g_value_dup_string (value);
      break;

    case PROP_KIND:
      priv->kind = g_value_get_enum (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
flatpak_ref_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  FlatpakRef *self = FLATPAK_REF (object);
  FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);

  switch (prop_id)
    {
    case PROP_NAME:
      g_value_set_string (value, priv->name);
      break;

    case PROP_ARCH:
      g_value_set_string (value, priv->arch);
      break;

    case PROP_BRANCH:
      g_value_set_string (value, priv->branch);
      break;

    case PROP_COMMIT:
      g_value_set_string (value, priv->commit);
      break;

    case PROP_KIND:
      g_value_set_enum (value, priv->kind);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
flatpak_ref_class_init (FlatpakRefClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = flatpak_ref_get_property;
  object_class->set_property = flatpak_ref_set_property;
  object_class->finalize = flatpak_ref_finalize;

  g_object_class_install_property (object_class,
                                   PROP_NAME,
                                   g_param_spec_string ("name",
                                                        "Name",
                                                        "The name of the application or runtime",
                                                        NULL,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
                                   PROP_ARCH,
                                   g_param_spec_string ("arch",
                                                        "Architecture",
                                                        "The architecture of the application or runtime",
                                                        NULL,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
                                   PROP_BRANCH,
                                   g_param_spec_string ("branch",
                                                        "Branch",
                                                        "The branch of the application or runtime",
                                                        NULL,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
                                   PROP_COMMIT,
                                   g_param_spec_string ("commit",
                                                        "Commit",
                                                        "The commit",
                                                        NULL,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
                                   PROP_KIND,
                                   g_param_spec_enum ("kind",
                                                      "Kind",
                                                      "The kind of artifact",
                                                      FLATPAK_TYPE_REF_KIND,
                                                      FLATPAK_REF_KIND_APP,
                                                      G_PARAM_READWRITE));
}

static void
flatpak_ref_init (FlatpakRef *self)
{
  FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);

  priv->kind = FLATPAK_REF_KIND_APP;
}

/**
 * flatpak_ref_get_name:
 * @self: a #FlatpakRef
 *
 * Gets the name of the ref.
 *
 * Returns: (transfer none): the name
 */
const char *
flatpak_ref_get_name (FlatpakRef *self)
{
  FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);

  return priv->name;
}

/**
 * flatpak_ref_get_arch:
 * @self: a #FlatpakRef
 *
 * Gets the arch or the ref.
 *
 * Returns: (transfer none): the arch
 */
const char *
flatpak_ref_get_arch (FlatpakRef *self)
{
  FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);

  return priv->arch;
}

/**
 * flatpak_ref_get_branch:
 * @self: a #FlatpakRef
 *
 * Gets the branch of the ref.
 *
 * Returns: (transfer none): the branch
 */
const char *
flatpak_ref_get_branch (FlatpakRef *self)
{
  FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);

  return priv->branch;
}

/**
 * flatpak_ref_get_commit:
 * @self: a #FlatpakRef
 *
 * Gets the commit of the ref.
 *
 * Returns: (transfer none): the commit
 */
const char *
flatpak_ref_get_commit (FlatpakRef *self)
{
  FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);

  return priv->commit;
}

/**
 * flatpak_ref_get_kind:
 * @self: a #FlatpakRef
 *
 * Gets the kind of artifact that this ref refers to.
 *
 * Returns: the kind of artifact
 */
FlatpakRefKind
flatpak_ref_get_kind (FlatpakRef *self)
{
  FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);

  return priv->kind;
}

/**
 * flatpak_ref_format_ref:
 * @self: a #FlatpakRef
 *
 * Convert an FlatpakRef object into a string representation that
 * can be parsed by flatpak_ref_parse().
 *
 * Returns: (transfer full): string representation
 */
char *
flatpak_ref_format_ref (FlatpakRef *self)
{
  FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);

  if (priv->kind == FLATPAK_REF_KIND_APP)
    return flatpak_build_app_ref (priv->name,
                                  priv->branch,
                                  priv->arch);
  else
    return flatpak_build_runtime_ref (priv->name,
                                      priv->branch,
                                      priv->arch);
}

/**
 * flatpak_ref_parse:
 * @ref: A string ref name, such as "app/org.test.App/86_64/master"
 * @error: return location for a #GError
 *
 * Tries to parse a full ref name and return a #FlatpakRef (without a
 * commit set) or fail if the ref is invalid somehow.
 *
 * Returns: (transfer full): an #FlatpakRef, or %NULL
 */
FlatpakRef *
flatpak_ref_parse (const char *ref, GError **error)
{
  g_auto(GStrv) parts = NULL;

  parts = flatpak_decompose_ref (ref, error);
  if (parts == NULL)
    return NULL;

  FlatpakRefKind kind;
  if (g_strcmp0 (parts[0], "app") == 0)
    {
      kind = FLATPAK_REF_KIND_APP;
    }
  else if (g_strcmp0 (parts[0], "runtime") == 0)
    {
      kind = FLATPAK_REF_KIND_RUNTIME;
    }
  else
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
                   "Invalid kind: %s", parts[0]);
      return NULL;
    }

  return FLATPAK_REF (g_object_new (FLATPAK_TYPE_REF,
                                    "kind", kind,
                                    "name", parts[1],
                                    "arch", parts[2],
                                    "branch", parts[3],
                                    NULL));
}