2012-08-01 04:29:50 +02:00
|
|
|
//===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Utility for creating a in-memory buffer that will be written to a file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-01-14 12:23:27 +01:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2015-03-23 19:07:13 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
2015-09-18 17:17:53 +02:00
|
|
|
#include "llvm/Support/Signals.h"
|
2014-06-12 19:38:55 +02:00
|
|
|
#include <system_error>
|
2012-08-01 04:29:50 +02:00
|
|
|
|
2014-12-11 21:12:55 +01:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2012-12-03 23:09:52 +01:00
|
|
|
using llvm::sys::fs::mapped_file_region;
|
2012-08-01 04:29:50 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
2014-09-02 19:49:23 +02:00
|
|
|
FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R,
|
2012-12-03 23:09:52 +01:00
|
|
|
StringRef Path, StringRef TmpPath)
|
2014-09-02 19:49:23 +02:00
|
|
|
: Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {}
|
2012-08-01 04:29:50 +02:00
|
|
|
|
|
|
|
FileOutputBuffer::~FileOutputBuffer() {
|
2016-09-02 03:10:53 +02:00
|
|
|
// Close the mapping before deleting the temp file, so that the removal
|
|
|
|
// succeeds.
|
|
|
|
Region.reset();
|
2014-01-10 22:40:29 +01:00
|
|
|
sys::fs::remove(Twine(TempPath));
|
2012-08-01 04:29:50 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 02:31:39 +02:00
|
|
|
ErrorOr<std::unique_ptr<FileOutputBuffer>>
|
|
|
|
FileOutputBuffer::create(StringRef FilePath, size_t Size, unsigned Flags) {
|
2012-08-01 04:29:50 +02:00
|
|
|
// If file already exists, it must be a regular file (to be mappable).
|
|
|
|
sys::fs::file_status Stat;
|
2014-06-13 04:24:39 +02:00
|
|
|
std::error_code EC = sys::fs::status(FilePath, Stat);
|
2012-08-01 04:29:50 +02:00
|
|
|
switch (Stat.type()) {
|
|
|
|
case sys::fs::file_type::file_not_found:
|
|
|
|
// If file does not exist, we'll create one.
|
|
|
|
break;
|
|
|
|
case sys::fs::file_type::regular_file: {
|
|
|
|
// If file is not currently writable, error out.
|
|
|
|
// FIXME: There is no sys::fs:: api for checking this.
|
|
|
|
// FIXME: In posix, you use the access() call to check this.
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
else
|
2014-06-13 19:20:48 +02:00
|
|
|
return make_error_code(errc::operation_not_permitted);
|
2012-08-01 04:29:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete target file.
|
2014-01-10 22:40:29 +01:00
|
|
|
EC = sys::fs::remove(FilePath);
|
2012-08-01 04:29:50 +02:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2012-12-03 23:09:52 +01:00
|
|
|
|
2013-07-08 17:22:09 +02:00
|
|
|
unsigned Mode = sys::fs::all_read | sys::fs::all_write;
|
|
|
|
// If requested, make the output file executable.
|
|
|
|
if (Flags & F_executable)
|
|
|
|
Mode |= sys::fs::all_exe;
|
|
|
|
|
2012-08-01 04:29:50 +02:00
|
|
|
// Create new file in same directory but with random name.
|
|
|
|
SmallString<128> TempFilePath;
|
|
|
|
int FD;
|
2013-07-08 17:22:09 +02:00
|
|
|
EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
|
|
|
|
TempFilePath, Mode);
|
2012-08-01 04:29:50 +02:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2012-12-03 23:09:52 +01:00
|
|
|
|
2015-09-18 17:17:53 +02:00
|
|
|
sys::RemoveFileOnSignal(TempFilePath);
|
|
|
|
|
2015-03-06 07:07:32 +01:00
|
|
|
#ifndef LLVM_ON_WIN32
|
|
|
|
// On Windows, CreateFileMapping (the mmap function on Windows)
|
|
|
|
// automatically extends the underlying file. We don't need to
|
|
|
|
// extend the file beforehand. _chsize (ftruncate on Windows) is
|
|
|
|
// pretty slow just like it writes specified amount of bytes,
|
|
|
|
// so we should avoid calling that.
|
2014-12-12 19:13:23 +01:00
|
|
|
EC = sys::fs::resize_file(FD, Size);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
2015-03-06 07:07:32 +01:00
|
|
|
#endif
|
2014-12-12 19:13:23 +01:00
|
|
|
|
2014-09-02 19:49:23 +02:00
|
|
|
auto MappedFile = llvm::make_unique<mapped_file_region>(
|
2014-12-11 21:12:55 +01:00
|
|
|
FD, mapped_file_region::readwrite, Size, 0, EC);
|
|
|
|
int Ret = close(FD);
|
2012-08-01 04:29:50 +02:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2014-12-11 21:12:55 +01:00
|
|
|
if (Ret)
|
|
|
|
return std::error_code(errno, std::generic_category());
|
2012-12-03 23:09:52 +01:00
|
|
|
|
2015-08-13 02:31:39 +02:00
|
|
|
std::unique_ptr<FileOutputBuffer> Buf(
|
2014-09-02 19:49:23 +02:00
|
|
|
new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
|
2015-08-13 02:31:39 +02:00
|
|
|
return std::move(Buf);
|
2012-12-03 23:09:52 +01:00
|
|
|
}
|
2012-08-01 04:29:50 +02:00
|
|
|
|
2014-12-12 18:35:34 +01:00
|
|
|
std::error_code FileOutputBuffer::commit() {
|
2012-08-01 04:29:50 +02:00
|
|
|
// Unmap buffer, letting OS flush dirty pages to file on disk.
|
2014-07-19 03:05:11 +02:00
|
|
|
Region.reset();
|
2012-12-03 23:09:52 +01:00
|
|
|
|
|
|
|
|
2012-08-01 04:29:50 +02:00
|
|
|
// Rename file to final name.
|
2015-09-18 17:17:53 +02:00
|
|
|
std::error_code EC = sys::fs::rename(Twine(TempPath), Twine(FinalPath));
|
|
|
|
sys::DontRemoveFileOnSignal(TempPath);
|
|
|
|
return EC;
|
2012-08-01 04:29:50 +02:00
|
|
|
}
|
2015-06-23 11:49:53 +02:00
|
|
|
} // namespace
|