diff options
Diffstat (limited to 'main/php_open_temporary_file.c')
-rw-r--r-- | main/php_open_temporary_file.c | 63 |
1 files changed, 54 insertions, 9 deletions
diff --git a/main/php_open_temporary_file.c b/main/php_open_temporary_file.c index b21f3ab10d..7b0f88762d 100644 --- a/main/php_open_temporary_file.c +++ b/main/php_open_temporary_file.c @@ -19,6 +19,7 @@ /* $Id$ */ #include "php.h" +#include "php_open_temporary_file.h" #include <errno.h> #include <sys/types.h> @@ -97,7 +98,13 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, zend_string **opened_path_p) { char *trailing_slash; +#ifdef PHP_WIN32 + char *opened_path = NULL; + size_t opened_path_len; + wchar_t *cwdw, *pfxw, pathw[MAXPATHLEN]; +#else char opened_path[MAXPATHLEN]; +#endif char cwd[MAXPATHLEN]; cwd_state new_state; int fd = -1; @@ -138,23 +145,47 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, zend_st trailing_slash = "/"; } +#ifndef PHP_WIN32 if (snprintf(opened_path, MAXPATHLEN, "%s%s%sXXXXXX", new_state.cwd, trailing_slash, pfx) >= MAXPATHLEN) { efree(new_state.cwd); return -1; } +#endif #ifdef PHP_WIN32 + cwdw = php_win32_ioutil_any_to_w(new_state.cwd); + pfxw = php_win32_ioutil_any_to_w(pfx); + if (!cwdw || !pfxw) { + free(cwdw); + free(pfxw); + efree(new_state.cwd); + return -1; + } + + if (GetTempFileNameW(cwdw, pfxw, 0, pathw)) { + opened_path = php_win32_ioutil_conv_w_to_any(pathw, PHP_WIN32_CP_IGNORE_LEN, &opened_path_len); + if (!opened_path || opened_path_len >= MAXPATHLEN) { + free(cwdw); + free(pfxw); + efree(new_state.cwd); + return -1; + } + assert(strlen(opened_path) == opened_path_len); - if (GetTempFileName(new_state.cwd, pfx, 0, opened_path)) { /* Some versions of windows set the temp file to be read-only, * which means that opening it will fail... */ if (VCWD_CHMOD(opened_path, 0600)) { + free(cwdw); + free(pfxw); efree(new_state.cwd); + free(opened_path); return -1; } fd = VCWD_OPEN_MODE(opened_path, open_flags, 0600); } + free(cwdw); + free(pfxw); #elif defined(HAVE_MKSTEMP) fd = mkstemp(opened_path); #else @@ -163,9 +194,16 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, zend_st } #endif +#ifdef PHP_WIN32 + if (fd != -1 && opened_path_p) { + *opened_path_p = zend_string_init(opened_path, opened_path_len, 0); + } + free(opened_path); +#else if (fd != -1 && opened_path_p) { *opened_path_p = zend_string_init(opened_path, strlen(opened_path), 0); } +#endif efree(new_state.cwd); return fd; } @@ -203,14 +241,18 @@ PHPAPI const char* php_get_temporary_directory(void) * the environment values TMP and TEMP (in order) first. */ { - char sTemp[MAX_PATH]; - DWORD len = GetTempPath(sizeof(sTemp),sTemp); + wchar_t sTemp[MAXPATHLEN]; + char *tmp; + size_t len = GetTempPathW(MAXPATHLEN, sTemp); assert(0 < len); /* should *never* fail! */ - if (sTemp[len - 1] == DEFAULT_SLASH) { - PG(php_sys_temp_dir) = estrndup(sTemp, len - 1); - } else { - PG(php_sys_temp_dir) = estrndup(sTemp, len); + + if (NULL == (tmp = php_win32_ioutil_conv_w_to_any(sTemp, len, &len))) { + return NULL; } + + PG(php_sys_temp_dir) = estrndup(tmp, len - 1); + + free(tmp); return PG(php_sys_temp_dir); } #else @@ -249,7 +291,7 @@ PHPAPI const char* php_get_temporary_directory(void) * This function should do its best to return a file pointer to a newly created * unique file, on every platform. */ -PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_string **opened_path_p, zend_bool open_basedir_check) +PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_string **opened_path_p, uint32_t flags) { int fd; const char *temp_dir; @@ -265,7 +307,7 @@ PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_strin def_tmp: temp_dir = php_get_temporary_directory(); - if (temp_dir && *temp_dir != '\0' && (!open_basedir_check || !php_check_open_basedir(temp_dir))) { + if (temp_dir && *temp_dir != '\0' && (!(flags & PHP_TMP_FILE_OPEN_BASEDIR_CHECK) || !php_check_open_basedir(temp_dir))) { return php_do_open_temporary_file(temp_dir, pfx, opened_path_p); } else { return -1; @@ -276,6 +318,9 @@ def_tmp: fd = php_do_open_temporary_file(dir, pfx, opened_path_p); if (fd == -1) { /* Use default temporary directory. */ + if (!(flags & PHP_TMP_FILE_SILENT)) { + php_error_docref(NULL, E_NOTICE, "file created in the system's temporary directory"); + } goto def_tmp; } return fd; |