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
17 #include "android-base/logging.h"
18 #include "android-base/test_utils.h"
19
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #if defined(_WIN32)
27 #include <windows.h>
28 #include <direct.h>
29 #define OS_PATH_SEPARATOR '\\'
30 #else
31 #define OS_PATH_SEPARATOR '/'
32 #endif
33
34 #include <string>
35
36 #ifdef _WIN32
mkstemp(char * template_name)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.
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);
45 }
46
mkdtemp(char * template_name)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
GetSystemTempDir()58 static std::string GetSystemTempDir() {
59 #if defined(__ANDROID__)
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 ".";
67 #elif defined(_WIN32)
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;
78 #else
79 return "/tmp";
80 #endif
81 }
82
TemporaryFile()83 TemporaryFile::TemporaryFile() {
84 init(GetSystemTempDir());
85 }
86
~TemporaryFile()87 TemporaryFile::~TemporaryFile() {
88 close(fd);
89 unlink(path);
90 }
91
init(const std::string & tmp_dir)92 void TemporaryFile::init(const std::string& tmp_dir) {
93 snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(),
94 OS_PATH_SEPARATOR);
95 fd = mkstemp(path);
96 }
97
TemporaryDir()98 TemporaryDir::TemporaryDir() {
99 init(GetSystemTempDir());
100 }
101
~TemporaryDir()102 TemporaryDir::~TemporaryDir() {
103 rmdir(path);
104 }
105
init(const std::string & tmp_dir)106 bool TemporaryDir::init(const std::string& tmp_dir) {
107 snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(),
108 OS_PATH_SEPARATOR);
109 return (mkdtemp(path) != nullptr);
110 }
111
CapturedStderr()112 CapturedStderr::CapturedStderr() : old_stderr_(-1) {
113 init();
114 }
115
~CapturedStderr()116 CapturedStderr::~CapturedStderr() {
117 reset();
118 }
119
fd() const120 int CapturedStderr::fd() const {
121 return temp_file_.fd;
122 }
123
init()124 void CapturedStderr::init() {
125 #if defined(_WIN32)
126 // On Windows, stderr is often buffered, so make sure it is unbuffered so
127 // that we can immediately read back what was written to stderr.
128 CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
129 #endif
130 old_stderr_ = dup(STDERR_FILENO);
131 CHECK_NE(-1, old_stderr_);
132 CHECK_NE(-1, dup2(fd(), STDERR_FILENO));
133 }
134
reset()135 void CapturedStderr::reset() {
136 CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO));
137 CHECK_EQ(0, close(old_stderr_));
138 // Note: cannot restore prior setvbuf() setting.
139 }
140