2
0

FileOutputBuffer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===- 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. #include "llvm/Support/FileOutputBuffer.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/Support/Errc.h"
  17. #include <system_error>
  18. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  19. #include <unistd.h>
  20. #else
  21. #include <io.h>
  22. #endif
  23. using llvm::sys::fs::mapped_file_region;
  24. namespace llvm {
  25. FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R,
  26. StringRef Path, StringRef TmpPath)
  27. : Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {}
  28. FileOutputBuffer::~FileOutputBuffer() {
  29. sys::fs::remove(Twine(TempPath));
  30. }
  31. std::error_code
  32. FileOutputBuffer::create(StringRef FilePath, size_t Size,
  33. std::unique_ptr<FileOutputBuffer> &Result,
  34. unsigned Flags) {
  35. // If file already exists, it must be a regular file (to be mappable).
  36. sys::fs::file_status Stat;
  37. std::error_code EC = sys::fs::status(FilePath, Stat);
  38. switch (Stat.type()) {
  39. case sys::fs::file_type::file_not_found:
  40. // If file does not exist, we'll create one.
  41. break;
  42. case sys::fs::file_type::regular_file: {
  43. // If file is not currently writable, error out.
  44. // FIXME: There is no sys::fs:: api for checking this.
  45. // FIXME: In posix, you use the access() call to check this.
  46. }
  47. break;
  48. default:
  49. if (EC)
  50. return EC;
  51. else
  52. return make_error_code(errc::operation_not_permitted);
  53. }
  54. // Delete target file.
  55. EC = sys::fs::remove(FilePath);
  56. if (EC)
  57. return EC;
  58. unsigned Mode = sys::fs::all_read | sys::fs::all_write;
  59. // If requested, make the output file executable.
  60. if (Flags & F_executable)
  61. Mode |= sys::fs::all_exe;
  62. // Create new file in same directory but with random name.
  63. SmallString<128> TempFilePath;
  64. int FD;
  65. EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%", FD,
  66. TempFilePath, Mode);
  67. if (EC)
  68. return EC;
  69. #ifndef LLVM_ON_WIN32
  70. // On Windows, CreateFileMapping (the mmap function on Windows)
  71. // automatically extends the underlying file. We don't need to
  72. // extend the file beforehand. _chsize (ftruncate on Windows) is
  73. // pretty slow just like it writes specified amount of bytes,
  74. // so we should avoid calling that.
  75. EC = sys::fs::resize_file(FD, Size);
  76. if (EC)
  77. return EC;
  78. #endif
  79. auto MappedFile = llvm::make_unique<mapped_file_region>(
  80. FD, mapped_file_region::readwrite, Size, 0, EC);
  81. int Ret = close(FD);
  82. if (EC)
  83. return EC;
  84. if (Ret)
  85. return std::error_code(errno, std::generic_category());
  86. Result.reset(
  87. new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
  88. return std::error_code();
  89. }
  90. std::error_code FileOutputBuffer::commit() {
  91. // Unmap buffer, letting OS flush dirty pages to file on disk.
  92. Region.reset();
  93. // Rename file to final name.
  94. return sys::fs::rename(Twine(TempPath), Twine(FinalPath));
  95. }
  96. } // namespace