Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 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 | |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame^] | 17 | #include "base/logging.h" |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 18 | #include "base/test_utils.h" |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame^] | 19 | #include "utils/Compat.h" // For OS_PATH_SEPARATOR. |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 20 | |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 24 | #include <sys/stat.h> |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 27 | #if defined(_WIN32) |
| 28 | #include <windows.h> |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame^] | 29 | #include <direct.h> |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 30 | #endif |
| 31 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 32 | #include <string> |
| 33 | |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame^] | 34 | #ifdef _WIN32 |
| 35 | int mkstemp(char* template_name) { |
| 36 | if (_mktemp(template_name) == nullptr) { |
| 37 | return -1; |
| 38 | } |
| 39 | // Use open() to match the close() that TemporaryFile's destructor does. |
| 40 | // Note that on Windows, this does CR/LF translation and _setmode() should |
| 41 | // be used to change that if appropriate. |
| 42 | return open(template_name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); |
| 43 | } |
| 44 | |
| 45 | char* mkdtemp(char* template_name) { |
| 46 | if (_mktemp(template_name) == nullptr) { |
| 47 | return nullptr; |
| 48 | } |
| 49 | if (_mkdir(template_name) == -1) { |
| 50 | return nullptr; |
| 51 | } |
| 52 | return template_name; |
| 53 | } |
| 54 | #endif |
| 55 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 56 | static std::string GetSystemTempDir() { |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 57 | #if defined(__ANDROID__) |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 58 | return "/data/local/tmp"; |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 59 | #elif defined(_WIN32) |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame^] | 60 | char tmp_dir[MAX_PATH]; |
| 61 | DWORD result = GetTempPathA(sizeof(tmp_dir), tmp_dir); |
| 62 | CHECK_NE(result, 0ul) << "GetTempPathA failed, error: " << GetLastError(); |
| 63 | CHECK_LT(result, sizeof(tmp_dir)) << "path truncated to: " << result; |
| 64 | |
| 65 | // GetTempPath() returns a path with a trailing slash, but init() |
| 66 | // does not expect that, so remove it. |
| 67 | CHECK_EQ(tmp_dir[result - 1], '\\'); |
| 68 | tmp_dir[result - 1] = '\0'; |
| 69 | return tmp_dir; |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 70 | #else |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 71 | return "/tmp"; |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 72 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 75 | TemporaryFile::TemporaryFile() { |
| 76 | init(GetSystemTempDir()); |
| 77 | } |
| 78 | |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 79 | TemporaryFile::~TemporaryFile() { |
| 80 | close(fd); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 81 | unlink(path); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 84 | void TemporaryFile::init(const std::string& tmp_dir) { |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame^] | 85 | snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(), |
| 86 | OS_PATH_SEPARATOR); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 87 | fd = mkstemp(path); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 88 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 89 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 90 | TemporaryDir::TemporaryDir() { |
| 91 | init(GetSystemTempDir()); |
| 92 | } |
| 93 | |
| 94 | TemporaryDir::~TemporaryDir() { |
| 95 | rmdir(path); |
| 96 | } |
| 97 | |
| 98 | bool TemporaryDir::init(const std::string& tmp_dir) { |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame^] | 99 | snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(), |
| 100 | OS_PATH_SEPARATOR); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 101 | return (mkdtemp(path) != nullptr); |
| 102 | } |