Skip to contentSkip to conversation

Provide an access() wrapper for Windows

PATCH In review David Sommerseth 4 participants 5 messages Dec 2, 2011

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Provide an access() wrapper for Windows

Commit 0f2bc0dd92f43c91e33bba8a66b06b98f281efc1 adds extra file and directory checks at startup. However, this patch makes use of some features not directly available in Windows.

This patch avoids #include <unistd.h> on Windows and provides a wrapper for the needed access() function. The wrapper function which is global for all platforms is called openvpn_access()

---
 misc.h    |   12 ++++++++++++
 options.c |    9 ++++-----
 syshead.h |    3 +++
 win32.h   |   20 ++++++++++++++++++++
 4 files changed, 39 insertions(+), 5 deletions(-)
misc.h +12 -0
index 9b35f18..223467e 100644
@@ -387,6 +387,18 @@ const char *openvpn_basename (const char *path);
 /* return the dirname of path - POSIX variant, which modifies the input */
 const char *openvpn_dirname (char *path);
 
+/* access() wrapper, for both Windows and POSIX environments */
+#ifdef WIN32
+static inline int
+openvpn_access( const char *path, int mode) {
+  return _access(path, mode);
+}
+#else
+static inline int
+openvpn_access( const char *path, int mode) {
+  return access(path, mode);
+}
+#endif
 /*
  * A printf-like function (that only recognizes a subset of standard printf
  * format operators) that prints arguments to an argv list instead
options.c +4 -5
index a29c115..891bb7f 100644
@@ -52,7 +52,6 @@
 #include "configure.h"
 #include "forward.h"
 #include <ctype.h>
-#include <unistd.h>
 
 #include "memdbg.h"
 
@@ -2617,18 +2616,18 @@ check_file_access(const int type, const char *file, const int mode, const char *
       char *fullpath = strdup(file);  /* POSIX dirname() implementaion may modify its arguments */
       const char *dirpath = openvpn_dirname(fullpath);
 
-      if (access (dirpath, mode|X_OK) != 0)
+      if (openvpn_access (dirpath, mode|X_OK) != 0)
           errcode = errno;
       free(fullpath);
     }
 
   /* Is the file itself accessible? */
-  if (!errcode && (type & CHKACC_FILE) && (access (file, mode) != 0) )
+  if (!errcode && (type & CHKACC_FILE) && (openvpn_access (file, mode) != 0) )
       errcode = errno;
 
   /* If the file exists and is accessible, is it writable? */
-  if (!errcode && (type & CHKACC_FILEXSTWR) && (access (file, F_OK) == 0) )
-    if (access (file, W_OK) != 0)
+  if (!errcode && (type & CHKACC_FILEXSTWR) && (openvpn_access (file, F_OK) == 0) )
+    if (openvpn_access (file, W_OK) != 0)
       errcode = errno;
 
   /* Scream if an error is found */
syshead.h +3 -0
index f3c0ac9..232c214 100644
@@ -52,9 +52,12 @@
 #ifdef WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <io.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
+#else
+#include <unistd.h>
 #endif
 
 #if defined(__APPLE__)
win32.h +20 -0
index 23c04be..2b9cb8b 100644
@@ -70,6 +70,26 @@ struct security_attributes
 
 #define HANDLE_DEFINED(h) ((h) != NULL && (h) != INVALID_HANDLE_VALUE)
 
+/* Provide wrapper for the access() function
+ * based on information found here:
+ * http://msdn.microsoft.com/en-us/library/1w06ktdy%28v=vs.80%29.aspx
+ */
+#ifndef R_OK
+#define R_OK 4
+#endif
+
+#ifndef W_OK
+#define W_OK 2
+#endif
+
+#ifndef X_OK
+#define X_OK 1
+#endif
+
+#ifndef F_OK
+#define F_OK 0
+#endif
+
 /*
  * Save old window title.
  */
Signature · 1 line
1.7.4.4
#1 lore raw

Why not:
#ifdef WIN32
#define access _access
#endif

Quoted 144 lines · David Sommerseth

Commit 0f2bc0dd92f43c91e33bba8a66b06b98f281efc1 adds extra file and directory checks at startup. However, this patch makes use of some features not directly available in Windows.

This patch avoids #include <unistd.h> on Windows and provides a wrapper for the needed access() function. The wrapper function which is global for all platforms is called openvpn_access()

---
 misc.h    |   12 ++++++++++++
 options.c |    9 ++++-----
 syshead.h |    3 +++
 win32.h   |   20 ++++++++++++++++++++
 4 files changed, 39 insertions(+), 5 deletions(-)
misc.h +12 -0
index 9b35f18..223467e 100644
@@ -387,6 +387,18 @@ const char *openvpn_basename (const char *path);
 /* return the dirname of path - POSIX variant, which modifies the input */
 const char *openvpn_dirname (char *path);
 
