1 //===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Utility for creating a in-memory buffer that will be written to a file.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/FileOutputBuffer.h"
16 #include "llvm/ADT/OwningPtr.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/system_error.h"
26 FileOutputBuffer::FileOutputBuffer(uint8_t *Start, uint8_t *End,
27 StringRef Path, StringRef TmpPath)
28 : BufferStart(Start), BufferEnd(End) {
29 FinalPath.assign(Path);
30 TempPath.assign(TmpPath);
34 FileOutputBuffer::~FileOutputBuffer() {
35 // If not already commited, delete buffer and remove temp file.
36 if ( BufferStart != NULL ) {
37 sys::fs::unmap_file_pages((void*)BufferStart, getBufferSize());
39 sys::fs::remove(Twine(TempPath), Existed);
44 error_code FileOutputBuffer::create(StringRef FilePath,
46 OwningPtr<FileOutputBuffer> &Result,
48 // If file already exists, it must be a regular file (to be mappable).
49 sys::fs::file_status Stat;
50 error_code EC = sys::fs::status(FilePath, Stat);
51 switch (Stat.type()) {
52 case sys::fs::file_type::file_not_found:
53 // If file does not exist, we'll create one.
55 case sys::fs::file_type::regular_file: {
56 // If file is not currently writable, error out.
57 // FIXME: There is no sys::fs:: api for checking this.
58 // FIXME: In posix, you use the access() call to check this.
65 return make_error_code(errc::operation_not_permitted);
68 // Delete target file.
70 EC = sys::fs::remove(FilePath, Existed);
74 // Create new file in same directory but with random name.
75 SmallString<128> TempFilePath;
77 EC = sys::fs::unique_file(Twine(FilePath) + ".tmp%%%%%%%",
78 FD, TempFilePath, false, 0644);
82 // The unique_file() interface leaks lower layers and returns a file
83 // descriptor. There is no way to directly close it, so use this hack
84 // to hand it off to raw_fd_ostream to close for us.
86 raw_fd_ostream Dummy(FD, /*shouldClose=*/true);
89 // Resize file to requested initial size
90 EC = sys::fs::resize_file(Twine(TempFilePath), Size);
94 // If requested, make the output file executable.
95 if ( Flags & F_executable ) {
96 sys::fs::file_status Stat2;
97 EC = sys::fs::status(Twine(TempFilePath), Stat2);
101 sys::fs::perms new_perms = Stat2.permissions();
102 if ( new_perms & sys::fs::owner_read )
103 new_perms |= sys::fs::owner_exe;
104 if ( new_perms & sys::fs::group_read )
105 new_perms |= sys::fs::group_exe;
106 if ( new_perms & sys::fs::others_read )
107 new_perms |= sys::fs::others_exe;
108 new_perms |= sys::fs::add_perms;
109 EC = sys::fs::permissions(Twine(TempFilePath), new_perms);
114 // Memory map new file.
116 EC = sys::fs::map_file_pages(Twine(TempFilePath), 0, Size, true, Base);
120 // Create FileOutputBuffer object to own mapped range.
121 uint8_t *Start = reinterpret_cast<uint8_t*>(Base);
122 Result.reset(new FileOutputBuffer(Start, Start+Size, FilePath, TempFilePath));
124 return error_code::success();
128 error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
129 // Unmap buffer, letting OS flush dirty pages to file on disk.
130 void *Start = reinterpret_cast<void*>(BufferStart);
131 error_code EC = sys::fs::unmap_file_pages(Start, getBufferSize());
135 // If requested, resize file as part of commit.
136 if ( NewSmallerSize != -1 ) {
137 EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
142 // Rename file to final name.
143 return sys::fs::rename(Twine(TempPath), Twine(FinalPath));