llvm.org GIT mirror llvm / fe4c063
Return ErrorOr from FileOutputBuffer::create. NFC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244848 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
3 changed file(s) with 23 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
1616 #include "llvm/ADT/SmallString.h"
1717 #include "llvm/ADT/StringRef.h"
1818 #include "llvm/Support/DataTypes.h"
19 #include "llvm/Support/ErrorOr.h"
1920 #include "llvm/Support/FileSystem.h"
2021
2122 namespace llvm {
3637 /// Factory method to create an OutputBuffer object which manages a read/write
3738 /// buffer of the specified size. When committed, the buffer will be written
3839 /// to the file at the specified path.
39 static std::error_code create(StringRef FilePath, size_t Size,
40 std::unique_ptr &Result,
41 unsigned Flags = 0);
40 static ErrorOr>
41 create(StringRef FilePath, size_t Size, unsigned Flags = 0);
4242
4343 /// Returns a pointer to the start of the buffer.
4444 uint8_t *getBufferStart() {
3333 sys::fs::remove(Twine(TempPath));
3434 }
3535
36 std::error_code
37 FileOutputBuffer::create(StringRef FilePath, size_t Size,
38 std::unique_ptr &Result,
39 unsigned Flags) {
36 ErrorOr>
37 FileOutputBuffer::create(StringRef FilePath, size_t Size, unsigned Flags) {
4038 // If file already exists, it must be a regular file (to be mappable).
4139 sys::fs::file_status Stat;
4240 std::error_code EC = sys::fs::status(FilePath, Stat);
9492 if (Ret)
9593 return std::error_code(errno, std::generic_category());
9694
97 Result.reset(
95 std::unique_ptr Buf(
9896 new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
99
100 return std::error_code();
97 return std::move(Buf);
10198 }
10299
103100 std::error_code FileOutputBuffer::commit() {
3838 SmallString<128> File1(TestDirectory);
3939 File1.append("/file1");
4040 {
41 std::unique_ptr Buffer;
42 ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
41 ErrorOr> BufferOrErr =
42 FileOutputBuffer::create(File1, 8192);
43 ASSERT_NO_ERROR(BufferOrErr.getError());
44 std::unique_ptr &Buffer = *BufferOrErr;
4345 // Start buffer with special header.
4446 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
4547 // Write to end of buffer to verify it is writable.
5860 SmallString<128> File2(TestDirectory);
5961 File2.append("/file2");
6062 {
61 std::unique_ptr Buffer2;
62 ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
63 ErrorOr> Buffer2OrErr =
64 FileOutputBuffer::create(File2, 8192);
65 ASSERT_NO_ERROR(Buffer2OrErr.getError());
66 std::unique_ptr &Buffer2 = *Buffer2OrErr;
6367 // Fill buffer with special header.
6468 memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
6569 // Do *not* commit buffer.
7377 SmallString<128> File3(TestDirectory);
7478 File3.append("/file3");
7579 {
76 std::unique_ptr Buffer;
77 ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
80 ErrorOr> BufferOrErr =
81 FileOutputBuffer::create(File3, 8192000);
82 ASSERT_NO_ERROR(BufferOrErr.getError());
83 std::unique_ptr &Buffer = *BufferOrErr;
7884 // Start buffer with special header.
7985 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
8086 // Write to end of buffer to verify it is writable.
9298 SmallString<128> File4(TestDirectory);
9399 File4.append("/file4");
94100 {
95 std::unique_ptr Buffer;
96 ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
97 FileOutputBuffer::F_executable));
101 ErrorOr> BufferOrErr =
102 FileOutputBuffer::create(File4, 8192, FileOutputBuffer::F_executable);
103 ASSERT_NO_ERROR(BufferOrErr.getError());
104 std::unique_ptr &Buffer = *BufferOrErr;
98105 // Start buffer with special header.
99106 memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
100107 // Commit buffer.