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
|
/* vi:set et sw=2 sts=2 cin cino=t0,f0,(0,{s,>2s,n-s,^-s,e-s:
* Copyright © 2019 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-base-private.h"
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include "libglnx.h"
const char *
flatpak_get_tzdir (void)
{
const gchar *tzdir;
tzdir = getenv ("TZDIR");
if (tzdir)
return tzdir;
return "/usr/share/zoneinfo";
}
char *
flatpak_get_timezone (void)
{
g_autofree gchar *symlink = NULL;
gchar *etc_timezone = NULL;
symlink = flatpak_resolve_link ("/etc/localtime", NULL);
if (symlink != NULL)
{
/* Resolve relative path */
g_autofree gchar *canonical = flatpak_canonicalize_filename (symlink);
char *canonical_suffix;
const gchar *tzdir = flatpak_get_tzdir ();
/* Strip the prefix and slashes if possible. */
if (g_str_has_prefix (canonical, tzdir))
{
canonical_suffix = canonical + strlen (tzdir);
while (*canonical_suffix == '/')
canonical_suffix++;
return g_strdup (canonical_suffix);
}
}
if (g_file_get_contents ("/etc/timezone", &etc_timezone,
NULL, NULL))
{
g_strchomp (etc_timezone);
return etc_timezone;
}
/* Final fall-back is UTC */
return g_strdup ("UTC");
}
char *
flatpak_readlink (const char *path,
GError **error)
{
return glnx_readlinkat_malloc (-1, path, NULL, error);
}
char *
flatpak_resolve_link (const char *path,
GError **error)
{
g_autofree char *link = flatpak_readlink (path, error);
g_autofree char *dirname = NULL;
if (link == NULL)
return NULL;
if (g_path_is_absolute (link))
return g_steal_pointer (&link);
dirname = g_path_get_dirname (path);
return g_build_filename (dirname, link, NULL);
}
char *
flatpak_realpath (const char *path,
GError **error)
{
struct stat stbuf;
if (!glnx_fstatat (AT_FDCWD, path, &stbuf, AT_SYMLINK_NOFOLLOW, error))
return NULL;
if (S_ISLNK (stbuf.st_mode))
{
g_autofree char *resolved = NULL;
resolved = flatpak_resolve_link (path, error);
if (!resolved)
return NULL;
return flatpak_canonicalize_filename (resolved);
}
return flatpak_canonicalize_filename (path);
}
/*
* Syntactically canonicalize a filename, similar to
* g_canonicalize_filename() in newer GLib.
*
* This function does not do I/O.
*/
char *
flatpak_canonicalize_filename (const char *path)
{
g_autoptr(GFile) file = g_file_new_for_path (path);
return g_file_get_path (file);
}
|