+/* access() wrapper, for both Windows and POSIX environments */
+#ifdef WIN32
+static inline int
+openvpn_access( const char *path, int mode) {
+  return _access(path, mode);
+}
+#else
+static inline int
+openvpn_access( const char *path, int mode) {
+  return access(path, mode);
+}
+#endif
 /*
 * A printf-like function (that only recognizes a subset of standard printf
 * format operators) that prints arguments to an argv list instead
options.c +0 -1
index a29c115..891bb7f 100644
@@ -52,7 +52,6 @@
 #include "configure.h"
 #include "forward.h"
 #include <ctype.h>
-#include <unistd.h>
 
 #include "memdbg.h"
 
@@ -2617,18 +2616,18 @@ check_file_access(const int type, const char
 *file, const int mode, const char *
      char *fullpath = strdup(file);  /* POSIX dirname() implementaion may

modify its arguments */

      const char *dirpath = openvpn_dirname(fullpath);
options.c +2 -2
-      if (access (dirpath, mode|X_OK) != 0)
+      if (openvpn_access (dirpath, mode|X_OK) != 0)
          errcode = errno;
      free(fullpath);
    }
 
  /* Is the file itself accessible? */
-  if (!errcode && (type & CHKACC_FILE) && (access (file, mode) != 0) )
+  if (!errcode && (type & CHKACC_FILE) && (openvpn_access (file, mode) !=

0) )

      errcode = errno;

/* If the file exists and is accessible, is it writable? */
- if (!errcode && (type & CHKACC_FILEXSTWR) && (access (file, F_OK) == 0) )

options.c +1 -1
-    if (access (file, W_OK) != 0)
+  if (!errcode && (type & CHKACC_FILEXSTWR) && (openvpn_access (file,

F_OK) == 0) )

options.c +1 -0
+    if (openvpn_access (file, W_OK) != 0)
      errcode = errno;
 
  /* Scream if an error is found */
syshead.h +3 -0
index f3c0ac9..232c214 100644
@@ -52,9 +52,12 @@
 #ifdef WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <io.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
+#else
+#include <unistd.h>
 #endif
 
 #if defined(__APPLE__)
win32.h +20 -1
index 23c04be..2b9cb8b 100644
@@ -70,6 +70,26 @@ struct security_attributes
 
 #define HANDLE_DEFINED(h) ((h) != NULL && (h) != INVALID_HANDLE_VALUE)
 
+/* Provide wrapper for the access() function
+ * based on information found here:
+ * http://msdn.microsoft.com/en-us/library/1w06ktdy%28v=vs.80%29.aspx
+ */
+#ifndef R_OK
+#define R_OK 4
+#endif
+
+#ifndef W_OK
+#define W_OK 2
+#endif
+
+#ifndef X_OK
+#define X_OK 1
+#endif
+
+#ifndef F_OK
+#define F_OK 0
+#endif
+
 /*
 * Save old window title.
 */
--

1.7.4.4

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Openvpn-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#2 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 1 line · Alon Bar-Lev

Why not: #ifdef WIN32 #define access _access #endif

No particular reason, other than the approach with static inline functions seems more cleaner in my eyes. The first version of this patch declared an access() function in win32.h instead. But that didn't compile so well in Visual Studio.

As it is a wrapper function for platform neutrality, prefixing it with openvpn_ makes that much clearer when reading the code.

kind regards,

David Sommerseth

Quoted 77 lines

On Fri, Dec 2, 2011 at 2:17 PM, David Sommerseth <[email protected]... <mailto:[email protected]...>> wrote:

Commit 0f2bc0dd92f43c91e33bba8a66b06b98f281efc1 adds extra file and directory checks at startup. However, this patch makes use of some features not directly available in Windows.

This patch avoids #include <unistd.h> on Windows and provides a wrapper for the needed access() function. The wrapper function which is global for all platforms is called openvpn_access()

<mailto:[email protected]...>> Cc: Heiko Hund <[email protected]...
<mailto:[email protected]...>> --- misc.h | 12 ++++++++++++
options.c | 9 ++++----- syshead.h | 3 +++ win32.h | 20
++++++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-)

misc.h index 9b35f18..223467e 100644 --- renamed from misc.h +0 -0

a/misc.h +++ b/misc.h @@ -387,6 +387,18 @@ const char *openvpn_basename (const char *path); /* return the dirname of path - POSIX variant, which modifies the input */ const char *openvpn_dirname (char *path);

misc.h index 9b35f18..223467e 100644 --- renamed from misc.h +2 -0
+/* access() wrapper, for both Windows and POSIX environments */ 
+#ifdef WIN32 +static inline int +openvpn_access( const char *path,

int mode) { + return _access(path, mode); +} +#else +static inline int +openvpn_access( const char *path, int mode) { + return access(path, mode); +} +#endif /* * A printf-like function (that only recognizes a subset of standard printf * format operators) that prints arguments to an argv list instead diff --git a/options.c b/options.c index a29c115..891bb7f 100644 --- a/options.c +++ b/options.c @@ -52,7 +52,6 @@ #include "configure.h" #include "forward.h" #include <ctype.h> -#include <unistd.h>

