summaryrefslogtreecommitdiff
path: root/src/libostree/ostree-ref.c
blob: eb77d97c74e47f3e4d30a79bc23303bc8e12009e (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
/*
 * 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:
 *  - Philip Withnall <withnall@endlessm.com>
 */

#include "config.h"

#include <gio/gio.h>
#include <glib.h>
#include <glib-object.h>
#include <libglnx.h>

#include "ostree-autocleanups.h"
#include "ostree-core.h"
#include "ostree-core-private.h"
#include "ostree-ref.h"

G_DEFINE_BOXED_TYPE (OstreeCollectionRef, ostree_collection_ref,
                     ostree_collection_ref_dup, ostree_collection_ref_free)

/**
 * ostree_collection_ref_new:
 * @collection_id: (nullable): a collection ID, or %NULL for a plain ref
 * @ref_name: a ref name
 *
 * Create a new #OstreeCollectionRef containing (@collection_id, @ref_name). If
 * @collection_id is %NULL, this is equivalent to a plain ref name string (not a
 * refspec; no remote name is included), which can be used for non-P2P
 * operations.
 *
 * Returns: (transfer full) (nullable): a new #OstreeCollectionRef
 * Since: 2018.6
 */
OstreeCollectionRef *
ostree_collection_ref_new (const gchar *collection_id,
                           const gchar *ref_name)
{
  g_autoptr(OstreeCollectionRef) collection_ref = NULL;

  g_return_val_if_fail (collection_id == NULL ||
                        ostree_validate_collection_id (collection_id, NULL), NULL);
  g_return_val_if_fail (ostree_validate_rev (ref_name, NULL), NULL);

  collection_ref = g_new0 (OstreeCollectionRef, 1);
  collection_ref->collection_id = g_strdup (collection_id);
  collection_ref->ref_name = g_strdup (ref_name);

  return g_steal_pointer (&collection_ref);
}

/**
 * ostree_collection_ref_dup:
 * @ref: (not nullable): an #OstreeCollectionRef
 *
 * Create a copy of the given @ref.
 *
 * Returns: (transfer full): a newly allocated copy of @ref
 * Since: 2018.6
 */
OstreeCollectionRef *
ostree_collection_ref_dup (const OstreeCollectionRef *ref)
{
  g_return_val_if_fail (ref != NULL, NULL);

  return ostree_collection_ref_new (ref->collection_id, ref->ref_name);
}

/**
 * ostree_collection_ref_free:
 * @ref: (transfer full): an #OstreeCollectionRef
 *
 * Free the given @ref.
 *
 * Since: 2018.6
 */
void
ostree_collection_ref_free (OstreeCollectionRef *ref)
{
  g_return_if_fail (ref != NULL);

  g_free (ref->collection_id);
  g_free (ref->ref_name);
  g_free (ref);
}

/**
 * ostree_collection_ref_hash:
 * @ref: (not nullable): an #OstreeCollectionRef
 *
 * Hash the given @ref. This function is suitable for use with #GHashTable.
 * @ref must be non-%NULL.
 *
 * Returns: hash value for @ref
 * Since: 2018.6
 */
guint
ostree_collection_ref_hash (gconstpointer ref)
{
  const OstreeCollectionRef *_ref = ref;

  if (_ref->collection_id != NULL)
    return g_str_hash (_ref->collection_id) ^ g_str_hash (_ref->ref_name);
  else
    return g_str_hash (_ref->ref_name);
}

/**
 * ostree_collection_ref_equal:
 * @ref1: (not nullable): an #OstreeCollectionRef
 * @ref2 : (not nullable): another #OstreeCollectionRef
 *
 * Compare @ref1 and @ref2 and return %TRUE if they have the same collection ID and
 * ref name, and %FALSE otherwise. Both @ref1 and @ref2 must be non-%NULL.
 *
 * Returns: %TRUE if @ref1 and @ref2 are equal, %FALSE otherwise
 * Since: 2018.6
 */
gboolean
ostree_collection_ref_equal (gconstpointer ref1,
                             gconstpointer ref2)
{
  const OstreeCollectionRef *_ref1 = ref1, *_ref2 = ref2;

  return (g_strcmp0 (_ref1->collection_id, _ref2->collection_id) == 0 &&
          g_strcmp0 (_ref1->ref_name, _ref2->ref_name) == 0);
}

/**
 * ostree_collection_ref_dupv:
 * @refs: (array zero-terminated=1): %NULL-terminated array of #OstreeCollectionRefs
 *
 * Copy an array of #OstreeCollectionRefs, including deep copies of all its
 * elements. @refs must be %NULL-terminated; it may be empty, but must not be
 * %NULL.
 *
 * Returns: (transfer full) (array zero-terminated=1): a newly allocated copy of @refs
 * Since: 2018.6
 */
OstreeCollectionRef **
ostree_collection_ref_dupv (const OstreeCollectionRef * const *refs)
{
  gsize i,  n_refs = g_strv_length ((gchar **) refs);  /* hack */
  g_auto(OstreeCollectionRefv) new_refs = NULL;

  g_return_val_if_fail (refs != NULL, NULL);

  new_refs = g_new0 (OstreeCollectionRef*, n_refs + 1);

  for (i = 0; i < n_refs; i++)
    new_refs[i] = ostree_collection_ref_dup (refs[i]);
  new_refs[i] = NULL;

  return g_steal_pointer (&new_refs);
}

/**
 * ostree_collection_ref_freev:
 * @refs: (transfer full) (array zero-terminated=1): an array of #OstreeCollectionRefs
 *
 * Free the given array of @refs, including freeing all its elements. @refs
 * must be %NULL-terminated; it may be empty, but must not be %NULL.
 *
 * Since: 2018.6
 */
void
ostree_collection_ref_freev (OstreeCollectionRef **refs)
{
  gsize i;

  g_return_if_fail (refs != NULL);

  for (i = 0; refs[i] != NULL; i++)
    ostree_collection_ref_free (refs[i]);
  g_free (refs);
}