15fce8c4fSNick Kledzik //===- FileOutputBuffer.cpp - File Output Buffer ----------------*- C++ -*-===//
25fce8c4fSNick Kledzik //
35fce8c4fSNick Kledzik //                     The LLVM Compiler Infrastructure
45fce8c4fSNick Kledzik //
55fce8c4fSNick Kledzik // This file is distributed under the University of Illinois Open Source
65fce8c4fSNick Kledzik // License. See LICENSE.TXT for details.
75fce8c4fSNick Kledzik //
85fce8c4fSNick Kledzik //===----------------------------------------------------------------------===//
95fce8c4fSNick Kledzik //
105fce8c4fSNick Kledzik // Utility for creating a in-memory buffer that will be written to a file.
115fce8c4fSNick Kledzik //
125fce8c4fSNick Kledzik //===----------------------------------------------------------------------===//
135fce8c4fSNick Kledzik 
14d9903888SChandler Carruth #include "llvm/Support/FileOutputBuffer.h"
1516132e6fSBenjamin Kramer #include "llvm/ADT/STLExtras.h"
1616132e6fSBenjamin Kramer #include "llvm/ADT/SmallString.h"
1716132e6fSBenjamin Kramer #include "llvm/Support/Errc.h"
18*a16fe65bSRui Ueyama #include "llvm/Support/Memory.h"
19d4b24edaSRafael Espindola #include "llvm/Support/Path.h"
207dbb5778SRafael Espindola #include "llvm/Support/Signals.h"
21a6e9c3e4SRafael Espindola #include <system_error>
225fce8c4fSNick Kledzik 
237eb1f185SRafael Espindola #if !defined(_MSC_VER) && !defined(__MINGW32__)
247eb1f185SRafael Espindola #include <unistd.h>
257eb1f185SRafael Espindola #else
267eb1f185SRafael Espindola #include <io.h>
277eb1f185SRafael Espindola #endif
287eb1f185SRafael Espindola 
29*a16fe65bSRui Ueyama using namespace llvm;
30*a16fe65bSRui Ueyama using namespace llvm::sys;
315fce8c4fSNick Kledzik 
32*a16fe65bSRui Ueyama // A FileOutputBuffer which creates a temporary file in the same directory
33*a16fe65bSRui Ueyama // as the final output file. The final output file is atomically replaced
34*a16fe65bSRui Ueyama // with the temporary file on commit().
35*a16fe65bSRui Ueyama class OnDiskBuffer : public FileOutputBuffer {
36*a16fe65bSRui Ueyama public:
37*a16fe65bSRui Ueyama   OnDiskBuffer(StringRef Path, StringRef TempPath,
38*a16fe65bSRui Ueyama                std::unique_ptr<fs::mapped_file_region> Buf)
39*a16fe65bSRui Ueyama       : FileOutputBuffer(Path), Buffer(std::move(Buf)), TempPath(TempPath) {}
405fce8c4fSNick Kledzik 
41*a16fe65bSRui Ueyama   static ErrorOr<std::unique_ptr<OnDiskBuffer>>
42*a16fe65bSRui Ueyama   create(StringRef Path, size_t Size, unsigned Mode);
43*a16fe65bSRui Ueyama 
44*a16fe65bSRui Ueyama   uint8_t *getBufferStart() const override { return (uint8_t *)Buffer->data(); }
45*a16fe65bSRui Ueyama 
46*a16fe65bSRui Ueyama   uint8_t *getBufferEnd() const override {
47*a16fe65bSRui Ueyama     return (uint8_t *)Buffer->data() + Buffer->size();
48*a16fe65bSRui Ueyama   }
49*a16fe65bSRui Ueyama 
50*a16fe65bSRui Ueyama   size_t getBufferSize() const override { return Buffer->size(); }
51*a16fe65bSRui Ueyama 
52*a16fe65bSRui Ueyama   std::error_code commit() override {
53*a16fe65bSRui Ueyama     // Unmap buffer, letting OS flush dirty pages to file on disk.
54*a16fe65bSRui Ueyama     Buffer.reset();
55*a16fe65bSRui Ueyama 
56*a16fe65bSRui Ueyama     // Atomically replace the existing file with the new one.
57*a16fe65bSRui Ueyama     auto EC = fs::rename(TempPath, FinalPath);
58*a16fe65bSRui Ueyama     sys::DontRemoveFileOnSignal(TempPath);
59*a16fe65bSRui Ueyama     return EC;
60*a16fe65bSRui Ueyama   }
61*a16fe65bSRui Ueyama 
62*a16fe65bSRui Ueyama   ~OnDiskBuffer() override {
631a4398a1SReid Kleckner     // Close the mapping before deleting the temp file, so that the removal
641a4398a1SReid Kleckner     // succeeds.
65*a16fe65bSRui Ueyama     Buffer.reset();
66*a16fe65bSRui Ueyama     fs::remove(TempPath);
675fce8c4fSNick Kledzik   }
685fce8c4fSNick Kledzik 
69*a16fe65bSRui Ueyama private:
70*a16fe65bSRui Ueyama   std::unique_ptr<fs::mapped_file_region> Buffer;
71*a16fe65bSRui Ueyama   std::string TempPath;
72*a16fe65bSRui Ueyama };
73*a16fe65bSRui Ueyama 
74*a16fe65bSRui Ueyama // A FileOutputBuffer which keeps data in memory and writes to the final
75*a16fe65bSRui Ueyama // output file on commit(). This is used only when we cannot use OnDiskBuffer.
76*a16fe65bSRui Ueyama class InMemoryBuffer : public FileOutputBuffer {
77*a16fe65bSRui Ueyama public:
78*a16fe65bSRui Ueyama   InMemoryBuffer(StringRef Path, MemoryBlock Buf, unsigned Mode)
79*a16fe65bSRui Ueyama       : FileOutputBuffer(Path), Buffer(Buf), Mode(Mode) {}
80*a16fe65bSRui Ueyama 
81*a16fe65bSRui Ueyama   static ErrorOr<std::unique_ptr<InMemoryBuffer>>
82*a16fe65bSRui Ueyama   create(StringRef Path, size_t Size, unsigned Mode) {
83*a16fe65bSRui Ueyama     std::error_code EC;
84*a16fe65bSRui Ueyama     MemoryBlock MB = Memory::allocateMappedMemory(
85*a16fe65bSRui Ueyama         Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
865fce8c4fSNick Kledzik     if (EC)
875fce8c4fSNick Kledzik       return EC;
88*a16fe65bSRui Ueyama     return llvm::make_unique<InMemoryBuffer>(Path, MB, Mode);
895fce8c4fSNick Kledzik   }
905fce8c4fSNick Kledzik 
91*a16fe65bSRui Ueyama   uint8_t *getBufferStart() const override { return (uint8_t *)Buffer.base(); }
92*a16fe65bSRui Ueyama 
93*a16fe65bSRui Ueyama   uint8_t *getBufferEnd() const override {
94*a16fe65bSRui Ueyama     return (uint8_t *)Buffer.base() + Buffer.size();
95*a16fe65bSRui Ueyama   }
96*a16fe65bSRui Ueyama 
97*a16fe65bSRui Ueyama   size_t getBufferSize() const override { return Buffer.size(); }
98*a16fe65bSRui Ueyama 
99*a16fe65bSRui Ueyama   std::error_code commit() override {
100d4b24edaSRafael Espindola     int FD;
101*a16fe65bSRui Ueyama     std::error_code EC;
102*a16fe65bSRui Ueyama     if (auto EC = openFileForWrite(FinalPath, FD, fs::F_None, Mode))
103*a16fe65bSRui Ueyama       return EC;
104*a16fe65bSRui Ueyama     raw_fd_ostream OS(FD, /*shouldClose=*/true, /*unbuffered=*/true);
105*a16fe65bSRui Ueyama     OS << StringRef((const char *)Buffer.base(), Buffer.size());
106*a16fe65bSRui Ueyama     return std::error_code();
107d4b24edaSRafael Espindola   }
108d4b24edaSRafael Espindola 
109*a16fe65bSRui Ueyama private:
110*a16fe65bSRui Ueyama   OwningMemoryBlock Buffer;
111*a16fe65bSRui Ueyama   unsigned Mode;
112*a16fe65bSRui Ueyama };
113*a16fe65bSRui Ueyama 
114*a16fe65bSRui Ueyama ErrorOr<std::unique_ptr<OnDiskBuffer>>
115*a16fe65bSRui Ueyama OnDiskBuffer::create(StringRef Path, size_t Size, unsigned Mode) {
116*a16fe65bSRui Ueyama   // Create new file in same directory but with random name.
117*a16fe65bSRui Ueyama   SmallString<128> TempPath;
118*a16fe65bSRui Ueyama   int FD;
119*a16fe65bSRui Ueyama   if (auto EC = fs::createUniqueFile(Path + ".tmp%%%%%%%", FD, TempPath, Mode))
1205fce8c4fSNick Kledzik     return EC;
1215fce8c4fSNick Kledzik 
122*a16fe65bSRui Ueyama   sys::RemoveFileOnSignal(TempPath);
1237dbb5778SRafael Espindola 
124da9bc2e5SRui Ueyama #ifndef LLVM_ON_WIN32
125da9bc2e5SRui Ueyama   // On Windows, CreateFileMapping (the mmap function on Windows)
126da9bc2e5SRui Ueyama   // automatically extends the underlying file. We don't need to
127da9bc2e5SRui Ueyama   // extend the file beforehand. _chsize (ftruncate on Windows) is
128da9bc2e5SRui Ueyama   // pretty slow just like it writes specified amount of bytes,
129*a16fe65bSRui Ueyama   // so we should avoid calling that function.
130*a16fe65bSRui Ueyama   if (auto EC = fs::resize_file(FD, Size))
131c69f13bfSRafael Espindola     return EC;
132da9bc2e5SRui Ueyama #endif
133c69f13bfSRafael Espindola 
134*a16fe65bSRui Ueyama   // Mmap it.
135*a16fe65bSRui Ueyama   std::error_code EC;
136*a16fe65bSRui Ueyama   auto MappedFile = llvm::make_unique<fs::mapped_file_region>(
137*a16fe65bSRui Ueyama       FD, fs::mapped_file_region::readwrite, Size, 0, EC);
138*a16fe65bSRui Ueyama   close(FD);
1395fce8c4fSNick Kledzik   if (EC)
1405fce8c4fSNick Kledzik     return EC;
141*a16fe65bSRui Ueyama   return llvm::make_unique<OnDiskBuffer>(Path, TempPath, std::move(MappedFile));
1425fce8c4fSNick Kledzik }
1435fce8c4fSNick Kledzik 
144*a16fe65bSRui Ueyama // Create an instance of FileOutputBuffer.
145*a16fe65bSRui Ueyama ErrorOr<std::unique_ptr<FileOutputBuffer>>
146*a16fe65bSRui Ueyama FileOutputBuffer::create(StringRef Path, size_t Size, unsigned Flags) {
147*a16fe65bSRui Ueyama   unsigned Mode = fs::all_read | fs::all_write;
148*a16fe65bSRui Ueyama   if (Flags & F_executable)
149*a16fe65bSRui Ueyama     Mode |= fs::all_exe;
1505fce8c4fSNick Kledzik 
151*a16fe65bSRui Ueyama   fs::file_status Stat;
152*a16fe65bSRui Ueyama   fs::status(Path, Stat);
153d4b24edaSRafael Espindola 
154*a16fe65bSRui Ueyama   // Usually, we want to create OnDiskBuffer to create a temporary file in
155*a16fe65bSRui Ueyama   // the same directory as the destination file and atomically replaces it
156*a16fe65bSRui Ueyama   // by rename(2).
157*a16fe65bSRui Ueyama   //
158*a16fe65bSRui Ueyama   // However, if the destination file is a special file, we don't want to
159*a16fe65bSRui Ueyama   // use rename (e.g. we don't want to replace /dev/null with a regular
160*a16fe65bSRui Ueyama   // file.) If that's the case, we create an in-memory buffer, open the
161*a16fe65bSRui Ueyama   // destination file and write to it on commit().
162*a16fe65bSRui Ueyama   switch (Stat.type()) {
163*a16fe65bSRui Ueyama   case fs::file_type::directory_file:
164*a16fe65bSRui Ueyama     return errc::is_a_directory;
165*a16fe65bSRui Ueyama   case fs::file_type::regular_file:
166*a16fe65bSRui Ueyama   case fs::file_type::file_not_found:
167*a16fe65bSRui Ueyama   case fs::file_type::status_error:
168*a16fe65bSRui Ueyama     return OnDiskBuffer::create(Path, Size, Mode);
169*a16fe65bSRui Ueyama   default:
170*a16fe65bSRui Ueyama     return InMemoryBuffer::create(Path, Size, Mode);
1715fce8c4fSNick Kledzik   }
172*a16fe65bSRui Ueyama }
173