| 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 |  | 
| Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/logging.h" | 
|  | 18 | #include "android-base/test_utils.h" | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 19 |  | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 20 | #include <fcntl.h> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 21 | #include <stdio.h> | 
|  | 22 | #include <stdlib.h> | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 24 | #include <unistd.h> | 
|  | 25 |  | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 26 | #if defined(_WIN32) | 
|  | 27 | #include <windows.h> | 
| Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 28 | #include <direct.h> | 
| Elliott Hughes | 38e2b63 | 2016-02-17 11:53:54 -0800 | [diff] [blame] | 29 | #define OS_PATH_SEPARATOR '\\' | 
|  | 30 | #else | 
|  | 31 | #define OS_PATH_SEPARATOR '/' | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 32 | #endif | 
|  | 33 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 34 | #include <string> | 
|  | 35 |  | 
| Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 36 | #ifdef _WIN32 | 
|  | 37 | int mkstemp(char* template_name) { | 
|  | 38 | if (_mktemp(template_name) == nullptr) { | 
|  | 39 | return -1; | 
|  | 40 | } | 
|  | 41 | // Use open() to match the close() that TemporaryFile's destructor does. | 
| Spencer Low | 2fbeb0c | 2015-09-01 14:57:58 -0700 | [diff] [blame] | 42 | // Use O_BINARY to match base file APIs. | 
|  | 43 | return open(template_name, O_CREAT | O_EXCL | O_RDWR | O_BINARY, | 
|  | 44 | S_IRUSR | S_IWUSR); | 
| Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
|  | 47 | char* mkdtemp(char* template_name) { | 
|  | 48 | if (_mktemp(template_name) == nullptr) { | 
|  | 49 | return nullptr; | 
|  | 50 | } | 
|  | 51 | if (_mkdir(template_name) == -1) { | 
|  | 52 | return nullptr; | 
|  | 53 | } | 
|  | 54 | return template_name; | 
|  | 55 | } | 
|  | 56 | #endif | 
|  | 57 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 58 | static std::string GetSystemTempDir() { | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 59 | #if defined(__ANDROID__) | 
| Yabin Cui | 57e9cea | 2016-12-14 17:45:49 -0800 | [diff] [blame] | 60 | const char* tmpdir = "/data/local/tmp"; | 
|  | 61 | if (access(tmpdir, R_OK | W_OK | X_OK) == 0) { | 
|  | 62 | return tmpdir; | 
|  | 63 | } | 
|  | 64 | // Tests running in app context can't access /data/local/tmp, | 
|  | 65 | // so try current directory if /data/local/tmp is not accessible. | 
|  | 66 | return "."; | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 67 | #elif defined(_WIN32) | 
| Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 68 | char tmp_dir[MAX_PATH]; | 
|  | 69 | DWORD result = GetTempPathA(sizeof(tmp_dir), tmp_dir); | 
|  | 70 | CHECK_NE(result, 0ul) << "GetTempPathA failed, error: " << GetLastError(); | 
|  | 71 | CHECK_LT(result, sizeof(tmp_dir)) << "path truncated to: " << result; | 
|  | 72 |  | 
|  | 73 | // GetTempPath() returns a path with a trailing slash, but init() | 
|  | 74 | // does not expect that, so remove it. | 
|  | 75 | CHECK_EQ(tmp_dir[result - 1], '\\'); | 
|  | 76 | tmp_dir[result - 1] = '\0'; | 
|  | 77 | return tmp_dir; | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 78 | #else | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 79 | return "/tmp"; | 
| Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 80 | #endif | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 83 | TemporaryFile::TemporaryFile() { | 
|  | 84 | init(GetSystemTempDir()); | 
|  | 85 | } | 
|  | 86 |  | 
| Yabin Cui | 464ea61 | 2017-12-06 14:20:07 -0800 | [diff] [blame] | 87 | TemporaryFile::TemporaryFile(const std::string& tmp_dir) { | 
|  | 88 | init(tmp_dir); | 
|  | 89 | } | 
|  | 90 |  | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 91 | TemporaryFile::~TemporaryFile() { | 
| Tianjie Xu | f9bc1b0 | 2017-09-11 12:01:09 -0700 | [diff] [blame] | 92 | if (fd != -1) { | 
|  | 93 | close(fd); | 
|  | 94 | } | 
| Yabin Cui | ef58cef | 2018-03-08 17:03:04 -0800 | [diff] [blame] | 95 | if (remove_file_) { | 
|  | 96 | unlink(path); | 
|  | 97 | } | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Tianjie Xu | f9bc1b0 | 2017-09-11 12:01:09 -0700 | [diff] [blame] | 100 | int TemporaryFile::release() { | 
|  | 101 | int result = fd; | 
|  | 102 | fd = -1; | 
|  | 103 | return result; | 
|  | 104 | } | 
|  | 105 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 106 | void TemporaryFile::init(const std::string& tmp_dir) { | 
| Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 107 | snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(), | 
|  | 108 | OS_PATH_SEPARATOR); | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 109 | fd = mkstemp(path); | 
| Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 110 | } | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 111 |  | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 112 | TemporaryDir::TemporaryDir() { | 
|  | 113 | init(GetSystemTempDir()); | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | TemporaryDir::~TemporaryDir() { | 
|  | 117 | rmdir(path); | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | bool TemporaryDir::init(const std::string& tmp_dir) { | 
| Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 121 | snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(), | 
|  | 122 | OS_PATH_SEPARATOR); | 
| Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 123 | return (mkdtemp(path) != nullptr); | 
|  | 124 | } | 
| Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 125 |  | 
|  | 126 | CapturedStderr::CapturedStderr() : old_stderr_(-1) { | 
|  | 127 | init(); | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | CapturedStderr::~CapturedStderr() { | 
|  | 131 | reset(); | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | int CapturedStderr::fd() const { | 
|  | 135 | return temp_file_.fd; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | void CapturedStderr::init() { | 
|  | 139 | #if defined(_WIN32) | 
|  | 140 | // On Windows, stderr is often buffered, so make sure it is unbuffered so | 
|  | 141 | // that we can immediately read back what was written to stderr. | 
|  | 142 | CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0)); | 
|  | 143 | #endif | 
|  | 144 | old_stderr_ = dup(STDERR_FILENO); | 
|  | 145 | CHECK_NE(-1, old_stderr_); | 
|  | 146 | CHECK_NE(-1, dup2(fd(), STDERR_FILENO)); | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | void CapturedStderr::reset() { | 
|  | 150 | CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO)); | 
|  | 151 | CHECK_EQ(0, close(old_stderr_)); | 
|  | 152 | // Note: cannot restore prior setvbuf() setting. | 
|  | 153 | } |