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