xref: /openbsd-src/gnu/llvm/lldb/tools/lldb-fuzzer/utils/TempFile.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1 //===-- TempFile.cpp ------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Support/FileSystem.h"
10 #include <TempFile.h>
11 
12 using namespace lldb_fuzzer;
13 using namespace llvm;
14 
~TempFile()15 TempFile::~TempFile() {
16   if (!m_path.empty())
17     sys::fs::remove(m_path.str(), true);
18 }
19 
Create(uint8_t * data,size_t size)20 std::unique_ptr<TempFile> TempFile::Create(uint8_t *data, size_t size) {
21   int fd;
22   std::unique_ptr<TempFile> temp_file = std::make_unique<TempFile>();
23   std::error_code ec = sys::fs::createTemporaryFile("lldb-fuzzer", "input", fd,
24                                                     temp_file->m_path);
25   if (ec)
26     return nullptr;
27 
28   raw_fd_ostream os(fd, true);
29   os.write(reinterpret_cast<const char *>(data), size);
30   os.close();
31 
32   return temp_file;
33 }
34