]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
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 | ||
1a4d82fc | 14 | #include "llvm/Support/Errc.h" |
1a4d82fc | 15 | #include "llvm/ADT/STLExtras.h" |
85aaf69f SL |
16 | #include "llvm/ADT/SmallVector.h" |
17 | #include "llvm/Support/FileOutputBuffer.h" | |
223e47cc | 18 | #include "llvm/Support/raw_ostream.h" |
1a4d82fc | 19 | #include <system_error> |
223e47cc | 20 | |
85aaf69f SL |
21 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
22 | #include <unistd.h> | |
23 | #else | |
24 | #include <io.h> | |
25 | #endif | |
26 | ||
970d7e83 | 27 | using llvm::sys::fs::mapped_file_region; |
223e47cc LB |
28 | |
29 | namespace llvm { | |
1a4d82fc | 30 | FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R, |
970d7e83 | 31 | StringRef Path, StringRef TmpPath) |
1a4d82fc | 32 | : Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {} |
223e47cc | 33 | |
223e47cc | 34 | FileOutputBuffer::~FileOutputBuffer() { |
1a4d82fc | 35 | sys::fs::remove(Twine(TempPath)); |
223e47cc LB |
36 | } |
37 | ||
1a4d82fc JJ |
38 | std::error_code |
39 | FileOutputBuffer::create(StringRef FilePath, size_t Size, | |
40 | std::unique_ptr<FileOutputBuffer> &Result, | |
41 | unsigned Flags) { | |
223e47cc LB |
42 | // If file already exists, it must be a regular file (to be mappable). |
43 | sys::fs::file_status Stat; | |
1a4d82fc | 44 | std::error_code EC = sys::fs::status(FilePath, Stat); |
223e47cc LB |
45 | switch (Stat.type()) { |
46 | case sys::fs::file_type::file_not_found: | |
47 | // If file does not exist, we'll create one. | |
48 | break; | |
49 | case sys::fs::file_type::regular_file: { | |
50 | // If file is not currently writable, error out. | |
51 | // FIXME: There is no sys::fs:: api for checking this. | |
52 | // FIXME: In posix, you use the access() call to check this. | |
53 | } | |
54 | break; | |
55 | default: | |
56 | if (EC) | |
57 | return EC; | |
58 | else | |
59 | return make_error_code(errc::operation_not_permitted); | |
60 | } | |
61 | ||
62 | // Delete target file. | |
1a4d82fc | 63 | EC = sys::fs::remove(FilePath); |
223e47cc LB |
64 | if (EC) |
65 | return EC; | |
970d7e83 | 66 | |
1a4d82fc JJ |
67 | unsigned Mode = sys::fs::all_read | sys::fs::all_write; |
68 | // If requested, make the output file executable. | |
69 | if (Flags & F_executable) | |
70 | Mode |= sys::fs::all_exe; | |
71 | ||
223e47cc LB |
72 | // Create new file in same directory but with random name. |
73 | SmallString<128> TempFilePath; | |
74 | int FD; | |
1a4d82fc JJ |
75 | EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD, |
76 | TempFilePath, Mode); | |
223e47cc LB |
77 | if (EC) |
78 | return EC; | |
970d7e83 | 79 | |
85aaf69f SL |
80 | EC = sys::fs::resize_file(FD, Size); |
81 | if (EC) | |
82 | return EC; | |
83 | ||
1a4d82fc | 84 | auto MappedFile = llvm::make_unique<mapped_file_region>( |
85aaf69f SL |
85 | FD, mapped_file_region::readwrite, Size, 0, EC); |
86 | int Ret = close(FD); | |
223e47cc LB |
87 | if (EC) |
88 | return EC; | |
85aaf69f SL |
89 | if (Ret) |
90 | return std::error_code(errno, std::generic_category()); | |
970d7e83 | 91 | |
1a4d82fc JJ |
92 | Result.reset( |
93 | new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath)); | |
223e47cc | 94 | |
1a4d82fc | 95 | return std::error_code(); |
970d7e83 | 96 | } |
223e47cc | 97 | |
85aaf69f | 98 | std::error_code FileOutputBuffer::commit() { |
223e47cc | 99 | // Unmap buffer, letting OS flush dirty pages to file on disk. |
1a4d82fc | 100 | Region.reset(); |
970d7e83 | 101 | |
970d7e83 | 102 | |
223e47cc LB |
103 | // Rename file to final name. |
104 | return sys::fs::rename(Twine(TempPath), Twine(FinalPath)); | |
105 | } | |
223e47cc | 106 | } // namespace |