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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2016-2024 Matthias Klumpp <matthias@tenstral.net>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* 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.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/>.
*/
/**
* SECTION:asc-unit
* @short_description: A data source unit (package, bundle, database, ...) for #AscCompose to process
* @include: appstream-compose.h
*/
#include "config.h"
#include "asc-unit.h"
#include "as-utils-private.h"
typedef struct {
AsBundleKind bundle_kind;
gchar *bundle_id;
gchar *bundle_id_safe;
GPtrArray *contents;
GPtrArray *relevant_paths;
gpointer user_data;
} AscUnitPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (AscUnit, asc_unit, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (asc_unit_get_instance_private (o))
static void
asc_unit_init (AscUnit *unit)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
priv->bundle_kind = AS_BUNDLE_KIND_UNKNOWN;
priv->contents = g_ptr_array_new_with_free_func (g_free);
priv->relevant_paths = g_ptr_array_new_with_free_func (g_free);
}
static void
asc_unit_finalize (GObject *object)
{
AscUnit *unit = ASC_UNIT (object);
AscUnitPrivate *priv = GET_PRIVATE (unit);
g_free (priv->bundle_id);
g_free (priv->bundle_id_safe);
g_ptr_array_unref (priv->contents);
g_ptr_array_unref (priv->relevant_paths);
G_OBJECT_CLASS (asc_unit_parent_class)->finalize (object);
}
static void
asc_unit_class_init (AscUnitClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = asc_unit_finalize;
}
/**
* asc_unit_get_bundle_kind:
* @unit: an #AscUnit instance.
*
* Gets the bundle kind of this unit.
**/
AsBundleKind
asc_unit_get_bundle_kind (AscUnit *unit)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
return priv->bundle_kind;
}
/**
* asc_unit_set_bundle_kind:
* @unit: an #AscUnit instance.
*
* Sets the kind of the bundle this unit represents.
**/
void
asc_unit_set_bundle_kind (AscUnit *unit, AsBundleKind kind)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
priv->bundle_kind = kind;
}
/**
* asc_unit_get_bundle_id:
* @unit: an #AscUnit instance.
*
* Gets the ID name of the bundle (a package / Flatpak / any entity containing metadata)
* that this unit represents.
**/
const gchar *
asc_unit_get_bundle_id (AscUnit *unit)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
return priv->bundle_id;
}
/**
* asc_unit_get_bundle_id_safe:
* @unit: an #AscUnit instance.
*
* Gets the ID name of the bundle, normalized to be safe to use
* in filenames. This may *not* be the same name as set via asc_unit_get_bundle_id()
**/
const gchar *
asc_unit_get_bundle_id_safe (AscUnit *unit)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
return priv->bundle_id_safe;
}
/**
* asc_unit_set_bundle_id:
* @unit: an #AscUnit instance.
* @id: The new ID.
*
* Sets the ID of the bundle represented by this unit.
**/
void
asc_unit_set_bundle_id (AscUnit *unit, const gchar *id)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
GString *tmp;
as_assign_string_safe (priv->bundle_id, id);
tmp = g_string_new (priv->bundle_id);
if (g_strcmp0 (tmp->str, "/") == 0) {
g_string_truncate (tmp, 0);
g_string_append (tmp, "root");
} else {
as_gstring_replace (tmp, "/", "-", 0);
as_gstring_replace (tmp, "\\", "-", 0);
as_gstring_replace (tmp, ":", "_", 0);
if (g_str_has_prefix (tmp->str, "-") || g_str_has_prefix (tmp->str, "."))
g_string_erase (tmp, 0, 1);
if (g_strcmp0 (tmp->str, "") == 0)
g_string_append (tmp, "BADNAME");
}
g_free (priv->bundle_id_safe);
priv->bundle_id_safe = g_string_free (tmp, FALSE);
}
/**
* asc_unit_get_contents:
* @unit: an #AscUnit instance.
*
* Get a list of all files contained by this unit.
*
* Returns: (transfer none) (element-type utf8) : A file listing
**/
GPtrArray *
asc_unit_get_contents (AscUnit *unit)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
return priv->contents;
}
/**
* asc_unit_set_contents:
* @unit: an #AscUnit instance.
* @contents: (element-type utf8): A list of files contained by this unit.
*
* Set list of files this unit contains.
**/
void
asc_unit_set_contents (AscUnit *unit, GPtrArray *contents)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
if (priv->contents == contents)
return;
g_ptr_array_unref (priv->contents);
priv->contents = g_ptr_array_ref (contents);
}
/**
* asc_unit_get_relevant_paths:
* @unit: an #AscUnit instance.
*
* Get a list of paths that are relevant for data processing.
*
* Returns: (transfer none) (element-type utf8) : A list of paths
**/
GPtrArray *
asc_unit_get_relevant_paths (AscUnit *unit)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
return priv->relevant_paths;
}
/**
* asc_unit_add_relevant_path:
* @unit: an #AscUnit instance.
* @path: path to be considered relevant
*
* Add a path to the list of relevant directories.
* A unit may only read data in paths that were previously
* registered as relevant.
**/
void
asc_unit_add_relevant_path (AscUnit *unit, const gchar *path)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
/* duplicate check */
for (guint i = 0; i < priv->relevant_paths->len; i++) {
if (g_strcmp0 (g_ptr_array_index (priv->relevant_paths, i), path) == 0)
return;
}
g_ptr_array_add (priv->relevant_paths, g_strdup (path));
}
/**
* asc_unit_open:
* @unit: an #AscUnit instance.
* @error: A #GError
*
* Open this unit, populating its content listing.
**/
gboolean
asc_unit_open (AscUnit *unit, GError **error)
{
AscUnitClass *klass;
g_return_val_if_fail (ASC_IS_UNIT (unit), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
klass = ASC_UNIT_GET_CLASS (unit);
g_return_val_if_fail (klass->open != NULL, FALSE);
return klass->open (unit, error);
}
/**
* asc_unit_close:
* @unit: an #AscUnit instance.
*
* Close this unit, possibly freeing its resources. Calls to read_data() or
* get_contents() may not produce results until open() is called again.
**/
void
asc_unit_close (AscUnit *unit)
{
AscUnitClass *klass;
g_return_if_fail (ASC_IS_UNIT (unit));
klass = ASC_UNIT_GET_CLASS (unit);
g_return_if_fail (klass->close != NULL);
klass->close (unit);
}
/**
* asc_unit_file_exists:
* @unit: an #AscUnit instance.
* @filename: The filename to check.
*
* Returns %TRUE if the filename exists and is readable using %asc_unit_read_data.
**/
gboolean
asc_unit_file_exists (AscUnit *unit, const gchar *filename)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
AscUnitClass *klass;
g_return_val_if_fail (ASC_IS_UNIT (unit), FALSE);
klass = ASC_UNIT_GET_CLASS (unit);
if (klass->file_exists == NULL && priv->contents != NULL) {
/* fallback */
for (guint i = 0; i < priv->contents->len; i++) {
if (g_strcmp0 (filename, g_ptr_array_index (priv->contents, i)) == 0)
return TRUE;
}
return FALSE;
}
g_return_val_if_fail (klass->file_exists != NULL, FALSE);
return klass->file_exists (unit, filename);
}
/**
* asc_unit_dir_exists:
* @unit: an #AscUnit instance.
* @dirname: The directory name to check.
*
* Returns %TRUE if the directory exists and files in it are readable.
**/
gboolean
asc_unit_dir_exists (AscUnit *unit, const gchar *dirname)
{
AscUnitClass *klass;
g_return_val_if_fail (ASC_IS_UNIT (unit), FALSE);
klass = ASC_UNIT_GET_CLASS (unit);
g_return_val_if_fail (klass->dir_exists != NULL, FALSE);
return klass->dir_exists (unit, dirname);
}
/**
* asc_unit_read_data:
* @unit: an #AscUnit instance.
* @filename: The file to read data for.
* @error: A #GError
*
* Read the contents of the selected file into memory and return them.
**/
GBytes *
asc_unit_read_data (AscUnit *unit, const gchar *filename, GError **error)
{
AscUnitClass *klass;
g_return_val_if_fail (ASC_IS_UNIT (unit), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
klass = ASC_UNIT_GET_CLASS (unit);
g_return_val_if_fail (klass->read_data != NULL, FALSE);
return klass->read_data (unit, filename, error);
}
/**
* asc_unit_get_user_data:
* @unit: an #AscUnit instance.
*
* Get user-defined data. This is a helper
* function for bindings.
**/
gpointer
asc_unit_get_user_data (AscUnit *unit)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
return priv->user_data;
}
/**
* asc_unit_set_user_data:
* @unit: an #AscUnit instance.
* @user_data: the user data
*
* Assign user-defined data to this object. This is a helper
* function for bindings.
**/
void
asc_unit_set_user_data (AscUnit *unit, gpointer user_data)
{
AscUnitPrivate *priv = GET_PRIVATE (unit);
priv->user_data = user_data;
}
/**
* asc_unit_new:
*
* Creates a new #AscUnit.
**/
AscUnit *
asc_unit_new (void)
{
AscUnit *unit;
unit = g_object_new (ASC_TYPE_UNIT, NULL);
return ASC_UNIT (unit);
}
|