Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 56901 invoked from network); 2 Sep 2009 06:48:15 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 2 Sep 2009 06:48:15 -0000 Received: (qmail 33606 invoked by uid 500); 2 Sep 2009 06:48:15 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 33516 invoked by uid 500); 2 Sep 2009 06:48:14 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 33505 invoked by uid 99); 2 Sep 2009 06:48:14 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Sep 2009 06:48:14 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Sep 2009 06:48:13 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C95AF2388908; Wed, 2 Sep 2009 06:47:52 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r810379 - /commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c Date: Wed, 02 Sep 2009 06:47:52 -0000 To: commits@commons.apache.org From: mturk@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090902064752.C95AF2388908@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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; }