You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/09/02 08:47:52 UTC

svn commit: r810379 - /commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c

Author: mturk
Date: Wed Sep  2 06:47:52 2009
New Revision: 810379

URL: http://svn.apache.org/viewvc?rev=810379&view=rev
Log:
Separate temp dir and temp file creation

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c?rev=810379&r1=810378&r2=810379&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c Wed Sep  2 06:47:52 2009
@@ -31,7 +31,6 @@
     L"TEMP",
     L"TMPDIR",
     L"TEMPDIR",
-    L"USERPROFILE",
     NULL
 };
 
@@ -63,7 +62,7 @@
         return ACR_EBADF;
 }
 
-static HANDLE gettemp(wchar_t *path, DWORD flags)
+static HANDLE getftemp(wchar_t *path, DWORD flags)
 {
     wchar_t *start, *trv, *suffp;
     wchar_t *pad;
@@ -148,14 +147,91 @@
     return INVALID_HANDLE_VALUE;
 }
 
+static int getdtemp(wchar_t *path)
+{
+    wchar_t *start, *trv, *suffp;
+    wchar_t *pad;
+    DWORD    rc;
+    int      randnum;
+
+    if (randseed == 0) {
+        randseed = GetTickCount();
+        srand(randseed);
+    }
+
+    for (trv = path; *trv; ++trv)
+        ;
+    suffp = trv;
+    --trv;
+    if (trv < path) {
+        ACR_SET_OS_ERROR(ACR_EINVAL);
+        return -1;
+    }
+
+    /* Fill space with random characters */
+    while (*trv == L'X') {
+        randnum = rand() % 62;
+        *trv-- = padchar[randnum];
+    }
+    start = trv + 1;
+
+    /*
+     * check the target directory.
+     */
+    for (;; --trv) {
+        if (trv <= path)
+            break;
+        if (*trv == L'/' || *trv == L'\\') {
+            wchar_t s = *trv;
+            *trv = L'\0';
+            rc = GetFileAttributesW(path);
+            *trv = s;
+            if (rv == INVALID_FILE_ATTRIBUTES)
+                return -1;
+            if (!(da & FILE_ATTRIBUTE_DIRECTORY)) {
+                ACR_SET_OS_ERROR(ACR_ENOTDIR);
+                return -1;
+            }
+            break;
+        }
+    }
+
+    for (;;) {
+        if (!CreateDirectoryW(path, NULL)) {
+            if (GetLastError() != ERROR_ALREADY_EXISTS)
+                return -1;
+        }
+        else
+            return 0;
+        /* If we have a collision, cycle through the space of filenames */
+        for (trv = start;;) {
+            if (*trv == L'\0' || trv == suffp) {
+                /* XXX: is this the correct return code? */
+                ACR_SET_OS_ERROR(ACR_EINVAL);
+                return -1;
+            }
+            pad = wcschr((wchar_t *)padchar, *trv);
+            if (pad == NULL || !*++pad) {
+                *trv++ = padchar[0];
+            }
+            else {
+                *trv++ = *pad;
+                break;
+            }
+        }
+    }
+    /*NOTREACHED*/
+    return -1;
+}
+
 static int _temp_test(const wchar_t *path)
 {
     wchar_t tp[ACR_MBUFF_SIZ];
     HANDLE  fh;
 
-    wcslcpy(tp, path, PATH_MAX);
-    wcslcat(tp, L"\\.acrXXXXXX", PATH_MAX);
-    fh = gettemp(tp, FILE_ATTRIBUTE_NORMAL);
+    wcslcpy(tp, path, TMP_PATH_MAX);
+    wcslcat(tp, L"\\.acrXXXXXX", TMP_PATH_MAX);
+    fh = getftemp(tp, FILE_ATTRIBUTE_NORMAL);
     if (IS_VALID_HANDLE(f)) {
         CloseHandle(fh);
         return 1;
@@ -209,6 +285,19 @@
     if (_temp_path[0]) {
         return _temp_path;
     }
+    else {
+        /* Finally try the users %USERPROFILE% */
+        char *val = ACR_EnvGet(L"USERPROFILE");
+        if (val && *val) {
+            if (wcslen(val) < TMP_PATH_MAX) {
+                if (_temp_test(val))
+                    wcslcpy(_temp_path, val, TMP_PATH_MAX);
+            }
+        }
+    }
+    if (_temp_path[0]) {
+        return _temp_path;
+    }
     else
         return NULL;
 }