| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2007-2009 Richard Hughes <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the GNU Lesser General Public License Version 2.1 |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* SECTION:pk-package-id |
| 24 |
* @title: PackageIDs |
| 25 |
* @short_description: Functions for process PackageIDs |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "config.h" |
| 29 |
|
| 30 |
#include <glib.h> |
| 31 |
|
| 32 |
#include <packagekit-glib2/pk-package-id.h> |
| 33 |
|
| 34 |
/** |
| 35 |
* pk_package_id_split: |
| 36 |
* @package_id: the ; delimited PackageID to split |
| 37 |
* |
| 38 |
* Splits a PackageID into the correct number of parts, checking the correct |
| 39 |
* number of delimiters are present. |
| 40 |
* |
| 41 |
* Return value: (transfer full): a GStrv or %NULL if invalid, use g_strfreev() to free |
| 42 |
* |
| 43 |
* Since: 0.5.3 |
| 44 |
**/ |
| 45 |
gchar ** |
| 46 |
pk_package_id_split (const gchar *package_id) |
| 47 |
{ |
| 48 |
gchar **sections = NULL; |
| 49 |
|
| 50 |
if (package_id == NULL) |
| 51 |
goto out; |
| 52 |
|
| 53 |
/* split by delimeter ';' */ |
| 54 |
sections = g_strsplit (package_id, ";", -1); |
| 55 |
if (g_strv_length (sections) != 4) |
| 56 |
goto out; |
| 57 |
|
| 58 |
/* name has to be valid */ |
| 59 |
if (sections[0][0] != '\0') |
| 60 |
return sections; |
| 61 |
out: |
| 62 |
g_strfreev (sections); |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* pk_package_id_check: |
| 68 |
* @package_id: the PackageID to check |
| 69 |
* |
| 70 |
* Check if a Packageid is well formed. |
| 71 |
* |
| 72 |
* Return value: %TRUE if the PackageID was well formed. |
| 73 |
* |
| 74 |
* Since: 0.5.0 |
| 75 |
**/ |
| 76 |
gboolean |
| 77 |
pk_package_id_check (const gchar *package_id) |
| 78 |
{ |
| 79 |
g_auto(GStrv) sections = NULL; |
| 80 |
gboolean ret; |
| 81 |
|
| 82 |
/* NULL check */ |
| 83 |
if (package_id == NULL) |
| 84 |
return FALSE; |
| 85 |
|
| 86 |
/* UTF8 */ |
| 87 |
ret = g_utf8_validate (package_id, -1, NULL); |
| 88 |
if (!ret) |
| 89 |
return FALSE; |
| 90 |
|
| 91 |
/* correct number of sections */ |
| 92 |
sections = pk_package_id_split (package_id); |
| 93 |
if (sections == NULL) |
| 94 |
return FALSE; |
| 95 |
return TRUE; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* pk_package_id_build: |
| 100 |
* @name: the package name |
| 101 |
* @version: the package version |
| 102 |
* @arch: the package architecture |
| 103 |
* @data: the package extra data |
| 104 |
* |
| 105 |
* Generate a PackageID. |
| 106 |
* |
| 107 |
* Return value: returns a string to form the PackageID. |
| 108 |
* |
| 109 |
* Since: 0.5.0 |
| 110 |
**/ |
| 111 |
gchar * |
| 112 |
pk_package_id_build (const gchar *name, const gchar *version, |
| 113 |
const gchar *arch, const gchar *data) |
| 114 |
{ |
| 115 |
g_return_val_if_fail (name != NULL, NULL); |
| 116 |
return g_strjoin (";", |
| 117 |
name, |
| 118 |
version != NULL ? version : "", |
| 119 |
arch != NULL ? arch : "", |
| 120 |
data != NULL ? data : "", |
| 121 |
NULL); |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* pk_arch_base_ix86: |
| 126 |
**/ |
| 127 |
static gboolean |
| 128 |
pk_arch_base_ix86 (const gchar *arch) |
| 129 |
{ |
| 130 |
if (g_strcmp0 (arch, "i386") == 0 || |
| 131 |
g_strcmp0 (arch, "i486") == 0 || |
| 132 |
g_strcmp0 (arch, "i586") == 0 || |
| 133 |
g_strcmp0 (arch, "i686") == 0) |
| 134 |
return TRUE; |
| 135 |
return FALSE; |
| 136 |
} |
| 137 |
|
| 138 |
/* |
| 139 |
* pk_package_id_equal_fuzzy_arch_section: |
| 140 |
**/ |
| 141 |
static gboolean |
| 142 |
pk_package_id_equal_fuzzy_arch_section (const gchar *arch1, const gchar *arch2) |
| 143 |
{ |
| 144 |
if (g_strcmp0 (arch1, arch2) == 0) |
| 145 |
return TRUE; |
| 146 |
if (pk_arch_base_ix86 (arch1) && pk_arch_base_ix86 (arch2)) |
| 147 |
return TRUE; |
| 148 |
return FALSE; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* pk_package_id_equal_fuzzy_arch: |
| 153 |
* @package_id1: the first PackageID |
| 154 |
* @package_id2: the second PackageID |
| 155 |
* |
| 156 |
* Only compare the name, version, and arch, where the architecture will fuzzy |
| 157 |
* match with i*86. |
| 158 |
* |
| 159 |
* Return value: %TRUE if the PackageIDs can be considered equal. |
| 160 |
* |
| 161 |
* Since: 0.5.0 |
| 162 |
**/ |
| 163 |
gboolean |
| 164 |
pk_package_id_equal_fuzzy_arch (const gchar *package_id1, const gchar *package_id2) |
| 165 |
{ |
| 166 |
g_auto(GStrv) sections1 = NULL; |
| 167 |
g_auto(GStrv) sections2 = NULL; |
| 168 |
|
| 169 |
sections1 = pk_package_id_split (package_id1); |
| 170 |
sections2 = pk_package_id_split (package_id2); |
| 171 |
if (g_strcmp0 (sections1[0], sections2[0]) == 0 && |
| 172 |
g_strcmp0 (sections1[1], sections2[1]) == 0 && |
| 173 |
pk_package_id_equal_fuzzy_arch_section (sections1[2], sections2[2])) |
| 174 |
return TRUE; |
| 175 |
return FALSE; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* pk_package_id_to_printable: |
| 180 |
* @package_id: the PackageID |
| 181 |
* |
| 182 |
* Formats the PackageID to be printable to the user. |
| 183 |
* |
| 184 |
* Return value: the name-version.arch formatted string, use g_free() to free. |
| 185 |
* |
| 186 |
* Since: 0.5.2 |
| 187 |
**/ |
| 188 |
gchar * |
| 189 |
pk_package_id_to_printable (const gchar *package_id) |
| 190 |
{ |
| 191 |
g_auto(GStrv) parts = NULL; |
| 192 |
gchar *value = NULL; |
| 193 |
GString *string = NULL; |
| 194 |
|
| 195 |
/* invalid */ |
| 196 |
if (package_id == NULL) |
| 197 |
goto out; |
| 198 |
|
| 199 |
/* split */ |
| 200 |
parts = pk_package_id_split (package_id); |
| 201 |
if (parts == NULL) |
| 202 |
goto out; |
| 203 |
|
| 204 |
/* name */ |
| 205 |
string = g_string_new (parts[PK_PACKAGE_ID_NAME]); |
| 206 |
|
| 207 |
/* version if present */ |
| 208 |
if (parts[PK_PACKAGE_ID_VERSION][0] != '\0') |
| 209 |
g_string_append_printf (string, "_%s", parts[PK_PACKAGE_ID_VERSION]); |
| 210 |
|
| 211 |
/* arch if present */ |
| 212 |
if (parts[PK_PACKAGE_ID_ARCH][0] != '\0') |
| 213 |
g_string_append_printf (string, ".%s", parts[PK_PACKAGE_ID_ARCH]); |
| 214 |
out: |
| 215 |
if (string != NULL) |
| 216 |
value = g_string_free (string, FALSE); |
| 217 |
return value; |
| 218 |
} |