[tracker/tracker-0.12] libtracker-common: Add new function tracker_file_open_fd()



commit 371c854e4f4d66db91cf499959ab795b79a3a51e
Author: Antoine Jacoutot <ajacoutot openbsd org>
Date:   Tue Jan 10 12:45:29 2012 +0100

    libtracker-common: Add new function tracker_file_open_fd()
    
    Add a new function to open fds in a portable way
    
    open(2) with the NOATIME flag is only available on Linux. Instead of
    duplicating the same code all over with ifdef, factorize into a function
    that opens file descriptors in a portable way.
    
    Fixes GB#666654.

 src/libtracker-common/tracker-file-utils.c |   26 +++++++++++++++++++-------
 src/libtracker-common/tracker-file-utils.h |    1 +
 src/tracker-extract/tracker-extract-abw.c  |    5 +----
 src/tracker-extract/tracker-extract-gif.c  |    5 +----
 src/tracker-extract/tracker-extract-mp3.c  |   16 +---------------
 src/tracker-extract/tracker-extract-pdf.c  |    5 +----
 src/tracker-extract/tracker-extract-text.c |    5 +----
 src/tracker-extract/tracker-extract-tiff.c |    5 +----
 src/tracker-extract/tracker-extract-xmp.c  |    5 +----
 9 files changed, 27 insertions(+), 46 deletions(-)
---
diff --git a/src/libtracker-common/tracker-file-utils.c b/src/libtracker-common/tracker-file-utils.c
index 27f54ae..1ee4cee 100644
--- a/src/libtracker-common/tracker-file-utils.c
+++ b/src/libtracker-common/tracker-file-utils.c
@@ -50,23 +50,35 @@
 
 static GHashTable *file_locks = NULL;
 
