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#ifndef INCLUDE_PERFETTO_BASE_TEMP_FILE_H_ 18#define INCLUDE_PERFETTO_BASE_TEMP_FILE_H_ 19 20#include <string> 21 22#include "perfetto/base/scoped_file.h" 23 24namespace perfetto { 25namespace base { 26 27class TempFile { 28 public: 29 static TempFile CreateUnlinked(); 30 static TempFile Create(); 31 32 TempFile(TempFile&&) noexcept; 33 TempFile& operator=(TempFile&&); 34 ~TempFile(); 35 36 const std::string& path() const { return path_; } 37 int fd() const { return *fd_; } 38 int operator*() const { return *fd_; } 39 40 // Unlinks the file from the filesystem but keeps the fd() open. 41 // It is safe to call this multiple times. 42 void Unlink(); 43 44 // Releases the underlying file descriptor. Will unlink the file from the 45 // filesystem if it was created via CreateUnlinked(). 46 ScopedFile ReleaseFD(); 47 48 private: 49 TempFile(); 50 TempFile(const TempFile&) = delete; 51 TempFile& operator=(const TempFile&) = delete; 52 53 ScopedFile fd_; 54 std::string path_; 55}; 56 57class TempDir { 58 public: 59 static TempDir Create(); 60 61 TempDir(TempDir&&) noexcept; 62 TempDir& operator=(TempDir&&); 63 ~TempDir(); 64 65 const std::string& path() const { return path_; } 66 67 private: 68 TempDir(); 69 TempDir(const TempDir&) = delete; 70 TempDir& operator=(const TempDir&) = delete; 71 72 std::string path_; 73}; 74 75} // namespace base 76} // namespace perfetto 77 78#endif // INCLUDE_PERFETTO_BASE_TEMP_FILE_H_ 79