Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 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 | |
Pascal Muetschard | 148cb84 | 2019-09-25 14:50:32 -0700 | [diff] [blame] | 17 | #include "perfetto/base/build_config.h" |
| 18 | #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 19 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 20 | #include "perfetto/ext/base/temp_file.h" |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 21 | |
Eric Seckler | e7e3f45 | 2019-02-13 15:12:05 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
Primiano Tucci | ff68cac | 2020-08-06 18:13:17 +0200 | [diff] [blame^] | 25 | #include "perfetto/ext/base/string_utils.h" |
| 26 | |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 27 | namespace perfetto { |
| 28 | namespace base { |
| 29 | |
Primiano Tucci | ff68cac | 2020-08-06 18:13:17 +0200 | [diff] [blame^] | 30 | std::string GetSysTempDir() { |
| 31 | const char* tmpdir = getenv("TMPDIR"); |
| 32 | if (tmpdir) |
| 33 | return base::StripSuffix(tmpdir, "/"); |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 34 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
Primiano Tucci | ff68cac | 2020-08-06 18:13:17 +0200 | [diff] [blame^] | 35 | return "/data/local/tmp"; |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 36 | #else |
Primiano Tucci | ff68cac | 2020-08-06 18:13:17 +0200 | [diff] [blame^] | 37 | return "/tmp"; |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 38 | #endif |
Primiano Tucci | ff68cac | 2020-08-06 18:13:17 +0200 | [diff] [blame^] | 39 | } |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 40 | |
| 41 | // static |
| 42 | TempFile TempFile::Create() { |
| 43 | TempFile temp_file; |
Primiano Tucci | ff68cac | 2020-08-06 18:13:17 +0200 | [diff] [blame^] | 44 | temp_file.path_ = GetSysTempDir() + "/perfetto-XXXXXXXX"; |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 45 | temp_file.fd_.reset(mkstemp(&temp_file.path_[0])); |
Eric Seckler | 1e47036 | 2019-10-02 10:00:49 +0100 | [diff] [blame] | 46 | if (PERFETTO_UNLIKELY(!temp_file.fd_)) { |
| 47 | PERFETTO_FATAL("Could not create temp file %s", temp_file.path_.c_str()); |
| 48 | } |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 49 | return temp_file; |
| 50 | } |
| 51 | |
| 52 | // static |
| 53 | TempFile TempFile::CreateUnlinked() { |
| 54 | TempFile temp_file = TempFile::Create(); |
| 55 | temp_file.Unlink(); |
| 56 | return temp_file; |
| 57 | } |
| 58 | |
| 59 | TempFile::TempFile() = default; |
| 60 | |
| 61 | TempFile::~TempFile() { |
| 62 | Unlink(); |
| 63 | } |
| 64 | |
| 65 | ScopedFile TempFile::ReleaseFD() { |
| 66 | Unlink(); |
| 67 | return std::move(fd_); |
| 68 | } |
| 69 | |
| 70 | void TempFile::Unlink() { |
| 71 | if (path_.empty()) |
| 72 | return; |
| 73 | PERFETTO_CHECK(unlink(path_.c_str()) == 0); |
| 74 | path_.clear(); |
| 75 | } |
| 76 | |
| 77 | TempFile::TempFile(TempFile&&) noexcept = default; |
| 78 | TempFile& TempFile::operator=(TempFile&&) = default; |
| 79 | |
| 80 | // static |
| 81 | TempDir TempDir::Create() { |
| 82 | TempDir temp_dir; |
Primiano Tucci | ff68cac | 2020-08-06 18:13:17 +0200 | [diff] [blame^] | 83 | temp_dir.path_ = GetSysTempDir() + "/perfetto-XXXXXXXX"; |
Primiano Tucci | 941b221 | 2018-03-14 22:46:31 +0000 | [diff] [blame] | 84 | PERFETTO_CHECK(mkdtemp(&temp_dir.path_[0])); |
| 85 | return temp_dir; |
| 86 | } |
| 87 | |
| 88 | TempDir::TempDir() = default; |
| 89 | |
| 90 | TempDir::~TempDir() { |
| 91 | PERFETTO_CHECK(rmdir(path_.c_str()) == 0); |
| 92 | } |
| 93 | |
| 94 | } // namespace base |
| 95 | } // namespace perfetto |
Pascal Muetschard | 148cb84 | 2019-09-25 14:50:32 -0700 | [diff] [blame] | 96 | |
Pascal Muetschard | 148cb84 | 2019-09-25 14:50:32 -0700 | [diff] [blame] | 97 | #endif // !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |