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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* SECTION:pk-common
* @title: Common functions
* @short_description: Common utility functions
*
* This file contains functions that may be useful.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>
#include <glib.h>
#include <packagekit-glib2/pk-common.h>
#include <packagekit-glib2/pk-common-private.h>
#include <packagekit-glib2/pk-enum.h>
/**
* pk_iso8601_present:
*
* Get the current date / time as an ISO8601 string.
*
* Return value: The current iso8601 date and time
*
* Since: 0.5.2
**/
gchar *
pk_iso8601_present (void)
{
GDateTime *datetime;
gchar *timespec;
datetime = g_date_time_new_now_local ();
timespec = g_date_time_format_iso8601 (datetime);
g_clear_pointer (&datetime, g_date_time_unref);
return timespec;
}
/**
* pk_iso8601_from_date:
* @date: a #GDate to convert
*
* Convert a date into an ISO8601 data string.
*
* Return value: If valid then a new ISO8601 date, else %NULL
*
* Since: 0.5.2
**/
gchar *
pk_iso8601_from_date (const GDate *date)
{
gsize retval;
gchar iso_date[128];
if (date == NULL)
return NULL;
retval = g_date_strftime (iso_date, 128, "%F", date);
if (retval == 0)
return NULL;
return g_strdup (iso_date);
}
/**
* pk_iso8601_to_date: (skip)
* @iso_date: The ISO8601 date to convert
*
* Convert an ISO8601 date string to a #GDate.
*
* Return value: If valid then a new %GDate, else NULL
*
* Since: 0.5.2
**/
GDate *
pk_iso8601_to_date (const gchar *iso_date)
{
guint retval;
GDateTime *datetime = NULL;
GDate *date = NULL;
if (iso_date == NULL || iso_date[0] == '\0')
goto out;
/* try to parse complete ISO8601 date */
if (g_strstr_len (iso_date, -1, " ") != NULL)
datetime = g_date_time_new_from_iso8601 (iso_date, NULL);
if (datetime) {
gint d = 0, m = 0, y = 0;
g_debug ("Parsed %s %" G_GINT64_FORMAT, iso_date, g_date_time_to_unix (datetime));
g_date_time_get_ymd (datetime, &y, &m, &d);
g_clear_pointer (&datetime, g_date_time_unref);
date = g_date_new_dmy (d, m, y);
goto out;
} else {
guint d = 0, m = 0, y = 0;
/* g_date_time_new_from_iso8601() blows goats and won't
* accept a valid ISO8601 formatted date without a
* time value - try and parse this case */
retval = sscanf (iso_date, "%u-%u-%u", &y, &m, &d);
if (retval != 3)
goto out;
/* check it's valid */
if (!g_date_valid_dmy (d, m, y))
goto out;
/* create valid object */
date = g_date_new_dmy (d, m, y);
}
out:
return date;
}
/**
* pk_iso8601_to_datetime: (skip)
* @iso_date: The ISO8601 date to convert
*
* Convert an ISO8601 date string to a #GDateTime.
*
* Return value: If valid then a new %GDateTime, else NULL
*
* Since: 0.8.11
**/
GDateTime *
pk_iso8601_to_datetime (const gchar *iso_date)
{
guint retval;
guint d = 0;
guint m = 0;
guint y = 0;
GDateTime *date = NULL;
if (iso_date == NULL || iso_date[0] == '\0')
goto out;
/* try to parse complete ISO8601 date */
if (g_strstr_len (iso_date, -1, " ") != NULL)
date = g_date_time_new_from_iso8601 (iso_date, NULL);
if (date) {
g_debug ("Parsed %s %" G_GINT64_FORMAT, iso_date, g_date_time_to_unix (date));
goto out;
}
/* g_time_val_from_iso8601() blows goats and won't
* accept a valid ISO8601 formatted date without a
* time value - try and parse this case */
retval = sscanf (iso_date, "%u-%u-%u", &y, &m, &d);
if (retval != 3)
goto out;
/* create valid object */
date = g_date_time_new_utc (y, m, d, 0, 0, 0);
out:
return date;
}
/**
* pk_ptr_array_to_strv:
* @array: (element-type utf8): the GPtrArray of strings
*
* Form a composite string array of strings.
* The data in the GPtrArray is copied.
*
* Return value: (transfer full) (array zero-terminated=1): the string array, or %NULL if invalid
*
* Since: 0.5.2
**/
gchar **
pk_ptr_array_to_strv (GPtrArray *array)
{
gchar **value;
const gchar *value_temp;
guint i;
g_return_val_if_fail (array != NULL, NULL);
/* copy the array to a strv */
value = g_new0 (gchar *, array->len + 1);
for (i = 0; i < array->len; i++) {
value_temp = (const gchar *) g_ptr_array_index (array, i);
value[i] = g_strdup (value_temp);
}
return value;
}
/**
* pk_get_machine_type:
*
* Get the machine type for the current host, e.g. "i386"
* Note: Don't use this function if you can get this data from /etc/foo
*
* Return value: a machine type
**/
static gchar *
pk_get_distro_id_machine_type (void)
{
gint retval;
struct utsname buf;
retval = uname (&buf);
if (retval != 0)
return g_strdup ("unknown");
return g_strdup (buf.machine);
}
/*
* pk_parse_os_release:
*
* Internal helper to parse os-release
**/
static gboolean
pk_parse_os_release (gchar **id, gchar **name, gchar **version_id, GError **error)
{
const gchar *filename = "/etc/os-release";
gboolean ret;
g_autofree gchar *contents = NULL;
g_autoptr(GKeyFile) key_file = NULL;
g_autoptr(GString) str = NULL;
/* load data */
if (!g_file_test (filename, G_FILE_TEST_EXISTS))
filename = "/usr/lib/os-release";
if (!g_file_get_contents (filename, &contents, NULL, error))
return FALSE;
/* make a valid GKeyFile from the .ini data by prepending a header */
str = g_string_new (contents);
g_string_prepend (str, "[os-release]\n");
key_file = g_key_file_new ();
ret = g_key_file_load_from_data (key_file, str->str, -1, G_KEY_FILE_NONE, error);
if (!ret) {
return FALSE;
}
/* get keys */
if (id != NULL) {
g_autofree gchar *tmp = g_key_file_get_string (key_file, "os-release", "ID", error);
if (tmp == NULL)
return FALSE;
*id = g_shell_unquote (tmp, error);
if (*id == NULL)
return FALSE;
}
if (name != NULL) {
g_autofree gchar *tmp = g_key_file_get_string (key_file, "os-release", "NAME", error);
if (tmp == NULL)
return FALSE;
*name = g_shell_unquote (tmp, error);
if (*name == NULL)
return FALSE;
}
if (version_id != NULL) {
g_autofree gchar *tmp = g_key_file_get_string (key_file, "os-release", "VERSION_ID", error);
if (tmp == NULL)
return FALSE;
*version_id = g_shell_unquote (tmp, error);
if (*version_id == NULL)
return FALSE;
}
return TRUE;
}
/**
* pk_get_distro_id:
*
* Get the distribution ID for the current host, typically "distro;version;arch"
*
* Return value: a distribution ID
**/
gchar *
pk_get_distro_id (void)
{
gboolean ret;
g_autoptr(GError) error = NULL;
g_autofree gchar *arch = NULL;
g_autofree gchar *id = NULL;
g_autofree gchar *version = NULL;
/* we don't want distro specific results in 'make check' */
if (g_getenv ("PK_SELF_TEST") != NULL)
return g_strdup ("selftest;11.91;i686");
ret = pk_parse_os_release (&id, NULL, &version, &error);
if (!ret) {
g_warning ("failed to load os-release: %s", error->message);
return NULL;
}
arch = pk_get_distro_id_machine_type ();
return g_strdup_printf ("%s;%s;%s", id, version, arch);
}
/**
* pk_get_distro_name:
* @error: the #GError to store any failure, or %NULL
*
* Get the distribution name for this host as specified by NAME in /etc/os-release, e.g. "Fedora",
*
* Return value: a distro name
**/
gchar *
pk_get_distro_name (GError **error)
{
gboolean ret;
gchar *name = NULL;
ret = pk_parse_os_release (NULL, &name, NULL, error);
if (!ret)
return NULL;
return name;
}
/**
* pk_get_distro_version_id:
* @error: the #GError to store any failure, or %NULL
*
* Get the distribution version ID as specified by VERSION_ID in /etc/os-release, e.g. "23"
*
* Return value: a distribution version
**/
gchar *
pk_get_distro_version_id (GError **error)
{
gboolean ret;
gchar *version_id = NULL;
ret = pk_parse_os_release (NULL, NULL, &version_id, error);
if (!ret)
return NULL;
return version_id;
}
|