1 /*
2 * Copyright (C) 2018 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 "perfetto/ext/base/temp_file.h"
18
19 #include "perfetto/base/build_config.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
26 #include <Windows.h>
27 #include <direct.h>
28 #include <fileapi.h>
29 #include <io.h>
30 #else
31 #include <unistd.h>
32 #endif
33
34 #include "perfetto/base/logging.h"
35 #include "perfetto/ext/base/file_utils.h"
36 #include "perfetto/ext/base/string_utils.h"
37
38 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
39 namespace {
GetTempName()40 std::string GetTempName() {
41 char name[] = "perfetto-XXXXXX";
42 PERFETTO_CHECK(_mktemp_s(name, sizeof(name)) == 0);
43 return name;
44 }
45 } // namespace
46 #endif
47
48 namespace perfetto {
49 namespace base {
50
GetSysTempDir()51 std::string GetSysTempDir() {
52 const char* tmpdir = nullptr;
53 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
54 if ((tmpdir = getenv("TMP")))
55 return tmpdir;
56 if ((tmpdir = getenv("TEMP")))
57 return tmpdir;
58 return "C:\\TEMP";
59 #else
60 if ((tmpdir = getenv("TMPDIR")))
61 return base::StripSuffix(tmpdir, "/");
62 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
63 return "/data/local/tmp";
64 #else
65 return "/tmp";
66 #endif // !OS_ANDROID
67 #endif // !OS_WIN
68 }
69
70 // static
Create()71 TempFile TempFile::Create() {
72 TempFile temp_file;
73 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
74 temp_file.path_ = GetSysTempDir() + "\\" + GetTempName();
75 // Several tests want to read-back the temp file while still open. On Windows,
76 // that requires FILE_SHARE_READ. FILE_SHARE_READ is NOT settable when using
77 // the POSIX-compat equivalent function _open(). Hence the CreateFileA +
78 // _open_osfhandle dance here.
79 HANDLE h =
80 ::CreateFileA(temp_file.path_.c_str(), GENERIC_READ | GENERIC_WRITE,
81 FILE_SHARE_DELETE | FILE_SHARE_READ, nullptr, CREATE_ALWAYS,
82 FILE_ATTRIBUTE_TEMPORARY, nullptr);
83 PERFETTO_CHECK(PlatformHandleChecker::IsValid(h));
84 // According to MSDN, when using _open_osfhandle the caller must not call
85 // CloseHandle(). Ownership is moved to the file descriptor, which then needs
86 // to be closed with just with _close().
87 temp_file.fd_.reset(_open_osfhandle(reinterpret_cast<intptr_t>(h), 0));
88 #else
89 temp_file.path_ = GetSysTempDir() + "/perfetto-XXXXXXXX";
90 temp_file.fd_.reset(mkstemp(&temp_file.path_[0]));
91 #endif
92 if (PERFETTO_UNLIKELY(!temp_file.fd_)) {
93 PERFETTO_FATAL("Could not create temp file %s", temp_file.path_.c_str());
94 }
95 return temp_file;
96 }
97
98 // static
CreateUnlinked()99 TempFile TempFile::CreateUnlinked() {
100 TempFile temp_file = TempFile::Create();
101 temp_file.Unlink();
102 return temp_file;
103 }
104
105 TempFile::TempFile() = default;
106
~TempFile()107 TempFile::~TempFile() {
108 Unlink();
109 }
110
ReleaseFD()111 ScopedFile TempFile::ReleaseFD() {
112 Unlink();
113 return std::move(fd_);
114 }
115
Unlink()116 void TempFile::Unlink() {
117 if (path_.empty())
118 return;
119 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
120 // If the FD is still open DeleteFile will mark the file as pending deletion
121 // and delete it only when the process exists.
122 PERFETTO_CHECK(DeleteFileA(path_.c_str()));
123 #else
124 PERFETTO_CHECK(unlink(path_.c_str()) == 0);
125 #endif
126 path_.clear();
127 }
128
129 TempFile::TempFile(TempFile&&) noexcept = default;
130 TempFile& TempFile::operator=(TempFile&&) = default;
131
132 // static
Create()133 TempDir TempDir::Create() {
134 TempDir temp_dir;
135 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
136 temp_dir.path_ = GetSysTempDir() + "\\" + GetTempName();
137 PERFETTO_CHECK(_mkdir(temp_dir.path_.c_str()) == 0);
138 #else
139 temp_dir.path_ = GetSysTempDir() + "/perfetto-XXXXXXXX";
140 PERFETTO_CHECK(mkdtemp(&temp_dir.path_[0]));
141 #endif
142 return temp_dir;
143 }
144
145 TempDir::TempDir() = default;
146 TempDir::TempDir(TempDir&&) noexcept = default;
147 TempDir& TempDir::operator=(TempDir&&) = default;
148
~TempDir()149 TempDir::~TempDir() {
150 if (path_.empty())
151 return; // For objects that get std::move()d.
152 PERFETTO_CHECK(Rmdir(path_));
153 }
154
155 } // namespace base
156 } // namespace perfetto
157