[gnome-builder/gnome-builder-3-32] io: add readlink helper for GFile
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/gnome-builder-3-32] io: add readlink helper for GFile
- Date: Tue, 26 Mar 2019 23:01:35 +0000 (UTC)
commit 5ca48f6d690ed09b7e177b31e63033403f9fe9df
Author: Christian Hergert <chergert redhat com>
Date: Tue Mar 26 14:27:44 2019 -0700
io: add readlink helper for GFile
This a recursive readlink helper that locates the first symlink
in the path and expands it.
src/libide/io/ide-gfile-private.h | 29 +++++++++++++++
src/libide/io/ide-gfile.c | 74 +++++++++++++++++++++++++++++++++++++++
src/libide/io/meson.build | 5 +++
3 files changed, 108 insertions(+)
---
diff --git a/src/libide/io/ide-gfile-private.h b/src/libide/io/ide-gfile-private.h
new file mode 100644
index 000000000..b066a0475
--- /dev/null
+++ b/src/libide/io/ide-gfile-private.h
@@ -0,0 +1,29 @@
+/* ide-gfile-private.h
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+GFile *_ide_g_file_readlink (GFile *file);
+
+G_END_DECLS
diff --git a/src/libide/io/ide-gfile.c b/src/libide/io/ide-gfile.c
index 7eaef19e9..f7dbea6be 100644
--- a/src/libide/io/ide-gfile.c
+++ b/src/libide/io/ide-gfile.c
@@ -25,6 +25,7 @@
#include <libide-threading.h>
#include "ide-gfile.h"
+#include "ide-gfile-private.h"
static GPtrArray *g_ignored;
G_LOCK_DEFINE_STATIC (ignored);
@@ -822,3 +823,76 @@ ide_g_file_walk (GFile *directory,
ide_g_file_walk (child, attributes, cancellable, callback, callback_data);
}
}
+
+static gboolean
+iter_parents (GFile **fileptr)
+{
+ g_autoptr(GFile) item = *fileptr;
+ *fileptr = g_file_get_parent (item);
+ return *fileptr != NULL;
+}
+
+static gboolean
+is_symlink (GFile *file,
+ gchar **target)
+{
+ g_autoptr(GFileInfo) info = NULL;
+
+ g_assert (G_IS_FILE (file));
+ g_assert (target != NULL);
+
+ *target = NULL;
+
+ if (!g_file_is_native (file))
+ return FALSE;
+
+ info = g_file_query_info (file,
+ G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK","
+ G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
+ G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+ NULL, NULL);
+
+ if (info == NULL)
+ return FALSE;
+
+ if (g_file_info_get_is_symlink (info))
+ {
+ *target = g_strdup (g_file_info_get_symlink_target (info));
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+GFile *
+_ide_g_file_readlink (GFile *file)
+{
+ g_autoptr(GFile) iter = NULL;
+
+ g_return_val_if_fail (G_IS_FILE (file), NULL);
+
+ if (!g_file_is_native (file))
+ return g_object_ref (file);
+
+ iter = g_file_dup (file);
+
+ do
+ {
+ g_autofree gchar *target = NULL;
+
+ if (is_symlink (iter, &target))
+ {
+ g_autoptr(GFile) parent = g_file_get_parent (iter);
+ g_autoptr(GFile) base = g_file_get_child (parent, target);
+ g_autofree gchar *relative = g_file_get_relative_path (iter, file);
+
+ if (relative == NULL)
+ return g_steal_pointer (&base);
+ else
+ return g_file_get_child (base, relative);
+ }
+ }
+ while (iter_parents (&iter));
+
+ return g_object_ref (file);
+}
diff --git a/src/libide/io/meson.build b/src/libide/io/meson.build
index 42ffb4b1d..b5b6f4c68 100644
--- a/src/libide/io/meson.build
+++ b/src/libide/io/meson.build
@@ -18,6 +18,10 @@ libide_io_public_headers = [
'libide-io.h',
]
+libide_io_private_headers = [
+ 'ide-gfile-private.h',
+]
+
install_headers(libide_io_public_headers, subdir: libide_io_header_subdir)
#
@@ -65,5 +69,6 @@ libide_io_dep = declare_dependency(
gnome_builder_public_sources += files(libide_io_public_sources)
gnome_builder_public_headers += files(libide_io_public_headers)
+gnome_builder_private_headers += files(libide_io_private_headers)
gnome_builder_include_subdirs += libide_io_header_subdir
gnome_builder_gir_extra_args += ['--c-include=libide-io.h', '-DIDE_IO_COMPILATION']
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]