#include "memdbg.h"

misc.h index 9b35f18..223467e 100644 --- renamed from misc.h +0 -0
@@ -2617,18 +2616,18 @@ check_file_access(const int type, const char 
 *file, const int mode, const char * char *fullpath = strdup(file);  /*

POSIX dirname() implementaion may modify its arguments */ const char *dirpath = openvpn_dirname(fullpath);

- if (access (dirpath, mode|X_OK) != 0) + if (openvpn_access
(dirpath, mode|X_OK) != 0) errcode = errno; free(fullpath); }

/* Is the file itself accessible? */ - if (!errcode && (type & CHKACC_FILE) && (access (file, mode) != 0) ) + if (!errcode && (type & CHKACC_FILE) && (openvpn_access (file, mode) != 0) ) errcode = errno;

/* If the file exists and is accessible, is it writable? */ - if (!errcode && (type & CHKACC_FILEXSTWR) && (access (file, F_OK) == 0)
) - if (access (file, W_OK) != 0) + if (!errcode && (type &
CHKACC_FILEXSTWR) && (openvpn_access (file, F_OK) == 0) ) + if
(openvpn_access (file, W_OK) != 0) errcode = errno;

/* Scream if an error is found */ diff --git a/syshead.h b/syshead.h index f3c0ac9..232c214 100644 --- a/syshead.h +++ b/syshead.h @@ -52,9 +52,12 @@ #ifdef WIN32 #include <windows.h> #include <winsock2.h> +#include <io.h> #define sleep(x) Sleep((x)*1000) #define random rand #define srandom srand +#else +#include <unistd.h> #endif

#if defined(__APPLE__) diff --git a/win32.h b/win32.h index 23c04be..2b9cb8b 100644 --- a/win32.h +++ b/win32.h @@ -70,6 +70,26 @@ struct security_attributes

#define HANDLE_DEFINED(h) ((h) != NULL && (h) !=
INVALID_HANDLE_VALUE)

+/* Provide wrapper for the access() function + * based on information found here: + *
http://msdn.microsoft.com/en-us/library/1w06ktdy%28v=vs.80%29.aspx + */ +#ifndef R_OK +#define R_OK 4 +#endif + +#ifndef W_OK +#define W_OK 2 +#endif + +#ifndef X_OK +#define X_OK 1 +#endif + +#ifndef F_OK +#define F_OK 0 +#endif + /* * Save old window title. */ -- 1.7.4.4

Quoted 3 lines

------------------------------------------------------------------------------

All the data continuously generated in your IT infrastructure

Quoted 12 lines

contains a definitive record of customers, application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________ Openvpn-devel mailing list [email protected] <mailto:[email protected]>
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Quoted 3 lines

------------------------------------------------------------------------------

All the data continuously generated in your IT infrastructure

Quoted 10 lines

contains a definitive record of customers, application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d

_______________________________________________ Openvpn-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7YxrEACgkQDC186MBRfrqMsACgiUfyx5DXS+AqduQMFro5yPpC 6UwAoI1ihEMnMsGNZkRgrEyNfRXUlvBT
=GPvs
-----END PGP SIGNATURE-----

#3 lore raw
In reply to Alon Bar-Lev
Quoted 4 lines · Alon Bar-Lev

Why not:
#ifdef WIN32
#define access _access
#endif

We should write "#ifndef _MSC_VER". MingW's access() handles X_OK correctly. From MingW's <io.h>:

#ifdef __USE_MINGW_ACCESS
/* Old versions of MSVCRT access() just ignored X_OK, while the version
shipped with Vista, returns an error code. This will restore the old behaviour */
static inline int __mingw_access (const char* __fname, int __mode)
{ return _access (__fname, __mode & ~X_OK); }
#define access(__f,__m) __mingw_access (__f, __m)
#endif

--gv

#4 lore raw

If using mingw there is no reason why not use autoconf/automake. If using MSVC you know what you get.

This is how I see it:

config-msvc.h
#define HAVE_ACCESS 1
#define access _access

compat.h
#ifndef HAVE_ACCESS
int access(...);
#endif

compat.c
#ifndef HAVE_ACCESS
int access(...) {
}
#endif

At every file:
#ifdef HAVE_CONFIG_H
#include <config.h>
#elif defined(_MSC_VER)
#include <config-msvc.h>
#endif

Quoted 34 lines · Gisle Vanem
Quoted 4 lines · Alon Bar-Lev

Why not:
#ifdef WIN32
#define access _access
#endif

We should write "#ifndef _MSC_VER". MingW's access() handles X_OK correctly. From MingW's <io.h>:

#ifdef __USE_MINGW_ACCESS
/*  Old versions of MSVCRT access() just ignored X_OK, while the version shipped with Vista, returns an error code.  This will restore the old behaviour  */
static inline int __mingw_access (const char* __fname, int __mode) { return  _access (__fname, __mode & ~X_OK); }
#define access(__f,__m)  __mingw_access (__f, __m)
#endif

--gv

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Openvpn-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel