blob: e68ecaaa98c915648751a40d1b332fee0a904922 [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
Chandler Carruthd9903882015-01-14 11:23:27 +000014#include "llvm/Support/FileOutputBuffer.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/Support/Errc.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000018#include <system_error>
Nick Kledzik5fce8c42012-08-01 02:29:50 +000019
Rafael Espindola7eb1f182014-12-11 20:12:55 +000020#if !defined(_MSC_VER) && !defined(__MINGW32__)
21#include <unistd.h>
22#else
23#include <io.h>
24#endif
25
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000026using llvm::sys::fs::mapped_file_region;
Nick Kledzik5fce8c42012-08-01 02:29:50 +000027
28namespace llvm {
David Blaikief55e31a2014-09-02 17:49:23 +000029FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R,
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000030 StringRef Path, StringRef TmpPath)
David Blaikief55e31a2014-09-02 17:49:23 +000031 : Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {}
Nick Kledzik5fce8c42012-08-01 02:29:50 +000032
Nick Kledzik5fce8c42012-08-01 02:29:50 +000033FileOutputBuffer::~FileOutputBuffer() {
Rafael Espindola81e7fd02014-01-10 21:40:29 +000034 sys::fs::remove(Twine(TempPath));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000035}
36
Rafael Espindola169284a2015-08-13 00:31:39 +000037ErrorOr<std::unique_ptr<FileOutputBuffer>>
38FileOutputBuffer::create(StringRef FilePath, size_t Size, unsigned Flags) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000039 // If file already exists, it must be a regular file (to be mappable).
40 sys::fs::file_status Stat;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000041 std::error_code EC = sys::fs::status(FilePath, Stat);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000042 switch (Stat.type()) {
43 case sys::fs::file_type::file_not_found:
44 // If file does not exist, we'll create one.
45 break;
46 case sys::fs::file_type::regular_file: {
47 // If file is not currently writable, error out.
48 // FIXME: There is no sys::fs:: api for checking this.
49 // FIXME: In posix, you use the access() call to check this.
50 }
51 break;
52 default:
53 if (EC)
54 return EC;
55 else
Rafael Espindola2a826e42014-06-13 17:20:48 +000056 return make_error_code(errc::operation_not_permitted);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000057 }
58
59 // Delete target file.
Rafael Espindola81e7fd02014-01-10 21:40:29 +000060 EC = sys::fs::remove(FilePath);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000061 if (EC)
62 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000063
Rafael Espindola9a780152013-07-08 15:22:09 +000064 unsigned Mode = sys::fs::all_read | sys::fs::all_write;
65 // If requested, make the output file executable.
66 if (Flags & F_executable)
67 Mode |= sys::fs::all_exe;
68
Nick Kledzik5fce8c42012-08-01 02:29:50 +000069 // Create new file in same directory but with random name.
70 SmallString<128> TempFilePath;
71 int FD;
Rafael Espindola9a780152013-07-08 15:22:09 +000072 EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
73 TempFilePath, Mode);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000074 if (EC)
75 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000076
Rui Ueyamada9bc2e2015-03-06 06:07:32 +000077#ifndef LLVM_ON_WIN32
78 // On Windows, CreateFileMapping (the mmap function on Windows)
79 // automatically extends the underlying file. We don't need to
80 // extend the file beforehand. _chsize (ftruncate on Windows) is
81 // pretty slow just like it writes specified amount of bytes,
82 // so we should avoid calling that.
Rafael Espindolac69f13b2014-12-12 18:13:23 +000083 EC = sys::fs::resize_file(FD, Size);
84 if (EC)
85 return EC;
Rui Ueyamada9bc2e2015-03-06 06:07:32 +000086#endif
Rafael Espindolac69f13b2014-12-12 18:13:23 +000087
David Blaikief55e31a2014-09-02 17:49:23 +000088 auto MappedFile = llvm::make_unique<mapped_file_region>(
Rafael Espindola7eb1f182014-12-11 20:12:55 +000089 FD, mapped_file_region::readwrite, Size, 0, EC);
90 int Ret = close(FD);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000091 if (EC)
92 return EC;
Rafael Espindola7eb1f182014-12-11 20:12:55 +000093 if (Ret)
94 return std::error_code(errno, std::generic_category());
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000095
Rafael Espindola169284a2015-08-13 00:31:39 +000096 std::unique_ptr<FileOutputBuffer> Buf(
David Blaikief55e31a2014-09-02 17:49:23 +000097 new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
Rafael Espindola169284a2015-08-13 00:31:39 +000098 return std::move(Buf);
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000099}
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000100
Rafael Espindola5753cf32014-12-12 17:35:34 +0000101std::error_code FileOutputBuffer::commit() {
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000102 // Unmap buffer, letting OS flush dirty pages to file on disk.
David Blaikieb61064e2014-07-19 01:05:11 +0000103 Region.reset();
Michael J. Spencer7fe24f52012-12-03 22:09:52 +0000104
Michael J. Spencer7fe24f52012-12-03 22:09:52 +0000105
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000106 // Rename file to final name.
107 return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
108}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000109} // namespace