LLVM  16.0.0git
FileUtilities.h
Go to the documentation of this file.
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 
14 #ifndef LLVM_SUPPORT_FILEUTILITIES_H
15 #define LLVM_SUPPORT_FILEUTILITIES_H
16 
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Error.h"
20 
21 #include <system_error>
22 
23 namespace llvm {
24 
25  /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
26  /// the files match, 1 if they are different, and 2 if there is a file error.
27  /// This function allows you to specify an absolute and relative FP error that
28  /// is allowed to exist. If you specify a string to fill in for the error
29  /// option, it will set the string to an error message if an error occurs, or
30  /// if the files are different.
31  ///
32  int DiffFilesWithTolerance(StringRef FileA,
33  StringRef FileB,
34  double AbsTol, double RelTol,
35  std::string *Error = nullptr);
36 
37 
38  /// FileRemover - This class is a simple object meant to be stack allocated.
39  /// If an exception is thrown from a region, the object removes the filename
40  /// specified (if deleteIt is true).
41  ///
42  class FileRemover {
43  SmallString<128> Filename;
44  bool DeleteIt;
45  public:
46  FileRemover() : DeleteIt(false) {}
47 
48  explicit FileRemover(const Twine& filename, bool deleteIt = true)
49  : DeleteIt(deleteIt) {
50  filename.toVector(Filename);
51  }
52 
54  if (DeleteIt) {
55  // Ignore problems deleting the file.
56  sys::fs::remove(Filename);
57  }
58  }
59 
60  /// setFile - Give ownership of the file to the FileRemover so it will
61  /// be removed when the object is destroyed. If the FileRemover already
62  /// had ownership of a file, remove it first.
63  void setFile(const Twine& filename, bool deleteIt = true) {
64  if (DeleteIt) {
65  // Ignore problems deleting the file.
66  sys::fs::remove(Filename);
67  }
68 
69  Filename.clear();
70  filename.toVector(Filename);
71  DeleteIt = deleteIt;
72  }
73 
74  /// releaseFile - Take ownership of the file away from the FileRemover so it
75  /// will not be removed when the object is destroyed.
76  void releaseFile() { DeleteIt = false; }
77  };
78 
79  enum class atomic_write_error {
83  };
84 
85  class AtomicFileWriteError : public llvm::ErrorInfo<AtomicFileWriteError> {
86  public:
88 
89  void log(raw_ostream &OS) const override;
90 
92  static char ID;
93 
94  private:
95  // Users are not expected to use error_code.
96  std::error_code convertToErrorCode() const override {
98  }
99  };
100 
101  // atomic_write_error + whatever the Writer can return
102 
103  /// Creates a unique file with name according to the given \p TempPathModel,
104  /// writes content of \p Buffer to the file and renames it to \p FinalPath.
105  ///
106  /// \returns \c AtomicFileWriteError in case of error.
107  llvm::Error writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
108  StringRef Buffer);
109 
111  writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
113 
114  /// FilePermssionsApplier helps to copy permissions from an input file to
115  /// an output one. It memorizes the status of the input file and can apply
116  /// permissions and dates to the output file.
118  public:
119  static Expected<FilePermissionsApplier> create(StringRef InputFilename);
120 
121  /// Apply stored permissions to the \p OutputFilename.
122  /// Copy LastAccess and ModificationTime if \p CopyDates is true.
123  /// Overwrite stored permissions if \p OverwritePermissions is specified.
124  Error apply(StringRef OutputFilename, bool CopyDates = false,
125  Optional<sys::fs::perms> OverwritePermissions = std::nullopt);
126 
127  private:
129  : InputFilename(InputFilename), InputStatus(Status) {}
130 
131  StringRef InputFilename;
132  sys::fs::file_status InputStatus;
133  };
134 } // End llvm namespace
135 
136 #endif
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::atomic_write_error::output_stream_error
@ output_stream_error
FileSystem.h
llvm::AtomicFileWriteError::AtomicFileWriteError
AtomicFileWriteError(atomic_write_error Error)
Definition: FileUtilities.h:87
llvm::FileRemover
FileRemover - This class is a simple object meant to be stack allocated.
Definition: FileUtilities.h:42
StringRef.h
llvm::FileRemover::releaseFile
void releaseFile()
releaseFile - Take ownership of the file away from the FileRemover so it will not be removed when the...
Definition: FileUtilities.h:76
llvm::FilePermissionsApplier
FilePermssionsApplier helps to copy permissions from an input file to an output one.
Definition: FileUtilities.h:117
Error.h
llvm::AtomicFileWriteError::ID
static char ID
Definition: FileUtilities.h:92
llvm::FilePermissionsApplier::create
static Expected< FilePermissionsApplier > create(StringRef InputFilename)
Definition: FileUtilities.cpp:329
llvm::Optional
Definition: APInt.h:33
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::atomic_write_error
atomic_write_error
Definition: FileUtilities.h:79
llvm::FileRemover::FileRemover
FileRemover(const Twine &filename, bool deleteIt=true)
Definition: FileUtilities.h:48
llvm::writeFileAtomically
llvm::Error writeFileAtomically(StringRef TempPathModel, StringRef FinalPath, StringRef Buffer)
Creates a unique file with name according to the given TempPathModel, writes content of Buffer to the...
Definition: FileUtilities.cpp:287
false
Definition: StackSlotColoring.cpp:141
llvm::atomic_write_error::failed_to_rename_temp_file
@ failed_to_rename_temp_file
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::AtomicFileWriteError
Definition: FileUtilities.h:85
llvm::SmallString< 128 >
llvm::sys::fs::file_status
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:226
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:82
llvm::ErrorInfo
Base class for user error types.
Definition: Error.h:347
llvm::AtomicFileWriteError::Error
const atomic_write_error Error
Definition: FileUtilities.h:91
Status
Definition: SIModeRegister.cpp:29
llvm::FileRemover::setFile
void setFile(const Twine &filename, bool deleteIt=true)
setFile - Give ownership of the file to the FileRemover so it will be removed when the object is dest...
Definition: FileUtilities.h:63
llvm::sys::fs::remove
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::AtomicFileWriteError::log
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: FileUtilities.cpp:270
llvm::atomic_write_error::failed_to_create_uniq_file
@ failed_to_create_uniq_file
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:79
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::DiffFilesWithTolerance
int DiffFilesWithTolerance(StringRef FileA, StringRef FileB, double AbsTol, double RelTol, std::string *Error=nullptr)
DiffFilesWithTolerance - Compare the two files specified, returning 0 if the files match,...
Definition: FileUtilities.cpp:178
llvm::FilePermissionsApplier::apply
Error apply(StringRef OutputFilename, bool CopyDates=false, Optional< sys::fs::perms > OverwritePermissions=std::nullopt)
Apply stored permissions to the OutputFilename.
Definition: FileUtilities.cpp:342
llvm::sys::path::filename
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:577
llvm::FileRemover::FileRemover
FileRemover()
Definition: FileUtilities.h:46
OutputFilename
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
llvm::FileRemover::~FileRemover
~FileRemover()
Definition: FileUtilities.h:53