blob: 307ff09afedc1a350e18dce03dec155902e26c02 [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 Espindoladb4ed0b2014-06-13 02:24:39 +000037std::error_code
38FileOutputBuffer::create(StringRef FilePath, size_t Size,
39 std::unique_ptr<FileOutputBuffer> &Result,
40 unsigned Flags) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000041 // If file already exists, it must be a regular file (to be mappable).
42 sys::fs::file_status Stat;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000043 std::error_code EC = sys::fs::status(FilePath, Stat);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000044 switch (Stat.type()) {
45 case sys::fs::file_type::file_not_found:
46 // If file does not exist, we'll create one.
47 break;
48 case sys::fs::file_type::regular_file: {
49 // If file is not currently writable, error out.
50 // FIXME: There is no sys::fs:: api for checking this.
51 // FIXME: In posix, you use the access() call to check this.
52 }
53 break;
54 default:
55 if (EC)
56 return EC;
57 else
Rafael Espindola2a826e42014-06-13 17:20:48 +000058 return make_error_code(errc::operation_not_permitted);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000059 }
60
61 // Delete target file.
Rafael Espindola81e7fd02014-01-10 21:40:29 +000062 EC = sys::fs::remove(FilePath);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000063 if (EC)
64 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000065
Rafael Espindola9a780152013-07-08 15:22:09 +000066 unsigned Mode = sys::fs::all_read | sys::fs::all_write;
67 // If requested, make the output file executable.
68 if (Flags & F_executable)
69 Mode |= sys::fs::all_exe;
70
Nick Kledzik5fce8c42012-08-01 02:29:50 +000071 // Create new file in same directory but with random name.
72 SmallString<128> TempFilePath;
73 int FD;
Rafael Espindola9a780152013-07-08 15:22:09 +000074 EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
75 TempFilePath, Mode);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000076 if (EC)
77 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000078
Rui Ueyamada9bc2e2015-03-06 06:07:32 +000079#ifndef LLVM_ON_WIN32
80 // On Windows, CreateFileMapping (the mmap function on Windows)
81 // automatically extends the underlying file. We don't need to
82 // extend the file beforehand. _chsize (ftruncate on Windows) is
83 // pretty slow just like it writes specified amount of bytes,
84 // so we should avoid calling that.
Rafael Espindolac69f13b2014-12-12 18:13:23 +000085 EC = sys::fs::resize_file(FD, Size);
86 if (EC)
87 return EC;
Rui Ueyamada9bc2e2015-03-06 06:07:32 +000088#endif
Rafael Espindolac69f13b2014-12-12 18:13:23 +000089
David Blaikief55e31a2014-09-02 17:49:23 +000090 auto MappedFile = llvm::make_unique<mapped_file_region>(
Rafael Espindola7eb1f182014-12-11 20:12:55 +000091 FD, mapped_file_region::readwrite, Size, 0, EC);
92 int Ret = close(FD);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000093 if (EC)
94 return EC;
Rafael Espindola7eb1f182014-12-11 20:12:55 +000095 if (Ret)
96 return std::error_code(errno, std::generic_category());
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000097
David Blaikief55e31a2014-09-02 17:49:23 +000098 Result.reset(
99 new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000100
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000101 return std::error_code();
Michael J. Spencer7fe24f52012-12-03 22:09:52 +0000102}
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000103
Rafael Espindola5753cf32014-12-12 17:35:34 +0000104std::error_code FileOutputBuffer::commit() {
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000105 // Unmap buffer, letting OS flush dirty pages to file on disk.
David Blaikieb61064e2014-07-19 01:05:11 +0000106 Region.reset();
Michael J. Spencer7fe24f52012-12-03 22:09:52 +0000107
Michael J. Spencer7fe24f52012-12-03 22:09:52 +0000108
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000109 // Rename file to final name.
110 return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
111}
Nick Kledzik5fce8c42012-08-01 02:29:50 +0000112} // namespace