2012-08-15 19:34:23 +00: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-18 16:17:27 +00:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2015-05-27 18:44:32 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
2017-01-14 15:37:50 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2015-12-30 11:46:15 +00:00
|
|
|
#include "llvm/Support/Signals.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include <system_error>
|
2012-08-15 19:34:23 +00:00
|
|
|
|
2015-01-18 16:17:27 +00:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2013-04-08 18:41:23 +00:00
|
|
|
using llvm::sys::fs::mapped_file_region;
|
2012-08-15 19:34:23 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2015-01-18 16:17:27 +00:00
|
|
|
FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R,
|
2017-01-14 15:37:50 +00:00
|
|
|
StringRef Path, StringRef TmpPath,
|
|
|
|
bool IsRegular)
|
|
|
|
: Region(std::move(R)), FinalPath(Path), TempPath(TmpPath),
|
|
|
|
IsRegular(IsRegular) {}
|
2012-08-15 19:34:23 +00:00
|
|
|
|
|
|
|
FileOutputBuffer::~FileOutputBuffer() {
|
2017-01-02 19:17:04 +00:00
|
|
|
// Close the mapping before deleting the temp file, so that the removal
|
|
|
|
// succeeds.
|
|
|
|
Region.reset();
|
2014-11-24 09:08:18 +00:00
|
|
|
sys::fs::remove(Twine(TempPath));
|
2012-08-15 19:34:23 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
ErrorOr<std::unique_ptr<FileOutputBuffer>>
|
|
|
|
FileOutputBuffer::create(StringRef FilePath, size_t Size, unsigned Flags) {
|
2017-01-14 15:37:50 +00:00
|
|
|
// Check file is not a regular file, in which case we cannot remove it.
|
2012-08-15 19:34:23 +00:00
|
|
|
sys::fs::file_status Stat;
|
2014-11-24 09:08:18 +00:00
|
|
|
std::error_code EC = sys::fs::status(FilePath, Stat);
|
2017-01-14 15:37:50 +00:00
|
|
|
bool IsRegular = true;
|
2012-08-15 19:34:23 +00: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;
|
2017-04-16 16:01:22 +00:00
|
|
|
case sys::fs::file_type::directory_file:
|
|
|
|
return errc::is_a_directory;
|
2012-08-15 19:34:23 +00:00
|
|
|
default:
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
2017-01-14 15:37:50 +00:00
|
|
|
IsRegular = false;
|
2012-08-15 19:34:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 15:37:50 +00:00
|
|
|
if (IsRegular) {
|
|
|
|
// Delete target file.
|
|
|
|
EC = sys::fs::remove(FilePath);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
}
|
2013-12-22 00:04:03 +00:00
|
|
|
|
2012-08-15 19:34:23 +00:00
|
|
|
SmallString<128> TempFilePath;
|
|
|
|
int FD;
|
2017-01-14 15:37:50 +00:00
|
|
|
if (IsRegular) {
|
|
|
|
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;
|
|
|
|
// Create new file in same directory but with random name.
|
|
|
|
EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
|
|
|
|
TempFilePath, Mode);
|
|
|
|
} else {
|
|
|
|
// Create a temporary file. Since this is a special file, we will not move
|
|
|
|
// it and the new file can be in another filesystem. This avoids trying to
|
|
|
|
// create a temporary file in /dev when outputting to /dev/null for example.
|
|
|
|
EC = sys::fs::createTemporaryFile(sys::path::filename(FilePath), "", FD,
|
|
|
|
TempFilePath);
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:34:23 +00:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2013-04-08 18:41:23 +00:00
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
sys::RemoveFileOnSignal(TempFilePath);
|
|
|
|
|
2015-05-27 18:44:32 +00: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.
|
2015-01-18 16:17:27 +00:00
|
|
|
EC = sys::fs::resize_file(FD, Size);
|
2012-08-15 19:34:23 +00:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2015-05-27 18:44:32 +00:00
|
|
|
#endif
|
2013-04-08 18:41:23 +00:00
|
|
|
|
2015-01-18 16:17:27 +00:00
|
|
|
auto MappedFile = llvm::make_unique<mapped_file_region>(
|
|
|
|
FD, mapped_file_region::readwrite, Size, 0, EC);
|
|
|
|
int Ret = close(FD);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
if (Ret)
|
|
|
|
return std::error_code(errno, std::generic_category());
|
|
|
|
|
2017-01-14 15:37:50 +00:00
|
|
|
std::unique_ptr<FileOutputBuffer> Buf(new FileOutputBuffer(
|
|
|
|
std::move(MappedFile), FilePath, TempFilePath, IsRegular));
|
2015-12-30 11:46:15 +00:00
|
|
|
return std::move(Buf);
|
2013-04-08 18:41:23 +00:00
|
|
|
}
|
2012-08-15 19:34:23 +00:00
|
|
|
|
2015-01-18 16:17:27 +00:00
|
|
|
std::error_code FileOutputBuffer::commit() {
|
2012-08-15 19:34:23 +00:00
|
|
|
// Unmap buffer, letting OS flush dirty pages to file on disk.
|
2014-11-24 09:08:18 +00:00
|
|
|
Region.reset();
|
2013-04-08 18:41:23 +00:00
|
|
|
|
2017-01-14 15:37:50 +00:00
|
|
|
std::error_code EC;
|
|
|
|
if (IsRegular) {
|
|
|
|
// Rename file to final name.
|
|
|
|
EC = sys::fs::rename(Twine(TempPath), Twine(FinalPath));
|
|
|
|
sys::DontRemoveFileOnSignal(TempPath);
|
|
|
|
} else {
|
|
|
|
EC = sys::fs::copy_file(TempPath, FinalPath);
|
|
|
|
std::error_code RMEC = sys::fs::remove(TempPath);
|
|
|
|
sys::DontRemoveFileOnSignal(TempPath);
|
|
|
|
if (RMEC)
|
|
|
|
return RMEC;
|
|
|
|
}
|
2013-04-08 18:41:23 +00:00
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
return EC;
|
2012-08-15 19:34:23 +00:00
|
|
|
}
|
2015-07-05 14:21:36 +00:00
|
|
|
} // namespace
|