[Openvpn-devel] [M] Change in openvpn[master]: platform: Add function to determine temp directory

"cron2 \(Code Review\) via Openvpn-devel" <[email protected]>
Newsgroups net.sourceforge.lists.openvpn-devel
Message-ID <0c1d5a5dacc9812b31df09c847e526840bade5b8-EmailReplacePatchSet-HTML@gerrit.openvpn.net>
cron2 has uploaded a new patch set (#2) to the change originally created by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1780?usp=email )

The following approvals got outdated and were removed:
Code-Review+2 by razvanc


Change subject: platform: Add function to determine temp directory
......................................................................

platform: Add function to determine temp directory

Move the code out of options.c and make it shareable.
Replace various stripped down versions of the code from
test_*

Change-Id: I40f72dc038a38e5ad1d3dd04cb10cb7d5cad5d19
Signed-off-by: Frank Lichtenheld <[email protected]>
Acked-by: Razvan Cojocaru <[email protected]>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1780
Message-Id: <[email protected]>
URL: https://www.mail-archive.com/[email protected]/msg38408.html
Signed-off-by: Gert Doering <[email protected]>
---
M src/openvpn/options.c
M src/openvpn/platform.c
M src/openvpn/platform.h
M tests/unit_tests/openvpn/test_ssl.c
M tests/unit_tests/openvpn/test_tls_crypt.c
5 files changed, 40 insertions(+), 35 deletions(-)


  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/80/1780/2

diff --git a/src/openvpn/options.c b/src/openvpn/options.c
index d0f447c..4aebfea 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/options.c
@@ -885,25 +885,8 @@
     o->auth_token_generate = false;
 
     /* Set default --tmp-dir */
-#ifdef _WIN32
-    /* On Windows, find temp dir via environment variables */
-    o->tmp_dir = win_get_tempdir();
+    o->tmp_dir = platform_get_tmp_dir();
 
-    if (!o->tmp_dir)
-    {
-        /* Error out if we can't find a valid temporary directory, which should
-         * be very unlikely. */
-        msg(M_USAGE, "Could not find a suitable temporary directory."
-                     " (GetTempPath() failed).  Consider using --tmp-dir");
-    }
-#else  /* ifdef _WIN32 */
-    /* Non-windows platforms use $TMPDIR, and if not set, default to '/tmp' */
-    o->tmp_dir = getenv("TMPDIR");
-    if (!o->tmp_dir)
-    {
-        o->tmp_dir = "/tmp";
-    }
-#endif /* _WIN32 */
     o->allow_recursive_routing = false;
 
 #ifndef ENABLE_DCO
diff --git a/src/openvpn/platform.c b/src/openvpn/platform.c
index 85b6408..6c766c2 100644
--- a/src/openvpn/platform.c
+++ b/src/openvpn/platform.c
@@ -584,6 +584,34 @@
     return NULL;
 }
 
+const char *
+platform_get_tmp_dir(void)
+{
+    const char *ret;
+#ifdef _WIN32
+    /* On Windows, find temp dir via environment variables */
+    ret = win_get_tempdir();
+
+    if (!ret)
+    {
+        /* Error out if we can't find a valid temporary directory, which should
+         * be very unlikely. */
+        msg(M_USAGE, "Could not find a suitable temporary directory."
+                     " (GetTempPath() failed).  Consider using --tmp-dir");
+    }
+#else
+    /* Non-windows platforms use $TMPDIR, and if not set, default to '/tmp' */
+    ret = getenv("TMPDIR");
+    if (!ret)
+    {
+        ret = "/tmp";
+    }
+#endif
+
+    return ret;
+}
+
+
 /*
  * Put a directory and filename together.
  */
diff --git a/src/openvpn/platform.h b/src/openvpn/platform.h
index 8e7ab7e..784427bb 100644
--- a/src/openvpn/platform.h
+++ b/src/openvpn/platform.h
@@ -128,6 +128,13 @@
 const char *platform_create_temp_file(const char *directory, const char *prefix,
                                       struct gc_arena *gc);
 
+/**
+ * Get a directory for temporary files
+ *
+ * @return path to a directory
+ */
+const char *platform_get_tmp_dir(void);
+
 /** Put a directory and filename together. */
 const char *platform_gen_path(const char *directory, const char *filename, struct gc_arena *gc);
 
diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn/test_ssl.c
index cb9c480..d6b63d0 100644
--- a/tests/unit_tests/openvpn/test_ssl.c
+++ b/tests/unit_tests/openvpn/test_ssl.c
@@ -117,19 +117,6 @@
     "-----END PRIVATE KEY-----\n";
 
 
-static const char *
-get_tmp_dir(void)
-{
-    const char *ret;
-#ifdef _WIN32
-    ret = win_get_tempdir();
-#else
-    ret = "/tmp";
-#endif
-    assert_non_null(ret);
-    return ret;
-}
-
 static struct
 {
     struct gc_arena gc;
@@ -142,8 +129,8 @@
 {
     (void)state;
     global_state.gc = gc_new();
-    global_state.certfile = platform_create_temp_file(get_tmp_dir(), "cert", &global_state.gc);
-    global_state.keyfile = platform_create_temp_file(get_tmp_dir(), "key", &global_state.gc);
+    global_state.certfile = platform_create_temp_file(platform_get_tmp_dir(), "cert", &global_state.gc);
+    global_state.keyfile = platform_create_temp_file(platform_get_tmp_dir(), "key", &global_state.gc);
 
     int certfd = open(global_state.certfile, O_RDWR);
     int keyfd = open(global_state.keyfile, O_RDWR);
@@ -190,7 +177,7 @@
     cert = ctx.crt_chain;
 #endif
 
-    const char *tmpfile = platform_create_temp_file(get_tmp_dir(), "ut_pem", &gc);
+    const char *tmpfile = platform_create_temp_file(platform_get_tmp_dir(), "ut_pem", &gc);
     backend_x509_write_pem(cert, tmpfile);
 
     struct buffer exported_pem = buffer_read_from_file(tmpfile, &gc);
diff --git a/tests/unit_tests/openvpn/test_tls_crypt.c b/tests/unit_tests/openvpn/test_tls_crypt.c
index fa313d0..db03437 100644
--- a/tests/unit_tests/openvpn/test_tls_crypt.c
+++ b/tests/unit_tests/openvpn/test_tls_crypt.c
@@ -720,7 +720,7 @@
         tls_options.tls_crypt_v2_verify_script = "/bin/true";
     }
 
-    tls_options.tmp_dir = "/tmp";
+    tls_options.tmp_dir = platform_get_tmp_dir();
 
     /* Since we override rand_bytes the tmpfile name is non-random as well.
      * Build the expected name via the same code path as

-- 
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1780?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I40f72dc038a38e5ad1d3dd04cb10cb7d5cad5d19
Gerrit-Change-Number: 1780
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <[email protected]>
Gerrit-Reviewer: plaisthos <[email protected]>
Gerrit-Reviewer: razvanc <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.