[glib] gunixmounts: Add g_unix_mount_for() support



commit da509fd67d1e78adb20e1e132bd14b2bcbb036f2
Author: Ondrej Holy <oholy redhat com>
Date:   Thu Sep 29 10:45:50 2016 +0200

    gunixmounts: Add g_unix_mount_for() support
    
    GLib has g_unix_mount_at (mount_path) already, let's add g_unix_mount_for
    (file_path) for whatever path. GLib already contains some private code
    for such task. Let's make this code public. This functionality is needed
    by GVfs (see Bug 771431) in order to avoid copy-and-pasting.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=772160

 gio/Makefile.am         |    1 +
 gio/glocalfile.c        |    9 +++++----
 gio/glocalfileprivate.h |   30 ++++++++++++++++++++++++++++++
 gio/gunixmounts.c       |   35 +++++++++++++++++++++++++++++++++++
 gio/gunixmounts.h       |    3 +++
 5 files changed, 74 insertions(+), 4 deletions(-)
---
diff --git a/gio/Makefile.am b/gio/Makefile.am
index ffe5ee2..cbeeaa1 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -193,6 +193,7 @@ local_sources = \
        ghttpproxy.h                    \
        glocalfile.c                    \
        glocalfile.h                    \
+       glocalfileprivate.h             \
        glocalfileenumerator.c          \
        glocalfileenumerator.h          \
        glocalfileinfo.c                \
diff --git a/gio/glocalfile.c b/gio/glocalfile.c
index 4075d4b..a4c9fa5 100644
--- a/gio/glocalfile.c
+++ b/gio/glocalfile.c
@@ -51,6 +51,7 @@
 
 #include "gfileattribute.h"
 #include "glocalfile.h"
+#include "glocalfileprivate.h"
 #include "glocalfileinfo.h"
 #include "glocalfileenumerator.h"
 #include "glocalfileinputstream.h"
@@ -1706,8 +1707,8 @@ find_mountpoint_for (const char *file,
     }
 }
 
-static char *
-find_topdir_for (const char *file)
+char *
+_g_local_file_find_topdir_for (const char *file)
 {
   char *dir;
   char *mountpoint = NULL;
@@ -1961,8 +1962,8 @@ g_local_file_trash (GFile         *file,
 
       uid = geteuid ();
       g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
-      
-      topdir = find_topdir_for (local->filename);
+
+      topdir = _g_local_file_find_topdir_for (local->filename);
       if (topdir == NULL)
        {
           g_set_io_error (error,
diff --git a/gio/glocalfileprivate.h b/gio/glocalfileprivate.h
new file mode 100644
index 0000000..b4090a7
--- /dev/null
+++ b/gio/glocalfileprivate.h
@@ -0,0 +1,30 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Ondrej Holy <oholy redhat com>
+ */
+
+#ifndef __G_LOCAL_FILE_PRIVATE_H__
+#define __G_LOCAL_FILE_PRIVATE_H__
+
+G_BEGIN_DECLS
+
+gchar *_g_local_file_find_topdir_for (const char *file_path);
+
+G_END_DECLS
+
+#endif /* __G_LOCAL_FILE_PRIVATE_H__ */
diff --git a/gio/gunixmounts.c b/gio/gunixmounts.c
index b5e066f..52be25a 100644
--- a/gio/gunixmounts.c
+++ b/gio/gunixmounts.c
@@ -64,6 +64,7 @@
 #endif
 
 #include "gunixmounts.h"
+#include "glocalfileprivate.h"
 #include "gfile.h"
 #include "gfilemonitor.h"
 #include "glibintl.h"
@@ -1444,6 +1445,40 @@ g_unix_mount_at (const char *mount_path,
 }
 
 /**
+ * g_unix_mount_for: (skip)
+ * @file_path: file path on some unix mount.
+ * @time_read: (out) (allow-none): guint64 to contain a timestamp.
+ *
+ * Gets a #GUnixMountEntry for a given file path. If @time_read
+ * is set, it will be filled with a unix timestamp for checking
+ * if the mounts have changed since with g_unix_mounts_changed_since().
+ *
+ * Returns: (transfer full): a #GUnixMountEntry.
+ *
+ * Since: 2.52
+ **/
+GUnixMountEntry *
+g_unix_mount_for (const char *file_path,
+                  guint64    *time_read)
+{
+  GUnixMountEntry *entry;
+
+  g_return_val_if_fail (file_path != NULL, NULL);
+
+  entry = g_unix_mount_at (file_path, time_read);
+  if (entry == NULL)
+    {
+      char *topdir;
+
+      topdir = _g_local_file_find_topdir_for (file_path);
+      entry = g_unix_mount_at (topdir, time_read);
+      g_free (topdir);
+    }
+
+  return entry;
+}
+
+/**
  * g_unix_mount_points_get: (skip)
  * @time_read: (out) (allow-none): guint64 to contain a timestamp.
  *
diff --git a/gio/gunixmounts.h b/gio/gunixmounts.h
index faf8119..90b9f03 100644
--- a/gio/gunixmounts.h
+++ b/gio/gunixmounts.h
@@ -119,6 +119,9 @@ GList *        g_unix_mounts_get                    (guint64            *time_re
 GLIB_AVAILABLE_IN_ALL
 GUnixMountEntry *g_unix_mount_at                    (const char         *mount_path,
                                                     guint64            *time_read);
+GLIB_AVAILABLE_IN_2_52
+GUnixMountEntry *g_unix_mount_for                   (const char         *file_path,
+                                                     guint64            *time_read);
 GLIB_AVAILABLE_IN_ALL
 gboolean       g_unix_mounts_changed_since          (guint64             time);
 GLIB_AVAILABLE_IN_ALL


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]