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 | |
15 | TempFile::~TempFile() { |
16 | if (!m_path.empty()) |
17 | sys::fs::remove(path: m_path.str(), IgnoreNonExisting: true); |
18 | } |
19 | |
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(Prefix: "lldb-fuzzer", Suffix: "input", ResultFD&: fd, |
24 | ResultPath&: temp_file->m_path); |
25 | if (ec) |
26 | return nullptr; |
27 | |
28 | raw_fd_ostream os(fd, true); |
29 | os.write(Ptr: reinterpret_cast<const char *>(data), Size: size); |
30 | os.close(); |
31 | |
32 | return temp_file; |
33 | } |
34 |