[libgsystem] Add gs_file_read_noatime()



commit f28754547bc28c091aa3bb5aa400d54d07218409
Author: Colin Walters <walters verbum org>
Date:   Fri Aug 10 16:43:07 2012 -0400

    Add gs_file_read_noatime()
    
    Based on a patch from William Jon McCann <mccann redhat com>
    
    This will be used by libgnome-desktop and hopefully other things.

 Makefile-libgsystem.am |    2 +
 gsystem-file-utils.c   |   78 ++++++++++++++++++++++++++++++++++++++++++++++++
 gsystem-file-utils.h   |   34 +++++++++++++++++++++
 libgsystem.h           |    3 +-
 4 files changed, 116 insertions(+), 1 deletions(-)
---
diff --git a/Makefile-libgsystem.am b/Makefile-libgsystem.am
index fb09b51..b8a6004 100644
--- a/Makefile-libgsystem.am
+++ b/Makefile-libgsystem.am
@@ -20,6 +20,8 @@ noinst_LTLIBRARIES += libgsystem.la
 libgsystem_la_SOURCES = \
 	$(libgsystem_srcpath)/gsystem-local-alloc.h \
 	$(libgsystem_srcpath)/gsystem-local-alloc.c \
+	$(libgsystem_srcpath)/gsystem-file-utils.h \
+	$(libgsystem_srcpath)/gsystem-file-utils.c \
 	$(libgsystem_srcpath)/libgsystem.h \
 	$(NULL)
 
diff --git a/gsystem-file-utils.c b/gsystem-file-utils.c
new file mode 100644
index 0000000..71902a1
--- /dev/null
+++ b/gsystem-file-utils.c
@@ -0,0 +1,78 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 William Jon McCann <mccann redhat com>
+ * Copyright (C) 2012 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.
+ */
+
+#include "config.h"
+
+#include "gsystem-file-utils.h"
+ 
+static int
+_open_fd_noatime (const char *path)
+{
+  int fd;
+
+#ifdef O_NOATIME
+  fd = g_open (path, O_RDONLY | O_NOATIME, 0);
+  /* Only the owner or superuser may use O_NOATIME; so we may get
+   * EPERM.  EINVAL may happen if the kernel is really old...
+   */
+  if (fd == -1 && (errno == EPERM || errno == EINVAL))
+#endif
+    fd = g_open (path, O_RDONLY, 0);
+  
+  return fd;
+}
+
+/**
+ * gs_file_read_noatime:
+ * @file: a #GFile
+ * @cancellable: a #GCancellable
+ * @error: a #GError
+ *
+ * Like g_file_read(), 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 input stream, or %NULL on error
+ */
+GInputStream *
+gs_file_read_noatime (GFile         *file,
+                      GCancellable  *cancellable,
+                      GError       **error)
+{
+  gs_lfree char *path = NULL;
+  int fd;
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return NULL;
+
+  path = g_file_get_path (file);
+  if (path == NULL)
+    return NULL;
+
+  fd = _open_fd (path);
+  if (fd < 0)
+    {
+      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno));
+      return NULL;
+    }
+
+  return g_unix_input_stream_new (fd, TRUE);
+}
diff --git a/gsystem-file-utils.h b/gsystem-file-utils.h
new file mode 100644
index 0000000..f8c1c73
--- /dev/null
+++ b/gsystem-file-utils.h
@@ -0,0 +1,34 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 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.
+ */
+
+#ifndef __GSYSTEM_FILE_UTILS_H__
+#define __GSYSTEM_FILE_UTILS_H__
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+GInputStream *gs_file_read_noatime (GFile         *path,
+                                    GCancellable  *cancellable,
+                                    GError       **error);
+
+G_END_DECLS
+
+#endif
diff --git a/libgsystem.h b/libgsystem.h
index 884dc99..7b6a8c8 100644
--- a/libgsystem.h
+++ b/libgsystem.h
@@ -25,7 +25,8 @@
 
 G_BEGIN_DECLS
 
-#include "gsystem-local-alloc.h"
+#include <gsystem-local-alloc.h>
+#include <gsystem-file-utils.h>
 
 G_END_DECLS
 



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