~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/file_io/unix/tempdir.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
#include "apr_private.h"
 
17
#include "apr_file_io.h"
 
18
#include "apr_strings.h"
 
19
#include "apr_env.h"
 
20
 
 
21
 
 
22
/* Try to open a temporary file in the temporary dir, write to it,
 
23
   and then close it. */
 
24
static int test_tempdir(const char *temp_dir, apr_pool_t *p)
 
25
{
 
26
    apr_file_t *dummy_file;
 
27
    char *path = apr_pstrcat(p, temp_dir, "/apr-tmp.XXXXXX", NULL);
 
28
 
 
29
    if (apr_file_mktemp(&dummy_file, path, 0, p) == APR_SUCCESS) {
 
30
        if (apr_file_putc('!', dummy_file) == APR_SUCCESS) {
 
31
            if (apr_file_close(dummy_file) == APR_SUCCESS) {
 
32
                return 1;
 
33
            }
 
34
        }
 
35
    }
 
36
    return 0;
 
37
}
 
38
 
 
39
 
 
40
APR_DECLARE(apr_status_t) apr_temp_dir_get(const char **temp_dir, 
 
41
                                           apr_pool_t *p)
 
42
{
 
43
    apr_status_t apr_err;
 
44
    const char *try_dirs[] = { "/tmp", "/usr/tmp", "/var/tmp" };
 
45
    const char *try_envs[] = { "TMP", "TEMP", "TMPDIR" };
 
46
    const char *dir;
 
47
    char *cwd;
 
48
    int i;
 
49
 
 
50
    /* Our goal is to find a temporary directory suitable for writing
 
51
       into.  We'll only pay the price once if we're successful -- we
 
52
       cache our successful find.  Here's the order in which we'll try
 
53
       various paths:
 
54
 
 
55
          $TMP
 
56
          $TEMP
 
57
          $TMPDIR
 
58
          "C:\TEMP"     (windows only)
 
59
          "SYS:\TMP"    (netware only)
 
60
          "/tmp"
 
61
          "/var/tmp"
 
62
          "/usr/tmp"
 
63
          P_tmpdir      (POSIX define)
 
64
          `pwd` 
 
65
 
 
66
       NOTE: This algorithm is basically the same one used by Python
 
67
       2.2's tempfile.py module.  */
 
68
 
 
69
    /* Try the environment first. */
 
70
    for (i = 0; i < (sizeof(try_envs) / sizeof(const char *)); i++) {
 
71
        char *value;
 
72
        apr_err = apr_env_get(&value, try_envs[i], p);
 
73
        if ((apr_err == APR_SUCCESS) && value) {
 
74
            apr_size_t len = strlen(value);
 
75
            if (len && (len < APR_PATH_MAX) && test_tempdir(value, p)) {
 
76
                dir = value;
 
77
                goto end;
 
78
            }
 
79
        }
 
80
    }
 
81
 
 
82
#ifdef WIN32
 
83
    /* Next, on Win32, try the C:\TEMP directory. */
 
84
    if (test_tempdir("C:\\TEMP", p)) {
 
85
        dir = "C:\\TEMP";
 
86
        goto end;
 
87
    }
 
88
#endif
 
89
#ifdef NETWARE
 
90
    /* Next, on NetWare, try the SYS:/TMP directory. */
 
91
    if (test_tempdir("SYS:/TMP", p)) {
 
92
        dir = "SYS:/TMP";
 
93
        goto end;
 
94
    }
 
95
#endif
 
96
 
 
97
    /* Next, try a set of hard-coded paths. */
 
98
    for (i = 0; i < (sizeof(try_dirs) / sizeof(const char *)); i++) {
 
99
        if (test_tempdir(try_dirs[i], p)) {
 
100
            dir = try_dirs[i];
 
101
            goto end;
 
102
        }
 
103
    }
 
104
 
 
105
#ifdef P_tmpdir
 
106
    /* 
 
107
     * If we have it, use the POSIX definition of where 
 
108
     * the tmpdir should be 
 
109
     */
 
110
    if (test_tempdir(P_tmpdir, p)) {
 
111
        dir = P_tmpdir;
 
112
        goto end;
 
113
    }
 
114
#endif
 
115
    
 
116
    /* Finally, try the current working directory. */
 
117
    if (APR_SUCCESS == apr_filepath_get(&cwd, APR_FILEPATH_NATIVE, p)) {
 
118
        if (test_tempdir(cwd, p)) {
 
119
            dir = cwd;
 
120
            goto end;
 
121
        }
 
122
    }
 
123
 
 
124
    /* We didn't find a suitable temp dir anywhere */
 
125
    return APR_EGENERAL;
 
126
 
 
127
end:
 
128
    *temp_dir = apr_pstrdup(p, dir);
 
129
    return APR_SUCCESS;
 
130
}