1 //===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Utility for creating a in-memory buffer that will be written to a file.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/FileOutputBuffer.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Support/Errc.h"
18 #include "llvm/Support/Signals.h"
19 #include <system_error>
20 
21 #if !defined(_MSC_VER) && !defined(__MINGW32__)
22 #include <unistd.h>
23 #else
24 #include <io.h>
25 #endif
26 
27 using llvm::sys::fs::mapped_file_region;
28 
29 namespace llvm {
30 FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R,
31                                    StringRef Path, StringRef TmpPath)
32     : Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {}
33 
34 FileOutputBuffer::~FileOutputBuffer() {
35   // Close the mapping before deleting the temp file, so that the removal
36   // succeeds.
37   Region.reset();
38   sys::fs::remove(Twine(TempPath));
39 }
40 
41 ErrorOr<std::unique_ptr<FileOutputBuffer>>
42 FileOutputBuffer::create(StringRef FilePath, size_t Size, unsigned Flags) {
43   // If file already exists, it must be a regular file (to be mappable).
44   sys::fs::file_status Stat;
45   std::error_code EC = sys::fs::status(FilePath, Stat);
46   switch (Stat.type()) {
47     case sys::fs::file_type::file_not_found:
48       // If file does not exist, we'll create one.
49       break;
50     case sys::fs::file_type::regular_file: {
51         // If file is not currently writable, error out.
52         // FIXME: There is no sys::fs:: api for checking this.
53         // FIXME: In posix, you use the access() call to check this.
54       }
55       break;
56     default:
57       if (EC)
58         return EC;
59       else
60         return make_error_code(errc::operation_not_permitted);
61   }
62 
63   // Delete target file.
64   EC = sys::fs::remove(FilePath);
65   if (EC)
66     return EC;
67 
68   unsigned Mode = sys::fs::all_read | sys::fs::all_write;
69   // If requested, make the output file executable.
70   if (Flags & F_executable)
71     Mode |= sys::fs::all_exe;
72 
73   // Create new file in same directory but with random name.
74   SmallString<128> TempFilePath;
75   int FD;
76   EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
77                                  TempFilePath, Mode);
78   if (EC)
79     return EC;
80 
81   sys::RemoveFileOnSignal(TempFilePath);
82 
83 #ifndef LLVM_ON_WIN32
84   // On Windows, CreateFileMapping (the mmap function on Windows)
85   // automatically extends the underlying file. We don't need to
86   // extend the file beforehand. _chsize (ftruncate on Windows) is
87   // pretty slow just like it writes specified amount of bytes,
88   // so we should avoid calling that.
89   EC = sys::fs::resize_file(FD, Size);
90   if (EC)
91     return EC;
92 #endif
93 
94   auto MappedFile = llvm::make_unique<mapped_file_region>(
95       FD, mapped_file_region::readwrite, Size, 0, EC);
96   int Ret = close(FD);
97   if (EC)
98     return EC;
99   if (Ret)
100     return std::error_code(errno, std::generic_category());
101 
102   std::unique_ptr<FileOutputBuffer> Buf(
103       new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
104   return std::move(Buf);
105 }
106 
107 std::error_code FileOutputBuffer::commit() {
108   // Unmap buffer, letting OS flush dirty pages to file on disk.
109   Region.reset();
110 
111 
112   // Rename file to final name.
113   std::error_code EC = sys::fs::rename(Twine(TempPath), Twine(FinalPath));
114   sys::DontRemoveFileOnSignal(TempPath);
115   return EC;
116 }
117 } // namespace
118