build: Move sources into src/ again
This is necessary if we want to build when srcdir == builddir, otherwise we blow up because "ostree" is a source directory and a binary.
This commit is contained in:
1 parent
9dc6ddce08
commit
18f0b537a4
47 files changed
+5284
-42
No files matched your search
@@ -0,0 +1,178 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <gio/gio.h>
|
||||
#include <gio/gunixinputstream.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "otutil.h"
|
||||
|
||||
gboolean
|
||||
ot_util_ensure_directory (const char *path, gboolean with_parents, GError **error)
|
||||
{
|
||||
GFile *dir;
|
||||
GError *temp_error = NULL;
|
||||
gboolean ret = FALSE;
|
||||
|
||||
dir = g_file_new_for_path (path);
|
||||
if (with_parents)
|
||||
ret = g_file_make_directory_with_parents (dir, NULL, &temp_error);
|
||||
else
|
||||
ret = g_file_make_directory (dir, NULL, &temp_error);
|
||||
if (!ret)
|
||||
{
|
||||
if (!g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
|
||||
{
|
||||
g_propagate_error (error, temp_error);
|
||||
goto out;
|
||||
}
|
||||
else
|
||||
g_clear_error (&temp_error);
|
||||
}
|
||||
|
||||
ret = TRUE;
|
||||
out:
|
||||
g_clear_object (&dir);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
char *
|
||||
ot_util_get_file_contents_utf8 (const char *path,
|
||||
GError **error)
|
||||
{
|
||||
GFile *file = NULL;
|
||||
char *ret = NULL;
|
||||
|
||||
file = ot_util_new_file_for_path (path);
|
||||
if (!ot_util_gfile_load_contents_utf8 (file, NULL, &ret, NULL, error))
|
||||
goto out;
|
||||
|
||||
out:
|
||||
g_clear_object (&file);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ot_util_gfile_load_contents_utf8 (GFile *file,
|
||||
GCancellable *cancellable,
|
||||
char **contents_out,
|
||||
char **etag_out,
|
||||
GError **error)
|
||||
{
|
||||
char *ret_contents = NULL;
|
||||
char *ret_etag = NULL;
|
||||
gsize len;
|
||||
gboolean ret = FALSE;
|
||||
|
||||
if (!g_file_load_contents (file, cancellable, &ret_contents, &len, &ret_etag, error))
|
||||
goto out;
|
||||
if (!g_utf8_validate (ret_contents, len, NULL))
|
||||
{
|
||||
g_set_error (error,
|
||||
G_IO_ERROR,
|
||||
G_IO_ERROR_INVALID_DATA,
|
||||
"Invalid UTF-8");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (contents_out)
|
||||
*contents_out = ret_contents;
|
||||
else
|
||||
g_free (ret_contents);
|
||||
ret_contents = NULL;
|
||||
if (etag_out)
|
||||
*etag_out = ret_etag;
|
||||
else
|
||||
g_free (ret_etag);
|
||||
ret_etag = NULL;
|
||||
ret = TRUE;
|
||||
out:
|
||||
g_free (ret_contents);
|
||||
g_free (ret_etag);
|
||||
return ret;
|
||||
}
|
||||
|
||||
GInputStream *
|
||||
ot_util_read_file_noatime (GFile *file, GCancellable *cancellable, GError **error)
|
||||
{
|
||||
GInputStream *ret = NULL;
|
||||
int fd;
|
||||
int flags = O_RDONLY;
|
||||
char *path = NULL;
|
||||
|
||||
path = g_file_get_path (file);
|
||||
#ifdef O_NOATIME
|
||||
flags |= O_NOATIME;
|
||||
#endif
|
||||
fd = open (path, flags);
|
||||
if (fd < 0)
|
||||
{
|
||||
ot_util_set_error_from_errno (error, errno);
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = (GInputStream*)g_unix_input_stream_new (fd, TRUE);
|
||||
|
||||
out:
|
||||
g_free (path);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Like g_file_new_for_path, but only do local stuff, not GVFS */
|
||||
GFile *
|
||||
ot_util_new_file_for_path (const char *path)
|
||||
{
|
||||
return g_vfs_get_file_for_path (g_vfs_get_local (), path);
|
||||
}
|
||||
|
||||
const char *
|
||||
ot_gfile_get_path_cached (GFile *file)
|
||||
{
|
||||
const char *path;
|
||||
|
||||
path = g_object_get_data ((GObject*)file, "ostree-file-path");
|
||||
if (!path)
|
||||
{
|
||||
path = g_file_get_path (file);
|
||||
g_object_set_data_full ((GObject*)file, "ostree-file-path", (char*)path, (GDestroyNotify)g_free);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
const char *
|
||||
ot_gfile_get_basename_cached (GFile *file)
|
||||
{
|
||||
const char *name;
|
||||
|
||||
name = g_object_get_data ((GObject*)file, "ostree-file-name");
|
||||
if (!name)
|
||||
{
|
||||
name = g_file_get_basename (file);
|
||||
g_object_set_data_full ((GObject*)file, "ostree-file-name", (char*)name, (GDestroyNotify)g_free);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>.
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#ifndef __OSTREE_GIO_UTILS_H__
|
||||
#define __OSTREE_GIO_UTILS_H__
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
GFile *ot_util_new_file_for_path (const char *path);
|
||||
|
||||
const char *ot_gfile_get_path_cached (GFile *file);
|
||||
|
||||
const char *ot_gfile_get_basename_cached (GFile *file);
|
||||
|
||||
gboolean ot_util_ensure_directory (const char *path, gboolean with_parents, GError **error);
|
||||
|
||||
char * ot_util_get_file_contents_utf8 (const char *path, GError **error);
|
||||
|
||||
gboolean ot_util_gfile_load_contents_utf8 (GFile *file,
|
||||
GCancellable *cancellable,
|
||||
char **contents_out,
|
||||
char **etag_out,
|
||||
GError **error);
|
||||
|
||||
GInputStream *ot_util_read_file_noatime (GFile *file, GCancellable *cancellable, GError **error);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,160 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "otutil.h"
|
||||
|
||||
#if GLIB_CHECK_VERSION(2,32,0)
|
||||
/* nothing */
|
||||
#else
|
||||
/* Code copied from glib/glib/genviron.c */
|
||||
/* GLIB - Library of useful routines for C programming
|
||||
* Copyright (C) 1995-1998 Peter Mattis, Spencer Kimball and Josh MacDonald
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
||||
* file for a list of people on the GLib Team. See the ChangeLog
|
||||
* files for a list of changes. These files are distributed with
|
||||
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
||||
*/
|
||||
|
||||
static gint
|
||||
ot_g_environ_find (gchar **envp,
|
||||
const gchar *variable)
|
||||
{
|
||||
gint len, i;
|
||||
|
||||
len = strlen (variable);
|
||||
|
||||
for (i = 0; envp[i]; i++)
|
||||
{
|
||||
if (strncmp (envp[i], variable, len) == 0 &&
|
||||
envp[i][len] == '=')
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
const gchar *
|
||||
ot_g_environ_getenv (gchar **envp,
|
||||
const gchar *variable)
|
||||
{
|
||||
gint index;
|
||||
|
||||
g_return_val_if_fail (envp != NULL, NULL);
|
||||
g_return_val_if_fail (variable != NULL, NULL);
|
||||
|
||||
index = ot_g_environ_find (envp, variable);
|
||||
if (index != -1)
|
||||
return envp[index] + strlen (variable) + 1;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gchar **
|
||||
ot_g_environ_setenv (gchar **envp,
|
||||
const gchar *variable,
|
||||
const gchar *value,
|
||||
gboolean overwrite)
|
||||
{
|
||||
gint index;
|
||||
|
||||
g_return_val_if_fail (envp != NULL, NULL);
|
||||
g_return_val_if_fail (variable != NULL, NULL);
|
||||
g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
|
||||
|
||||
index = ot_g_environ_find (envp, variable);
|
||||
if (index != -1)
|
||||
{
|
||||
if (overwrite)
|
||||
{
|
||||
g_free (envp[index]);
|
||||
envp[index] = g_strdup_printf ("%s=%s", variable, value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
gint length;
|
||||
|
||||
length = g_strv_length (envp);
|
||||
envp = g_renew (gchar *, envp, length + 2);
|
||||
envp[length] = g_strdup_printf ("%s=%s", variable, value);
|
||||
envp[length + 1] = NULL;
|
||||
}
|
||||
|
||||
return envp;
|
||||
}
|
||||
|
||||
gchar **
|
||||
ot_g_environ_unsetenv (gchar **envp,
|
||||
const gchar *variable)
|
||||
{
|
||||
gint len;
|
||||
gchar **e, **f;
|
||||
|
||||
g_return_val_if_fail (envp != NULL, NULL);
|
||||
g_return_val_if_fail (variable != NULL, NULL);
|
||||
g_return_val_if_fail (strchr (variable, '=') == NULL, NULL);
|
||||
|
||||
len = strlen (variable);
|
||||
|
||||
/* Note that we remove *all* environment entries for
|
||||
* the variable name, not just the first.
|
||||
*/
|
||||
e = f = envp;
|
||||
while (*e != NULL)
|
||||
{
|
||||
if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
|
||||
{
|
||||
*f = *e;
|
||||
f++;
|
||||
}
|
||||
e++;
|
||||
}
|
||||
*f = NULL;
|
||||
|
||||
return envp;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>.
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#ifndef __OSTREE_GLIB_COMPAT_H__
|
||||
#define __OSTREE_GLIB_COMPAT_H__
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#if GLIB_CHECK_VERSION(2,32,0)
|
||||
#define ot_g_environ_getenv g_environ_getenv
|
||||
#define ot_g_environ_setenv g_environ_setenv
|
||||
#define ot_g_environ_unsetenv g_environ_unsetenv
|
||||
#else
|
||||
const gchar *
|
||||
ot_g_environ_getenv (gchar **envp,
|
||||
const gchar *variable);
|
||||
|
||||
gchar **
|
||||
ot_g_environ_setenv (gchar **envp,
|
||||
const gchar *variable,
|
||||
const gchar *value,
|
||||
gboolean overwrite);
|
||||
|
||||
gchar **
|
||||
ot_g_environ_unsetenv (gchar **envp,
|
||||
const gchar *variable);
|
||||
#endif
|
||||
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "otutil.h"
|
||||
|
||||
void
|
||||
ot_util_usage_error (GOptionContext *context, const char *message, GError **error)
|
||||
{
|
||||
gchar *help = g_option_context_get_help (context, TRUE, NULL);
|
||||
g_printerr ("%s\n", help);
|
||||
g_free (help);
|
||||
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, message);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>.
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#ifndef __OSTREE_OPT_UTILS_H__
|
||||
#define __OSTREE_OPT_UTILS_H__
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
void ot_util_usage_error (GOptionContext *context, const char *message, GError **error);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,273 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "ot-unix-utils.h"
|
||||
|
||||
#include <gio/gio.h>
|
||||
#include <gio/gunixoutputstream.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <dirent.h>
|
||||
|
||||
gboolean
|
||||
ot_util_spawn_pager (GOutputStream **out_stream,
|
||||
GError **error)
|
||||
{
|
||||
const char *pager;
|
||||
char *argv[2];
|
||||
int stdin_fd;
|
||||
pid_t pid;
|
||||
gboolean ret = FALSE;
|
||||
GOutputStream *ret_stream = NULL;
|
||||
|
||||
if (!isatty (1))
|
||||
{
|
||||
ret_stream = (GOutputStream*)g_unix_output_stream_new (1, TRUE);
|
||||
}
|
||||
else
|
||||
{
|
||||
pager = g_getenv ("GIT_PAGER");
|
||||
if (pager == NULL)
|
||||
pager = "less";
|
||||
|
||||
argv[0] = (char*)pager;
|
||||
argv[1] = NULL;
|
||||
|
||||
if (!g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
|
||||
NULL, NULL, &pid, &stdin_fd, NULL, NULL, error))
|
||||
{
|
||||
g_prefix_error (error, "%s", "Failed to spawn pager: ");
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret_stream = (GOutputStream*)g_unix_output_stream_new (stdin_fd, TRUE);
|
||||
}
|
||||
|
||||
*out_stream = ret_stream;
|
||||
ret_stream = NULL;
|
||||
ret = TRUE;
|
||||
out:
|
||||
g_clear_object (&ret_stream);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
compare_filenames_by_component_length (const char *a,
|
||||
const char *b)
|
||||
{
|
||||
char *a_slash, *b_slash;
|
||||
|
||||
a_slash = strchr (a, '/');
|
||||
b_slash = strchr (b, '/');
|
||||
while (a_slash && b_slash)
|
||||
{
|
||||
a = a_slash + 1;
|
||||
b = b_slash + 1;
|
||||
a_slash = strchr (a, '/');
|
||||
b_slash = strchr (b, '/');
|
||||
}
|
||||
if (a_slash)
|
||||
return -1;
|
||||
else if (b_slash)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
GPtrArray *
|
||||
ot_util_sort_filenames_by_component_length (GPtrArray *files)
|
||||
{
|
||||
GPtrArray *array = g_ptr_array_sized_new (files->len);
|
||||
memcpy (array->pdata, files->pdata, sizeof (gpointer) * files->len);
|
||||
g_ptr_array_sort (array, (GCompareFunc) compare_filenames_by_component_length);
|
||||
return array;
|
||||
}
|
||||
|
||||
int
|
||||
ot_util_count_filename_components (const char *path)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while (path)
|
||||
{
|
||||
i++;
|
||||
path = strchr (path, '/');
|
||||
if (path)
|
||||
path++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ot_util_filename_has_dotdot (const char *path)
|
||||
{
|
||||
char *p;
|
||||
char last;
|
||||
|
||||
if (strcmp (path, "..") == 0)
|
||||
return TRUE;
|
||||
if (g_str_has_prefix (path, "../"))
|
||||
return TRUE;
|
||||
p = strstr (path, "/..");
|
||||
if (!p)
|
||||
return FALSE;
|
||||
last = *(p + 1);
|
||||
return last == '\0' || last == '/';
|
||||
}
|
||||
|
||||
GPtrArray *
|
||||
ot_util_path_split (const char *path)
|
||||
{
|
||||
GPtrArray *ret = NULL;
|
||||
const char *p;
|
||||
const char *slash;
|
||||
int i;
|
||||
|
||||
g_return_val_if_fail (path[0] != '/', NULL);
|
||||
|
||||
ret = g_ptr_array_new ();
|
||||
g_ptr_array_set_free_func (ret, g_free);
|
||||
|
||||
p = path;
|
||||
do {
|
||||
slash = strchr (p, '/');
|
||||
if (!slash)
|
||||
{
|
||||
g_ptr_array_add (ret, g_strdup (p));
|
||||
p = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_ptr_array_add (ret, g_strndup (p, slash - p));
|
||||
p = slash + 1;
|
||||
}
|
||||
} while (p && *p);
|
||||
|
||||
/* Canonicalize by removing duplicate '.' */
|
||||
for (i = ret->len-1; i >= 0; i--)
|
||||
{
|
||||
if (strcmp (ret->pdata[i], ".") == 0)
|
||||
g_ptr_array_remove_index (ret, i);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *
|
||||
ot_util_path_join_n (const char *base, GPtrArray *components, int n)
|
||||
{
|
||||
int max = MIN(n+1, components->len);
|
||||
GPtrArray *subcomponents;
|
||||
char *path;
|
||||
int i;
|
||||
|
||||
subcomponents = g_ptr_array_new ();
|
||||
|
||||
if (base != NULL)
|
||||
g_ptr_array_add (subcomponents, (char*)base);
|
||||
|
||||
for (i = 0; i < max; i++)
|
||||
{
|
||||
g_ptr_array_add (subcomponents, components->pdata[i]);
|
||||
}
|
||||
g_ptr_array_add (subcomponents, NULL);
|
||||
|
||||
path = g_build_filenamev ((char**)subcomponents->pdata);
|
||||
g_ptr_array_free (subcomponents, TRUE);
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
void
|
||||
ot_util_set_error_from_errno (GError **error,
|
||||
gint saved_errno)
|
||||
{
|
||||
g_set_error_literal (error,
|
||||
G_IO_ERROR,
|
||||
0,
|
||||
g_strerror (saved_errno));
|
||||
errno = saved_errno;
|
||||
}
|
||||
|
||||
int
|
||||
ot_util_open_file_read (const char *path, GError **error)
|
||||
{
|
||||
char *dirname = NULL;
|
||||
char *basename = NULL;
|
||||
DIR *dir = NULL;
|
||||
int fd = -1;
|
||||
|
||||
dirname = g_path_get_dirname (path);
|
||||
basename = g_path_get_basename (path);
|
||||
dir = opendir (dirname);
|
||||
if (dir == NULL)
|
||||
{
|
||||
ot_util_set_error_from_errno (error, errno);
|
||||
goto out;
|
||||
}
|
||||
|
||||
fd = ot_util_open_file_read_at (dirfd (dir), basename, error);
|
||||
|
||||
out:
|
||||
g_free (basename);
|
||||
g_free (dirname);
|
||||
if (dir != NULL)
|
||||
closedir (dir);
|
||||
return fd;
|
||||
}
|
||||
|
||||
int
|
||||
ot_util_open_file_read_at (int dirfd, const char *name, GError **error)
|
||||
{
|
||||
int fd;
|
||||
int flags = O_RDONLY;
|
||||
|
||||
#ifdef O_CLOEXEC
|
||||
flags |= O_CLOEXEC;
|
||||
#endif
|
||||
#ifdef O_NOATIME
|
||||
flags |= O_NOATIME;
|
||||
#endif
|
||||
fd = openat (dirfd, name, flags);
|
||||
if (fd < 0)
|
||||
ot_util_set_error_from_errno (error, errno);
|
||||
return fd;
|
||||
}
|
||||
|
||||
void
|
||||
ot_util_fatal_literal (const char *msg)
|
||||
{
|
||||
g_printerr ("%s\n", msg);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
void
|
||||
ot_util_fatal_gerror (GError *error)
|
||||
{
|
||||
g_assert (error != NULL);
|
||||
ot_util_fatal_literal (error->message);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>.
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#ifndef __OSTREE_UNIX_UTILS_H__
|
||||
#define __OSTREE_UNIX_UTILS_H__
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
/* I just put all this shit here. Sue me. */
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
gboolean ot_util_spawn_pager (GOutputStream **out_stream, GError **error);
|
||||
|
||||
void ot_util_fatal_literal (const char *msg) G_GNUC_NORETURN;
|
||||
|
||||
void ot_util_fatal_gerror (GError *error) G_GNUC_NORETURN;
|
||||
|
||||
gboolean ot_util_filename_has_dotdot (const char *path);
|
||||
|
||||
GPtrArray *ot_util_sort_filenames_by_component_length (GPtrArray *files);
|
||||
|
||||
GPtrArray* ot_util_path_split (const char *path);
|
||||
|
||||
char *ot_util_path_join_n (const char *base, GPtrArray *components, int n);
|
||||
|
||||
int ot_util_count_filename_components (const char *path);
|
||||
|
||||
int ot_util_open_file_read (const char *path, GError **error);
|
||||
|
||||
int ot_util_open_file_read_at (int dirfd, const char *name, GError **error);
|
||||
|
||||
void ot_util_set_error_from_errno (GError **error, gint saved_errno);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,126 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "otutil.h"
|
||||
|
||||
GHashTable *
|
||||
ot_util_variant_asv_to_hash_table (GVariant *variant)
|
||||
{
|
||||
GHashTable *ret;
|
||||
GVariantIter *viter;
|
||||
char *key;
|
||||
GVariant *value;
|
||||
|
||||
ret = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
|
||||
viter = g_variant_iter_new (variant);
|
||||
while (g_variant_iter_next (viter, "{s@v}", &key, &value))
|
||||
g_hash_table_replace (ret, key, g_variant_ref_sink (value));
|
||||
|
||||
g_variant_iter_free (viter);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ot_util_variant_save (GFile *dest,
|
||||
GVariant *variant,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
gboolean ret = FALSE;
|
||||
GOutputStream *out = NULL;
|
||||
gsize bytes_written;
|
||||
|
||||
out = (GOutputStream*)g_file_replace (dest, NULL, 0, FALSE, cancellable, error);
|
||||
if (!out)
|
||||
goto out;
|
||||
|
||||
if (!g_output_stream_write_all (out,
|
||||
g_variant_get_data (variant),
|
||||
g_variant_get_size (variant),
|
||||
&bytes_written,
|
||||
cancellable,
|
||||
error))
|
||||
goto out;
|
||||
if (!g_output_stream_close (out, cancellable, error))
|
||||
goto out;
|
||||
|
||||
ret = TRUE;
|
||||
out:
|
||||
g_clear_object (&out);
|
||||
return ret;
|
||||
}
|
||||
|
||||
GVariant *
|
||||
ot_util_variant_take_ref (GVariant *variant)
|
||||
{
|
||||
#if GLIB_CHECK_VERSION(2,32,0)
|
||||
return g_variant_take_ref (variant);
|
||||
#else
|
||||
if (g_variant_is_floating (variant))
|
||||
return g_variant_ref_sink (variant);
|
||||
return variant;
|
||||
#endif
|
||||
}
|
||||
|
||||
gboolean
|
||||
ot_util_variant_map (GFile *src,
|
||||
const GVariantType *type,
|
||||
GVariant **out_variant,
|
||||
GError **error)
|
||||
{
|
||||
gboolean ret = FALSE;
|
||||
GMappedFile *mfile = NULL;
|
||||
char *path = NULL;
|
||||
GVariant *ret_variant = NULL;
|
||||
|
||||
path = g_file_get_path (src);
|
||||
mfile = g_mapped_file_new (path, FALSE, error);
|
||||
if (!mfile)
|
||||
goto out;
|
||||
|
||||
ret_variant = g_variant_new_from_data (type,
|
||||
g_mapped_file_get_contents (mfile),
|
||||
g_mapped_file_get_length (mfile),
|
||||
FALSE,
|
||||
(GDestroyNotify) g_mapped_file_unref,
|
||||
mfile);
|
||||
mfile = NULL;
|
||||
g_variant_ref_sink (ret_variant);
|
||||
|
||||
ret = TRUE;
|
||||
*out_variant = ret_variant;
|
||||
ret_variant = NULL;
|
||||
out:
|
||||
if (ret_variant)
|
||||
g_variant_unref (ret_variant);
|
||||
if (mfile)
|
||||
g_mapped_file_unref (mfile);
|
||||
g_free (path);
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>.
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#ifndef __OSTREE_VARIANT_UTILS_H__
|
||||
#define __OSTREE_VARIANT_UTILS_H__
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
GHashTable *ot_util_variant_asv_to_hash_table (GVariant *variant);
|
||||
|
||||
GVariant * ot_util_variant_take_ref (GVariant *variant);
|
||||
|
||||
gboolean ot_util_variant_save (GFile *dest,
|
||||
GVariant *variant,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
gboolean ot_util_variant_map (GFile *src,
|
||||
const GVariantType *type,
|
||||
GVariant **out_variant,
|
||||
GError **error);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||
*
|
||||
* Copyright (C) 2011 Colin Walters <walters@verbum.org>.
|
||||
*
|
||||
* 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 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#ifndef __OSTREE_UTIL_H__
|
||||
|
||||
#include <ot-gio-utils.h>
|
||||
#include <ot-glib-compat.h>
|
||||
#include <ot-opt-utils.h>
|
||||
#include <ot-unix-utils.h>
|
||||
#include <ot-variant-utils.h>
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user