Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:30

0001 //===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines a family of utility functions which are useful for doing
0010 // various things with files.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_SUPPORT_FILEUTILITIES_H
0015 #define LLVM_SUPPORT_FILEUTILITIES_H
0016 
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/Support/Error.h"
0019 #include "llvm/Support/FileSystem.h"
0020 
0021 #include <system_error>
0022 
0023 namespace llvm {
0024 
0025   /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
0026   /// the files match, 1 if they are different, and 2 if there is a file error.
0027   /// This function allows you to specify an absolute and relative FP error that
0028   /// is allowed to exist.  If you specify a string to fill in for the error
0029   /// option, it will set the string to an error message if an error occurs, or
0030   /// if the files are different.
0031   ///
0032   int DiffFilesWithTolerance(StringRef FileA,
0033                              StringRef FileB,
0034                              double AbsTol, double RelTol,
0035                              std::string *Error = nullptr);
0036 
0037 
0038   /// FileRemover - This class is a simple object meant to be stack allocated.
0039   /// If an exception is thrown from a region, the object removes the filename
0040   /// specified (if deleteIt is true).
0041   ///
0042   class FileRemover {
0043     SmallString<128> Filename;
0044     bool DeleteIt;
0045   public:
0046     FileRemover() : DeleteIt(false) {}
0047 
0048     explicit FileRemover(const Twine& filename, bool deleteIt = true)
0049       : DeleteIt(deleteIt) {
0050       filename.toVector(Filename);
0051     }
0052 
0053     ~FileRemover() {
0054       if (DeleteIt) {
0055         // Ignore problems deleting the file.
0056         sys::fs::remove(Filename);
0057       }
0058     }
0059 
0060     /// setFile - Give ownership of the file to the FileRemover so it will
0061     /// be removed when the object is destroyed.  If the FileRemover already
0062     /// had ownership of a file, remove it first.
0063     void setFile(const Twine& filename, bool deleteIt = true) {
0064       if (DeleteIt) {
0065         // Ignore problems deleting the file.
0066         sys::fs::remove(Filename);
0067       }
0068 
0069       Filename.clear();
0070       filename.toVector(Filename);
0071       DeleteIt = deleteIt;
0072     }
0073 
0074     /// releaseFile - Take ownership of the file away from the FileRemover so it
0075     /// will not be removed when the object is destroyed.
0076     void releaseFile() { DeleteIt = false; }
0077   };
0078 
0079   /// FilePermssionsApplier helps to copy permissions from an input file to
0080   /// an output one. It memorizes the status of the input file and can apply
0081   /// permissions and dates to the output file.
0082   class FilePermissionsApplier {
0083   public:
0084     static Expected<FilePermissionsApplier> create(StringRef InputFilename);
0085 
0086     /// Apply stored permissions to the \p OutputFilename.
0087     /// Copy LastAccess and ModificationTime if \p CopyDates is true.
0088     /// Overwrite stored permissions if \p OverwritePermissions is specified.
0089     Error
0090     apply(StringRef OutputFilename, bool CopyDates = false,
0091           std::optional<sys::fs::perms> OverwritePermissions = std::nullopt);
0092 
0093   private:
0094     FilePermissionsApplier(StringRef InputFilename, sys::fs::file_status Status)
0095         : InputFilename(InputFilename), InputStatus(Status) {}
0096 
0097     StringRef InputFilename;
0098     sys::fs::file_status InputStatus;
0099   };
0100 } // End llvm namespace
0101 
0102 #endif