[PATCH 8/8] Files: add a wrapper for zip_open() on Win32

Lubomir I. Ivanov neolit123 at gmail.com
Wed Dec 18 16:03:58 UTC 2013


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

For this wrapper we use zip_fdopen(), which allows
us to pass a descriptor instead of a path as zip_open()
does not have a wchar_t version.

This wrapper is read-only, because of how zip_fdopen()
works.

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---
thiago suggested that we rename the function to have a _readonly
suffix. this makes good sense because the api user can get
confused why he can't open a zip file for writing with this
wrapper function.
---
 dive.h    |  2 ++
 file.c    |  4 ++--
 linux.c   | 10 ++++++++++
 macos.c   | 10 ++++++++++
 windows.c | 20 ++++++++++++++++++++
 5 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/dive.h b/dive.h
index 46cbbf6..1de40b1 100644
--- a/dive.h
+++ b/dive.h
@@ -638,6 +638,8 @@ extern int subsurface_open(const char *path, int oflags, mode_t mode);
 extern FILE *subsurface_fopen(const char *path, const char *mode);
 extern int subsurface_sqlite3_open(const char *path, sqlite3 **handle);
 extern void *subsurface_opendir(const char *path);
+extern struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp);
+extern int subsurface_zip_close(struct zip *zip);
 
 extern void shift_times(const timestamp_t amount);
 
diff --git a/file.c b/file.c
index b265023..8f6dfb0 100644
--- a/file.c
+++ b/file.c
@@ -81,7 +81,7 @@ static int try_to_open_zip(const char *filename, struct memblock *mem, char **er
 {
 	int success = 0;
 	/* Grr. libzip needs to re-open the file, it can't take a buffer */
-	struct zip *zip = zip_open(filename, ZIP_CHECKCONS, NULL);
+	struct zip *zip = subsurface_zip_open_readonly(filename, ZIP_CHECKCONS, NULL);
 
 	if (zip) {
 		int index;
@@ -93,7 +93,7 @@ static int try_to_open_zip(const char *filename, struct memblock *mem, char **er
 			zip_fclose(file);
 			success++;
 		}
-		zip_close(zip);
+		subsurface_zip_close(zip);
 	}
 	return success;
 }
diff --git a/linux.c b/linux.c
index 5380d2c..521aaad 100644
--- a/linux.c
+++ b/linux.c
@@ -120,3 +120,13 @@ void *subsurface_opendir(const char *path)
 {
 	return (void *)opendir(path);
 }
+
+struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp)
+{
+	return zip_open(path, flags, errorp);
+}
+
+int subsurface_zip_close(struct zip *zip, char *tmpfile)
+{
+	return zip_close(zip);
+}
diff --git a/macos.c b/macos.c
index ffae52a..a127a0e 100644
--- a/macos.c
+++ b/macos.c
@@ -100,3 +100,13 @@ void *subsurface_opendir(const char *path)
 {
 	return (void *)opendir(path);
 }
+
+struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp)
+{
+	return zip_open(path, flags, errorp);
+}
+
+int subsurface_zip_close(struct zip *zip, char *tmpfile)
+{
+	return zip_close(zip);
+}
diff --git a/windows.c b/windows.c
index ad07ee9..255e4e6 100644
--- a/windows.c
+++ b/windows.c
@@ -9,6 +9,7 @@
 #include <sqlite3.h>
 #include <assert.h>
 #include <dirent.h>
+#include <zip.h>
 
 const char system_divelist_default_font[] = "Sans 8";
 
@@ -165,3 +166,22 @@ void *subsurface_opendir(const char *path)
 	}
 	return (void *)ret;
 }
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+/* we use zip_fdopen since zip_open doesn't have a wchar_t version */
+struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp)
+{
+	int fd = subsurface_open(path, O_RDONLY | O_BINARY, 0);
+	struct zip *ret = zip_fdopen(fd, flags, errorp);
+	if (!ret)
+		close(fd);
+	return ret;
+}
+
+int subsurface_zip_close(struct zip *zip)
+{
+	return zip_close(zip);
+}
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list