[libgsystem] fileutils: Add API to mmap() a file using O_NOATIME



commit 76d4b356378dc3549d402bfdd0bc953da7106f08
Author: Colin Walters <walters verbum org>
Date:   Sun Dec 2 15:17:16 2012 -0500

    fileutils: Add API to mmap() a file using O_NOATIME
    
    This is similar to what git does.

 gsystem-file-utils.c |   43 +++++++++++++++++++++++++++++++++++++++++++
 gsystem-file-utils.h |    3 +++
 2 files changed, 46 insertions(+), 0 deletions(-)
---
diff --git a/gsystem-file-utils.c b/gsystem-file-utils.c
index 081cf18..555f968 100644
--- a/gsystem-file-utils.c
+++ b/gsystem-file-utils.c
@@ -86,6 +86,49 @@ gs_file_read_noatime (GFile         *file,
 }
 
 /**
+ * gs_file_map_noatime:
+ * @file: a #GFile
+ * @cancellable: a #GCancellable
+ * @error: a #GError
+ *
+ * Like g_mapped_file_new(), but try to avoid updating the file's
+ * access time.  This should be used by background scanning
+ * components such as search indexers, antivirus programs, etc.
+ *
+ * Returns: (transfer full): A new mapped file, or %NULL on error
+ */
+GMappedFile *
+gs_file_map_noatime (GFile         *file,
+                     GCancellable  *cancellable,
+                     GError       **error)
+{
+  const char *path;
+  int fd;
+  GMappedFile *ret;
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return NULL;
+
+  path = gs_file_get_path_cached (file);
+  if (path == NULL)
+    return NULL;
+
+  fd = _open_fd_noatime (path);
+  if (fd < 0)
+    {
+      int errsv = errno;
+      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno),
+                   "%s", g_strerror (errsv));
+      return NULL;
+    }
+  
+  ret = g_mapped_file_new_from_fd (fd, FALSE, error);
+  (void) close (fd); /* Ignore errors - we always want to close */
+
+  return ret;
+}
+
+/**
  * gs_file_get_path_cached:
  *
  * Like g_file_get_path(), but returns a constant copy so callers
diff --git a/gsystem-file-utils.h b/gsystem-file-utils.h
index 47d3c4e..b0db9e1 100644
--- a/gsystem-file-utils.h
+++ b/gsystem-file-utils.h
@@ -32,6 +32,9 @@ const char *gs_file_get_basename_cached (GFile *file);
 GInputStream *gs_file_read_noatime (GFile         *path,
                                     GCancellable  *cancellable,
                                     GError       **error);
+GMappedFile *gs_file_map_noatime (GFile         *path,
+                                  GCancellable  *cancellable,
+                                  GError       **error);
 
 gboolean gs_file_rename (GFile          *src,
                          GFile          *dest,



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