llvm.org GIT mirror llvm / 0fa582d
Convert FileOutputBuffer to Expected. NFC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317649 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 1 year, 9 months ago
6 changed file(s) with 18 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"
19 #include "llvm/Support/Error.h"
2020 #include "llvm/Support/FileSystem.h"
2121
2222 namespace llvm {
3636 /// Factory method to create an OutputBuffer object which manages a read/write
3737 /// buffer of the specified size. When committed, the buffer will be written
3838 /// to the file at the specified path.
39 static ErrorOr>
39 static Expected>
4040 create(StringRef FilePath, size_t Size, unsigned Flags = 0);
4141
4242 /// Returns a pointer to the start of the buffer.
175175
176176 uint64_t Filesize = Layout.SB->BlockSize * Layout.SB->NumBlocks;
177177 auto OutFileOrError = FileOutputBuffer::create(Filename, Filesize);
178 if (OutFileOrError.getError())
179 return llvm::make_error(generic_error_code::invalid_path,
180 Filename);
178 if (auto E = OutFileOrError.takeError())
179 return E;
181180 FileBufferByteStream Buffer(std::move(*OutFileOrError),
182181 llvm::support::little);
183182 BinaryStreamWriter Writer(Buffer);
3737 std::unique_ptr Buf)
3838 : FileOutputBuffer(Path), Buffer(std::move(Buf)), TempPath(TempPath) {}
3939
40 static ErrorOr>
40 static Expected>
4141 create(StringRef Path, size_t Size, unsigned Mode);
4242
4343 uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
7777 InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
7878 : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
7979
80 static ErrorOr>
80 static Expected>
8181 create(StringRef Path, size_t Size, unsigned Mode) {
8282 std::error_code EC;
8383 MemoryBlock MB = Memory::allocateMappedMemory(
8484 Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
8585 if (EC)
86 return EC;
86 return errorCodeToError(EC);
8787 return llvm::make_unique(Path, MB, Mode);
8888 }
8989
110110 unsigned Mode;
111111 };
112112
113 ErrorOr>
113 Expected>
114114 OnDiskBuffer::create(StringRef Path, size_t Size, unsigned Mode) {
115115 // Create new file in same directory but with random name.
116116 SmallString<128> TempPath;
117117 int FD;
118118 if (auto EC = fs::createUniqueFile(Path + ".tmp%%%%%%%", FD, TempPath, Mode))
119 return EC;
119 return errorCodeToError(EC);
120120
121121 sys::RemoveFileOnSignal(TempPath);
122122
127127 // pretty slow just like it writes specified amount of bytes,
128128 // so we should avoid calling that function.
129129 if (auto EC = fs::resize_file(FD, Size))
130 return EC;
130 return errorCodeToError(EC);
131131 #endif
132132
133133 // Mmap it.
136136 FD, fs::mapped_file_region::readwrite, Size, 0, EC);
137137 close(FD);
138138 if (EC)
139 return EC;
139 return errorCodeToError(EC);
140140 return llvm::make_unique(Path, TempPath, std::move(MappedFile));
141141 }
142142
143143 // Create an instance of FileOutputBuffer.
144 ErrorOr>
144 Expected>
145145 FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
146146 unsigned Mode = fs::all_read | fs::all_write;
147147 if (Flags & F_executable)
160160 // destination file and write to it on commit().
161161 switch (Stat.type()) {
162162 case fs::file_type::directory_file:
163 return errc::is_a_directory;
163 return errorCodeToError(errc::is_a_directory);
164164 case fs::file_type::regular_file:
165165 case fs::file_type::file_not_found:
166166 case fs::file_type::status_error:
201201 auto FileOrErr =
202202 FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
203203 if (!FileOrErr)
204 reportError(OutputFile, FileOrErr.getError());
204 reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
205205 std::unique_ptr FileBuffer = std::move(*FileOrErr);
206206 std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
207207 FileBuffer->getBufferStart());
145145 std::unique_ptr OutputBuffer = Merger.getMergedManifest();
146146 if (!OutputBuffer)
147147 reportError("empty manifest not written");
148 ErrorOr> FileOrErr =
148 Expected> FileOrErr =
149149 FileOutputBuffer::create(OutputFile, OutputBuffer->getBufferSize());
150150 if (!FileOrErr)
151 reportError(OutputFile, FileOrErr.getError());
151 reportError(OutputFile, errorToErrorCode(FileOrErr.takeError()));
152152 std::unique_ptr FileBuffer = std::move(*FileOrErr);
153153 std::copy(OutputBuffer->getBufferStart(), OutputBuffer->getBufferEnd(),
154154 FileBuffer->getBufferStart());
112112 template
113113 void WriteObjectFile(const Object &Obj, StringRef File) {
114114 std::unique_ptr Buffer;
115 ErrorOr> BufferOrErr =
115 Expected> BufferOrErr =
116116 FileOutputBuffer::create(File, Obj.totalSize(),
117117 FileOutputBuffer::F_executable);
118 if (BufferOrErr.getError())
118 if (BufferOrErr.takeError())
119119 error("failed to open " + OutputFilename);
120120 else
121121 Buffer = std::move(*BufferOrErr);