| 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/Compiler.h" |
| 19 | #include "llvm/Support/Error.h" |
| 20 | #include "llvm/Support/FileSystem.h" |
| 21 | |
| 22 | #include <system_error> |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if |
| 27 | /// the files match, 1 if they are different, and 2 if there is a file error. |
| 28 | /// This function allows you to specify an absolute and relative FP error that |
| 29 | /// is allowed to exist. If you specify a string to fill in for the error |
| 30 | /// option, it will set the string to an error message if an error occurs, or |
| 31 | /// if the files are different. |
| 32 | /// |
| 33 | LLVM_ABI int DiffFilesWithTolerance(StringRef FileA, StringRef FileB, |
| 34 | double AbsTol, double RelTol, |
| 35 | std::string *Error = nullptr); |
| 36 | |
| 37 | /// FileRemover - This class is a simple object meant to be stack allocated. |
| 38 | /// If an exception is thrown from a region, the object removes the filename |
| 39 | /// specified (if deleteIt is true). |
| 40 | /// |
| 41 | class FileRemover { |
| 42 | SmallString<128> Filename; |
| 43 | bool DeleteIt; |
| 44 | |
| 45 | public: |
| 46 | FileRemover() : DeleteIt(false) {} |
| 47 | |
| 48 | explicit FileRemover(const Twine &filename, bool deleteIt = true) |
| 49 | : DeleteIt(deleteIt) { |
| 50 | filename.toVector(Out&: Filename); |
| 51 | } |
| 52 | |
| 53 | ~FileRemover() { |
| 54 | if (DeleteIt) { |
| 55 | // Ignore problems deleting the file. |
| 56 | sys::fs::remove(path: 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(path: Filename); |
| 67 | } |
| 68 | |
| 69 | Filename.clear(); |
| 70 | filename.toVector(Out&: 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 | /// FilePermssionsApplier helps to copy permissions from an input file to |
| 80 | /// an output one. It memorizes the status of the input file and can apply |
| 81 | /// permissions and dates to the output file. |
| 82 | class FilePermissionsApplier { |
| 83 | public: |
| 84 | LLVM_ABI static Expected<FilePermissionsApplier> |
| 85 | create(StringRef InputFilename); |
| 86 | |
| 87 | /// Apply stored permissions to the \p OutputFilename. |
| 88 | /// Copy LastAccess and ModificationTime if \p CopyDates is true. |
| 89 | /// Overwrite stored permissions if \p OverwritePermissions is specified. |
| 90 | LLVM_ABI Error |
| 91 | apply(StringRef OutputFilename, bool CopyDates = false, |
| 92 | std::optional<sys::fs::perms> OverwritePermissions = std::nullopt); |
| 93 | |
| 94 | private: |
| 95 | FilePermissionsApplier(StringRef InputFilename, sys::fs::file_status Status) |
| 96 | : InputFilename(InputFilename), InputStatus(Status) {} |
| 97 | |
| 98 | StringRef InputFilename; |
| 99 | sys::fs::file_status InputStatus; |
| 100 | }; |
| 101 | } // namespace llvm |
| 102 | |
| 103 | #endif |
| 104 | |