1193323Sed//===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
2193323Sed//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6193323Sed//
7193323Sed//===----------------------------------------------------------------------===//
8193323Sed//
9193323Sed// This file defines a family of utility functions which are useful for doing
10193323Sed// various things with files.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed#ifndef LLVM_SUPPORT_FILEUTILITIES_H
15193323Sed#define LLVM_SUPPORT_FILEUTILITIES_H
16193323Sed
17360784Sdim#include "llvm/ADT/StringRef.h"
18360784Sdim#include "llvm/Support/Errc.h"
19360784Sdim#include "llvm/Support/ErrorHandling.h"
20221345Sdim#include "llvm/Support/FileSystem.h"
21218893Sdim#include "llvm/Support/Path.h"
22193323Sed
23193323Sednamespace llvm {
24193323Sed
25193323Sed  /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
26193323Sed  /// the files match, 1 if they are different, and 2 if there is a file error.
27221345Sdim  /// This function allows you to specify an absolute and relative FP error that
28193323Sed  /// is allowed to exist.  If you specify a string to fill in for the error
29193323Sed  /// option, it will set the string to an error message if an error occurs, or
30193323Sed  /// if the files are different.
31193323Sed  ///
32261991Sdim  int DiffFilesWithTolerance(StringRef FileA,
33261991Sdim                             StringRef FileB,
34193323Sed                             double AbsTol, double RelTol,
35276479Sdim                             std::string *Error = nullptr);
36193323Sed
37193323Sed
38193323Sed  /// FileRemover - This class is a simple object meant to be stack allocated.
39193323Sed  /// If an exception is thrown from a region, the object removes the filename
40193323Sed  /// specified (if deleteIt is true).
41193323Sed  ///
42193323Sed  class FileRemover {
43221345Sdim    SmallString<128> Filename;
44193323Sed    bool DeleteIt;
45193323Sed  public:
46206083Srdivacky    FileRemover() : DeleteIt(false) {}
47206083Srdivacky
48221345Sdim    explicit FileRemover(const Twine& filename, bool deleteIt = true)
49221345Sdim      : DeleteIt(deleteIt) {
50221345Sdim      filename.toVector(Filename);
51221345Sdim    }
52193323Sed
53193323Sed    ~FileRemover() {
54193323Sed      if (DeleteIt) {
55193323Sed        // Ignore problems deleting the file.
56288943Sdim        sys::fs::remove(Filename);
57193323Sed      }
58193323Sed    }
59193323Sed
60206083Srdivacky    /// setFile - Give ownership of the file to the FileRemover so it will
61206083Srdivacky    /// be removed when the object is destroyed.  If the FileRemover already
62206083Srdivacky    /// had ownership of a file, remove it first.
63221345Sdim    void setFile(const Twine& filename, bool deleteIt = true) {
64221345Sdim      if (DeleteIt) {
65221345Sdim        // Ignore problems deleting the file.
66288943Sdim        sys::fs::remove(Filename);
67221345Sdim      }
68206083Srdivacky
69221345Sdim      Filename.clear();
70221345Sdim      filename.toVector(Filename);
71206083Srdivacky      DeleteIt = deleteIt;
72206083Srdivacky    }
73206083Srdivacky
74193323Sed    /// releaseFile - Take ownership of the file away from the FileRemover so it
75193323Sed    /// will not be removed when the object is destroyed.
76193323Sed    void releaseFile() { DeleteIt = false; }
77193323Sed  };
78360784Sdim
79360784Sdim  enum class atomic_write_error {
80360784Sdim    failed_to_create_uniq_file = 0,
81360784Sdim    output_stream_error,
82360784Sdim    failed_to_rename_temp_file
83360784Sdim  };
84360784Sdim
85360784Sdim  class AtomicFileWriteError : public llvm::ErrorInfo<AtomicFileWriteError> {
86360784Sdim  public:
87360784Sdim    AtomicFileWriteError(atomic_write_error Error) : Error(Error) {}
88360784Sdim
89360784Sdim    void log(raw_ostream &OS) const override;
90360784Sdim
91360784Sdim    const atomic_write_error Error;
92360784Sdim    static char ID;
93360784Sdim
94360784Sdim  private:
95360784Sdim    // Users are not expected to use error_code.
96360784Sdim    std::error_code convertToErrorCode() const override {
97360784Sdim      return llvm::inconvertibleErrorCode();
98360784Sdim    }
99360784Sdim  };
100360784Sdim
101360784Sdim  // atomic_write_error + whatever the Writer can return
102360784Sdim
103360784Sdim  /// Creates a unique file with name according to the given \p TempPathModel,
104360784Sdim  /// writes content of \p Buffer to the file and renames it to \p FinalPath.
105360784Sdim  ///
106360784Sdim  /// \returns \c AtomicFileWriteError in case of error.
107360784Sdim  llvm::Error writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
108360784Sdim                                  StringRef Buffer);
109360784Sdim
110360784Sdim  llvm::Error
111360784Sdim  writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
112360784Sdim                      std::function<llvm::Error(llvm::raw_ostream &)> Writer);
113193323Sed} // End llvm namespace
114193323Sed
115193323Sed#endif
116