blob: 3b3d698ccb33eb4c6c4f03d0c04b82e01d068854 [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/logging.h"
18#include "android-base/test_utils.h"
Dan Alberte3ea0582015-03-13 23:06:01 -070019
Dan Albert26cb8ff2015-04-29 11:32:23 -070020#include <fcntl.h>
Dan Alberte3ea0582015-03-13 23:06:01 -070021#include <stdio.h>
22#include <stdlib.h>
Dan Albert26cb8ff2015-04-29 11:32:23 -070023#include <sys/stat.h>
Dan Alberte3ea0582015-03-13 23:06:01 -070024#include <unistd.h>
25
Dan Albert26cb8ff2015-04-29 11:32:23 -070026#if defined(_WIN32)
27#include <windows.h>
Spencer Low1a19bb22015-07-31 20:21:35 -070028#include <direct.h>
Elliott Hughesa57126b2016-02-17 11:53:54 -080029#define OS_PATH_SEPARATOR '\\'
30#else
31#define OS_PATH_SEPARATOR '/'
Dan Albert26cb8ff2015-04-29 11:32:23 -070032#endif
33
Alex Valléed0ac74c2015-05-06 16:26:00 -040034#include <string>
35
Spencer Low1a19bb22015-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 Low87c71692015-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 Low1a19bb22015-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éed0ac74c2015-05-06 16:26:00 -040058static std::string GetSystemTempDir() {
Dan Albert26cb8ff2015-04-29 11:32:23 -070059#if defined(__ANDROID__)
Alex Valléed0ac74c2015-05-06 16:26:00 -040060 return "/data/local/tmp";
Dan Albert26cb8ff2015-04-29 11:32:23 -070061#elif defined(_WIN32)
Spencer Low1a19bb22015-07-31 20:21:35 -070062 char tmp_dir[MAX_PATH];
63 DWORD result = GetTempPathA(sizeof(tmp_dir), tmp_dir);
64 CHECK_NE(result, 0ul) << "GetTempPathA failed, error: " << GetLastError();
65 CHECK_LT(result, sizeof(tmp_dir)) << "path truncated to: " << result;
66
67 // GetTempPath() returns a path with a trailing slash, but init()
68 // does not expect that, so remove it.
69 CHECK_EQ(tmp_dir[result - 1], '\\');
70 tmp_dir[result - 1] = '\0';
71 return tmp_dir;
Dan Albert26cb8ff2015-04-29 11:32:23 -070072#else
Alex Valléed0ac74c2015-05-06 16:26:00 -040073 return "/tmp";
Dan Albert26cb8ff2015-04-29 11:32:23 -070074#endif
Dan Alberte3ea0582015-03-13 23:06:01 -070075}
76
Alex Valléed0ac74c2015-05-06 16:26:00 -040077TemporaryFile::TemporaryFile() {
78 init(GetSystemTempDir());
79}
80
Dan Alberte3ea0582015-03-13 23:06:01 -070081TemporaryFile::~TemporaryFile() {
82 close(fd);
Alex Valléed0ac74c2015-05-06 16:26:00 -040083 unlink(path);
Dan Alberte3ea0582015-03-13 23:06:01 -070084}
85
Alex Valléed0ac74c2015-05-06 16:26:00 -040086void TemporaryFile::init(const std::string& tmp_dir) {
Spencer Low1a19bb22015-07-31 20:21:35 -070087 snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(),
88 OS_PATH_SEPARATOR);
Alex Valléed0ac74c2015-05-06 16:26:00 -040089 fd = mkstemp(path);
Dan Alberte3ea0582015-03-13 23:06:01 -070090}
Alex Valléed0ac74c2015-05-06 16:26:00 -040091
Alex Valléed0ac74c2015-05-06 16:26:00 -040092TemporaryDir::TemporaryDir() {
93 init(GetSystemTempDir());
94}
95
96TemporaryDir::~TemporaryDir() {
97 rmdir(path);
98}
99
100bool TemporaryDir::init(const std::string& tmp_dir) {
Spencer Low1a19bb22015-07-31 20:21:35 -0700101 snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(),
102 OS_PATH_SEPARATOR);
Alex Valléed0ac74c2015-05-06 16:26:00 -0400103 return (mkdtemp(path) != nullptr);
104}
Wei Wangcaee0eb2016-10-21 09:23:39 -0700105
106CapturedStderr::CapturedStderr() : old_stderr_(-1) {
107 init();
108}
109
110CapturedStderr::~CapturedStderr() {
111 reset();
112}
113
114int CapturedStderr::fd() const {
115 return temp_file_.fd;
116}
117
118void CapturedStderr::init() {
119#if defined(_WIN32)
120 // On Windows, stderr is often buffered, so make sure it is unbuffered so
121 // that we can immediately read back what was written to stderr.
122 CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
123#endif
124 old_stderr_ = dup(STDERR_FILENO);
125 CHECK_NE(-1, old_stderr_);
126 CHECK_NE(-1, dup2(fd(), STDERR_FILENO));
127}
128
129void CapturedStderr::reset() {
130 CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO));
131 CHECK_EQ(0, close(old_stderr_));
132 // Note: cannot restore prior setvbuf() setting.
133}