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"
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"
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);
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());
38 sys::fs::remove(Twine(TempPath), Existed);
43 error_code FileOutputBuffer::create(StringRef FilePath,
45 OwningPtr<FileOutputBuffer> &Result,
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.
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.
64 return make_error_code(errc::operation_not_permitted);
67 // Delete target file.
69 EC = sys::fs::remove(FilePath, Existed);
73 // Create new file in same directory but with random name.
74 SmallString<128> TempFilePath;
76 EC = sys::fs::unique_file(Twine(FilePath) + ".tmp%%%%%%%",
77 FD, TempFilePath, false, 0644);
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.
85 raw_fd_ostream Dummy(FD, /*shouldClose=*/true);
88 // Resize file to requested initial size
89 EC = sys::fs::resize_file(Twine(TempFilePath), Size);
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);
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);
113 // Memory map new file.
115 EC = sys::fs::map_file_pages(Twine(TempFilePath), 0, Size, true, Base);
119 // Create FileOutputBuffer object to own mapped range.
120 uint8_t *Start = reinterpret_cast<uint8_t*>(Base);
121 Result.reset(new FileOutputBuffer(Start, Start+Size, FilePath, TempFilePath));
123 return error_code::success();
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<void*>(BufferStart);
130 error_code EC = sys::fs::unmap_file_pages(Start, getBufferSize());
134 // If requested, resize file as part of commit.
135 if ( NewSmallerSize != -1 ) {
136 EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
141 // Rename file to final name.
142 return sys::fs::rename(Twine(TempPath), Twine(FinalPath));