1 //===- Buffer.cpp ---------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/FileOutputBuffer.h"
11 #include "llvm/Support/FileSystem.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include "llvm/Support/Process.h"
21 static Error
createEmptyFile(StringRef FileName
) {
22 // Create an empty tempfile and atomically swap it in place with the desired
24 Expected
<sys::fs::TempFile
> Temp
=
25 sys::fs::TempFile::create(FileName
+ ".temp-empty-%%%%%%%");
26 return Temp
? Temp
->keep(FileName
) : Temp
.takeError();
29 Error
FileBuffer::allocate(size_t Size
) {
30 // When a 0-sized file is requested, skip allocation but defer file
31 // creation/truncation until commit() to avoid side effects if something
32 // happens between allocate() and commit().
35 return Error::success();
38 Expected
<std::unique_ptr
<FileOutputBuffer
>> BufferOrErr
=
39 FileOutputBuffer::create(getName(), Size
, FileOutputBuffer::F_executable
);
40 // FileOutputBuffer::create() returns an Error that is just a wrapper around
41 // std::error_code. Wrap it in FileError to include the actual filename.
43 return createFileError(getName(), BufferOrErr
.takeError());
44 Buf
= std::move(*BufferOrErr
);
45 return Error::success();
48 Error
FileBuffer::commit() {
50 return createEmptyFile(getName());
52 assert(Buf
&& "allocate() not called before commit()!");
53 Error Err
= Buf
->commit();
54 // FileOutputBuffer::commit() returns an Error that is just a wrapper around
55 // std::error_code. Wrap it in FileError to include the actual filename.
56 return Err
? createFileError(getName(), std::move(Err
)) : std::move(Err
);
59 uint8_t *FileBuffer::getBufferStart() {
60 return reinterpret_cast<uint8_t *>(Buf
->getBufferStart());
63 Error
MemBuffer::allocate(size_t Size
) {
64 Buf
= WritableMemoryBuffer::getNewMemBuffer(Size
, getName());
65 return Error::success();
68 Error
MemBuffer::commit() { return Error::success(); }
70 uint8_t *MemBuffer::getBufferStart() {
71 return reinterpret_cast<uint8_t *>(Buf
->getBufferStart());
74 std::unique_ptr
<WritableMemoryBuffer
> MemBuffer::releaseMemoryBuffer() {
75 return std::move(Buf
);
78 } // end namespace objcopy
79 } // end namespace llvm