1 //===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines a family of utility functions which are useful for doing
10 // various things with files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_FILEUTILITIES_H
15 #define LLVM_SUPPORT_FILEUTILITIES_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Errc.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/Path.h"
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.
32 int DiffFilesWithTolerance(StringRef FileA
,
34 double AbsTol
, double RelTol
,
35 std::string
*Error
= nullptr);
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).
43 SmallString
<128> Filename
;
46 FileRemover() : DeleteIt(false) {}
48 explicit FileRemover(const Twine
& filename
, bool deleteIt
= true)
49 : DeleteIt(deleteIt
) {
50 filename
.toVector(Filename
);
55 // Ignore problems deleting the file.
56 sys::fs::remove(Filename
);
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) {
65 // Ignore problems deleting the file.
66 sys::fs::remove(Filename
);
70 filename
.toVector(Filename
);
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; }
79 enum class atomic_write_error
{
80 failed_to_create_uniq_file
= 0,
82 failed_to_rename_temp_file
85 class AtomicFileWriteError
: public llvm::ErrorInfo
<AtomicFileWriteError
> {
87 AtomicFileWriteError(atomic_write_error Error
) : Error(Error
) {}
89 void log(raw_ostream
&OS
) const override
;
91 const atomic_write_error Error
;
95 // Users are not expected to use error_code.
96 std::error_code
convertToErrorCode() const override
{
97 return llvm::inconvertibleErrorCode();
101 // atomic_write_error + whatever the Writer can return
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.
106 /// \returns \c AtomicFileWriteError in case of error.
107 llvm::Error
writeFileAtomically(StringRef TempPathModel
, StringRef FinalPath
,
111 writeFileAtomically(StringRef TempPathModel
, StringRef FinalPath
,
112 std::function
<llvm::Error(llvm::raw_ostream
&)> Writer
);
113 } // End llvm namespace