[gvfs] daemon: Move seek type conversion to shared library



commit 2fc159a7886a6696ca8caa218897b14150485db3
Author: Ross Lagerwall <rosslagerwall gmail com>
Date:   Sun Oct 19 09:26:02 2014 +0100

    daemon: Move seek type conversion to shared library
    
    Since converting a GSeekType into an lseek type is repeated in a few
    places, move it into shared code.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=738967

 daemon/gvfsbackendafc.c  |   12 +-----------
 daemon/gvfsbackendsmb.c  |   24 ++----------------------
 daemon/gvfsbackendtest.c |   17 +++--------------
 daemon/gvfsdaemonutils.c |   22 ++++++++++++++++++++++
 daemon/gvfsdaemonutils.h |    2 ++
 5 files changed, 30 insertions(+), 47 deletions(-)
---
diff --git a/daemon/gvfsbackendafc.c b/daemon/gvfsbackendafc.c
index 4d0213f..bb9a38e 100644
--- a/daemon/gvfsbackendafc.c
+++ b/daemon/gvfsbackendafc.c
@@ -1341,18 +1341,8 @@ g_vfs_backend_afc_seek (GVfsBackendAfc *self,
   int afc_seek_type;
   FileHandle *fh;
 
-  switch (type)
+  if ((afc_seek_type = gvfs_seek_type_to_lseek (type)) == -1)
     {
-    case G_SEEK_SET:
-      afc_seek_type = SEEK_SET;
-      break;
-    case G_SEEK_CUR:
-      afc_seek_type = SEEK_CUR;
-      break;
-    case G_SEEK_END:
-      afc_seek_type = SEEK_END;
-      break;
-    default:
       g_vfs_job_failed(job, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
                        _("Invalid seek type"));
       return 1;
diff --git a/daemon/gvfsbackendsmb.c b/daemon/gvfsbackendsmb.c
index 4b0df51..ea5024c 100644
--- a/daemon/gvfsbackendsmb.c
+++ b/daemon/gvfsbackendsmb.c
@@ -876,18 +876,8 @@ do_seek_on_read (GVfsBackend *backend,
   off_t res;
   smbc_lseek_fn smbc_lseek;
 
-  switch (type)
+  if ((whence = gvfs_seek_type_to_lseek (type)) == -1)
     {
-    case G_SEEK_SET:
-      whence = SEEK_SET;
-      break;
-    case G_SEEK_CUR:
-      whence = SEEK_CUR;
-      break;
-    case G_SEEK_END:
-      whence = SEEK_END;
-      break;
-    default:
       g_vfs_job_failed (G_VFS_JOB (job),
                        G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                        _("Unsupported seek type"));
@@ -1365,18 +1355,8 @@ do_seek_on_write (GVfsBackend *backend,
   off_t res;
   smbc_lseek_fn smbc_lseek;
 
-  switch (type)
+  if ((whence = gvfs_seek_type_to_lseek (type)) == -1)
     {
-    case G_SEEK_SET:
-      whence = SEEK_SET;
-      break;
-    case G_SEEK_CUR:
-      whence = SEEK_CUR;
-      break;
-    case G_SEEK_END:
-      whence = SEEK_END;
-      break;
-    default:
       g_vfs_job_failed (G_VFS_JOB (job),
                        G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                        _("Unsupported seek type"));
diff --git a/daemon/gvfsbackendtest.c b/daemon/gvfsbackendtest.c
index 6a7d18e..f657766 100644
--- a/daemon/gvfsbackendtest.c
+++ b/daemon/gvfsbackendtest.c
@@ -43,6 +43,7 @@
 #include "gvfsjobopenforwrite.h"
 #include "gvfsjobclosewrite.h"
 #include "gvfsjobqueryinfowrite.h"
+#include "gvfsdaemonutils.h"
 
 G_DEFINE_TYPE (GVfsBackendTest, g_vfs_backend_test, G_VFS_TYPE_BACKEND)
 
@@ -218,20 +219,8 @@ do_seek_on_read (GVfsBackend *backend,
 
   g_print ("seek_on_read (%d, %u)\n", (int)offset, type);
 
-  switch (type)
-    {
-    default:
-    case G_SEEK_SET:
-      whence = SEEK_SET;
-      break;
-    case G_SEEK_CUR:
-      whence = SEEK_CUR;
-      break;
-    case G_SEEK_END:
-      whence = SEEK_END;
-      break;
-    }
-      
+  if ((whence = gvfs_seek_type_to_lseek (type)) == -1)
+    whence = SEEK_SET;
   
   fd = GPOINTER_TO_INT (handle);
 
diff --git a/daemon/gvfsdaemonutils.c b/daemon/gvfsdaemonutils.c
index 3c33127..2be975c 100644
--- a/daemon/gvfsdaemonutils.c
+++ b/daemon/gvfsdaemonutils.c
@@ -215,3 +215,25 @@ gvfs_file_info_populate_content_types (GFileInfo  *info,
 
   g_free (free_mimetype);
 }
+
+/**
+ * gvfs_seek_type_to_lseek:
+ * @type: the seek type
+ *
+ * Takes a GSeekType and converts it to an lseek type.
+ **/
+int
+gvfs_seek_type_to_lseek (GSeekType type)
+{
+  switch (type)
+    {
+    case G_SEEK_CUR:
+      return SEEK_CUR;
+    case G_SEEK_SET:
+      return SEEK_SET;
+    case G_SEEK_END:
+      return SEEK_END;
+    default:
+      return -1;
+    }
+}
diff --git a/daemon/gvfsdaemonutils.h b/daemon/gvfsdaemonutils.h
index 0000caa..f195136 100644
--- a/daemon/gvfsdaemonutils.h
+++ b/daemon/gvfsdaemonutils.h
@@ -40,6 +40,8 @@ void       gvfs_file_info_populate_content_types  (GFileInfo        *info,
                                                     const char       *basename,
                                                     GFileType         type);
 
+int          gvfs_seek_type_to_lseek                (GSeekType         type);
+
 G_END_DECLS
 
 #endif /* __G_VFS_DAEMON_UTILS_H__ */


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