#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Memory.h"
#include <system_error>
#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
#else
#include <io.h>
#endif
using namespace llvm;
using namespace llvm::sys;
namespace {
class OnDiskBuffer : public FileOutputBuffer {
public:
OnDiskBuffer(StringRef Path, fs::TempFile Temp, fs::mapped_file_region Buf)
: FileOutputBuffer(Path), Buffer(std::move(Buf)), Temp(std::move(Temp)) {}
uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.data(); }
uint8_t *getBufferEnd() const override {
return (uint8_t *)Buffer.data() + Buffer.size();
}
size_t getBufferSize() const override { return Buffer.size(); }
Error commit() override {
Buffer.unmap();
return Temp.keep(FinalPath);
}
~OnDiskBuffer() override {
Buffer.unmap();
consumeError(Temp.discard());
}
void discard() override {
consumeError(Temp.discard());
}
private:
fs::mapped_file_region Buffer;
fs::TempFile Temp;
};
class InMemoryBuffer : public FileOutputBuffer {
public:
InMemoryBuffer(StringRef Path, MemoryBlock Buf, std::size_t BufSize,
unsigned Mode)
: FileOutputBuffer(Path), Buffer(Buf), BufferSize(BufSize),
Mode(Mode) {}
uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
uint8_t *getBufferEnd() const override {
return (uint8_t *)Buffer.base() + BufferSize;
}
size_t getBufferSize() const override { return BufferSize; }
Error commit() override {
if (FinalPath == "-") {
llvm::outs() << StringRef((const char *)Buffer.base(), BufferSize);
llvm::outs().flush();
return Error::success();
}
using namespace sys::fs;
int FD;
std::error_code EC;
if (auto EC =
openFileForWrite(FinalPath, FD, CD_CreateAlways, OF_None, Mode))
return errorCodeToError(EC);
raw_fd_ostream OS(FD, true, true);
OS << StringRef((const char *)Buffer.base(), BufferSize);
return Error::success();
}
private:
OwningMemoryBlock Buffer;
size_t BufferSize;
unsigned Mode;
};
}
static Expected<std::unique_ptr<InMemoryBuffer>>
createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
std::error_code EC;
MemoryBlock MB = Memory::allocateMappedMemory(
Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
if (EC)
return errorCodeToError(EC);
return std::make_unique<InMemoryBuffer>(Path, MB, Size, Mode);
}
static Expected<std::unique_ptr<FileOutputBuffer>>
createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
Expected<fs::TempFile> FileOrErr =
fs::TempFile::create(Path + ".tmp%%%%%%%", Mode);
if (!FileOrErr)
return FileOrErr.takeError();
fs::TempFile File = std::move(*FileOrErr);
if (auto EC = fs::resize_file_before_mapping_readwrite(File.FD, Size)) {
consumeError(File.discard());
return errorCodeToError(EC);
}
std::error_code EC;
fs::mapped_file_region MappedFile =
fs::mapped_file_region(fs::convertFDToNativeFile(File.FD),
fs::mapped_file_region::readwrite, Size, 0, EC);
if (EC) {
consumeError(File.discard());
return createInMemoryBuffer(Path, Size, Mode);
}
return std::make_unique<OnDiskBuffer>(Path, std::move(File),
std::move(MappedFile));
}
Expected<std::unique_ptr<FileOutputBuffer>>
FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
if (Path == "-")
return createInMemoryBuffer("-", Size, 0);
unsigned Mode = fs::all_read | fs::all_write;
if (Flags & F_executable)
Mode |= fs::all_exe;
if (Size == 0)
return createInMemoryBuffer(Path, Size, Mode);
fs::file_status Stat;
fs::status(Path, Stat);
switch (Stat.type()) {
case fs::file_type::directory_file:
return errorCodeToError(errc::is_a_directory);
case fs::file_type::regular_file:
case fs::file_type::file_not_found:
case fs::file_type::status_error:
if (Flags & F_no_mmap)
return createInMemoryBuffer(Path, Size, Mode);
else
return createOnDiskBuffer(Path, Size, Mode);
default:
return createInMemoryBuffer(Path, Size, Mode);
}
}