FileUtilities.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines a family of utility functions which are useful for doing
  10. // various things with files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_FILEUTILITIES_H
  14. #define LLVM_SUPPORT_FILEUTILITIES_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Support/Errc.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Support/Path.h"
  20. namespace llvm {
  21. /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
  22. /// the files match, 1 if they are different, and 2 if there is a file error.
  23. /// This function allows you to specify an absolute and relative FP error that
  24. /// is allowed to exist. If you specify a string to fill in for the error
  25. /// option, it will set the string to an error message if an error occurs, or
  26. /// if the files are different.
  27. ///
  28. int DiffFilesWithTolerance(StringRef FileA,
  29. StringRef FileB,
  30. double AbsTol, double RelTol,
  31. std::string *Error = nullptr);
  32. /// FileRemover - This class is a simple object meant to be stack allocated.
  33. /// If an exception is thrown from a region, the object removes the filename
  34. /// specified (if deleteIt is true).
  35. ///
  36. class FileRemover {
  37. SmallString<128> Filename;
  38. bool DeleteIt;
  39. public:
  40. FileRemover() : DeleteIt(false) {}
  41. explicit FileRemover(const Twine& filename, bool deleteIt = true)
  42. : DeleteIt(deleteIt) {
  43. filename.toVector(Filename);
  44. }
  45. ~FileRemover() {
  46. if (DeleteIt) {
  47. // Ignore problems deleting the file.
  48. sys::fs::remove(Filename);
  49. }
  50. }
  51. /// setFile - Give ownership of the file to the FileRemover so it will
  52. /// be removed when the object is destroyed. If the FileRemover already
  53. /// had ownership of a file, remove it first.
  54. void setFile(const Twine& filename, bool deleteIt = true) {
  55. if (DeleteIt) {
  56. // Ignore problems deleting the file.
  57. sys::fs::remove(Filename);
  58. }
  59. Filename.clear();
  60. filename.toVector(Filename);
  61. DeleteIt = deleteIt;
  62. }
  63. /// releaseFile - Take ownership of the file away from the FileRemover so it
  64. /// will not be removed when the object is destroyed.
  65. void releaseFile() { DeleteIt = false; }
  66. };
  67. enum class atomic_write_error {
  68. failed_to_create_uniq_file = 0,
  69. output_stream_error,
  70. failed_to_rename_temp_file
  71. };
  72. class AtomicFileWriteError : public llvm::ErrorInfo<AtomicFileWriteError> {
  73. public:
  74. AtomicFileWriteError(atomic_write_error Error) : Error(Error) {}
  75. void log(raw_ostream &OS) const override;
  76. const atomic_write_error Error;
  77. static char ID;
  78. private:
  79. // Users are not expected to use error_code.
  80. std::error_code convertToErrorCode() const override {
  81. return llvm::inconvertibleErrorCode();
  82. }
  83. };
  84. // atomic_write_error + whatever the Writer can return
  85. /// Creates a unique file with name according to the given \p TempPathModel,
  86. /// writes content of \p Buffer to the file and renames it to \p FinalPath.
  87. ///
  88. /// \returns \c AtomicFileWriteError in case of error.
  89. llvm::Error writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
  90. StringRef Buffer);
  91. llvm::Error
  92. writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
  93. std::function<llvm::Error(llvm::raw_ostream &)> Writer);
  94. } // End llvm namespace
  95. #endif