blob: 5096369e7dc9f33e6f5f232e27011813bf5b4fe3 [file] [log] [blame]
Dan Alberte3ea0582015-03-13 23:06:01 -07001/*
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 Hughesb6351622015-12-04 22:00:26 -080017#include "android-base/test_utils.h"
Dan Alberte3ea0582015-03-13 23:06:01 -070018
Dan Albert26cb8ff2015-04-29 11:32:23 -070019#include <fcntl.h>
Dan Alberte3ea0582015-03-13 23:06:01 -070020#include <stdio.h>
21#include <stdlib.h>
Dan Albert26cb8ff2015-04-29 11:32:23 -070022#include <sys/stat.h>
Dan Alberte3ea0582015-03-13 23:06:01 -070023#include <unistd.h>
24
Dan Albert26cb8ff2015-04-29 11:32:23 -070025#if defined(_WIN32)
26#include <windows.h>
Spencer Low1a19bb22015-07-31 20:21:35 -070027#include <direct.h>
Elliott Hughesa57126b2016-02-17 11:53:54 -080028#define OS_PATH_SEPARATOR '\\'
29#else
30#define OS_PATH_SEPARATOR '/'
Dan Albert26cb8ff2015-04-29 11:32:23 -070031#endif
32
Alex Valléed0ac74c2015-05-06 16:26:00 -040033#include <string>
34
Elliott Hughes1c1409f2018-05-23 09:16:46 -070035#include <android-base/file.h>
36#include <android-base/logging.h>
37
Spencer Low1a19bb22015-07-31 20:21:35 -070038#ifdef _WIN32
39int 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 Low87c71692015-09-01 14:57:58 -070044 // 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 Low1a19bb22015-07-31 20:21:35 -070047}
48
49char* 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éed0ac74c2015-05-06 16:26:00 -040060static std::string GetSystemTempDir() {
Dan Albert26cb8ff2015-04-29 11:32:23 -070061#if defined(__ANDROID__)
Yabin Cuie0cb8022016-12-14 17:45:49 -080062 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 Albert26cb8ff2015-04-29 11:32:23 -070069#elif defined(_WIN32)
Spencer Low1a19bb22015-07-31 20:21:35 -070070 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 Albert26cb8ff2015-04-29 11:32:23 -070080#else
Alex Valléed0ac74c2015-05-06 16:26:00 -040081 return "/tmp";
Dan Albert26cb8ff2015-04-29 11:32:23 -070082#endif
Dan Alberte3ea0582015-03-13 23:06:01 -070083}
84
Alex Valléed0ac74c2015-05-06 16:26:00 -040085TemporaryFile::TemporaryFile() {
86 init(GetSystemTempDir());
87}
88
Yabin Cuia11c57e2017-12-06 14:20:07 -080089TemporaryFile::TemporaryFile(const std::string& tmp_dir) {
90 init(tmp_dir);
91}
92
Dan Alberte3ea0582015-03-13 23:06:01 -070093TemporaryFile::~TemporaryFile() {
Tianjie Xu1852b802017-09-11 12:01:09 -070094 if (fd != -1) {
95 close(fd);
96 }
Yabin Cuib7abae12018-03-08 17:03:04 -080097 if (remove_file_) {
98 unlink(path);
99 }
Dan Alberte3ea0582015-03-13 23:06:01 -0700100}
101
Tianjie Xu1852b802017-09-11 12:01:09 -0700102int TemporaryFile::release() {
103 int result = fd;
104 fd = -1;
105 return result;
106}
107
Alex Valléed0ac74c2015-05-06 16:26:00 -0400108void TemporaryFile::init(const std::string& tmp_dir) {
Spencer Low1a19bb22015-07-31 20:21:35 -0700109 snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(),
110 OS_PATH_SEPARATOR);
Alex Valléed0ac74c2015-05-06 16:26:00 -0400111 fd = mkstemp(path);
Dan Alberte3ea0582015-03-13 23:06:01 -0700112}
Alex Valléed0ac74c2015-05-06 16:26:00 -0400113
Alex Valléed0ac74c2015-05-06 16:26:00 -0400114TemporaryDir::TemporaryDir() {
115 init(GetSystemTempDir());
116}
117
118TemporaryDir::~TemporaryDir() {
119 rmdir(path);
120}
121
122bool TemporaryDir::init(const std::string& tmp_dir) {
Spencer Low1a19bb22015-07-31 20:21:35 -0700123 snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(),
124 OS_PATH_SEPARATOR);
Alex Valléed0ac74c2015-05-06 16:26:00 -0400125 return (mkdtemp(path) != nullptr);
126}
Wei Wangcaee0eb2016-10-21 09:23:39 -0700127
Elliott Hughes1c1409f2018-05-23 09:16:46 -0700128CapturedStdFd::CapturedStdFd(int std_fd) : std_fd_(std_fd), old_fd_(-1) {
129 Init();
Wei Wangcaee0eb2016-10-21 09:23:39 -0700130}
131
Elliott Hughes1c1409f2018-05-23 09:16:46 -0700132CapturedStdFd::~CapturedStdFd() {
133 Reset();
Wei Wangcaee0eb2016-10-21 09:23:39 -0700134}
135
Elliott Hughes1c1409f2018-05-23 09:16:46 -0700136int CapturedStdFd::fd() const {
Wei Wangcaee0eb2016-10-21 09:23:39 -0700137 return temp_file_.fd;
138}
139
Elliott Hughes1c1409f2018-05-23 09:16:46 -0700140std::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
147void CapturedStdFd::Init() {
Wei Wangcaee0eb2016-10-21 09:23:39 -0700148#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 Hughes1c1409f2018-05-23 09:16:46 -0700151 if (std_fd_ == STDERR_FILENO) CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
Wei Wangcaee0eb2016-10-21 09:23:39 -0700152#endif
Elliott Hughes1c1409f2018-05-23 09:16:46 -0700153 old_fd_ = dup(std_fd_);
154 CHECK_NE(-1, old_fd_);
155 CHECK_NE(-1, dup2(fd(), std_fd_));
Wei Wangcaee0eb2016-10-21 09:23:39 -0700156}
157
Elliott Hughes1c1409f2018-05-23 09:16:46 -0700158void CapturedStdFd::Reset() {
159 CHECK_NE(-1, dup2(old_fd_, std_fd_));
160 CHECK_EQ(0, close(old_fd_));
Wei Wangcaee0eb2016-10-21 09:23:39 -0700161 // Note: cannot restore prior setvbuf() setting.
162}