blob: 9d8dfb2fd4cfbc71cc8621bc0c79271a404c1798 [file] [log] [blame]
Dan Albert58310b42015-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 Hughes4f713192015-12-04 22:00:26 -080017#include "android-base/logging.h"
18#include "android-base/test_utils.h"
Dan Albert58310b42015-03-13 23:06:01 -070019
Dan Albert0c4b3a32015-04-29 11:32:23 -070020#include <fcntl.h>
Dan Albert58310b42015-03-13 23:06:01 -070021#include <stdio.h>
22#include <stdlib.h>
Dan Albert0c4b3a32015-04-29 11:32:23 -070023#include <sys/stat.h>
Dan Albert58310b42015-03-13 23:06:01 -070024#include <unistd.h>
25
Dan Albert0c4b3a32015-04-29 11:32:23 -070026#if defined(_WIN32)
27#include <windows.h>
Spencer Low40d0c7a2015-07-31 20:21:35 -070028#include <direct.h>
Elliott Hughes38e2b632016-02-17 11:53:54 -080029#define OS_PATH_SEPARATOR '\\'
30#else
31#define OS_PATH_SEPARATOR '/'
Dan Albert0c4b3a32015-04-29 11:32:23 -070032#endif
33
Alex Vallée47d67c92015-05-06 16:26:00 -040034#include <string>
35
Spencer Low40d0c7a2015-07-31 20:21:35 -070036#ifdef _WIN32
37int 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 Low2fbeb0c2015-09-01 14:57:58 -070042 // 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 Low40d0c7a2015-07-31 20:21:35 -070045}
46
47char* 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ée47d67c92015-05-06 16:26:00 -040058static std::string GetSystemTempDir() {
Dan Albert0c4b3a32015-04-29 11:32:23 -070059#if defined(__ANDROID__)
Yabin Cui57e9cea2016-12-14 17:45:49 -080060 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 Albert0c4b3a32015-04-29 11:32:23 -070067#elif defined(_WIN32)
Spencer Low40d0c7a2015-07-31 20:21:35 -070068 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 Albert0c4b3a32015-04-29 11:32:23 -070078#else
Alex Vallée47d67c92015-05-06 16:26:00 -040079 return "/tmp";
Dan Albert0c4b3a32015-04-29 11:32:23 -070080#endif
Dan Albert58310b42015-03-13 23:06:01 -070081}
82
Alex Vallée47d67c92015-05-06 16:26:00 -040083TemporaryFile::TemporaryFile() {
84 init(GetSystemTempDir());
85}
86
Yabin Cui464ea612017-12-06 14:20:07 -080087TemporaryFile::TemporaryFile(const std::string& tmp_dir) {
88 init(tmp_dir);
89}
90
Dan Albert58310b42015-03-13 23:06:01 -070091TemporaryFile::~TemporaryFile() {
Tianjie Xuf9bc1b02017-09-11 12:01:09 -070092 if (fd != -1) {
93 close(fd);
94 }
Alex Vallée47d67c92015-05-06 16:26:00 -040095 unlink(path);
Dan Albert58310b42015-03-13 23:06:01 -070096}
97
Tianjie Xuf9bc1b02017-09-11 12:01:09 -070098int TemporaryFile::release() {
99 int result = fd;
100 fd = -1;
101 return result;
102}
103
Alex Vallée47d67c92015-05-06 16:26:00 -0400104void TemporaryFile::init(const std::string& tmp_dir) {
Spencer Low40d0c7a2015-07-31 20:21:35 -0700105 snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(),
106 OS_PATH_SEPARATOR);
Alex Vallée47d67c92015-05-06 16:26:00 -0400107 fd = mkstemp(path);
Dan Albert58310b42015-03-13 23:06:01 -0700108}
Alex Vallée47d67c92015-05-06 16:26:00 -0400109
Alex Vallée47d67c92015-05-06 16:26:00 -0400110TemporaryDir::TemporaryDir() {
111 init(GetSystemTempDir());
112}
113
114TemporaryDir::~TemporaryDir() {
115 rmdir(path);
116}
117
118bool TemporaryDir::init(const std::string& tmp_dir) {
Spencer Low40d0c7a2015-07-31 20:21:35 -0700119 snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(),
120 OS_PATH_SEPARATOR);
Alex Vallée47d67c92015-05-06 16:26:00 -0400121 return (mkdtemp(path) != nullptr);
122}
Wei Wang8c176302016-10-21 09:23:39 -0700123
124CapturedStderr::CapturedStderr() : old_stderr_(-1) {
125 init();
126}
127
128CapturedStderr::~CapturedStderr() {
129 reset();
130}
131
132int CapturedStderr::fd() const {
133 return temp_file_.fd;
134}
135
136void CapturedStderr::init() {
137#if defined(_WIN32)
138 // On Windows, stderr is often buffered, so make sure it is unbuffered so
139 // that we can immediately read back what was written to stderr.
140 CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
141#endif
142 old_stderr_ = dup(STDERR_FILENO);
143 CHECK_NE(-1, old_stderr_);
144 CHECK_NE(-1, dup2(fd(), STDERR_FILENO));
145}
146
147void CapturedStderr::reset() {
148 CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO));
149 CHECK_EQ(0, close(old_stderr_));
150 // Note: cannot restore prior setvbuf() setting.
151}