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 <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 ///
177 int llvm::DiffFilesWithTolerance(StringRef NameA,
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.
183   ErrorOr<std::unique_ptr<MemoryBuffer>> F1OrErr = MemoryBuffer::getFile(NameA);
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 
191   ErrorOr<std::unique_ptr<MemoryBuffer>> F2OrErr = MemoryBuffer::getFile(NameB);
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 
269 void llvm::AtomicFileWriteError::log(raw_ostream &OS) const {
270   OS << "atomic_write_error: ";
271   switch (Error) {
272   case atomic_write_error::failed_to_create_uniq_file:
273     OS << "failed_to_create_uniq_file";
274     return;
275   case atomic_write_error::output_stream_error:
276     OS << "output_stream_error";
277     return;
278   case atomic_write_error::failed_to_rename_temp_file:
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 
286 llvm::Error llvm::writeFileAtomically(StringRef TempPathModel,
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 
295 llvm::Error llvm::writeFileAtomically(
296     StringRef TempPathModel, StringRef FinalPath,
297     std::function<llvm::Error(llvm::raw_ostream &)> Writer) {
298   SmallString<128> GeneratedUniqPath;
299   int TempFD;
300   if (sys::fs::createUniqueFile(TempPathModel, TempFD, GeneratedUniqPath)) {
301     return llvm::make_error<AtomicFileWriteError>(
302         atomic_write_error::failed_to_create_uniq_file);
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>(
315         atomic_write_error::output_stream_error);
316   }
317 
318   if (sys::fs::rename(/*from=*/GeneratedUniqPath, /*to=*/FinalPath)) {
319     return llvm::make_error<AtomicFileWriteError>(
320         atomic_write_error::failed_to_rename_temp_file);
321   }
322 
323   RemoveTmpFileOnFail.releaseFile();
324   return Error::success();
325 }
326 
327 Expected<FilePermissionsApplier>
328 FilePermissionsApplier::create(StringRef InputFilename) {
329   sys::fs::file_status Status;
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 
341 Error FilePermissionsApplier::apply(
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,
357                                                      sys::fs::CD_OpenExisting))
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 
392 char llvm::AtomicFileWriteError::ID;
393