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