| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2007-2008 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-common |
| 24 |
* @title: Common functions |
| 25 |
* @short_description: Common utility functions |
| 26 |
* |
| 27 |
* This file contains functions that may be useful. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include "config.h" |
| 31 |
|
| 32 |
#include <stdlib.h> |
| 33 |
#include <stdio.h> |
| 34 |
|
| 35 |
#include <string.h> |
| 36 |
#include <sys/utsname.h> |
| 37 |
|
| 38 |
#include <glib.h> |
| 39 |
#include <packagekit-glib2/pk-common.h> |
| 40 |
#include <packagekit-glib2/pk-common-private.h> |
| 41 |
#include <packagekit-glib2/pk-enum.h> |
| 42 |
|
| 43 |
/** |
| 44 |
* pk_iso8601_present: |
| 45 |
* |
| 46 |
* Get the current date / time as an ISO8601 string. |
| 47 |
* |
| 48 |
* Return value: The current iso8601 date and time |
| 49 |
* |
| 50 |
* Since: 0.5.2 |
| 51 |
**/ |
| 52 |
gchar * |
| 53 |
pk_iso8601_present (void) |
| 54 |
{ |
| 55 |
GDateTime *datetime; |
| 56 |
gchar *timespec; |
| 57 |
|
| 58 |
datetime = g_date_time_new_now_local (); |
| 59 |
timespec = g_date_time_format_iso8601 (datetime); |
| 60 |
g_clear_pointer (&datetime, g_date_time_unref); |
| 61 |
|
| 62 |
return timespec; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* pk_iso8601_from_date: |
| 67 |
* @date: a #GDate to convert |
| 68 |
* |
| 69 |
* Convert a date into an ISO8601 data string. |
| 70 |
* |
| 71 |
* Return value: If valid then a new ISO8601 date, else %NULL |
| 72 |
* |
| 73 |
* Since: 0.5.2 |
| 74 |
**/ |
| 75 |
gchar * |
| 76 |
pk_iso8601_from_date (const GDate *date) |
| 77 |
{ |
| 78 |
gsize retval; |
| 79 |
gchar iso_date[128]; |
| 80 |
|
| 81 |
if (date == NULL) |
| 82 |
return NULL; |
| 83 |
retval = g_date_strftime (iso_date, 128, "%F", date); |
| 84 |
if (retval == 0) |
| 85 |
return NULL; |
| 86 |
return g_strdup (iso_date); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* pk_iso8601_to_date: (skip) |
| 91 |
* @iso_date: The ISO8601 date to convert |
| 92 |
* |
| 93 |
* Convert an ISO8601 date string to a #GDate. |
| 94 |
* |
| 95 |
* Return value: If valid then a new %GDate, else NULL |
| 96 |
* |
| 97 |
* Since: 0.5.2 |
| 98 |
**/ |
| 99 |
GDate * |
| 100 |
pk_iso8601_to_date (const gchar *iso_date) |
| 101 |
{ |
| 102 |
guint retval; |
| 103 |
GDateTime *datetime = NULL; |
| 104 |
GDate *date = NULL; |
| 105 |
|
| 106 |
if (iso_date == NULL || iso_date[0] == '\0') |
| 107 |
goto out; |
| 108 |
|
| 109 |
/* try to parse complete ISO8601 date */ |
| 110 |
if (g_strstr_len (iso_date, -1, " ") != NULL) |
| 111 |
datetime = g_date_time_new_from_iso8601 (iso_date, NULL); |
| 112 |
|
| 113 |
if (datetime) { |
| 114 |
gint d = 0, m = 0, y = 0; |
| 115 |
g_debug ("Parsed %s %" G_GINT64_FORMAT, iso_date, g_date_time_to_unix (datetime)); |
| 116 |
g_date_time_get_ymd (datetime, &y, &m, &d); |
| 117 |
g_clear_pointer (&datetime, g_date_time_unref); |
| 118 |
date = g_date_new_dmy (d, m, y); |
| 119 |
goto out; |
| 120 |
} else { |
| 121 |
guint d = 0, m = 0, y = 0; |
| 122 |
|
| 123 |
/* g_date_time_new_from_iso8601() blows goats and won't |
| 124 |
* accept a valid ISO8601 formatted date without a |
| 125 |
* time value - try and parse this case */ |
| 126 |
retval = sscanf (iso_date, "%u-%u-%u", &y, &m, &d); |
| 127 |
if (retval != 3) |
| 128 |
goto out; |
| 129 |
|
| 130 |
/* check it's valid */ |
| 131 |
if (!g_date_valid_dmy (d, m, y)) |
| 132 |
goto out; |
| 133 |
|
| 134 |
/* create valid object */ |
| 135 |
date = g_date_new_dmy (d, m, y); |
| 136 |
} |
| 137 |
|
| 138 |
out: |
| 139 |
return date; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* pk_iso8601_to_datetime: (skip) |
| 144 |
* @iso_date: The ISO8601 date to convert |
| 145 |
* |
| 146 |
* Convert an ISO8601 date string to a #GDateTime. |
| 147 |
* |
| 148 |
* Return value: If valid then a new %GDateTime, else NULL |
| 149 |
* |
| 150 |
* Since: 0.8.11 |
| 151 |
**/ |
| 152 |
GDateTime * |
| 153 |
pk_iso8601_to_datetime (const gchar *iso_date) |
| 154 |
{ |
| 155 |
guint retval; |
| 156 |
guint d = 0; |
| 157 |
guint m = 0; |
| 158 |
guint y = 0; |
| 159 |
GDateTime *date = NULL; |
| 160 |
|
| 161 |
if (iso_date == NULL || iso_date[0] == '\0') |
| 162 |
goto out; |
| 163 |
|
| 164 |
/* try to parse complete ISO8601 date */ |
| 165 |
if (g_strstr_len (iso_date, -1, " ") != NULL) |
| 166 |
date = g_date_time_new_from_iso8601 (iso_date, NULL); |
| 167 |
|
| 168 |
if (date) { |
| 169 |
g_debug ("Parsed %s %" G_GINT64_FORMAT, iso_date, g_date_time_to_unix (date)); |
| 170 |
goto out; |
| 171 |
} |
| 172 |
|
| 173 |
/* g_time_val_from_iso8601() blows goats and won't |
| 174 |
* accept a valid ISO8601 formatted date without a |
| 175 |
* time value - try and parse this case */ |
| 176 |
retval = sscanf (iso_date, "%u-%u-%u", &y, &m, &d); |
| 177 |
if (retval != 3) |
| 178 |
goto out; |
| 179 |
|
| 180 |
/* create valid object */ |
| 181 |
date = g_date_time_new_utc (y, m, d, 0, 0, 0); |
| 182 |
out: |
| 183 |
return date; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* pk_ptr_array_to_strv: |
| 188 |
* @array: (element-type utf8): the GPtrArray of strings |
| 189 |
* |
| 190 |
* Form a composite string array of strings. |
| 191 |
* The data in the GPtrArray is copied. |
| 192 |
* |
| 193 |
* Return value: (transfer full) (array zero-terminated=1): the string array, or %NULL if invalid |
| 194 |
* |
| 195 |
* Since: 0.5.2 |
| 196 |
**/ |
| 197 |
gchar ** |
| 198 |
pk_ptr_array_to_strv (GPtrArray *array) |
| 199 |
{ |
| 200 |
gchar **value; |
| 201 |
const gchar *value_temp; |
| 202 |
guint i; |
| 203 |
|
| 204 |
g_return_val_if_fail (array != NULL, NULL); |
| 205 |
|
| 206 |
/* copy the array to a strv */ |
| 207 |
value = g_new0 (gchar *, array->len + 1); |
| 208 |
for (i = 0; i < array->len; i++) { |
| 209 |
value_temp = (const gchar *) g_ptr_array_index (array, i); |
| 210 |
value[i] = g_strdup (value_temp); |
| 211 |
} |
| 212 |
|
| 213 |
return value; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* pk_get_machine_type: |
| 218 |
* |
| 219 |
* Get the machine type for the current host, e.g. "i386" |
| 220 |
* Note: Don't use this function if you can get this data from /etc/foo |
| 221 |
* |
| 222 |
* Return value: a machine type |
| 223 |
**/ |
| 224 |
static gchar * |
| 225 |
pk_get_distro_id_machine_type (void) |
| 226 |
{ |
| 227 |
gint retval; |
| 228 |
struct utsname buf; |
| 229 |
|
| 230 |
retval = uname (&buf); |
| 231 |
if (retval != 0) |
| 232 |
return g_strdup ("unknown"); |
| 233 |
return g_strdup (buf.machine); |
| 234 |
} |
| 235 |
|
| 236 |
/* |
| 237 |
* pk_parse_os_release: |
| 238 |
* |
| 239 |
* Internal helper to parse os-release |
| 240 |
**/ |
| 241 |
static gboolean |
| 242 |
pk_parse_os_release (gchar **id, gchar **name, gchar **version_id, GError **error) |
| 243 |
{ |
| 244 |
const gchar *filename = "/etc/os-release"; |
| 245 |
gboolean ret; |
| 246 |
g_autofree gchar *contents = NULL; |
| 247 |
g_autoptr(GKeyFile) key_file = NULL; |
| 248 |
g_autoptr(GString) str = NULL; |
| 249 |
|
| 250 |
/* load data */ |
| 251 |
if (!g_file_test (filename, G_FILE_TEST_EXISTS)) |
| 252 |
filename = "/usr/lib/os-release"; |
| 253 |
if (!g_file_get_contents (filename, &contents, NULL, error)) |
| 254 |
return FALSE; |
| 255 |
|
| 256 |
/* make a valid GKeyFile from the .ini data by prepending a header */ |
| 257 |
str = g_string_new (contents); |
| 258 |
g_string_prepend (str, "[os-release]\n"); |
| 259 |
key_file = g_key_file_new (); |
| 260 |
ret = g_key_file_load_from_data (key_file, str->str, -1, G_KEY_FILE_NONE, error); |
| 261 |
if (!ret) { |
| 262 |
return FALSE; |
| 263 |
} |
| 264 |
|
| 265 |
/* get keys */ |
| 266 |
if (id != NULL) { |
| 267 |
g_autofree gchar *tmp = g_key_file_get_string (key_file, "os-release", "ID", error); |
| 268 |
if (tmp == NULL) |
| 269 |
return FALSE; |
| 270 |
*id = g_shell_unquote (tmp, error); |
| 271 |
if (*id == NULL) |
| 272 |
return FALSE; |
| 273 |
} |
| 274 |
if (name != NULL) { |
| 275 |
g_autofree gchar *tmp = g_key_file_get_string (key_file, "os-release", "NAME", error); |
| 276 |
if (tmp == NULL) |
| 277 |
return FALSE; |
| 278 |
*name = g_shell_unquote (tmp, error); |
| 279 |
if (*name == NULL) |
| 280 |
return FALSE; |
| 281 |
} |
| 282 |
if (version_id != NULL) { |
| 283 |
g_autofree gchar *tmp = g_key_file_get_string (key_file, "os-release", "VERSION_ID", error); |
| 284 |
if (tmp == NULL) |
| 285 |
return FALSE; |
| 286 |
*version_id = g_shell_unquote (tmp, error); |
| 287 |
if (*version_id == NULL) |
| 288 |
return FALSE; |
| 289 |
} |
| 290 |
return TRUE; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* pk_get_distro_id: |
| 295 |
* |
| 296 |
* Get the distribution ID for the current host, typically "distro;version;arch" |
| 297 |
* |
| 298 |
* Return value: a distribution ID |
| 299 |
**/ |
| 300 |
gchar * |
| 301 |
pk_get_distro_id (void) |
| 302 |
{ |
| 303 |
gboolean ret; |
| 304 |
g_autoptr(GError) error = NULL; |
| 305 |
g_autofree gchar *arch = NULL; |
| 306 |
g_autofree gchar *id = NULL; |
| 307 |
g_autofree gchar *version = NULL; |
| 308 |
|
| 309 |
/* we don't want distro specific results in 'make check' */ |
| 310 |
if (g_getenv ("PK_SELF_TEST") != NULL) |
| 311 |
return g_strdup ("selftest;11.91;i686"); |
| 312 |
|
| 313 |
ret = pk_parse_os_release (&id, NULL, &version, &error); |
| 314 |
if (!ret) { |
| 315 |
g_warning ("failed to load os-release: %s", error->message); |
| 316 |
return NULL; |
| 317 |
} |
| 318 |
|
| 319 |
arch = pk_get_distro_id_machine_type (); |
| 320 |
return g_strdup_printf ("%s;%s;%s", id, version, arch); |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* pk_get_distro_name: |
| 325 |
* @error: the #GError to store any failure, or %NULL |
| 326 |
* |
| 327 |
* Get the distribution name for this host as specified by NAME in /etc/os-release, e.g. "Fedora", |
| 328 |
* |
| 329 |
* Return value: a distro name |
| 330 |
**/ |
| 331 |
gchar * |
| 332 |
pk_get_distro_name (GError **error) |
| 333 |
{ |
| 334 |
gboolean ret; |
| 335 |
gchar *name = NULL; |
| 336 |
|
| 337 |
ret = pk_parse_os_release (NULL, &name, NULL, error); |
| 338 |
if (!ret) |
| 339 |
return NULL; |
| 340 |
|
| 341 |
return name; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* pk_get_distro_version_id: |
| 346 |
* @error: the #GError to store any failure, or %NULL |
| 347 |
* |
| 348 |
* Get the distribution version ID as specified by VERSION_ID in /etc/os-release, e.g. "23" |
| 349 |
* |
| 350 |
* Return value: a distribution version |
| 351 |
**/ |
| 352 |
gchar * |
| 353 |
pk_get_distro_version_id (GError **error) |
| 354 |
{ |
| 355 |
gboolean ret; |
| 356 |
gchar *version_id = NULL; |
| 357 |
|
| 358 |
ret = pk_parse_os_release (NULL, NULL, &version_id, error); |
| 359 |
if (!ret) |
| 360 |
return NULL; |
| 361 |
|
| 362 |
return version_id; |
| 363 |
} |