xref: /llvm-project/llvm/lib/Support/FileOutputBuffer.cpp (revision f6490e047a853bb38d435b8039bbeb4b41e863d3)
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/Memory.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/Support/Signals.h"
21 #include <system_error>
22 
23 #if !defined(_MSC_VER) && !defined(__MINGW32__)
24 #include <unistd.h>
25 #else
26 #include <io.h>
27 #endif
28 
29 using namespace llvm;
30 using namespace llvm::sys;
31 
32 // A FileOutputBuffer which creates a temporary file in the same directory
33 // as the final output file. The final output file is atomically replaced
34 // with the temporary file on commit().
35 class OnDiskBuffer : public FileOutputBuffer {
36 public:
37   OnDiskBuffer(StringRef Path, StringRef TempPath,
38                std::unique_ptr<fs::mapped_file_region> Buf)
39       : FileOutputBuffer(Path), Buffer(std::move(Buf)), TempPath(TempPath) {}
40 
41   uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
42 
43   uint8_t *getBufferEnd() const override {
44     return (uint8_t *)Buffer->data() + Buffer->size();
45   }
46 
47   size_t getBufferSize() const override { return Buffer->size(); }
48 
49   Error commit() override {
50     // Unmap buffer, letting OS flush dirty pages to file on disk.
51     Buffer.reset();
52 
53     // Atomically replace the existing file with the new one.
54     auto EC = fs::rename(TempPath, FinalPath);
55     sys::DontRemoveFileOnSignal(TempPath);
56     return errorCodeToError(EC);
57   }
58 
59   ~OnDiskBuffer() override {
60     // Close the mapping before deleting the temp file, so that the removal
61     // succeeds.
62     Buffer.reset();
63     fs::remove(TempPath);
64   }
65 
66 private:
67   std::unique_ptr<fs::mapped_file_region> Buffer;
68   std::string TempPath;
69 };
70 
71 // A FileOutputBuffer which keeps data in memory and writes to the final
72 // output file on commit(). This is used only when we cannot use OnDiskBuffer.
73 class InMemoryBuffer : public FileOutputBuffer {
74 public:
75   InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
76       : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
77 
78   uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
79 
80   uint8_t *getBufferEnd() const override {
81     return (uint8_t *)Buffer.base() + Buffer.size();
82   }
83 
84   size_t getBufferSize() const override { return Buffer.size(); }
85 
86   Error commit() override {
87     int FD;
88     std::error_code EC;
89     if (auto EC = openFileForWrite(FinalPath, FD, fs::F_None, Mode))
90       return errorCodeToError(EC);
91     raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true);
92     OS << StringRef((const char *)Buffer.base(), Buffer.size());
93     return Error::success();
94   }
95 
96 private:
97   OwningMemoryBlock Buffer;
98   unsigned Mode;
99 };
100 
101 static Expected<std::unique_ptr<InMemoryBuffer>>
102 createInMemoryBuffer(StringRef Path, size_t Size, unsigned Mode) {
103   std::error_code EC;
104   MemoryBlock MB = Memory::allocateMappedMemory(
105       Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
106   if (EC)
107     return errorCodeToError(EC);
108   return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
109 }
110 
111 static Expected<std::unique_ptr<OnDiskBuffer>>
112 createOnDiskBuffer(StringRef Path, size_t Size, unsigned Mode) {
113   // Create new file in same directory but with random name.
114   SmallString<128> TempPath;
115   int FD;
116   if (auto EC = fs::createUniqueFile(Path + ".tmp%%%%%%%", FD, TempPath, Mode))
117     return errorCodeToError(EC);
118 
119   sys::RemoveFileOnSignal(TempPath);
120 
121 #ifndef LLVM_ON_WIN32
122   // On Windows, CreateFileMapping (the mmap function on Windows)
123   // automatically extends the underlying file. We don't need to
124   // extend the file beforehand. _chsize (ftruncate on Windows) is
125   // pretty slow just like it writes specified amount of bytes,
126   // so we should avoid calling that function.
127   if (auto EC = fs::resize_file(FD, Size))
128     return errorCodeToError(EC);
129 #endif
130 
131   // Mmap it.
132   std::error_code EC;
133   auto MappedFile = llvm::make_unique<fs::mapped_file_region>(
134       FD, fs::mapped_file_region::readwrite, Size, 0, EC);
135   close(FD);
136   if (EC)
137     return errorCodeToError(EC);
138   return llvm::make_unique<OnDiskBuffer>(Path, TempPath, std::move(MappedFile));
139 }
140 
141 // Create an instance of FileOutputBuffer.
142 Expected<std::unique_ptr<FileOutputBuffer>>
143 FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
144   unsigned Mode = fs::all_read | fs::all_write;
145   if (Flags & F_executable)
146     Mode |= fs::all_exe;
147 
148   fs::file_status Stat;
149   fs::status(Path, Stat);
150 
151   // Usually, we want to create OnDiskBuffer to create a temporary file in
152   // the same directory as the destination file and atomically replaces it
153   // by rename(2).
154   //
155   // However, if the destination file is a special file, we don't want to
156   // use rename (e.g. we don't want to replace /dev/null with a regular
157   // file.) If that's the case, we create an in-memory buffer, open the
158   // destination file and write to it on commit().
159   switch (Stat.type()) {
160   case fs::file_type::directory_file:
161     return errorCodeToError(errc::is_a_directory);
162   case fs::file_type::regular_file:
163   case fs::file_type::file_not_found:
164   case fs::file_type::status_error:
165     return createOnDiskBuffer(Path, Size, Mode);
166   default:
167     return createInMemoryBuffer(Path, Size, Mode);
168   }
169 }
170