blob: e6e4da346520cfb53e585fb7118d6bd58dbfd7b1 [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
Rafael Espindola7eb1f182014-12-11 20:12:55 +000021#if !defined(_MSC_VER) && !defined(__MINGW32__)
22#include <unistd.h>
23#else
24#include <io.h>
25#endif
26
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000027using llvm::sys::fs::mapped_file_region;
Nick Kledzik5fce8c42012-08-01 02:29:50 +000028
29namespace llvm {
David Blaikief55e31a2014-09-02 17:49:23 +000030FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R,
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000031 StringRef Path, StringRef TmpPath)
David Blaikief55e31a2014-09-02 17:49:23 +000032 : Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {}
Nick Kledzik5fce8c42012-08-01 02:29:50 +000033
Nick Kledzik5fce8c42012-08-01 02:29:50 +000034FileOutputBuffer::~FileOutputBuffer() {
Rafael Espindola81e7fd02014-01-10 21:40:29 +000035 sys::fs::remove(Twine(TempPath));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000036}
37
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000038std::error_code
39FileOutputBuffer::create(StringRef FilePath, size_t Size,
40 std::unique_ptr<FileOutputBuffer> &Result,
41 unsigned Flags) {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000042 // If file already exists, it must be a regular file (to be mappable).
43 sys::fs::file_status Stat;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000044 std::error_code EC = sys::fs::status(FilePath, Stat);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000045 switch (Stat.type()) {
46 case sys::fs::file_type::file_not_found:
47 // If file does not exist, we'll create one.
48 break;
49 case sys::fs::file_type::regular_file: {
50 // If file is not currently writable, error out.
51 // FIXME: There is no sys::fs:: api for checking this.
52 // FIXME: In posix, you use the access() call to check this.
53 }
54 break;
55 default:
56 if (EC)
57 return EC;
58 else
Rafael Espindola2a826e42014-06-13 17:20:48 +000059 return make_error_code(errc::operation_not_permitted);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000060 }
61
62 // Delete target file.
Rafael Espindola81e7fd02014-01-10 21:40:29 +000063 EC = sys::fs::remove(FilePath);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000064 if (EC)
65 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000066
Rafael Espindola9a780152013-07-08 15:22:09 +000067 unsigned Mode = sys::fs::all_read | sys::fs::all_write;
68 // If requested, make the output file executable.
69 if (Flags & F_executable)
70 Mode |= sys::fs::all_exe;
71
Nick Kledzik5fce8c42012-08-01 02:29:50 +000072 // Create new file in same directory but with random name.
73 SmallString<128> TempFilePath;
74 int FD;
Rafael Espindola9a780152013-07-08 15:22:09 +000075 EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
76 TempFilePath, Mode);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000077 if (EC)
78 return EC;
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000079
David Blaikief55e31a2014-09-02 17:49:23 +000080 auto MappedFile = llvm::make_unique<mapped_file_region>(
Rafael Espindola7eb1f182014-12-11 20:12:55 +000081 FD, mapped_file_region::readwrite, Size, 0, EC);
82 int Ret = close(FD);
Nick Kledzik5fce8c42012-08-01 02:29:50 +000083 if (EC)
84 return EC;
Rafael Espindola7eb1f182014-12-11 20:12:55 +000085 if (Ret)
86 return std::error_code(errno, std::generic_category());
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000087
David Blaikief55e31a2014-09-02 17:49:23 +000088 Result.reset(
89 new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
Nick Kledzik5fce8c42012-08-01 02:29:50 +000090
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000091 return std::error_code();
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000092}
Nick Kledzik5fce8c42012-08-01 02:29:50 +000093
Rafael Espindola5753cf32014-12-12 17:35:34 +000094std::error_code FileOutputBuffer::commit() {
Nick Kledzik5fce8c42012-08-01 02:29:50 +000095 // Unmap buffer, letting OS flush dirty pages to file on disk.
David Blaikieb61064e2014-07-19 01:05:11 +000096 Region.reset();
Michael J. Spencer7fe24f52012-12-03 22:09:52 +000097
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