summaryrefslogtreecommitdiff
path: root/src/libostree/ostree-remote.c
blob: 04f08352701c3e602ac5c06d68e878ae3d32acac (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
/*
 * Copyright © 2011 Colin Walters <walters@verbum.org>
 * Copyright © 2015 Red Hat, Inc.
 * Copyright © 2017 Endless Mobile, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * This library 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 <https://www.gnu.org/licenses/>.
 *
 * Authors:
 *  - Colin Walters <walters@verbum.org>
 *  - Philip Withnall <withnall@endlessm.com>
 */

#include "config.h"

#include <gio/gio.h>
#include <glib.h>
#include <glib-object.h>
#include <stdint.h>
#include <string.h>

#include "ostree-remote.h"
#include "ostree-remote-private.h"
#include "ot-keyfile-utils.h"

/**
 * SECTION:remote
 *
 * The #OstreeRemote structure represents the configuration for a single remote
 * repository. Currently, all configuration is handled internally, and
 * #OstreeRemote objects are represented by their textual name handle, or by an
 * opaque pointer (which can be reference counted if needed).
 *
 * #OstreeRemote provides configuration for accessing a remote, but does not
 * provide the results of accessing a remote, such as information about what
 * refs are currently on a remote, or the commits they currently point to. Use
 * #OstreeRepo in combination with an #OstreeRemote to query that information.
 *
 * Since: 2018.6
 */

OstreeRemote *
ostree_remote_new (const gchar *name)
{
  return ostree_remote_new_dynamic (name, NULL);
}

OstreeRemote *
ostree_remote_new_dynamic (const gchar *name,
                           const gchar *refspec_name)
{
  OstreeRemote *remote;

  g_assert (name != NULL && *name != '\0');
  g_assert (refspec_name == NULL || *refspec_name != '\0');

  remote = g_slice_new0 (OstreeRemote);
  remote->ref_count = 1;
  remote->name = g_strdup (name);
  remote->refspec_name = g_strdup (refspec_name);
  remote->group = g_strdup_printf ("remote \"%s\"", (refspec_name != NULL) ? refspec_name : name);
  remote->keyring = g_strdup_printf ("%s.trustedkeys.gpg", (refspec_name != NULL) ? refspec_name : name);
  remote->options = g_key_file_new ();

  return remote;
}

OstreeRemote *
ostree_remote_new_from_keyfile (GKeyFile    *keyfile,
                                const gchar *group)
{
  g_autoptr(GMatchInfo) match = NULL;
  OstreeRemote *remote;
  g_autofree gchar *name = NULL;

  static gsize regex_initialized;
  static GRegex *regex;

  if (g_once_init_enter (&regex_initialized))
    {
      regex = g_regex_new ("^remote \"(.+)\"$", 0, 0, NULL);
      g_assert (regex);
      g_once_init_leave (&regex_initialized, 1);
    }

  /* Sanity check */
  g_return_val_if_fail (g_key_file_has_group (keyfile, group), NULL);

  /* If group name doesn't fit the pattern, fail. */
  if (!g_regex_match (regex, group, 0, &match))
    return NULL;

  name = g_match_info_fetch (match, 1);
  remote = ostree_remote_new (name);

  ot_keyfile_copy_group (keyfile, remote->options, group);

  return remote;
}

/**
 * ostree_remote_ref:
 * @remote: an #OstreeRemote
 *
 * Increase the reference count on the given @remote.
 *
 * Returns: (transfer full): a copy of @remote, for convenience
 * Since: 2018.6
 */
OstreeRemote *
ostree_remote_ref (OstreeRemote *remote)
{
  gint refcount;
  g_return_val_if_fail (remote != NULL, NULL);
  refcount = g_atomic_int_add (&remote->ref_count, 1);
  g_assert (refcount > 0);
  return remote;
}

/**
 * ostree_remote_unref:
 * @remote: (transfer full): an #OstreeRemote
 *
 * Decrease the reference count on the given @remote and free it if the
 * reference count reaches 0.
 *
 * Since: 2018.6
 */
void
ostree_remote_unref (OstreeRemote *remote)
{
  g_return_if_fail (remote != NULL);
  g_return_if_fail (remote->ref_count > 0);

  if (g_atomic_int_dec_and_test (&remote->ref_count))
    {
      g_clear_pointer (&remote->name, g_free);
      g_clear_pointer (&remote->refspec_name, g_free);
      g_clear_pointer (&remote->group, g_free);
      g_clear_pointer (&remote->keyring, g_free);
      g_clear_object (&remote->file);
      g_clear_pointer (&remote->options, g_key_file_free);
      g_slice_free (OstreeRemote, remote);
    }
}

G_DEFINE_BOXED_TYPE(OstreeRemote, ostree_remote,
                    ostree_remote_ref,
                    ostree_remote_unref);

/**
 * ostree_remote_get_name:
 * @remote: an #OstreeRemote
 *
 * Get the human-readable name of the remote. This is what the user configured,
 * if the remote was explicitly configured; and will otherwise be a stable,
 * arbitrary, string.
 *
 * Returns: remote’s name
 * Since: 2018.6
 */
const gchar *
ostree_remote_get_name (OstreeRemote *remote)
{
  g_return_val_if_fail (remote != NULL, NULL);
  g_return_val_if_fail (remote->ref_count > 0, NULL);

  return remote->name;
}

/**
 * ostree_remote_get_url:
 * @remote: an #OstreeRemote
 *
 * Get the URL from the remote.
 *
 * Returns: (transfer full): the remote's URL
 * Since: 2018.6
 */
gchar *
ostree_remote_get_url (OstreeRemote *remote)
{
  g_return_val_if_fail (remote != NULL, NULL);
  g_return_val_if_fail (remote->ref_count > 0, NULL);

  return g_key_file_get_string (remote->options, remote->group, "url", NULL);
}