/src/ostree/src/libostree/ostree-ref.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2017 Endless Mobile, Inc. |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.0+ |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library. If not, see <https://www.gnu.org/licenses/>. |
18 | | * |
19 | | * Authors: |
20 | | * - Philip Withnall <withnall@endlessm.com> |
21 | | */ |
22 | | |
23 | | #include "config.h" |
24 | | |
25 | | #include <gio/gio.h> |
26 | | #include <glib-object.h> |
27 | | #include <glib.h> |
28 | | #include <libglnx.h> |
29 | | |
30 | | #include "ostree-autocleanups.h" |
31 | | #include "ostree-core-private.h" |
32 | | #include "ostree-core.h" |
33 | | #include "ostree-ref.h" |
34 | | |
35 | 0 | G_DEFINE_BOXED_TYPE (OstreeCollectionRef, ostree_collection_ref, ostree_collection_ref_dup, |
36 | 0 | ostree_collection_ref_free) |
37 | 0 |
|
38 | 0 | /** |
39 | 0 | * ostree_collection_ref_new: |
40 | 0 | * @collection_id: (nullable): a collection ID, or %NULL for a plain ref |
41 | 0 | * @ref_name: a ref name |
42 | 0 | * |
43 | 0 | * Create a new #OstreeCollectionRef containing (@collection_id, @ref_name). If |
44 | 0 | * @collection_id is %NULL, this is equivalent to a plain ref name string (not a |
45 | 0 | * refspec; no remote name is included), which can be used for non-P2P |
46 | 0 | * operations. |
47 | 0 | * |
48 | 0 | * Returns: (transfer full): a new #OstreeCollectionRef |
49 | 0 | * Since: 2018.6 |
50 | 0 | */ |
51 | 0 | OstreeCollectionRef * |
52 | 0 | ostree_collection_ref_new (const gchar *collection_id, const gchar *ref_name) |
53 | 0 | { |
54 | 0 | g_autoptr (OstreeCollectionRef) collection_ref = NULL; |
55 | |
|
56 | 0 | g_return_val_if_fail ( |
57 | 0 | collection_id == NULL || ostree_validate_collection_id (collection_id, NULL), NULL); |
58 | 0 | g_return_val_if_fail (ostree_validate_rev (ref_name, NULL), NULL); |
59 | | |
60 | 0 | collection_ref = g_new0 (OstreeCollectionRef, 1); |
61 | 0 | collection_ref->collection_id = g_strdup (collection_id); |
62 | 0 | collection_ref->ref_name = g_strdup (ref_name); |
63 | |
|
64 | 0 | return g_steal_pointer (&collection_ref); |
65 | 0 | } |
66 | | |
67 | | /** |
68 | | * ostree_collection_ref_dup: |
69 | | * @ref: (not nullable): an #OstreeCollectionRef |
70 | | * |
71 | | * Create a copy of the given @ref. |
72 | | * |
73 | | * Returns: (transfer full): a newly allocated copy of @ref |
74 | | * Since: 2018.6 |
75 | | */ |
76 | | OstreeCollectionRef * |
77 | | ostree_collection_ref_dup (const OstreeCollectionRef *ref) |
78 | 0 | { |
79 | 0 | g_return_val_if_fail (ref != NULL, NULL); |
80 | | |
81 | 0 | return ostree_collection_ref_new (ref->collection_id, ref->ref_name); |
82 | 0 | } |
83 | | |
84 | | /** |
85 | | * ostree_collection_ref_free: |
86 | | * @ref: (transfer full): an #OstreeCollectionRef |
87 | | * |
88 | | * Free the given @ref. |
89 | | * |
90 | | * Since: 2018.6 |
91 | | */ |
92 | | void |
93 | | ostree_collection_ref_free (OstreeCollectionRef *ref) |
94 | 0 | { |
95 | 0 | g_return_if_fail (ref != NULL); |
96 | | |
97 | 0 | g_free (ref->collection_id); |
98 | 0 | g_free (ref->ref_name); |
99 | 0 | g_free (ref); |
100 | 0 | } |
101 | | |
102 | | /** |
103 | | * ostree_collection_ref_hash: |
104 | | * @ref: (not nullable) (type OstreeCollectionRef): an #OstreeCollectionRef |
105 | | * |
106 | | * Hash the given @ref. This function is suitable for use with #GHashTable. |
107 | | * @ref must be non-%NULL. |
108 | | * |
109 | | * Returns: hash value for @ref |
110 | | * Since: 2018.6 |
111 | | */ |
112 | | guint |
113 | | ostree_collection_ref_hash (gconstpointer ref) |
114 | 0 | { |
115 | 0 | const OstreeCollectionRef *_ref = ref; |
116 | |
|
117 | 0 | if (_ref->collection_id != NULL) |
118 | 0 | return g_str_hash (_ref->collection_id) ^ g_str_hash (_ref->ref_name); |
119 | 0 | else |
120 | 0 | return g_str_hash (_ref->ref_name); |
121 | 0 | } |
122 | | |
123 | | /** |
124 | | * ostree_collection_ref_equal: |
125 | | * @ref1: (not nullable) (type OstreeCollectionRef): an #OstreeCollectionRef |
126 | | * @ref2 : (not nullable) (type OstreeCollectionRef): another #OstreeCollectionRef |
127 | | * |
128 | | * Compare @ref1 and @ref2 and return %TRUE if they have the same collection ID and |
129 | | * ref name, and %FALSE otherwise. Both @ref1 and @ref2 must be non-%NULL. |
130 | | * |
131 | | * Returns: %TRUE if @ref1 and @ref2 are equal, %FALSE otherwise |
132 | | * Since: 2018.6 |
133 | | */ |
134 | | gboolean |
135 | | ostree_collection_ref_equal (gconstpointer ref1, gconstpointer ref2) |
136 | 0 | { |
137 | 0 | const OstreeCollectionRef *_ref1 = ref1, *_ref2 = ref2; |
138 | |
|
139 | 0 | return (g_strcmp0 (_ref1->collection_id, _ref2->collection_id) == 0 |
140 | 0 | && g_strcmp0 (_ref1->ref_name, _ref2->ref_name) == 0); |
141 | 0 | } |
142 | | |
143 | | /** |
144 | | * ostree_collection_ref_dupv: |
145 | | * @refs: (array zero-terminated=1): %NULL-terminated array of #OstreeCollectionRefs |
146 | | * |
147 | | * Copy an array of #OstreeCollectionRefs, including deep copies of all its |
148 | | * elements. @refs must be %NULL-terminated; it may be empty, but must not be |
149 | | * %NULL. |
150 | | * |
151 | | * Returns: (transfer full) (array zero-terminated=1): a newly allocated copy of @refs |
152 | | * Since: 2018.6 |
153 | | */ |
154 | | OstreeCollectionRef ** |
155 | | ostree_collection_ref_dupv (const OstreeCollectionRef *const *refs) |
156 | 0 | { |
157 | 0 | gsize i, n_refs = g_strv_length ((gchar **)refs); /* hack */ |
158 | 0 | g_auto (OstreeCollectionRefv) new_refs = NULL; |
159 | |
|
160 | 0 | g_return_val_if_fail (refs != NULL, NULL); |
161 | | |
162 | 0 | new_refs = g_new0 (OstreeCollectionRef *, n_refs + 1); |
163 | |
|
164 | 0 | for (i = 0; i < n_refs; i++) |
165 | 0 | new_refs[i] = ostree_collection_ref_dup (refs[i]); |
166 | 0 | new_refs[i] = NULL; |
167 | |
|
168 | 0 | return g_steal_pointer (&new_refs); |
169 | 0 | } |
170 | | |
171 | | /** |
172 | | * ostree_collection_ref_freev: |
173 | | * @refs: (transfer full) (array zero-terminated=1): an array of #OstreeCollectionRefs |
174 | | * |
175 | | * Free the given array of @refs, including freeing all its elements. @refs |
176 | | * must be %NULL-terminated; it may be empty, but must not be %NULL. |
177 | | * |
178 | | * Since: 2018.6 |
179 | | */ |
180 | | void |
181 | | ostree_collection_ref_freev (OstreeCollectionRef **refs) |
182 | 0 | { |
183 | 0 | gsize i; |
184 | |
|
185 | 0 | g_return_if_fail (refs != NULL); |
186 | | |
187 | 0 | for (i = 0; refs[i] != NULL; i++) |
188 | 0 | ostree_collection_ref_free (refs[i]); |
189 | 0 | g_free (refs); |
190 | 0 | } |