LLVM  15.0.0git
FileUtilities.cpp
Go to the documentation of this file.
1 //===- Support/FileUtilities.cpp - File System Utilities ------------------===//
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 implements a family of utility functions which are useful for doing
10 // various things with files.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/ErrorOr.h"
20 #include "llvm/Support/Process.h"
22 #include <cstdint>
23 #include <cstdlib>
24 #include <cstring>
25 #include <memory>
26 #include <system_error>
27 
28 using namespace llvm;
29 
30 static bool isSignedChar(char C) {
31  return (C == '+' || C == '-');
32 }
33 
34 static bool isExponentChar(char C) {
35  switch (C) {
36  case 'D': // Strange exponential notation.
37  case 'd': // Strange exponential notation.
38  case 'e':
39  case 'E': return true;
40  default: return false;
41  }
42 }
43 
44 static bool isNumberChar(char C) {
45  switch (C) {
46  case '0': case '1': case '2': case '3': case '4':
47  case '5': case '6': case '7': case '8': case '9':
48  case '.': return true;
49  default: return isSignedChar(C) || isExponentChar(C);
50  }
51 }
52 
53 static const char *BackupNumber(const char *Pos, const char *FirstChar) {
54  // If we didn't stop in the middle of a number, don't backup.
55  if (!isNumberChar(*Pos)) return Pos;
56 
57  // Otherwise, return to the start of the number.
58  bool HasPeriod = false;
59  while (Pos > FirstChar && isNumberChar(Pos[-1])) {
60  // Backup over at most one period.
61  if (Pos[-1] == '.') {
62  if (HasPeriod)
63  break;
64  HasPeriod = true;
65  }
66 
67  --Pos;
68  if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1]))
69  break;
70  }
71  return Pos;
72 }
73 
74 /// EndOfNumber - Return the first character that is not part of the specified
75 /// number. This assumes that the buffer is null terminated, so it won't fall
76 /// off the end.
77 static const char *EndOfNumber(const char *Pos) {
78  while (isNumberChar(*Pos))
79  ++Pos;
80  return Pos;
81 }
82 
83 /// CompareNumbers - compare two numbers, returning true if they are different.
84 static bool CompareNumbers(const char *&F1P, const char *&F2P,
85  const char *F1End, const char *F2End,
86  double AbsTolerance, double RelTolerance,
87  std::string *ErrorMsg) {
88  const char *F1NumEnd, *F2NumEnd;
89  double V1 = 0.0, V2 = 0.0;
90 
91  // If one of the positions is at a space and the other isn't, chomp up 'til
92  // the end of the space.
93  while (isSpace(static_cast<unsigned char>(*F1P)) && F1P != F1End)
94  ++F1P;
95  while (isSpace(static_cast<unsigned char>(*F2P)) && F2P != F2End)
96  ++F2P;
97 
98  // If we stop on numbers, compare their difference.
99  if (!isNumberChar(*F1P) || !isNumberChar(*F2P)) {
100  // The diff failed.
101  F1NumEnd = F1P;
102  F2NumEnd = F2P;
103  } else {
104  // Note that some ugliness is built into this to permit support for numbers
105  // that use "D" or "d" as their exponential marker, e.g. "1.234D45". This
106  // occurs in 200.sixtrack in spec2k.
107  V1 = strtod(F1P, const_cast<char**>(&F1NumEnd));
108  V2 = strtod(F2P, const_cast<char**>(&F2NumEnd));
109 
110  if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
111  // Copy string into tmp buffer to replace the 'D' with an 'e'.
112  SmallString<200> StrTmp(F1P, EndOfNumber(F1NumEnd)+1);
113  // Strange exponential notation!
114  StrTmp[static_cast<unsigned>(F1NumEnd-F1P)] = 'e';
115 
116  V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd));
117  F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]);
118  }
119 
120  if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
121  // Copy string into tmp buffer to replace the 'D' with an 'e'.
122  SmallString<200> StrTmp(F2P, EndOfNumber(F2NumEnd)+1);
123  // Strange exponential notation!
124  StrTmp[static_cast<unsigned>(F2NumEnd-F2P)] = 'e';
125 
126  V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd));
127  F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]);
128  }
129  }
130 
131  if (F1NumEnd == F1P || F2NumEnd == F2P) {
132  if (ErrorMsg) {
133  *ErrorMsg = "FP Comparison failed, not a numeric difference between '";
134  *ErrorMsg += F1P[0];
135  *ErrorMsg += "' and '";
136  *ErrorMsg += F2P[0];
137  *ErrorMsg += "'";
138  }
139  return true;
140  }
141 
142  // Check to see if these are inside the absolute tolerance
143  if (AbsTolerance < std::abs(V1-V2)) {
144  // Nope, check the relative tolerance...
145  double Diff;
146  if (V2)
147  Diff = std::abs(V1/V2 - 1.0);
148  else if (V1)
149  Diff = std::abs(V2/V1 - 1.0);
150  else
151  Diff = 0; // Both zero.
152  if (Diff > RelTolerance) {
153  if (ErrorMsg) {
154  raw_string_ostream(*ErrorMsg)
155  << "Compared: " << V1 << " and " << V2 << '\n'
156  << "abs. diff = " << std::abs(V1-V2) << " rel.diff = " << Diff << '\n'
157  << "Out of tolerance: rel/abs: " << RelTolerance << '/'
158  << AbsTolerance;
159  }
160  return true;
161  }
162  }
163 
164  // Otherwise, advance our read pointers to the end of the numbers.
165  F1P = F1NumEnd; F2P = F2NumEnd;
166  return false;
167 }
168 
169 /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
170 /// files match, 1 if they are different, and 2 if there is a file error. This
171 /// function differs from DiffFiles in that you can specify an absolete and
172 /// relative FP error that is allowed to exist. If you specify a string to fill
173 /// in for the error option, it will set the string to an error message if an
174 /// error occurs, allowing the caller to distinguish between a failed diff and a
175 /// file system error.
176 ///
178  StringRef NameB,
179  double AbsTol, double RelTol,
180  std::string *Error) {
181  // Now its safe to mmap the files into memory because both files
182  // have a non-zero size.
184  if (std::error_code EC = F1OrErr.getError()) {
185  if (Error)
186  *Error = EC.message();
187  return 2;
188  }
189  MemoryBuffer &F1 = *F1OrErr.get();
190 
192  if (std::error_code EC = F2OrErr.getError()) {
193  if (Error)
194  *Error = EC.message();
195  return 2;
196  }
197  MemoryBuffer &F2 = *F2OrErr.get();
198 
199  // Okay, now that we opened the files, scan them for the first difference.
200  const char *File1Start = F1.getBufferStart();
201  const char *File2Start = F2.getBufferStart();
202  const char *File1End = F1.getBufferEnd();
203  const char *File2End = F2.getBufferEnd();
204  const char *F1P = File1Start;
205  const char *F2P = File2Start;
206  uint64_t A_size = F1.getBufferSize();
207  uint64_t B_size = F2.getBufferSize();
208 
209  // Are the buffers identical? Common case: Handle this efficiently.
210  if (A_size == B_size &&
211  std::memcmp(File1Start, File2Start, A_size) == 0)
212  return 0;
213 
214  // Otherwise, we are done a tolerances are set.
215  if (AbsTol == 0 && RelTol == 0) {
216  if (Error)
217  *Error = "Files differ without tolerance allowance";
218  return 1; // Files different!
219  }
220 
221  bool CompareFailed = false;
222  while (true) {
223  // Scan for the end of file or next difference.
224  while (F1P < File1End && F2P < File2End && *F1P == *F2P) {
225  ++F1P;
226  ++F2P;
227  }
228 
229  if (F1P >= File1End || F2P >= File2End) break;
230 
231  // Okay, we must have found a difference. Backup to the start of the
232  // current number each stream is at so that we can compare from the
233  // beginning.
234  F1P = BackupNumber(F1P, File1Start);
235  F2P = BackupNumber(F2P, File2Start);
236 
237  // Now that we are at the start of the numbers, compare them, exiting if
238  // they don't match.
239  if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
240  CompareFailed = true;
241  break;
242  }
243  }
244 
245  // Okay, we reached the end of file. If both files are at the end, we
246  // succeeded.
247  bool F1AtEnd = F1P >= File1End;
248  bool F2AtEnd = F2P >= File2End;
249  if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
250  // Else, we might have run off the end due to a number: backup and retry.
251  if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
252  if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
253  F1P = BackupNumber(F1P, File1Start);
254  F2P = BackupNumber(F2P, File2Start);
255 
256  // Now that we are at the start of the numbers, compare them, exiting if
257  // they don't match.
258  if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
259  CompareFailed = true;
260 
261  // If we found the end, we succeeded.
262  if (F1P < File1End || F2P < File2End)
263  CompareFailed = true;
264  }
265 
266  return CompareFailed;
267 }
268 
270  OS << "atomic_write_error: ";
271  switch (Error) {
273  OS << "failed_to_create_uniq_file";
274  return;
276  OS << "output_stream_error";
277  return;
279  OS << "failed_to_rename_temp_file";
280  return;
281  }
282  llvm_unreachable("unknown atomic_write_error value in "
283  "failed_to_rename_temp_file::log()");
284 }
285 
287  StringRef FinalPath, StringRef Buffer) {
288  return writeFileAtomically(TempPathModel, FinalPath,
289  [&Buffer](llvm::raw_ostream &OS) {
290  OS.write(Buffer.data(), Buffer.size());
291  return llvm::Error::success();
292  });
293 }
294 
296  StringRef TempPathModel, StringRef FinalPath,
298  SmallString<128> GeneratedUniqPath;
299  int TempFD;
300  if (sys::fs::createUniqueFile(TempPathModel, TempFD, GeneratedUniqPath)) {
301  return llvm::make_error<AtomicFileWriteError>(
303  }
304  llvm::FileRemover RemoveTmpFileOnFail(GeneratedUniqPath);
305 
306  raw_fd_ostream OS(TempFD, /*shouldClose=*/true);
307  if (llvm::Error Err = Writer(OS)) {
308  return Err;
309  }
310 
311  OS.close();
312  if (OS.has_error()) {
313  OS.clear_error();
314  return llvm::make_error<AtomicFileWriteError>(
316  }
317 
318  if (sys::fs::rename(/*from=*/GeneratedUniqPath, /*to=*/FinalPath)) {
319  return llvm::make_error<AtomicFileWriteError>(
321  }
322 
323  RemoveTmpFileOnFail.releaseFile();
324  return Error::success();
325 }
326 
330 
331  if (InputFilename != "-") {
332  if (auto EC = sys::fs::status(InputFilename, Status))
333  return createFileError(InputFilename, EC);
334  } else {
335  Status.permissions(static_cast<sys::fs::perms>(0777));
336  }
337 
338  return FilePermissionsApplier(InputFilename, Status);
339 }
340 
342  StringRef OutputFilename, bool CopyDates,
343  Optional<sys::fs::perms> OverwritePermissions) {
344  sys::fs::file_status Status = InputStatus;
345 
346  if (OverwritePermissions)
347  Status.permissions(*OverwritePermissions);
348 
349  int FD = 0;
350 
351  // Writing to stdout should not be treated as an error here, just
352  // do not set access/modification times or permissions.
353  if (OutputFilename == "-")
354  return Error::success();
355 
356  if (std::error_code EC = sys::fs::openFileForWrite(OutputFilename, FD,
358  return createFileError(OutputFilename, EC);
359 
360  if (CopyDates)
361  if (std::error_code EC = sys::fs::setLastAccessAndModificationTime(
362  FD, Status.getLastAccessedTime(), Status.getLastModificationTime()))
363  return createFileError(OutputFilename, EC);
364 
365  sys::fs::file_status OStat;
366  if (std::error_code EC = sys::fs::status(FD, OStat))
367  return createFileError(OutputFilename, EC);
368  if (OStat.type() == sys::fs::file_type::regular_file) {
369 #ifndef _WIN32
370  // Keep ownership if llvm-objcopy is called under root.
371  if (OutputFilename == InputFilename && OStat.getUser() == 0)
372  sys::fs::changeFileOwnership(FD, Status.getUser(), Status.getGroup());
373 #endif
374 
375  sys::fs::perms Perm = Status.permissions();
376  if (OutputFilename != InputFilename)
377  Perm = static_cast<sys::fs::perms>(Perm & ~sys::fs::getUmask() & ~06000);
378 #ifdef _WIN32
379  if (std::error_code EC = sys::fs::setPermissions(OutputFilename, Perm))
380 #else
381  if (std::error_code EC = sys::fs::setPermissions(FD, Perm))
382 #endif
383  return createFileError(OutputFilename, EC);
384  }
385 
386  if (std::error_code EC = sys::Process::SafelyCloseFileDescriptor(FD))
387  return createFileError(OutputFilename, EC);
388 
389  return Error::success();
390 }
391 
MemoryBuffer.h
BackupNumber
static const char * BackupNumber(const char *Pos, const char *FirstChar)
Definition: FileUtilities.cpp:53
llvm::sys::fs::openFileForWrite
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: FileSystem.h:1065
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::raw_fd_ostream::close
void close()
Manually flush the stream and close the file.
Definition: raw_ostream.cpp:793
llvm::atomic_write_error::output_stream_error
@ output_stream_error
llvm::FileRemover
FileRemover - This class is a simple object meant to be stack allocated.
Definition: FileUtilities.h:42
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:632
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
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
memcmp
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:899
Error.h
llvm::AtomicFileWriteError::ID
static char ID
Definition: FileUtilities.h:92
llvm::sys::fs::createUniqueFile
std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, OpenFlags Flags=OF_None, unsigned Mode=all_read|all_write)
Create a uniquely named file.
Definition: Path.cpp:818
llvm::FilePermissionsApplier::create
static Expected< FilePermissionsApplier > create(StringRef InputFilename)
Definition: FileUtilities.cpp:328
llvm::sys::fs::file_type::regular_file
@ regular_file
llvm::sys::fs::CD_OpenExisting
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
Definition: FileSystem.h:743
llvm::Optional
Definition: APInt.h:33
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::sys::fs::setLastAccessAndModificationTime
std::error_code setLastAccessAndModificationTime(int FD, TimePoint<> AccessTime, TimePoint<> ModificationTime)
Set the file modification and access time.
endif
__FakeVCSRevision h endif() endif() set(generated_files "$
Definition: CMakeLists.txt:16
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
isExponentChar
static bool isExponentChar(char C)
Definition: FileUtilities.cpp:34
InputFilename
static cl::opt< std::string > InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"))
llvm::MemoryBuffer::getFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: MemoryBuffer.cpp:239
Process.h
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:286
llvm::MemoryBuffer::getBufferEnd
const char * getBufferEnd() const
Definition: MemoryBuffer.h:66
llvm::raw_fd_ostream::has_error
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
Definition: raw_ostream.h:535
SmallString.h
llvm::createFileError
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition: Error.h:1320
llvm::MemoryBuffer::getBufferSize
size_t getBufferSize() const
Definition: MemoryBuffer.h:67
isSignedChar
static bool isSignedChar(char C)
Definition: FileUtilities.cpp:30
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::raw_fd_ostream::clear_error
void clear_error()
Set the flag read by has_error() to false.
Definition: raw_ostream.h:546
llvm::ErrorOr::getError
std::error_code getError() const
Definition: ErrorOr.h:153
llvm::atomic_write_error::failed_to_rename_temp_file
@ failed_to_rename_temp_file
llvm::raw_ostream::write
raw_ostream & write(unsigned char C)
Definition: raw_ostream.cpp:219
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::sys::fs::changeFileOwnership
std::error_code changeFileOwnership(int FD, uint32_t Owner, uint32_t Group)
Change ownership of a file.
llvm::SmallString
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
llvm::sys::Process::SafelyCloseFileDescriptor
static std::error_code SafelyCloseFileDescriptor(int FD)
llvm::sys::fs::file_status
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:226
uint64_t
ErrorOr.h
StringExtras.h
llvm::FilePermissionsApplier::apply
Error apply(StringRef OutputFilename, bool CopyDates=false, Optional< sys::fs::perms > OverwritePermissions=None)
Apply stored permissions to the OutputFilename.
Definition: FileUtilities.cpp:341
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:82
llvm::sys::fs::setPermissions
std::error_code setPermissions(const Twine &Path, perms Permissions)
Set file permissions.
llvm::sys::fs::basic_file_status::type
file_type type() const
Definition: FileSystem.h:184
Status
Definition: SIModeRegister.cpp:29
llvm::sys::fs::basic_file_status::getUser
uint32_t getUser() const
Definition: FileSystem.h:203
FileUtilities.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::sys::fs::rename
std::error_code rename(const Twine &from, const Twine &to)
Rename from to to.
llvm::AtomicFileWriteError::log
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: FileUtilities.cpp:269
llvm::atomic_write_error::failed_to_create_uniq_file
@ failed_to_create_uniq_file
llvm::raw_fd_ostream
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:444
llvm::StringRef::size
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
llvm::MemoryBuffer::getBufferStart
const char * getBufferStart() const
Definition: MemoryBuffer.h:65
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:177
CompareNumbers
static bool CompareNumbers(const char *&F1P, const char *&F2P, const char *F1End, const char *F2End, double AbsTolerance, double RelTolerance, std::string *ErrorMsg)
CompareNumbers - compare two numbers, returning true if they are different.
Definition: FileUtilities.cpp:84
llvm::sys::fs::perms
perms
Definition: FileSystem.h:86
llvm::sys::fs::getUmask
unsigned getUmask()
Get file creation mode mask of the process.
llvm::ErrorOr::get
reference get()
Definition: ErrorOr.h:150
OutputFilename
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
llvm::sys::fs::status
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::StringRef::data
const LLVM_NODISCARD char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:149
isNumberChar
static bool isNumberChar(char C)
Definition: FileUtilities.cpp:44
EndOfNumber
static const char * EndOfNumber(const char *Pos)
EndOfNumber - Return the first character that is not part of the specified number.
Definition: FileUtilities.cpp:77
raw_ostream.h
llvm::abs
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition: APFloat.h:1281