llvm.org GIT mirror llvm / adfe263
Initial commit of new FileOutputBuffer support class. Since the llvm::sys::fs::map_file_pages() support function it relies on is not yet implemented on Windows, the unit tests for FileOutputBuffer are currently conditionalized to run only on unix. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161099 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Kledzik 8 years ago
5 changed file(s) with 384 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //=== FileOutputBuffer.h - File Output Buffer -------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Utility for creating a in-memory buffer that will be written to a file.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_SUPPORT_FILEOUTPUTBUFFER_H
14 #define LLVM_SUPPORT_FILEOUTPUTBUFFER_H
15
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/DataTypes.h"
19
20 namespace llvm {
21
22 class error_code;
23 template class OwningPtr;
24
25 /// FileOutputBuffer - This interface provides simple way to create an in-memory
26 /// buffer which will be written to a file. During the lifetime of these
27 /// objects, the content or existence of the specified file is undefined. That
28 /// is, creating an OutputBuffer for a file may immediately remove the file.
29 /// If the FileOutputBuffer is committed, the target file's content will become
30 /// the buffer content at the time of the commit. If the FileOutputBuffer is
31 /// not committed, the file will be deleted in the FileOutputBuffer destructor.
32 class FileOutputBuffer {
33 public:
34
35 enum {
36 F_executable = 1 /// set the 'x' bit on the resulting file
37 };
38
39 /// Factory method to create an OutputBuffer object which manages a read/write
40 /// buffer of the specified size. When committed, the buffer will be written
41 /// to the file at the specified path.
42 static error_code create(StringRef FilePath, size_t Size,
43 OwningPtr &Result,
44 unsigned Flags=0);
45
46
47 /// Returns a pointer to the start of the buffer.
48 uint8_t *getBufferStart() const {
49 return BufferStart;
50 }
51
52 /// Returns a pointer to the end of the buffer.
53 uint8_t *getBufferEnd() const {
54 return BufferEnd;
55 }
56
57 /// Returns size of the buffer.
58 size_t getBufferSize() const {
59 return BufferEnd - BufferStart;
60 }
61
62 /// Returns path where file will show up if buffer is committed.
63 StringRef getPath() const {
64 return FinalPath;
65 }
66
67 /// Flushes the content of the buffer to its file and deallocates the
68 /// buffer. If commit() is not called before this object's destructor
69 /// is called, the file is deleted in the destructor. The optional parameter
70 /// is used if it turns out you want the file size to be smaller than
71 /// initially requested.
72 error_code commit(int64_t NewSmallerSize = -1);
73
74 /// If this object was previously committed, the destructor just deletes
75 /// this object. If this object was not committed, the destructor
76 /// deallocates the buffer and the target file is never written.
77 ~FileOutputBuffer();
78
79
80 protected:
81 FileOutputBuffer(const FileOutputBuffer &); // DO NOT IMPLEMENT
82 FileOutputBuffer &operator=(const FileOutputBuffer &); // DO NOT IMPLEMENT
83 FileOutputBuffer(uint8_t *Start, uint8_t *End,
84 StringRef Path, StringRef TempPath);
85
86 uint8_t *BufferStart;
87 uint8_t *BufferEnd;
88 SmallString<128> FinalPath;
89 SmallString<128> TempPath;
90 };
91
92
93
94 } // end namespace llvm
95
96 #endif
2222 Dwarf.cpp
2323 ErrorHandling.cpp
2424 FileUtilities.cpp
25 FileOutputBuffer.cpp
2526 FoldingSet.cpp
2627 FormattedStream.cpp
2728 GraphWriter.cpp
0 //===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Utility for creating a in-memory buffer that will be written to a file.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Support/FileOutputBuffer.h"
14
15 #include "llvm/ADT/OwningPtr.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/Support/system_error.h"
20
21
22 namespace llvm {
23
24
25 FileOutputBuffer::FileOutputBuffer(uint8_t *Start, uint8_t *End,
26 StringRef Path, StringRef TmpPath)
27 : BufferStart(Start), BufferEnd(End) {
28 FinalPath.assign(Path);
29 TempPath.assign(TmpPath);
30 }
31
32
33 FileOutputBuffer::~FileOutputBuffer() {
34 // If not already commited, delete buffer and remove temp file.
35 if ( BufferStart != NULL ) {
36 sys::fs::unmap_file_pages((void*)BufferStart, getBufferSize());
37 bool Existed;
38 sys::fs::remove(Twine(TempPath), Existed);
39 }
40 }
41
42
43 error_code FileOutputBuffer::create(StringRef FilePath,
44 size_t Size,
45 OwningPtr &Result,
46 unsigned Flags) {
47 // If file already exists, it must be a regular file (to be mappable).
48 sys::fs::file_status Stat;
49 error_code EC = sys::fs::status(FilePath, Stat);
50 switch (Stat.type()) {
51 case sys::fs::file_type::file_not_found:
52 // If file does not exist, we'll create one.
53 break;
54 case sys::fs::file_type::regular_file: {
55 // If file is not currently writable, error out.
56 // FIXME: There is no sys::fs:: api for checking this.
57 // FIXME: In posix, you use the access() call to check this.
58 }
59 break;
60 default:
61 if (EC)
62 return EC;
63 else
64 return make_error_code(errc::operation_not_permitted);
65 }
66
67 // Delete target file.
68 bool Existed;
69 EC = sys::fs::remove(FilePath, Existed);
70 if (EC)
71 return EC;
72
73 // Create new file in same directory but with random name.
74 SmallString<128> TempFilePath;
75 int FD;
76 EC = sys::fs::unique_file(Twine(FilePath) + ".tmp%%%%%%%",
77 FD, TempFilePath, false, 0644);
78 if (EC)
79 return EC;
80
81 // The unique_file() interface leaks lower layers and returns a file
82 // descriptor. There is no way to directly close it, so use this hack
83 // to hand it off to raw_fd_ostream to close for us.
84 {
85 raw_fd_ostream Dummy(FD, /*shouldClose=*/true);
86 }
87
88 // Resize file to requested initial size
89 EC = sys::fs::resize_file(Twine(TempFilePath), Size);
90 if (EC)
91 return EC;
92
93 // If requested, make the output file executable.
94 if ( Flags & F_executable ) {
95 sys::fs::file_status Stat2;
96 EC = sys::fs::status(Twine(TempFilePath), Stat2);
97 if (EC)
98 return EC;
99
100 sys::fs::perms new_perms = Stat2.permissions();
101 if ( new_perms & sys::fs::owner_read )
102 new_perms |= sys::fs::owner_exe;
103 if ( new_perms & sys::fs::group_read )
104 new_perms |= sys::fs::group_exe;
105 if ( new_perms & sys::fs::others_read )
106 new_perms |= sys::fs::others_exe;
107 new_perms |= sys::fs::add_perms;
108 EC = sys::fs::permissions(Twine(TempFilePath), new_perms);
109 if (EC)
110 return EC;
111 }
112
113 // Memory map new file.
114 void *Base;
115 EC = sys::fs::map_file_pages(Twine(TempFilePath), 0, Size, true, Base);
116 if (EC)
117 return EC;
118
119 // Create FileOutputBuffer object to own mapped range.
120 uint8_t *Start = reinterpret_cast(Base);
121 Result.reset(new FileOutputBuffer(Start, Start+Size, FilePath, TempFilePath));
122
123 return error_code::success();
124 }
125
126
127 error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
128 // Unmap buffer, letting OS flush dirty pages to file on disk.
129 void *Start = reinterpret_cast(BufferStart);
130 error_code EC = sys::fs::unmap_file_pages(Start, getBufferSize());
131 if (EC)
132 return EC;
133
134 // If requested, resize file as part of commit.
135 if ( NewSmallerSize != -1 ) {
136 EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
137 if (EC)
138 return EC;
139 }
140
141 // Rename file to final name.
142 return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
143 }
144
145
146 } // namespace
147
1111 ConstantRangeTest.cpp
1212 DataExtractorTest.cpp
1313 EndianTest.cpp
14 FileOutputBufferTest.cpp
1415 IntegersSubsetTest.cpp
1516 LeakDetectorTest.cpp
1617 ManagedStatic.cpp
0 //===- llvm/unittest/Support/FileOutputBuffer.cpp - unit tests ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/OwningPtr.h"
10 #include "llvm/Support/ErrorHandling.h"
11 #include "llvm/Support/FileOutputBuffer.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/PathV2.h"
14 #include "llvm/Support/raw_ostream.h"
15
16 #include "gtest/gtest.h"
17
18 using namespace llvm;
19 using namespace llvm::sys;
20
21 #define ASSERT_NO_ERROR(x) \
22 if (error_code ASSERT_NO_ERROR_ec = x) { \
23 errs() << #x ": did not return errc::success.\n" \
24 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
25 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
26 } else {}
27
28 namespace {
29
30
31 // NOTE: Temporarily run this test on unix only. Once the file mapping
32 // routines are ported to Windows, this conditional can be removed.
33 #if LLVM_ON_UNIX
34
35
36 TEST(FileOutputBuffer, Test) {
37 // Create unique temporary directory for these tests
38 SmallString<128> TestDirectory;
39 {
40 int fd;
41 ASSERT_NO_ERROR(
42 fs::unique_file("FileOutputBuffer-test-%%-%%-%%-%%/dir", fd,
43 TestDirectory));
44 ::close(fd);
45 TestDirectory = path::parent_path(TestDirectory);
46 }
47
48 // TEST 1: Verify commit case.
49 SmallString<128> File1(TestDirectory);
50 File1.append("/file1");
51 {
52 OwningPtr Buffer;
53 ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
54 // Start buffer with special header.
55 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
56 // Write to end of buffer to verify it is writable.
57 memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
58 // Commit buffer.
59 ASSERT_NO_ERROR(Buffer->commit());
60 }
61 // Verify file exists and starts with special header.
62 bool MagicMatches = false;
63 ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"),
64 MagicMatches));
65 EXPECT_TRUE(MagicMatches);
66 // Verify file is correct size.
67 uint64_t File1Size;
68 ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size));
69 ASSERT_EQ(File1Size, 8192ULL);
70
71 // TEST 2: Verify abort case.
72 SmallString<128> File2(TestDirectory);
73 File2.append("/file2");
74 {
75 OwningPtr Buffer2;
76 ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
77 // Fill buffer with special header.
78 memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
79 // Do *not* commit buffer.
80 }
81 // Verify file does not exist (because buffer not commited).
82 bool Exists = false;
83 ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
84 EXPECT_FALSE(Exists);
85
86
87 // TEST 3: Verify sizing down case.
88 SmallString<128> File3(TestDirectory);
89 File3.append("/file3");
90 {
91 OwningPtr Buffer;
92 ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
93 // Start buffer with special header.
94 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
95 // Write to end of buffer to verify it is writable.
96 memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20);
97 // Commit buffer, but size down to smaller size
98 ASSERT_NO_ERROR(Buffer->commit(5000));
99 }
100 // Verify file exists and starts with special header.
101 bool MagicMatches3 = false;
102 ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"),
103 MagicMatches3));
104 EXPECT_TRUE(MagicMatches3);
105 // Verify file is correct size.
106 uint64_t File3Size;
107 ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size));
108 ASSERT_EQ(File3Size, 5000ULL);
109
110
111 // TEST 4: Verify file can be made executable.
112 SmallString<128> File4(TestDirectory);
113 File4.append("/file4");
114 {
115 OwningPtr Buffer;
116 ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
117 FileOutputBuffer::F_executable));
118 // Start buffer with special header.
119 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
120 // Commit buffer.
121 ASSERT_NO_ERROR(Buffer->commit());
122 }
123 // Verify file exists and is executable.
124 fs::file_status Status;
125 ASSERT_NO_ERROR(fs::status(Twine(File4), Status));
126 bool IsExecutable = (Status.permissions() & fs::owner_exe);
127 EXPECT_TRUE(IsExecutable);
128
129 // Clean up.
130 uint32_t RemovedCount;
131 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount));
132 }
133
134 #endif // LLVM_ON_UNIX
135
136 } // anonymous namespace