From ce163b24acfa397755b7193eccf2ccc09482573e Mon Sep 17 00:00:00 2001
From: "Lubomir I. Ivanov" <neolit123@gmail.com>
Date: Thu, 13 Nov 2014 19:36:08 +0200
Subject: [PATCH 1/2] Add subsurface_access()

For our usage the method will acept UTF-8 paths,
which are converted to UTF-16 on Win32.

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
---
 dive.h            |  1 +
 libdivecomputer.c |  2 +-
 linux.c           |  6 ++++++
 macos.c           |  6 ++++++
 windows.c         | 13 +++++++++++++
 5 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/dive.h b/dive.h
index 272edf7..c745255 100644
--- a/dive.h
+++ b/dive.h
@@ -653,6 +653,7 @@ extern int subsurface_rename(const char *path, const char *newpath);
 extern int subsurface_open(const char *path, int oflags, mode_t mode);
 extern FILE *subsurface_fopen(const char *path, const char *mode);
 extern void *subsurface_opendir(const char *path);
+extern int subsurface_access(const char *path, int mode);
 extern struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp);
 extern int subsurface_zip_close(struct zip *zip);
 extern void subsurface_console_init(bool dedicated);
diff --git a/libdivecomputer.c b/libdivecomputer.c
index 991aaaa..17bd01a 100644
--- a/libdivecomputer.c
+++ b/libdivecomputer.c
@@ -849,7 +849,7 @@ const char *do_libdivecomputer_import(device_data_t *data)
 		err = do_device_import(data);
 		/* TODO: Show the logfile to the user on error. */
 		dc_device_close(data->device);
-	} else if (access(data->devname, R_OK | W_OK) != 0)
+	} else if (subsurface_access(data->devname, R_OK | W_OK) != 0)
 		err = translate("gettextFromC", "Insufficient privileges to open the device %s %s (%s)");
 
 	dc_context_free(data->context);
diff --git a/linux.c b/linux.c
index caf06b9..22d795d 100644
--- a/linux.c
+++ b/linux.c
@@ -1,5 +1,6 @@
 /* linux.c */
 /* implements Linux specific functions */
+#include <io.h>
 #include "dive.h"
 #include "display.h"
 #include "membuffer.h"
@@ -164,6 +165,11 @@ void *subsurface_opendir(const char *path)
 	return (void *)opendir(path);
 }
 
+int subsurface_access(const char *path, int mode)
+{
+	return access(path, mode);
+}
+
 struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp)
 {
 	return zip_open(path, flags, errorp);
diff --git a/macos.c b/macos.c
index 91992d8..8c2061e 100644
--- a/macos.c
+++ b/macos.c
@@ -1,5 +1,6 @@
 /* macos.c */
 /* implements Mac OS X specific functions */
+#include <io.h>
 #include <stdlib.h>
 #include <dirent.h>
 #include <fnmatch.h>
@@ -147,6 +148,11 @@ void *subsurface_opendir(const char *path)
 	return (void *)opendir(path);
 }
 
+int subsurface_access(const char *path, int mode)
+{
+	return access((path, mode);
+}
+
 struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp)
 {
 	return zip_open(path, flags, errorp);
diff --git a/windows.c b/windows.c
index 386b52e..02b396f 100644
--- a/windows.c
+++ b/windows.c
@@ -1,5 +1,6 @@
 /* windows.c */
 /* implements Windows specific functions */
+#include <io.h>
 #include "dive.h"
 #include "display.h"
 #undef _WIN32_WINNT
@@ -235,6 +236,18 @@ void *subsurface_opendir(const char *path)
 	return (void *)ret;
 }
 
+int subsurface_access(const char *path, int mode)
+{
+	int ret = -1;
+	if (!path)
+		return ret;
+	wchar_t *wpath = utf8_to_utf16(path);
+	if (wpath)
+		ret = _waccess(wpath, mode);
+	free((void *)wpath);
+	return ret;
+}
+
 #ifndef O_BINARY
 #define O_BINARY 0
 #endif
-- 
1.7.11.msysgit.0

