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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
/* vi:set et sw=2 sts=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e-s:
* 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.1 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-private.h"
#include "flatpak-ref.h"
#include "flatpak-ref-utils-private.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.
*
* Note that the identifiers must be unique within a repo (e.g. Flathub) based
* only on the name, arch, and branch 3-tuple, without regard to the kind. In
* other words if app/org.gnome.evince/x86_64/master exists,
* runtime/org.gnome.evince/x86_64/master must not exist. This requirement is
* not enforced by libflatpak but is enforced by GNOME Software's use of
* libappstream, since Appstream IDs are assumed to be unique.
*
* FlatpakRef objects are immutable and can be passed freely between threads.
*
* 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;
char *collection_id;
char *cached_full_ref;
};
G_DEFINE_TYPE_WITH_PRIVATE (FlatpakRef, flatpak_ref, G_TYPE_OBJECT)
enum {
PROP_0,
PROP_NAME,
PROP_ARCH,
PROP_BRANCH,
PROP_COMMIT,
PROP_KIND,
PROP_COLLECTION_ID,
};
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_free (priv->collection_id);
g_free ((char *)g_atomic_pointer_get (&priv->cached_full_ref));
G_OBJECT_CLASS (flatpak_ref_parent_class)->finalize (object);
}
/* These support setting e.g. the arch from referencing a ref.
* i.e. it would get "x86_64/master" as an argument. */
static char *
value_dup_ref_part (const GValue *value)
{
const char *part = value->data[0].v_pointer;
const char *slash;
slash = strchr (part, '/');
if (slash)
return g_strndup (part, slash - part);
return g_strdup (part);
}
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_assert (priv->name == NULL); /* Construct-only */
priv->name = value_dup_ref_part (value);
break;
case PROP_ARCH:
g_assert (priv->arch == NULL); /* Construct-only */
priv->arch = value_dup_ref_part (value);
break;
case PROP_BRANCH:
g_assert (priv->branch == NULL); /* Construct-only */
priv->branch = value_dup_ref_part (value);
break;
case PROP_COMMIT:
g_assert (priv->commit == NULL); /* Construct-only */
priv->commit = g_value_dup_string (value);
break;
case PROP_KIND:
priv->kind = g_value_get_enum (value);
break;
case PROP_COLLECTION_ID:
g_assert (priv->collection_id == NULL); /* Construct-only */
priv->collection_id = g_value_dup_string (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;
case PROP_COLLECTION_ID:
g_value_set_string (value, priv->collection_id);
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_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
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_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
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_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class,
PROP_COMMIT,
g_param_spec_string ("commit",
"Commit",
"The commit",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
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 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class,
PROP_COLLECTION_ID,
g_param_spec_string ("collection-id",
"Collection ID",
"The collection ID",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}
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_format_ref_cached:
* @self: a #FlatpakRef
*
* Like flatpak_ref_format_ref() but this returns the same string each time
* it's called rather than allocating a new one.
*
* Returns: (transfer none): string representation
*
* Since: 1.9.1
*/
const char *
flatpak_ref_format_ref_cached (FlatpakRef *self)
{
FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);
const char *full_ref;
char *full_ref_new;
full_ref = (const char *)g_atomic_pointer_get (&priv->cached_full_ref);
if (full_ref == NULL)
{
full_ref_new = flatpak_ref_format_ref (self);
if (!g_atomic_pointer_compare_and_exchange ((void**) &priv->cached_full_ref, NULL, full_ref_new))
g_free (full_ref_new); /* Raced with someone, free our version */
full_ref = (const char *)g_atomic_pointer_get (&priv->cached_full_ref); /* Now guaranteed to be non-NULL */
}
return full_ref;
}
/**
* flatpak_ref_parse:
* @ref: A string ref name, such as "app/org.test.App/x86_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_autoptr(FlatpakDecomposed) decomposed = NULL;
decomposed = flatpak_decomposed_new_from_ref (ref, error);
if (decomposed == NULL)
return NULL;
return FLATPAK_REF (g_object_new (FLATPAK_TYPE_REF,
"kind", flatpak_decomposed_get_kind (decomposed),
"name", flatpak_decomposed_peek_id (decomposed, NULL),
"arch", flatpak_decomposed_peek_arch (decomposed, NULL),
"branch", flatpak_decomposed_peek_branch (decomposed, NULL),
NULL));
}
/**
* flatpak_ref_get_collection_id:
* @self: a #FlatpakRef
*
* Gets the collection ID of the ref.
*
* Returns: (transfer none): the collection ID
*/
const char *
flatpak_ref_get_collection_id (FlatpakRef *self)
{
FlatpakRefPrivate *priv = flatpak_ref_get_instance_private (self);
return priv->collection_id;
}
|