-FILE *
-tracker_file_open (const gchar *path)
+int
+tracker_file_open_fd (const gchar *path)
 {
-	FILE *file;
 	int fd;
 
-	g_return_val_if_fail (path != NULL, NULL);
+	g_return_val_if_fail (path != NULL, -1);
 
 #if defined(__linux__)
-	fd = g_open (path, O_RDONLY | O_NOATIME);
+	fd = g_open (path, O_RDONLY | O_NOATIME, 0);
 	if (fd == -1 && errno == EPERM) {
-		fd = g_open (path, O_RDONLY);
+		fd = g_open (path, O_RDONLY, 0);
 	}
 #else
-	fd = g_open (path, O_RDONLY);
+	fd = g_open (path, O_RDONLY, 0);
 #endif
 
+	return fd;
+}
+
+FILE *
+tracker_file_open (const gchar *path)
+{
+	FILE *file;
+	int fd;
+
+	g_return_val_if_fail (path != NULL, NULL);
+
+	fd = tracker_file_open_fd (path);
+
 	if (fd == -1) {
 		return NULL;
 	}
diff --git a/src/libtracker-common/tracker-file-utils.h b/src/libtracker-common/tracker-file-utils.h
index fee22d0..9bcc88e 100644
--- a/src/libtracker-common/tracker-file-utils.h
+++ b/src/libtracker-common/tracker-file-utils.h
@@ -32,6 +32,7 @@ G_BEGIN_DECLS
 #endif
 
 /* File utils */
+int      tracker_file_open_fd                               (const gchar *path);
 FILE*    tracker_file_open                                  (const gchar *path);
 void     tracker_file_close                                 (FILE        *file,
                                                              gboolean     need_again_soon);
diff --git a/src/tracker-extract/tracker-extract-abw.c b/src/tracker-extract/tracker-extract-abw.c
index ca01656..6847787 100644
--- a/src/tracker-extract/tracker-extract-abw.c
+++ b/src/tracker-extract/tracker-extract-abw.c
@@ -180,10 +180,7 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
 	f = tracker_extract_info_get_file (info);
 	filename = g_file_get_path (f);
 
-	fd = g_open (filename, O_RDONLY | O_NOATIME, 0);
-	if (fd == -1 && errno == EPERM) {
-		fd = g_open (filename, O_RDONLY, 0);
-	}
+	fd = tracker_file_open_fd (filename);
 
 	if (fd == -1) {
 		g_warning ("Could not open abw file '%s': %s\n",
diff --git a/src/tracker-extract/tracker-extract-gif.c b/src/tracker-extract/tracker-extract-gif.c
index 94e5d3b..884edf5 100644
--- a/src/tracker-extract/tracker-extract-gif.c
+++ b/src/tracker-extract/tracker-extract-gif.c
@@ -606,10 +606,7 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
 		return FALSE;
 	}
 
-	fd = g_open (filename, O_RDONLY | O_NOATIME, 0);
-	if (fd == -1 && errno == EPERM) {
-		fd = g_open (filename, O_RDONLY, 0);
-	}
+	fd = tracker_file_open_fd (filename);
 
 	if (fd == -1) {
 		g_warning ("Could not open gif file '%s': %s\n",
diff --git a/src/tracker-extract/tracker-extract-mp3.c b/src/tracker-extract/tracker-extract-mp3.c
index 8fd204d..7484092 100644
--- a/src/tracker-extract/tracker-extract-mp3.c
+++ b/src/tracker-extract/tracker-extract-mp3.c
@@ -2093,25 +2093,11 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
 	md.size = size;
 	buffer_size = MIN (size, MAX_FILE_READ);
 
-#if defined(__linux__)
-	/* Can return -1 because of O_NOATIME, so we try again after
-	 * without as a last resort. This can happen due to
-	 * permissions.
-	 */
-	fd = g_open (filename, O_RDONLY | O_NOATIME);
-	if (fd == -1 && errno == EPERM) {
-		fd = g_open (filename, O_RDONLY);
+	fd = tracker_file_open_fd (filename);
 
-		if (fd == -1) {
-			return FALSE;
-		}
-	}
-#else
-	fd = open (filename, O_RDONLY);
 	if (fd == -1) {
 		return FALSE;
 	}
-#endif
 
 #ifndef G_OS_WIN32
 	/* We don't use GLib's mmap because size can not be specified */
diff --git a/src/tracker-extract/tracker-extract-pdf.c b/src/tracker-extract/tracker-extract-pdf.c
index 070d142..d8c8035 100644
--- a/src/tracker-extract/tracker-extract-pdf.c
+++ b/src/tracker-extract/tracker-extract-pdf.c
@@ -311,10 +311,7 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
 	file = tracker_extract_info_get_file (info);
 	filename = g_file_get_path (file);
 
-	fd = g_open (filename, O_RDONLY | O_NOATIME, 0);
-	if (fd == -1 && errno == EPERM) {
-		fd = g_open (filename, O_RDONLY, 0);
-	}
+	fd = tracker_file_open_fd (filename);
 
 	if (fd == -1) {
 		g_warning ("Could not open pdf file '%s': %s\n",
diff --git a/src/tracker-extract/tracker-extract-text.c b/src/tracker-extract/tracker-extract-text.c
index 8fdb881..51dddd1 100644
--- a/src/tracker-extract/tracker-extract-text.c
+++ b/src/tracker-extract/tracker-extract-text.c
@@ -58,10 +58,7 @@ get_file_content (GFile *file,
 	/* Get filename from URI */
 	path = g_file_get_path (file);
 
-	fd = g_open (path, O_RDONLY | O_NOATIME, 0);
-	if (fd == -1 && errno == EPERM) {
-		fd = g_open (path, O_RDONLY, 0);
-	}
+	fd = tracker_file_open_fd (path);
 
 	if (fd == -1) {
 		g_message ("Could not open file '%s': %s",
diff --git a/src/tracker-extract/tracker-extract-tiff.c b/src/tracker-extract/tracker-extract-tiff.c
index 20ceb88..39f1590 100644
--- a/src/tracker-extract/tracker-extract-tiff.c
+++ b/src/tracker-extract/tracker-extract-tiff.c
@@ -291,10 +291,7 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
 	metadata = tracker_extract_info_get_metadata_builder (info);
 	graph = tracker_extract_info_get_graph (info);
 
-	fd = g_open (filename, O_RDONLY | O_NOATIME, 0);
-	if (fd == -1 && errno == EPERM) {
-		fd = g_open (filename, O_RDONLY, 0);
-	}
+	fd = tracker_file_open_fd (filename);
 
 	if (fd == -1) {
 		g_warning ("Could not open tiff file '%s': %s\n",
diff --git a/src/tracker-extract/tracker-extract-xmp.c b/src/tracker-extract/tracker-extract-xmp.c
index 2f57be3..34afc08 100644
--- a/src/tracker-extract/tracker-extract-xmp.c
+++ b/src/tracker-extract/tracker-extract-xmp.c
@@ -154,10 +154,7 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
 	preupdate = tracker_extract_info_get_preupdate_builder (info);
 	metadata = tracker_extract_info_get_metadata_builder (info);
 
-	fd = g_open (filename, O_RDONLY | O_NOATIME, 0);
-	if (fd == -1 && errno == EPERM) {
-		fd = g_open (filename, O_RDONLY, 0);
-	}
+	fd = tracker_file_open_fd (filename);
 
 	if (fd == -1) {
 		g_warning ("Could not open xmp file '%s': %s\n",



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