summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThom May <thommay@apache.org>2003-08-23 10:01:22 +0000
committerThom May <thommay@apache.org>2003-08-23 10:01:22 +0000
commitf1e0f6d14581714d52d0e222213d7eaca14bf56e (patch)
tree3a179ee16f60114c9088d45880bcf4c5d69962a5
parenta4c9207456eab2f69b8865c6c1372aa098bcad83 (diff)
downloadapr-f1e0f6d14581714d52d0e222213d7eaca14bf56e.tar.gz
Add apr_temp_dir_get(), a function to get the most suitable directory to place
temp files based on environment variables and other factors. Obtained from: C Mike Pilato <cmpilato@collab.net> Reviewed by: Thom May git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@64591 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--NWGNUmakefile1
-rw-r--r--apr.dsp4
-rw-r--r--file_io/unix/Makefile.in3
-rw-r--r--file_io/unix/tempdir.c166
-rw-r--r--include/apr_file_io.h16
-rw-r--r--libapr.dsp4
6 files changed, 193 insertions, 1 deletions
diff --git a/NWGNUmakefile b/NWGNUmakefile
index d70976ba6..3f1083a03 100644
--- a/NWGNUmakefile
+++ b/NWGNUmakefile
@@ -286,6 +286,7 @@ FILES_lib_objs = \
$(OBJDIR)/sockets.o \
$(OBJDIR)/sockopt.o \
$(OBJDIR)/start.o \
+ $(OBJDIR)/tempdir.o \
$(OBJDIR)/thread.o \
$(OBJDIR)/thread_cond.o \
$(OBJDIR)/thread_mutex.o \
diff --git a/apr.dsp b/apr.dsp
index 285694be8..b58c6f1b3 100644
--- a/apr.dsp
+++ b/apr.dsp
@@ -155,6 +155,10 @@ SOURCE=.\file_io\win32\readwrite.c
SOURCE=.\file_io\win32\seek.c
# End Source File
+# Begin Source File
+
+SOURCE=.\file_io\unix\tempdir.c
+# End Source File
# End Group
# Begin Group "locks"
diff --git a/file_io/unix/Makefile.in b/file_io/unix/Makefile.in
index 01473a801..fbc93b1c7 100644
--- a/file_io/unix/Makefile.in
+++ b/file_io/unix/Makefile.in
@@ -15,7 +15,8 @@ TARGETS = \
pipe.lo \
readwrite.lo \
seek.lo \
- mktemp.lo
+ mktemp.lo \
+ tempdir.lo
# bring in rules.mk for standard functionality
@INCLUDE_RULES@
diff --git a/file_io/unix/tempdir.c b/file_io/unix/tempdir.c
new file mode 100644
index 000000000..b9bc80a8b
--- /dev/null
+++ b/file_io/unix/tempdir.c
@@ -0,0 +1,166 @@
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ * if any, must include the following acknowledgment:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowledgment may appear in the software itself,
+ * if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" must
+ * not be used to endorse or promote products derived from this
+ * software without prior written permission. For written
+ * permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ * nor may "Apache" appear in their name, without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
+#include "apr_private.h"
+#include "apr_file_io.h"
+#include "apr_strings.h"
+#include "apr_env.h"
+
+/*
+ * FIXME
+ * Currently, this variable is a bit of misnomer.
+ * The intention is to have this in APR's global pool so that we don't have
+ * to go through this every time.
+ */
+static char global_temp_dir[APR_PATH_MAX+1] = { 0 };
+
+/* Try to open a temporary file in the temporary dir, write to it,
+ and then close it. */
+static int test_tempdir(const char *temp_dir, apr_pool_t *p)
+{
+ apr_file_t *dummy_file;
+ const char *path = apr_pstrcat(p, temp_dir, "/apr-tmp", NULL);
+
+ if (apr_file_mktemp(&dummy_file, (char *)path, 0, p) == APR_SUCCESS) {
+ if (apr_file_putc('!', dummy_file) == APR_SUCCESS) {
+ if (apr_file_close(dummy_file) == APR_SUCCESS) {
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+
+APR_DECLARE(apr_status_t) apr_temp_dir_get(const char **temp_dir,
+ apr_pool_t *p)
+{
+ apr_status_t apr_err;
+ const char *try_dirs[] = { "/tmp", "/usr/tmp", "/var/tmp" };
+ const char *try_envs[] = { "TMP", "TEMP", "TMPDIR" };
+ char *cwd;
+ int i;
+
+ /* Our goal is to find a temporary directory suitable for writing
+ into. We'll only pay the price once if we're successful -- we
+ cache our successful find. Here's the order in which we'll try
+ various paths:
+
+ $TMP
+ $TEMP
+ $TMPDIR
+ "C:\TEMP" (windows only)
+ "/tmp"
+ "/var/tmp"
+ "/usr/tmp"
+ P_tmpdir (POSIX define)
+ `pwd`
+
+ NOTE: This algorithm is basically the same one used by Python
+ 2.2's tempfile.py module. */
+
+ /* Try the environment first. */
+ for (i = 0; i < (sizeof(try_envs) / sizeof(const char *)); i++) {
+ char *value;
+ apr_err = apr_env_get(&value, try_envs[i], p);
+ if ((apr_err == APR_SUCCESS) && value) {
+ apr_size_t len = strlen(value);
+ if (len && (len < APR_PATH_MAX) && test_tempdir(value, p)) {
+ memcpy(global_temp_dir, value, len + 1);
+ goto end;
+ }
+ }
+ }
+
+#ifdef WIN32
+ /* Next, on Win32, try the C:\TEMP directory. */
+ if (test_tempdir("C:\\TEMP", p)) {
+ memcpy(global_temp_dir, "C:\\TEMP", 7 + 1);
+ goto end;
+ }
+#endif
+
+ /* Next, try a set of hard-coded paths. */
+ for (i = 0; i < (sizeof(try_dirs) / sizeof(const char *)); i++) {
+ if (test_tempdir(try_dirs[i], p)) {
+ memcpy(global_temp_dir, try_dirs[i], strlen(try_dirs[i]) + 1);
+ goto end;
+ }
+ }
+
+#ifdef P_tmpdir
+ /*
+ * If we have it, use the POSIX definition of where
+ * the tmpdir should be
+ */
+ if (test_tempdir(P_tmpdir, p)) {
+ memcpy(global_temp_dir, P_tmpdir, strlen(P_tmpdir) +1);
+ goto end;
+ }
+#endif
+
+ /* Finally, try the current working directory. */
+ if (APR_SUCCESS == apr_filepath_get(&cwd, APR_FILEPATH_NATIVE, p)) {
+ if (test_tempdir(cwd, p)) {
+ memcpy(global_temp_dir, cwd, strlen(cwd) + 1);
+ goto end;
+ }
+ }
+
+end:
+ if (global_temp_dir[0]) {
+ *temp_dir = apr_pstrdup(p, global_temp_dir);
+ return APR_SUCCESS;
+ }
+ return APR_EGENERAL;
+}
diff --git a/include/apr_file_io.h b/include/apr_file_io.h
index 68ad4b1c4..dbc8d422d 100644
--- a/include/apr_file_io.h
+++ b/include/apr_file_io.h
@@ -764,6 +764,22 @@ APR_DECLARE(void) apr_file_unset_inherit(apr_file_t *file);
APR_DECLARE(apr_status_t) apr_file_mktemp(apr_file_t **fp, char *templ,
apr_int32_t flags, apr_pool_t *p);
+
+/**
+ * Find an existing directory suitable as a temporary storage location.
+ * @param temp_dir The temp directory.
+ * @param p The pool to use for any necessary allocations.
+ * @remark
+ * This function uses an algorithm to search for a directory that an
+ * an application can use for temporary storage. Once such a
+ * directory is found, that location is cached by the library. Thus,
+ * callers only pay the cost of this algorithm once if that one time
+ * is successful.
+ *
+ */
+APR_DECLARE(apr_status_t) apr_temp_dir_get(const char **temp_dir,
+ apr_pool_t *p);
+
/** @} */
#ifdef __cplusplus
diff --git a/libapr.dsp b/libapr.dsp
index 95191f378..070f5ed5f 100644
--- a/libapr.dsp
+++ b/libapr.dsp
@@ -161,6 +161,10 @@ SOURCE=.\file_io\win32\readwrite.c
SOURCE=.\file_io\win32\seek.c
# End Source File
+# Begin Source File
+
+SOURCE=.\file_io\unix\tempdir.c
+# End Source File
# End Group
# Begin Group "locks"