13cab2bb3Spatrick //===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===// 23cab2bb3Spatrick // 33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information. 53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 63cab2bb3Spatrick // 73cab2bb3Spatrick //===----------------------------------------------------------------------===// 83cab2bb3Spatrick // IO interface. 93cab2bb3Spatrick //===----------------------------------------------------------------------===// 103cab2bb3Spatrick 113cab2bb3Spatrick #ifndef LLVM_FUZZER_IO_H 123cab2bb3Spatrick #define LLVM_FUZZER_IO_H 133cab2bb3Spatrick 143cab2bb3Spatrick #include "FuzzerDefs.h" 153cab2bb3Spatrick 163cab2bb3Spatrick namespace fuzzer { 173cab2bb3Spatrick 183cab2bb3Spatrick long GetEpoch(const std::string &Path); 193cab2bb3Spatrick 203cab2bb3Spatrick Unit FileToVector(const std::string &Path, size_t MaxSize = 0, 213cab2bb3Spatrick bool ExitOnError = true); 223cab2bb3Spatrick 233cab2bb3Spatrick std::string FileToString(const std::string &Path); 243cab2bb3Spatrick 253cab2bb3Spatrick void CopyFileToErr(const std::string &Path); 263cab2bb3Spatrick 273cab2bb3Spatrick void WriteToFile(const uint8_t *Data, size_t Size, const std::string &Path); 283cab2bb3Spatrick // Write Data.c_str() to the file without terminating null character. 293cab2bb3Spatrick void WriteToFile(const std::string &Data, const std::string &Path); 303cab2bb3Spatrick void WriteToFile(const Unit &U, const std::string &Path); 313cab2bb3Spatrick 32d89ec533Spatrick void AppendToFile(const uint8_t *Data, size_t Size, const std::string &Path); 33d89ec533Spatrick void AppendToFile(const std::string &Data, const std::string &Path); 34d89ec533Spatrick 35*810390e3Srobert void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, long *Epoch, 36d89ec533Spatrick size_t MaxSize, bool ExitOnError, 37*810390e3Srobert std::vector<std::string> *VPaths = 0); 383cab2bb3Spatrick 393cab2bb3Spatrick // Returns "Dir/FileName" or equivalent for the current OS. 403cab2bb3Spatrick std::string DirPlusFile(const std::string &DirPath, 413cab2bb3Spatrick const std::string &FileName); 423cab2bb3Spatrick 433cab2bb3Spatrick // Returns the name of the dir, similar to the 'dirname' utility. 443cab2bb3Spatrick std::string DirName(const std::string &FileName); 453cab2bb3Spatrick 463cab2bb3Spatrick // Returns path to a TmpDir. 473cab2bb3Spatrick std::string TmpDir(); 483cab2bb3Spatrick 491f9cb04fSpatrick std::string TempPath(const char *Prefix, const char *Extension); 503cab2bb3Spatrick 513cab2bb3Spatrick bool IsInterestingCoverageFile(const std::string &FileName); 523cab2bb3Spatrick 533cab2bb3Spatrick void DupAndCloseStderr(); 543cab2bb3Spatrick 553cab2bb3Spatrick void CloseStdout(); 563cab2bb3Spatrick 57*810390e3Srobert // For testing. 58*810390e3Srobert FILE *GetOutputFile(); 59*810390e3Srobert void SetOutputFile(FILE *NewOutputFile); 60*810390e3Srobert 613cab2bb3Spatrick void Printf(const char *Fmt, ...); 623cab2bb3Spatrick void VPrintf(bool Verbose, const char *Fmt, ...); 633cab2bb3Spatrick 643cab2bb3Spatrick // Print using raw syscalls, useful when printing at early init stages. 653cab2bb3Spatrick void RawPrint(const char *Str); 663cab2bb3Spatrick 673cab2bb3Spatrick // Platform specific functions: 683cab2bb3Spatrick bool IsFile(const std::string &Path); 69d89ec533Spatrick bool IsDirectory(const std::string &Path); 703cab2bb3Spatrick size_t FileSize(const std::string &Path); 713cab2bb3Spatrick 723cab2bb3Spatrick void ListFilesInDirRecursive(const std::string &Dir, long *Epoch, 73*810390e3Srobert std::vector<std::string> *V, bool TopDir); 743cab2bb3Spatrick 75d89ec533Spatrick bool MkDirRecursive(const std::string &Dir); 763cab2bb3Spatrick void RmDirRecursive(const std::string &Dir); 773cab2bb3Spatrick 783cab2bb3Spatrick // Iterate files and dirs inside Dir, recursively. 793cab2bb3Spatrick // Call DirPreCallback/DirPostCallback on dirs before/after 803cab2bb3Spatrick // calling FileCallback on files. 813cab2bb3Spatrick void IterateDirRecursive(const std::string &Dir, 823cab2bb3Spatrick void (*DirPreCallback)(const std::string &Dir), 833cab2bb3Spatrick void (*DirPostCallback)(const std::string &Dir), 843cab2bb3Spatrick void (*FileCallback)(const std::string &Dir)); 853cab2bb3Spatrick 863cab2bb3Spatrick struct SizedFile { 873cab2bb3Spatrick std::string File; 883cab2bb3Spatrick size_t Size; 893cab2bb3Spatrick bool operator<(const SizedFile &B) const { return Size < B.Size; } 903cab2bb3Spatrick }; 913cab2bb3Spatrick 92*810390e3Srobert void GetSizedFilesFromDir(const std::string &Dir, std::vector<SizedFile> *V); 933cab2bb3Spatrick 943cab2bb3Spatrick char GetSeparator(); 95d89ec533Spatrick bool IsSeparator(char C); 963cab2bb3Spatrick // Similar to the basename utility: returns the file name w/o the dir prefix. 973cab2bb3Spatrick std::string Basename(const std::string &Path); 983cab2bb3Spatrick 993cab2bb3Spatrick FILE* OpenFile(int Fd, const char *Mode); 1003cab2bb3Spatrick 1013cab2bb3Spatrick int CloseFile(int Fd); 1023cab2bb3Spatrick 1033cab2bb3Spatrick int DuplicateFile(int Fd); 1043cab2bb3Spatrick 1053cab2bb3Spatrick void RemoveFile(const std::string &Path); 1063cab2bb3Spatrick void RenameFile(const std::string &OldPath, const std::string &NewPath); 1073cab2bb3Spatrick 1083cab2bb3Spatrick intptr_t GetHandleFromFd(int fd); 1093cab2bb3Spatrick 1103cab2bb3Spatrick void MkDir(const std::string &Path); 1113cab2bb3Spatrick void RmDir(const std::string &Path); 1123cab2bb3Spatrick 1133cab2bb3Spatrick const std::string &getDevNull(); 1143cab2bb3Spatrick 1153cab2bb3Spatrick } // namespace fuzzer 1163cab2bb3Spatrick 1173cab2bb3Spatrick #endif // LLVM_FUZZER_IO_H 118