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