2012-08-01 02:29:50 +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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/system_error.h"
|
|
|
|
|
2012-12-03 22:09:52 +00:00
|
|
|
using llvm::sys::fs::mapped_file_region;
|
2012-08-01 02:29:50 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2012-12-03 22:09:52 +00:00
|
|
|
FileOutputBuffer::FileOutputBuffer(mapped_file_region * R,
|
|
|
|
StringRef Path, StringRef TmpPath)
|
|
|
|
: Region(R)
|
|
|
|
, FinalPath(Path)
|
|
|
|
, TempPath(TmpPath) {
|
2012-08-01 02:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileOutputBuffer::~FileOutputBuffer() {
|
2012-12-03 22:09:52 +00:00
|
|
|
bool Existed;
|
|
|
|
sys::fs::remove(Twine(TempPath), Existed);
|
2012-08-01 02:29:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-03 22:09:52 +00:00
|
|
|
error_code FileOutputBuffer::create(StringRef FilePath,
|
|
|
|
size_t Size,
|
2012-08-01 02:29:50 +00:00
|
|
|
OwningPtr<FileOutputBuffer> &Result,
|
|
|
|
unsigned Flags) {
|
|
|
|
// If file already exists, it must be a regular file (to be mappable).
|
|
|
|
sys::fs::file_status Stat;
|
|
|
|
error_code EC = sys::fs::status(FilePath, Stat);
|
|
|
|
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
|
|
|
|
return make_error_code(errc::operation_not_permitted);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete target file.
|
|
|
|
bool Existed;
|
|
|
|
EC = sys::fs::remove(FilePath, Existed);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
2012-12-03 22:09:52 +00:00
|
|
|
|
2012-08-01 02:29:50 +00:00
|
|
|
// Create new file in same directory but with random name.
|
|
|
|
SmallString<128> TempFilePath;
|
|
|
|
int FD;
|
2012-12-03 22:09:52 +00:00
|
|
|
EC = sys::fs::unique_file(Twine(FilePath) + ".tmp%%%%%%%",
|
|
|
|
FD, TempFilePath, false, 0644);
|
2012-08-01 02:29:50 +00:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2012-12-03 22:09:52 +00:00
|
|
|
|
|
|
|
OwningPtr<mapped_file_region> MappedFile(
|
|
|
|
new mapped_file_region(FD, mapped_file_region::readwrite, Size, 0, EC));
|
2012-08-01 02:29:50 +00:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
2012-12-03 22:09:52 +00:00
|
|
|
|
2012-08-01 02:29:50 +00:00
|
|
|
// If requested, make the output file executable.
|
|
|
|
if ( Flags & F_executable ) {
|
|
|
|
sys::fs::file_status Stat2;
|
|
|
|
EC = sys::fs::status(Twine(TempFilePath), Stat2);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
2012-12-03 22:09:52 +00:00
|
|
|
|
2012-08-01 02:29:50 +00:00
|
|
|
sys::fs::perms new_perms = Stat2.permissions();
|
|
|
|
if ( new_perms & sys::fs::owner_read )
|
|
|
|
new_perms |= sys::fs::owner_exe;
|
|
|
|
if ( new_perms & sys::fs::group_read )
|
|
|
|
new_perms |= sys::fs::group_exe;
|
|
|
|
if ( new_perms & sys::fs::others_read )
|
|
|
|
new_perms |= sys::fs::others_exe;
|
|
|
|
new_perms |= sys::fs::add_perms;
|
|
|
|
EC = sys::fs::permissions(Twine(TempFilePath), new_perms);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
}
|
|
|
|
|
2012-12-03 22:09:52 +00:00
|
|
|
Result.reset(new FileOutputBuffer(MappedFile.get(), FilePath, TempFilePath));
|
|
|
|
if (Result)
|
|
|
|
MappedFile.take();
|
2012-08-01 02:29:50 +00:00
|
|
|
|
2012-12-03 22:09:52 +00:00
|
|
|
return error_code::success();
|
|
|
|
}
|
2012-08-01 02:29:50 +00:00
|
|
|
|
|
|
|
error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
|
|
|
|
// Unmap buffer, letting OS flush dirty pages to file on disk.
|
2012-12-03 22:09:52 +00:00
|
|
|
Region.reset(0);
|
|
|
|
|
2012-08-01 02:29:50 +00:00
|
|
|
// If requested, resize file as part of commit.
|
|
|
|
if ( NewSmallerSize != -1 ) {
|
2012-12-03 22:09:52 +00:00
|
|
|
error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
|
2012-08-01 02:29:50 +00:00
|
|
|
if (EC)
|
|
|
|
return EC;
|
|
|
|
}
|
2012-12-03 22:09:52 +00:00
|
|
|
|
2012-08-01 02:29:50 +00:00
|
|
|
// Rename file to final name.
|
|
|
|
return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
|
|
|
|
}
|
|
|
|
} // namespace
|