blob: 8a7b0d73f94367ece8350375896a97d5608cf8e3 [file] [log] [blame]
Nick Kledzik5fce8c42012-08-01 02:29:50 +00001//===- 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"
Nick Kledzik5fce8c42012-08-01 02:29:50 +000015#include "llvm/ADT/SmallVector.h"
Nick Kledzik5fce8c42012-08-01 02:29:50 +000016#include "llvm/Support/raw_ostream.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000017#include <system_error>
Nick Kledzik5fce8c42012-08-01 02:29:50 +000018
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000019using llvm::sys::fs::mapped_file_region;
Nick Kledzik5fce8c42012-08-01 02:29:50 +000020
21namespace llvm {
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000022FileOutputBuffer::FileOutputBuffer(mapped_file_region * R,
23 StringRef Path, StringRef TmpPath)
24 : Region(R)
25 , FinalPath(Path)
26 , TempPath(TmpPath) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000027}
28
Nick Kledzik5fce8c42012-08-01 02:29:50 +000029FileOutputBuffer::~FileOutputBuffer() {
Rafael Espindola81e7fd02014-01-10 21:40:29 +000030 sys::fs::remove(Twine(TempPath));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000031}
32
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000033std::error_code
34FileOutputBuffer::create(StringRef FilePath, size_t Size,
35 std::unique_ptr<FileOutputBuffer> &Result,
36 unsigned Flags) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000037 // If file already exists, it must be a regular file (to be mappable).
38 sys::fs::file_status Stat;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000039 std::error_code EC = sys::fs::status(FilePath, Stat);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000040 switch (Stat.type()) {
41 case sys::fs::file_type::file_not_found:
42 // If file does not exist, we'll create one.
43 break;
44 case sys::fs::file_type::regular_file: {
45 // If file is not currently writable, error out.
46 // FIXME: There is no sys::fs:: api for checking this.
47 // FIXME: In posix, you use the access() call to check this.
48 }
49 break;
50 default:
51 if (EC)
52 return EC;
53 else
Rafael Espindolaed6882b2014-06-12 11:58:49 +000054 return std::make_error_code(std::errc::operation_not_permitted);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000055 }
56
57 // Delete target file.
Rafael Espindola81e7fd02014-01-10 21:40:29 +000058 EC = sys::fs::remove(FilePath);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000059 if (EC)
60 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000061
Rafael Espindola9a780152013-07-08 15:22:09 +000062 unsigned Mode = sys::fs::all_read | sys::fs::all_write;
63 // If requested, make the output file executable.
64 if (Flags & F_executable)
65 Mode |= sys::fs::all_exe;
66
Nick Kledzik5fce8c42012-08-01 02:29:50 +000067 // Create new file in same directory but with random name.
68 SmallString<128> TempFilePath;
69 int FD;
Rafael Espindola9a780152013-07-08 15:22:09 +000070 EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
71 TempFilePath, Mode);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000072 if (EC)
73 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000074
Ahmed Charlesfba06642014-03-05 10:27:34 +000075 std::unique_ptr<mapped_file_region> MappedFile(new mapped_file_region(
Michael J. Spencer42ad29f2013-03-14 00:20:10 +000076 FD, true, mapped_file_region::readwrite, Size, 0, EC));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000077 if (EC)
78 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000079
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000080 Result.reset(new FileOutputBuffer(MappedFile.get(), FilePath, TempFilePath));
81 if (Result)
Ahmed Charlesfba06642014-03-05 10:27:34 +000082 MappedFile.release();
Nick Kledzik5fce8c42012-08-01 02:29:50 +000083
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000084 return std::error_code();
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000085}
Nick Kledzik5fce8c42012-08-01 02:29:50 +000086
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000087std::error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000088 // Unmap buffer, letting OS flush dirty pages to file on disk.
Craig Topperc10719f2014-04-07 04:17:22 +000089 Region.reset(nullptr);
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000090
Nick Kledzik5fce8c42012-08-01 02:29:50 +000091 // If requested, resize file as part of commit.
92 if ( NewSmallerSize != -1 ) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000093 std::error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000094 if (EC)
95 return EC;
96 }
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000097
Nick Kledzik5fce8c42012-08-01 02:29:50 +000098 // Rename file to final name.
99 return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
100}
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000101} // namespace