blob: c62655d58d5fa16f564c710008eb4b1dc3bb3b90 [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
Rafael Espindola2a826e42014-06-13 17:20:48 +000014#include "llvm/Support/Errc.h"
Nick Kledzik5fce8c42012-08-01 02:29:50 +000015#include "llvm/Support/FileOutputBuffer.h"
Nick Kledzik5fce8c42012-08-01 02:29:50 +000016#include "llvm/ADT/SmallVector.h"
David Blaikief55e31a2014-09-02 17:49:23 +000017#include "llvm/ADT/STLExtras.h"
Nick Kledzik5fce8c42012-08-01 02:29:50 +000018#include "llvm/Support/raw_ostream.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000019#include <system_error>
Nick Kledzik5fce8c42012-08-01 02:29:50 +000020
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000021using llvm::sys::fs::mapped_file_region;
Nick Kledzik5fce8c42012-08-01 02:29:50 +000022
23namespace llvm {
David Blaikief55e31a2014-09-02 17:49:23 +000024FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R,
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000025 StringRef Path, StringRef TmpPath)
David Blaikief55e31a2014-09-02 17:49:23 +000026 : Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {}
Nick Kledzik5fce8c42012-08-01 02:29:50 +000027
Nick Kledzik5fce8c42012-08-01 02:29:50 +000028FileOutputBuffer::~FileOutputBuffer() {
Rafael Espindola81e7fd02014-01-10 21:40:29 +000029 sys::fs::remove(Twine(TempPath));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000030}
31
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000032std::error_code
33FileOutputBuffer::create(StringRef FilePath, size_t Size,
34 std::unique_ptr<FileOutputBuffer> &Result,
35 unsigned Flags) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000036 // If file already exists, it must be a regular file (to be mappable).
37 sys::fs::file_status Stat;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000038 std::error_code EC = sys::fs::status(FilePath, Stat);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000039 switch (Stat.type()) {
40 case sys::fs::file_type::file_not_found:
41 // If file does not exist, we'll create one.
42 break;
43 case sys::fs::file_type::regular_file: {
44 // If file is not currently writable, error out.
45 // FIXME: There is no sys::fs:: api for checking this.
46 // FIXME: In posix, you use the access() call to check this.
47 }
48 break;
49 default:
50 if (EC)
51 return EC;
52 else
Rafael Espindola2a826e42014-06-13 17:20:48 +000053 return make_error_code(errc::operation_not_permitted);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000054 }
55
56 // Delete target file.
Rafael Espindola81e7fd02014-01-10 21:40:29 +000057 EC = sys::fs::remove(FilePath);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000058 if (EC)
59 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000060
Rafael Espindola9a780152013-07-08 15:22:09 +000061 unsigned Mode = sys::fs::all_read | sys::fs::all_write;
62 // If requested, make the output file executable.
63 if (Flags & F_executable)
64 Mode |= sys::fs::all_exe;
65
Nick Kledzik5fce8c42012-08-01 02:29:50 +000066 // Create new file in same directory but with random name.
67 SmallString<128> TempFilePath;
68 int FD;
Rafael Espindola9a780152013-07-08 15:22:09 +000069 EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
70 TempFilePath, Mode);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000071 if (EC)
72 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000073
David Blaikief55e31a2014-09-02 17:49:23 +000074 auto MappedFile = llvm::make_unique<mapped_file_region>(
75 FD, true, mapped_file_region::readwrite, Size, 0, EC);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000076 if (EC)
77 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000078
David Blaikief55e31a2014-09-02 17:49:23 +000079 Result.reset(
80 new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000081
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000082 return std::error_code();
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000083}
Nick Kledzik5fce8c42012-08-01 02:29:50 +000084
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000085std::error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000086 // Unmap buffer, letting OS flush dirty pages to file on disk.
David Blaikieb61064e2014-07-19 01:05:11 +000087 Region.reset();
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000088
Nick Kledzik5fce8c42012-08-01 02:29:50 +000089 // If requested, resize file as part of commit.
90 if ( NewSmallerSize != -1 ) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000091 std::error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000092 if (EC)
93 return EC;
94 }
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000095
Nick Kledzik5fce8c42012-08-01 02:29:50 +000096 // Rename file to final name.
97 return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
98}
Nick Kledzik5fce8c42012-08-01 02:29:50 +000099} // namespace