xref: /freebsd/contrib/llvm-project/llvm/lib/Support/FileUtilities.cpp (revision ccfd87fe2ac0e2e6aeb1911a7d7cce6712a8564f)
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 
14 #include "llvm/Support/FileUtilities.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/Process.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <cmath>
23 #include <cstdint>
24 #include <cstdlib>
25 #include <cstring>
26 #include <memory>
27 #include <system_error>
28 
29 using namespace llvm;
30 
31 static bool isSignedChar(char C) {
32   return (C == '+' || C == '-');
33 }
34 
35 static bool isExponentChar(char C) {
36   switch (C) {
37   case 'D':  // Strange exponential notation.
38   case 'd':  // Strange exponential notation.
39   case 'e':
40   case 'E': return true;
41   default: return false;
42   }
43 }
44 
45 static bool isNumberChar(char C) {
46   switch (C) {
47   case '0': case '1': case '2': case '3': case '4':
48   case '5': case '6': case '7': case '8': case '9':
49   case '.': return true;
50   default: return isSignedChar(C) || isExponentChar(C);
51   }
52 }
53 
54 static const char *BackupNumber(const char *Pos, const char *FirstChar) {
55   // If we didn't stop in the middle of a number, don't backup.
56   if (!isNumberChar(*Pos)) return Pos;
57 
58   // Otherwise, return to the start of the number.
59   bool HasPeriod = false;
60   while (Pos > FirstChar && isNumberChar(Pos[-1])) {
61     // Backup over at most one period.
62     if (Pos[-1] == '.') {
63       if (HasPeriod)
64         break;
65       HasPeriod = true;
66     }
67 
68     --Pos;
69     if (Pos > FirstChar && isSignedChar(Pos[0]) && !isExponentChar(Pos[-1]))
70       break;
71   }
72   return Pos;
73 }
74 
75 /// EndOfNumber - Return the first character that is not part of the specified
76 /// number.  This assumes that the buffer is null terminated, so it won't fall
77 /// off the end.
78 static const char *EndOfNumber(const char *Pos) {
79   while (isNumberChar(*Pos))
80     ++Pos;
81   return Pos;
82 }
83 
84 /// CompareNumbers - compare two numbers, returning true if they are different.
85 static bool CompareNumbers(const char *&F1P, const char *&F2P,
86                            const char *F1End, const char *F2End,
87                            double AbsTolerance, double RelTolerance,
88                            std::string *ErrorMsg) {
89   const char *F1NumEnd, *F2NumEnd;
90   double V1 = 0.0, V2 = 0.0;
91 
92   // If one of the positions is at a space and the other isn't, chomp up 'til
93   // the end of the space.
94   while (isSpace(static_cast<unsigned char>(*F1P)) && F1P != F1End)
95     ++F1P;
96   while (isSpace(static_cast<unsigned char>(*F2P)) && F2P != F2End)
97     ++F2P;
98 
99   // If we stop on numbers, compare their difference.
100   if (!isNumberChar(*F1P) || !isNumberChar(*F2P)) {
101     // The diff failed.
102     F1NumEnd = F1P;
103     F2NumEnd = F2P;
104   } else {
105     // Note that some ugliness is built into this to permit support for numbers
106     // that use "D" or "d" as their exponential marker, e.g. "1.234D45".  This
107     // occurs in 200.sixtrack in spec2k.
108     V1 = strtod(F1P, const_cast<char**>(&F1NumEnd));
109     V2 = strtod(F2P, const_cast<char**>(&F2NumEnd));
110 
111     if (*F1NumEnd == 'D' || *F1NumEnd == 'd') {
112       // Copy string into tmp buffer to replace the 'D' with an 'e'.
113       SmallString<200> StrTmp(F1P, EndOfNumber(F1NumEnd)+1);
114       // Strange exponential notation!
115       StrTmp[static_cast<unsigned>(F1NumEnd-F1P)] = 'e';
116 
117       V1 = strtod(&StrTmp[0], const_cast<char**>(&F1NumEnd));
118       F1NumEnd = F1P + (F1NumEnd-&StrTmp[0]);
119     }
120 
121     if (*F2NumEnd == 'D' || *F2NumEnd == 'd') {
122       // Copy string into tmp buffer to replace the 'D' with an 'e'.
123       SmallString<200> StrTmp(F2P, EndOfNumber(F2NumEnd)+1);
124       // Strange exponential notation!
125       StrTmp[static_cast<unsigned>(F2NumEnd-F2P)] = 'e';
126 
127       V2 = strtod(&StrTmp[0], const_cast<char**>(&F2NumEnd));
128       F2NumEnd = F2P + (F2NumEnd-&StrTmp[0]);
129     }
130   }
131 
132   if (F1NumEnd == F1P || F2NumEnd == F2P) {
133     if (ErrorMsg) {
134       *ErrorMsg = "FP Comparison failed, not a numeric difference between '";
135       *ErrorMsg += F1P[0];
136       *ErrorMsg += "' and '";
137       *ErrorMsg += F2P[0];
138       *ErrorMsg += "'";
139     }
140     return true;
141   }
142 
143   // Check to see if these are inside the absolute tolerance
144   if (AbsTolerance < std::abs(V1-V2)) {
145     // Nope, check the relative tolerance...
146     double Diff;
147     if (V2)
148       Diff = std::abs(V1/V2 - 1.0);
149     else if (V1)
150       Diff = std::abs(V2/V1 - 1.0);
151     else
152       Diff = 0;  // Both zero.
153     if (Diff > RelTolerance) {
154       if (ErrorMsg) {
155         raw_string_ostream(*ErrorMsg)
156           << "Compared: " << V1 << " and " << V2 << '\n'
157           << "abs. diff = " << std::abs(V1-V2) << " rel.diff = " << Diff << '\n'
158           << "Out of tolerance: rel/abs: " << RelTolerance << '/'
159           << AbsTolerance;
160       }
161       return true;
162     }
163   }
164 
165   // Otherwise, advance our read pointers to the end of the numbers.
166   F1P = F1NumEnd;  F2P = F2NumEnd;
167   return false;
168 }
169 
170 /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if the
171 /// files match, 1 if they are different, and 2 if there is a file error.  This
172 /// function differs from DiffFiles in that you can specify an absolete and
173 /// relative FP error that is allowed to exist.  If you specify a string to fill
174 /// in for the error option, it will set the string to an error message if an
175 /// error occurs, allowing the caller to distinguish between a failed diff and a
176 /// file system error.
177 ///
178 int llvm::DiffFilesWithTolerance(StringRef NameA,
179                                  StringRef NameB,
180                                  double AbsTol, double RelTol,
181                                  std::string *Error) {
182   // Now its safe to mmap the files into memory because both files
183   // have a non-zero size.
184   ErrorOr<std::unique_ptr<MemoryBuffer>> F1OrErr = MemoryBuffer::getFile(NameA);
185   if (std::error_code EC = F1OrErr.getError()) {
186     if (Error)
187       *Error = EC.message();
188     return 2;
189   }
190   MemoryBuffer &F1 = *F1OrErr.get();
191 
192   ErrorOr<std::unique_ptr<MemoryBuffer>> F2OrErr = MemoryBuffer::getFile(NameB);
193   if (std::error_code EC = F2OrErr.getError()) {
194     if (Error)
195       *Error = EC.message();
196     return 2;
197   }
198   MemoryBuffer &F2 = *F2OrErr.get();
199 
200   // Okay, now that we opened the files, scan them for the first difference.
201   const char *File1Start = F1.getBufferStart();
202   const char *File2Start = F2.getBufferStart();
203   const char *File1End = F1.getBufferEnd();
204   const char *File2End = F2.getBufferEnd();
205   const char *F1P = File1Start;
206   const char *F2P = File2Start;
207   uint64_t A_size = F1.getBufferSize();
208   uint64_t B_size = F2.getBufferSize();
209 
210   // Are the buffers identical?  Common case: Handle this efficiently.
211   if (A_size == B_size &&
212       std::memcmp(File1Start, File2Start, A_size) == 0)
213     return 0;
214 
215   // Otherwise, we are done a tolerances are set.
216   if (AbsTol == 0 && RelTol == 0) {
217     if (Error)
218       *Error = "Files differ without tolerance allowance";
219     return 1;   // Files different!
220   }
221 
222   bool CompareFailed = false;
223   while (true) {
224     // Scan for the end of file or next difference.
225     while (F1P < File1End && F2P < File2End && *F1P == *F2P) {
226       ++F1P;
227       ++F2P;
228     }
229 
230     if (F1P >= File1End || F2P >= File2End) break;
231 
232     // Okay, we must have found a difference.  Backup to the start of the
233     // current number each stream is at so that we can compare from the
234     // beginning.
235     F1P = BackupNumber(F1P, File1Start);
236     F2P = BackupNumber(F2P, File2Start);
237 
238     // Now that we are at the start of the numbers, compare them, exiting if
239     // they don't match.
240     if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error)) {
241       CompareFailed = true;
242       break;
243     }
244   }
245 
246   // Okay, we reached the end of file.  If both files are at the end, we
247   // succeeded.
248   bool F1AtEnd = F1P >= File1End;
249   bool F2AtEnd = F2P >= File2End;
250   if (!CompareFailed && (!F1AtEnd || !F2AtEnd)) {
251     // Else, we might have run off the end due to a number: backup and retry.
252     if (F1AtEnd && isNumberChar(F1P[-1])) --F1P;
253     if (F2AtEnd && isNumberChar(F2P[-1])) --F2P;
254     F1P = BackupNumber(F1P, File1Start);
255     F2P = BackupNumber(F2P, File2Start);
256 
257     // Now that we are at the start of the numbers, compare them, exiting if
258     // they don't match.
259     if (CompareNumbers(F1P, F2P, File1End, File2End, AbsTol, RelTol, Error))
260       CompareFailed = true;
261 
262     // If we found the end, we succeeded.
263     if (F1P < File1End || F2P < File2End)
264       CompareFailed = true;
265   }
266 
267   return CompareFailed;
268 }
269 
270 void llvm::AtomicFileWriteError::log(raw_ostream &OS) const {
271   OS << "atomic_write_error: ";
272   switch (Error) {
273   case atomic_write_error::failed_to_create_uniq_file:
274     OS << "failed_to_create_uniq_file";
275     return;
276   case atomic_write_error::output_stream_error:
277     OS << "output_stream_error";
278     return;
279   case atomic_write_error::failed_to_rename_temp_file:
280     OS << "failed_to_rename_temp_file";
281     return;
282   }
283   llvm_unreachable("unknown atomic_write_error value in "
284                    "failed_to_rename_temp_file::log()");
285 }
286 
287 llvm::Error llvm::writeFileAtomically(StringRef TempPathModel,
288                                       StringRef FinalPath, StringRef Buffer) {
289   return writeFileAtomically(TempPathModel, FinalPath,
290                              [&Buffer](llvm::raw_ostream &OS) {
291                                OS.write(Buffer.data(), Buffer.size());
292                                return llvm::Error::success();
293                              });
294 }
295 
296 llvm::Error llvm::writeFileAtomically(
297     StringRef TempPathModel, StringRef FinalPath,
298     std::function<llvm::Error(llvm::raw_ostream &)> Writer) {
299   SmallString<128> GeneratedUniqPath;
300   int TempFD;
301   if (sys::fs::createUniqueFile(TempPathModel, TempFD, GeneratedUniqPath)) {
302     return llvm::make_error<AtomicFileWriteError>(
303         atomic_write_error::failed_to_create_uniq_file);
304   }
305   llvm::FileRemover RemoveTmpFileOnFail(GeneratedUniqPath);
306 
307   raw_fd_ostream OS(TempFD, /*shouldClose=*/true);
308   if (llvm::Error Err = Writer(OS)) {
309     return Err;
310   }
311 
312   OS.close();
313   if (OS.has_error()) {
314     OS.clear_error();
315     return llvm::make_error<AtomicFileWriteError>(
316         atomic_write_error::output_stream_error);
317   }
318 
319   if (sys::fs::rename(/*from=*/GeneratedUniqPath, /*to=*/FinalPath)) {
320     return llvm::make_error<AtomicFileWriteError>(
321         atomic_write_error::failed_to_rename_temp_file);
322   }
323 
324   RemoveTmpFileOnFail.releaseFile();
325   return Error::success();
326 }
327 
328 Expected<FilePermissionsApplier>
329 FilePermissionsApplier::create(StringRef InputFilename) {
330   sys::fs::file_status Status;
331 
332   if (InputFilename != "-") {
333     if (auto EC = sys::fs::status(InputFilename, Status))
334       return createFileError(InputFilename, EC);
335   } else {
336     Status.permissions(static_cast<sys::fs::perms>(0777));
337   }
338 
339   return FilePermissionsApplier(InputFilename, Status);
340 }
341 
342 Error FilePermissionsApplier::apply(
343     StringRef OutputFilename, bool CopyDates,
344     std::optional<sys::fs::perms> OverwritePermissions) {
345   sys::fs::file_status Status = InputStatus;
346 
347   if (OverwritePermissions)
348     Status.permissions(*OverwritePermissions);
349 
350   int FD = 0;
351 
352   // Writing to stdout should not be treated as an error here, just
353   // do not set access/modification times or permissions.
354   if (OutputFilename == "-")
355     return Error::success();
356 
357   if (std::error_code EC = sys::fs::openFileForWrite(OutputFilename, FD,
358                                                      sys::fs::CD_OpenExisting))
359     return createFileError(OutputFilename, EC);
360 
361   if (CopyDates)
362     if (std::error_code EC = sys::fs::setLastAccessAndModificationTime(
363             FD, Status.getLastAccessedTime(), Status.getLastModificationTime()))
364       return createFileError(OutputFilename, EC);
365 
366   sys::fs::file_status OStat;
367   if (std::error_code EC = sys::fs::status(FD, OStat))
368     return createFileError(OutputFilename, EC);
369   if (OStat.type() == sys::fs::file_type::regular_file) {
370 #ifndef _WIN32
371     // Keep ownership if llvm-objcopy is called under root.
372     if (OutputFilename == InputFilename && OStat.getUser() == 0)
373       sys::fs::changeFileOwnership(FD, Status.getUser(), Status.getGroup());
374 #endif
375 
376     sys::fs::perms Perm = Status.permissions();
377     if (OutputFilename != InputFilename)
378       Perm = static_cast<sys::fs::perms>(Perm & ~sys::fs::getUmask() & ~06000);
379 #ifdef _WIN32
380     if (std::error_code EC = sys::fs::setPermissions(OutputFilename, Perm))
381 #else
382     if (std::error_code EC = sys::fs::setPermissions(FD, Perm))
383 #endif
384       return createFileError(OutputFilename, EC);
385   }
386 
387   if (std::error_code EC = sys::Process::SafelyCloseFileDescriptor(FD))
388     return createFileError(OutputFilename, EC);
389 
390   return Error::success();
391 }
392 
393 char llvm::AtomicFileWriteError::ID;
394