blob: 9df8a3ac1d89bcb6f945e926116ceeb6b21ff01c [file] [log] [blame]
Primiano Tucci941b2212018-03-14 22:46:31 +00001/*
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
Pascal Muetschard148cb842019-09-25 14:50:32 -070017#include "perfetto/base/build_config.h"
18#if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
19
Primiano Tucci2c5488f2019-06-01 03:27:28 +010020#include "perfetto/ext/base/temp_file.h"
Primiano Tucci941b2212018-03-14 22:46:31 +000021
Eric Secklere7e3f452019-02-13 15:12:05 +000022#include <stdlib.h>
Primiano Tucci941b2212018-03-14 22:46:31 +000023#include <unistd.h>
24
Primiano Tucciff68cac2020-08-06 18:13:17 +020025#include "perfetto/ext/base/string_utils.h"
26
Primiano Tucci941b2212018-03-14 22:46:31 +000027namespace perfetto {
28namespace base {
29
Primiano Tucciff68cac2020-08-06 18:13:17 +020030std::string GetSysTempDir() {
31 const char* tmpdir = getenv("TMPDIR");
32 if (tmpdir)
33 return base::StripSuffix(tmpdir, "/");
Primiano Tucci941b2212018-03-14 22:46:31 +000034#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
Primiano Tucciff68cac2020-08-06 18:13:17 +020035 return "/data/local/tmp";
Primiano Tucci941b2212018-03-14 22:46:31 +000036#else
Primiano Tucciff68cac2020-08-06 18:13:17 +020037 return "/tmp";
Primiano Tucci941b2212018-03-14 22:46:31 +000038#endif
Primiano Tucciff68cac2020-08-06 18:13:17 +020039}
Primiano Tucci941b2212018-03-14 22:46:31 +000040
41// static
42TempFile TempFile::Create() {
43 TempFile temp_file;
Primiano Tucciff68cac2020-08-06 18:13:17 +020044 temp_file.path_ = GetSysTempDir() + "/perfetto-XXXXXXXX";
Primiano Tucci941b2212018-03-14 22:46:31 +000045 temp_file.fd_.reset(mkstemp(&temp_file.path_[0]));
Eric Seckler1e470362019-10-02 10:00:49 +010046 if (PERFETTO_UNLIKELY(!temp_file.fd_)) {
47 PERFETTO_FATAL("Could not create temp file %s", temp_file.path_.c_str());
48 }
Primiano Tucci941b2212018-03-14 22:46:31 +000049 return temp_file;
50}
51
52// static
53TempFile TempFile::CreateUnlinked() {
54 TempFile temp_file = TempFile::Create();
55 temp_file.Unlink();
56 return temp_file;
57}
58
59TempFile::TempFile() = default;
60
61TempFile::~TempFile() {
62 Unlink();
63}
64
65ScopedFile TempFile::ReleaseFD() {
66 Unlink();
67 return std::move(fd_);
68}
69
70void TempFile::Unlink() {
71 if (path_.empty())
72 return;
73 PERFETTO_CHECK(unlink(path_.c_str()) == 0);
74 path_.clear();
75}
76
77TempFile::TempFile(TempFile&&) noexcept = default;
78TempFile& TempFile::operator=(TempFile&&) = default;
79
80// static
81TempDir TempDir::Create() {
82 TempDir temp_dir;
Primiano Tucciff68cac2020-08-06 18:13:17 +020083 temp_dir.path_ = GetSysTempDir() + "/perfetto-XXXXXXXX";
Primiano Tucci941b2212018-03-14 22:46:31 +000084 PERFETTO_CHECK(mkdtemp(&temp_dir.path_[0]));
85 return temp_dir;
86}
87
88TempDir::TempDir() = default;
89
90TempDir::~TempDir() {
91 PERFETTO_CHECK(rmdir(path_.c_str()) == 0);
92}
93
94} // namespace base
95} // namespace perfetto
Pascal Muetschard148cb842019-09-25 14:50:32 -070096
Pascal Muetschard148cb842019-09-25 14:50:32 -070097#endif // !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)