mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-11-01 15:11:24 +00:00
[Support] Make FileOutputBuffer work on Windows.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169167 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
604db310f0
commit
6bc86018d1
@ -14,85 +14,79 @@
|
||||
#ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
|
||||
#define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
|
||||
|
||||
#include "llvm/ADT/OwningPtr.h"
|
||||
#include "llvm/ADT/SmallString.h"
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
#include "llvm/Support/DataTypes.h"
|
||||
#include "llvm/Support/FileSystem.h"
|
||||
|
||||
namespace llvm {
|
||||
|
||||
class error_code;
|
||||
template<class T> class OwningPtr;
|
||||
|
||||
/// FileOutputBuffer - This interface provides simple way to create an in-memory
|
||||
/// buffer which will be written to a file. During the lifetime of these
|
||||
/// buffer which will be written to a file. During the lifetime of these
|
||||
/// objects, the content or existence of the specified file is undefined. That
|
||||
/// is, creating an OutputBuffer for a file may immediately remove the file.
|
||||
/// If the FileOutputBuffer is committed, the target file's content will become
|
||||
/// the buffer content at the time of the commit. If the FileOutputBuffer is
|
||||
/// If the FileOutputBuffer is committed, the target file's content will become
|
||||
/// the buffer content at the time of the commit. If the FileOutputBuffer is
|
||||
/// not committed, the file will be deleted in the FileOutputBuffer destructor.
|
||||
class FileOutputBuffer {
|
||||
public:
|
||||
|
||||
enum {
|
||||
F_executable = 1 /// set the 'x' bit on the resulting file
|
||||
};
|
||||
};
|
||||
|
||||
/// Factory method to create an OutputBuffer object which manages a read/write
|
||||
/// buffer of the specified size. When committed, the buffer will be written
|
||||
/// to the file at the specified path.
|
||||
static error_code create(StringRef FilePath, size_t Size,
|
||||
OwningPtr<FileOutputBuffer> &Result,
|
||||
unsigned Flags=0);
|
||||
|
||||
/// to the file at the specified path.
|
||||
static error_code create(StringRef FilePath, size_t Size,
|
||||
OwningPtr<FileOutputBuffer> &Result,
|
||||
unsigned Flags = 0);
|
||||
|
||||
/// Returns a pointer to the start of the buffer.
|
||||
uint8_t *getBufferStart() const {
|
||||
return BufferStart;
|
||||
uint8_t *getBufferStart() {
|
||||
return (uint8_t*)Region->data();
|
||||
}
|
||||
|
||||
|
||||
/// Returns a pointer to the end of the buffer.
|
||||
uint8_t *getBufferEnd() const {
|
||||
return BufferEnd;
|
||||
uint8_t *getBufferEnd() {
|
||||
return (uint8_t*)Region->data() + Region->size();
|
||||
}
|
||||
|
||||
|
||||
/// Returns size of the buffer.
|
||||
size_t getBufferSize() const {
|
||||
return BufferEnd - BufferStart;
|
||||
return Region->size();
|
||||
}
|
||||
|
||||
|
||||
/// Returns path where file will show up if buffer is committed.
|
||||
StringRef getPath() const {
|
||||
return FinalPath;
|
||||
}
|
||||
|
||||
/// Flushes the content of the buffer to its file and deallocates the
|
||||
|
||||
/// Flushes the content of the buffer to its file and deallocates the
|
||||
/// buffer. If commit() is not called before this object's destructor
|
||||
/// is called, the file is deleted in the destructor. The optional parameter
|
||||
/// is used if it turns out you want the file size to be smaller than
|
||||
/// initially requested.
|
||||
error_code commit(int64_t NewSmallerSize = -1);
|
||||
|
||||
|
||||
/// If this object was previously committed, the destructor just deletes
|
||||
/// this object. If this object was not committed, the destructor
|
||||
/// deallocates the buffer and the target file is never written.
|
||||
~FileOutputBuffer();
|
||||
|
||||
|
||||
private:
|
||||
FileOutputBuffer(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
|
||||
FileOutputBuffer &operator=(const FileOutputBuffer &) LLVM_DELETED_FUNCTION;
|
||||
protected:
|
||||
FileOutputBuffer(uint8_t *Start, uint8_t *End,
|
||||
StringRef Path, StringRef TempPath);
|
||||
|
||||
uint8_t *BufferStart;
|
||||
uint8_t *BufferEnd;
|
||||
|
||||
FileOutputBuffer(llvm::sys::fs::mapped_file_region *R,
|
||||
StringRef Path, StringRef TempPath);
|
||||
|
||||
OwningPtr<llvm::sys::fs::mapped_file_region> Region;
|
||||
SmallString<128> FinalPath;
|
||||
SmallString<128> TempPath;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // end namespace llvm
|
||||
|
||||
#endif
|
||||
|
@ -14,34 +14,26 @@
|
||||
#include "llvm/Support/FileOutputBuffer.h"
|
||||
#include "llvm/ADT/OwningPtr.h"
|
||||
#include "llvm/ADT/SmallVector.h"
|
||||
#include "llvm/Support/FileSystem.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include "llvm/Support/system_error.h"
|
||||
|
||||
using llvm::sys::fs::mapped_file_region;
|
||||
|
||||
namespace llvm {
|
||||
|
||||
|
||||
FileOutputBuffer::FileOutputBuffer(uint8_t *Start, uint8_t *End,
|
||||
StringRef Path, StringRef TmpPath)
|
||||
: BufferStart(Start), BufferEnd(End) {
|
||||
FinalPath.assign(Path);
|
||||
TempPath.assign(TmpPath);
|
||||
FileOutputBuffer::FileOutputBuffer(mapped_file_region * R,
|
||||
StringRef Path, StringRef TmpPath)
|
||||
: Region(R)
|
||||
, FinalPath(Path)
|
||||
, TempPath(TmpPath) {
|
||||
}
|
||||
|
||||
|
||||
FileOutputBuffer::~FileOutputBuffer() {
|
||||
// If not already commited, delete buffer and remove temp file.
|
||||
if ( BufferStart != NULL ) {
|
||||
sys::fs::unmap_file_pages((void*)BufferStart, getBufferSize());
|
||||
bool Existed;
|
||||
sys::fs::remove(Twine(TempPath), Existed);
|
||||
}
|
||||
bool Existed;
|
||||
sys::fs::remove(Twine(TempPath), Existed);
|
||||
}
|
||||
|
||||
|
||||
error_code FileOutputBuffer::create(StringRef FilePath,
|
||||
size_t Size,
|
||||
error_code FileOutputBuffer::create(StringRef FilePath,
|
||||
size_t Size,
|
||||
OwningPtr<FileOutputBuffer> &Result,
|
||||
unsigned Flags) {
|
||||
// If file already exists, it must be a regular file (to be mappable).
|
||||
@ -69,34 +61,27 @@ error_code FileOutputBuffer::create(StringRef FilePath,
|
||||
EC = sys::fs::remove(FilePath, Existed);
|
||||
if (EC)
|
||||
return EC;
|
||||
|
||||
|
||||
// Create new file in same directory but with random name.
|
||||
SmallString<128> TempFilePath;
|
||||
int FD;
|
||||
EC = sys::fs::unique_file(Twine(FilePath) + ".tmp%%%%%%%",
|
||||
FD, TempFilePath, false, 0644);
|
||||
EC = sys::fs::unique_file(Twine(FilePath) + ".tmp%%%%%%%",
|
||||
FD, TempFilePath, false, 0644);
|
||||
if (EC)
|
||||
return EC;
|
||||
|
||||
// The unique_file() interface leaks lower layers and returns a file
|
||||
// descriptor. There is no way to directly close it, so use this hack
|
||||
// to hand it off to raw_fd_ostream to close for us.
|
||||
{
|
||||
raw_fd_ostream Dummy(FD, /*shouldClose=*/true);
|
||||
}
|
||||
|
||||
// Resize file to requested initial size
|
||||
EC = sys::fs::resize_file(Twine(TempFilePath), Size);
|
||||
|
||||
OwningPtr<mapped_file_region> MappedFile(
|
||||
new mapped_file_region(FD, mapped_file_region::readwrite, Size, 0, EC));
|
||||
if (EC)
|
||||
return EC;
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
|
||||
sys::fs::perms new_perms = Stat2.permissions();
|
||||
if ( new_perms & sys::fs::owner_read )
|
||||
new_perms |= sys::fs::owner_exe;
|
||||
@ -110,38 +95,25 @@ error_code FileOutputBuffer::create(StringRef FilePath,
|
||||
return EC;
|
||||
}
|
||||
|
||||
// Memory map new file.
|
||||
void *Base;
|
||||
EC = sys::fs::map_file_pages(Twine(TempFilePath), 0, Size, true, Base);
|
||||
if (EC)
|
||||
return EC;
|
||||
|
||||
// Create FileOutputBuffer object to own mapped range.
|
||||
uint8_t *Start = reinterpret_cast<uint8_t*>(Base);
|
||||
Result.reset(new FileOutputBuffer(Start, Start+Size, FilePath, TempFilePath));
|
||||
|
||||
return error_code::success();
|
||||
}
|
||||
Result.reset(new FileOutputBuffer(MappedFile.get(), FilePath, TempFilePath));
|
||||
if (Result)
|
||||
MappedFile.take();
|
||||
|
||||
return error_code::success();
|
||||
}
|
||||
|
||||
error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
|
||||
// Unmap buffer, letting OS flush dirty pages to file on disk.
|
||||
void *Start = reinterpret_cast<void*>(BufferStart);
|
||||
error_code EC = sys::fs::unmap_file_pages(Start, getBufferSize());
|
||||
if (EC)
|
||||
return EC;
|
||||
|
||||
Region.reset(0);
|
||||
|
||||
// If requested, resize file as part of commit.
|
||||
if ( NewSmallerSize != -1 ) {
|
||||
EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
|
||||
error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
|
||||
if (EC)
|
||||
return EC;
|
||||
}
|
||||
|
||||
|
||||
// Rename file to final name.
|
||||
return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
@ -7,9 +7,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/Support/FileOutputBuffer.h"
|
||||
|
||||
#include "llvm/ADT/OwningPtr.h"
|
||||
#include "llvm/Support/ErrorHandling.h"
|
||||
#include "llvm/Support/FileOutputBuffer.h"
|
||||
#include "llvm/Support/FileSystem.h"
|
||||
#include "llvm/Support/PathV2.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
@ -27,13 +28,6 @@ using namespace llvm::sys;
|
||||
} else {}
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
// NOTE: Temporarily run this test on unix only. Once the file mapping
|
||||
// routines are ported to Windows, this conditional can be removed.
|
||||
#if LLVM_ON_UNIX
|
||||
|
||||
|
||||
TEST(FileOutputBuffer, Test) {
|
||||
// Create unique temporary directory for these tests
|
||||
SmallString<128> TestDirectory;
|
||||
@ -45,7 +39,7 @@ TEST(FileOutputBuffer, Test) {
|
||||
::close(fd);
|
||||
TestDirectory = path::parent_path(TestDirectory);
|
||||
}
|
||||
|
||||
|
||||
// TEST 1: Verify commit case.
|
||||
SmallString<128> File1(TestDirectory);
|
||||
File1.append("/file1");
|
||||
@ -61,7 +55,7 @@ TEST(FileOutputBuffer, Test) {
|
||||
}
|
||||
// Verify file exists and starts with special header.
|
||||
bool MagicMatches = false;
|
||||
ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"),
|
||||
ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"),
|
||||
MagicMatches));
|
||||
EXPECT_TRUE(MagicMatches);
|
||||
// Verify file is correct size.
|
||||
@ -82,8 +76,7 @@ TEST(FileOutputBuffer, Test) {
|
||||
// Verify file does not exist (because buffer not commited).
|
||||
bool Exists = false;
|
||||
ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
|
||||
EXPECT_FALSE(Exists);
|
||||
|
||||
EXPECT_FALSE(Exists);
|
||||
|
||||
// TEST 3: Verify sizing down case.
|
||||
SmallString<128> File3(TestDirectory);
|
||||
@ -100,7 +93,7 @@ TEST(FileOutputBuffer, Test) {
|
||||
}
|
||||
// Verify file exists and starts with special header.
|
||||
bool MagicMatches3 = false;
|
||||
ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"),
|
||||
ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"),
|
||||
MagicMatches3));
|
||||
EXPECT_TRUE(MagicMatches3);
|
||||
// Verify file is correct size.
|
||||
@ -108,13 +101,12 @@ TEST(FileOutputBuffer, Test) {
|
||||
ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
|
||||
ASSERT_EQ(File3Size, 5000ULL);
|
||||
|
||||
|
||||
// TEST 4: Verify file can be made executable.
|
||||
SmallString<128> File4(TestDirectory);
|
||||
File4.append("/file4");
|
||||
{
|
||||
OwningPtr<FileOutputBuffer> Buffer;
|
||||
ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
|
||||
ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
|
||||
FileOutputBuffer::F_executable));
|
||||
// Start buffer with special header.
|
||||
memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
|
||||
@ -131,7 +123,4 @@ TEST(FileOutputBuffer, Test) {
|
||||
uint32_t RemovedCount;
|
||||
ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount));
|
||||
}
|
||||
|
||||
#endif // LLVM_ON_UNIX
|
||||
|
||||
} // anonymous namespace
|
||||
|
Loading…
Reference in New Issue
Block a user