blob: 2e740ca04518dd4bb57e9cb17077b324a7316335 [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"
Nick Kledzik5fce8c42012-08-01 02:29:50 +000017#include "llvm/Support/raw_ostream.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000018#include <system_error>
Nick Kledzik5fce8c42012-08-01 02:29:50 +000019
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000020using llvm::sys::fs::mapped_file_region;
Nick Kledzik5fce8c42012-08-01 02:29:50 +000021
22namespace llvm {
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000023FileOutputBuffer::FileOutputBuffer(mapped_file_region * R,
24 StringRef Path, StringRef TmpPath)
25 : Region(R)
26 , FinalPath(Path)
27 , TempPath(TmpPath) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000028}
29
Nick Kledzik5fce8c42012-08-01 02:29:50 +000030FileOutputBuffer::~FileOutputBuffer() {
Rafael Espindola81e7fd02014-01-10 21:40:29 +000031 sys::fs::remove(Twine(TempPath));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000032}
33
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000034std::error_code
35FileOutputBuffer::create(StringRef FilePath, size_t Size,
36 std::unique_ptr<FileOutputBuffer> &Result,
37 unsigned Flags) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000038 // If file already exists, it must be a regular file (to be mappable).
39 sys::fs::file_status Stat;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000040 std::error_code EC = sys::fs::status(FilePath, Stat);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000041 switch (Stat.type()) {
42 case sys::fs::file_type::file_not_found:
43 // If file does not exist, we'll create one.
44 break;
45 case sys::fs::file_type::regular_file: {
46 // If file is not currently writable, error out.
47 // FIXME: There is no sys::fs:: api for checking this.
48 // FIXME: In posix, you use the access() call to check this.
49 }
50 break;
51 default:
52 if (EC)
53 return EC;
54 else
Rafael Espindola2a826e42014-06-13 17:20:48 +000055 return make_error_code(errc::operation_not_permitted);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000056 }
57
58 // Delete target file.
Rafael Espindola81e7fd02014-01-10 21:40:29 +000059 EC = sys::fs::remove(FilePath);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000060 if (EC)
61 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000062
Rafael Espindola9a780152013-07-08 15:22:09 +000063 unsigned Mode = sys::fs::all_read | sys::fs::all_write;
64 // If requested, make the output file executable.
65 if (Flags & F_executable)
66 Mode |= sys::fs::all_exe;
67
Nick Kledzik5fce8c42012-08-01 02:29:50 +000068 // Create new file in same directory but with random name.
69 SmallString<128> TempFilePath;
70 int FD;
Rafael Espindola9a780152013-07-08 15:22:09 +000071 EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
72 TempFilePath, Mode);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000073 if (EC)
74 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000075
Ahmed Charlesfba06642014-03-05 10:27:34 +000076 std::unique_ptr<mapped_file_region> MappedFile(new mapped_file_region(
Michael J. Spencer42ad29f2013-03-14 00:20:10 +000077 FD, true, mapped_file_region::readwrite, Size, 0, EC));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000078 if (EC)
79 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000080
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000081 Result.reset(new FileOutputBuffer(MappedFile.get(), FilePath, TempFilePath));
82 if (Result)
Ahmed Charlesfba06642014-03-05 10:27:34 +000083 MappedFile.release();
Nick Kledzik5fce8c42012-08-01 02:29:50 +000084
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000085 return std::error_code();
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000086}
Nick Kledzik5fce8c42012-08-01 02:29:50 +000087
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000088std::error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000089 // Unmap buffer, letting OS flush dirty pages to file on disk.
Craig Topperc10719f2014-04-07 04:17:22 +000090 Region.reset(nullptr);
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000091
Nick Kledzik5fce8c42012-08-01 02:29:50 +000092 // If requested, resize file as part of commit.
93 if ( NewSmallerSize != -1 ) {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000094 std::error_code EC = sys::fs::resize_file(Twine(TempPath), NewSmallerSize);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000095 if (EC)
96 return EC;
97 }
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000098
Nick Kledzik5fce8c42012-08-01 02:29:50 +000099 // Rename file to final name.
100 return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
101}
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000102} // namespace