george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===// |
| 2 | // |
chandlerc | 4028449 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // IO interface. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef LLVM_FUZZER_IO_H |
| 12 | #define LLVM_FUZZER_IO_H |
| 13 | |
| 14 | #include "FuzzerDefs.h" |
| 15 | |
| 16 | namespace fuzzer { |
| 17 | |
| 18 | long GetEpoch(const std::string &Path); |
| 19 | |
| 20 | Unit FileToVector(const std::string &Path, size_t MaxSize = 0, |
| 21 | bool ExitOnError = true); |
| 22 | |
| 23 | std::string FileToString(const std::string &Path); |
| 24 | |
| 25 | void CopyFileToErr(const std::string &Path); |
| 26 | |
| 27 | void WriteToFile(const Unit &U, const std::string &Path); |
| 28 | |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 29 | void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V, |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 30 | long *Epoch, size_t MaxSize, bool ExitOnError); |
| 31 | |
| 32 | // Returns "Dir/FileName" or equivalent for the current OS. |
| 33 | std::string DirPlusFile(const std::string &DirPath, |
| 34 | const std::string &FileName); |
| 35 | |
| 36 | // Returns the name of the dir, similar to the 'dirname' utility. |
| 37 | std::string DirName(const std::string &FileName); |
| 38 | |
| 39 | // Returns path to a TmpDir. |
| 40 | std::string TmpDir(); |
| 41 | |
kcc | 2e6ca5c | 2019-02-12 22:48:55 +0000 | [diff] [blame] | 42 | std::string TempPath(const char *Extension); |
| 43 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 44 | bool IsInterestingCoverageFile(const std::string &FileName); |
| 45 | |
| 46 | void DupAndCloseStderr(); |
| 47 | |
| 48 | void CloseStdout(); |
| 49 | |
| 50 | void Printf(const char *Fmt, ...); |
kcc | bfb5975 | 2019-02-12 03:12:40 +0000 | [diff] [blame] | 51 | void VPrintf(bool Verbose, const char *Fmt, ...); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 52 | |
| 53 | // Print using raw syscalls, useful when printing at early init stages. |
| 54 | void RawPrint(const char *Str); |
| 55 | |
| 56 | // Platform specific functions: |
| 57 | bool IsFile(const std::string &Path); |
kcc | dc00cd3 | 2017-08-29 20:51:24 +0000 | [diff] [blame] | 58 | size_t FileSize(const std::string &Path); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 59 | |
| 60 | void ListFilesInDirRecursive(const std::string &Dir, long *Epoch, |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 61 | Vector<std::string> *V, bool TopDir); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 62 | |
kcc | 64bcb92 | 2019-02-13 04:04:45 +0000 | [diff] [blame^] | 63 | void RmDirRecursive(const std::string &Dir); |
| 64 | |
| 65 | // Iterate files and dirs inside Dir, recursively. |
| 66 | // Call DirPreCallback/DirPostCallback on dirs before/after |
| 67 | // calling FileCallback on files. |
| 68 | void IterateDirRecurisve(const std::string &Dir, |
| 69 | void (*DirPreCallback)(const std::string &Dir), |
| 70 | void (*DirPostCallback)(const std::string &Dir), |
| 71 | void (*FileCallback)(const std::string &Dir)); |
| 72 | |
kcc | 7f5f222 | 2017-09-12 21:58:07 +0000 | [diff] [blame] | 73 | struct SizedFile { |
| 74 | std::string File; |
| 75 | size_t Size; |
| 76 | bool operator<(const SizedFile &B) const { return Size < B.Size; } |
| 77 | }; |
| 78 | |
| 79 | void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V); |
| 80 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 81 | char GetSeparator(); |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 82 | // Similar to the basename utility: returns the file name w/o the dir prefix. |
morehouse | 68f4643 | 2018-08-30 15:54:44 +0000 | [diff] [blame] | 83 | std::string Basename(const std::string &Path); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 84 | |
| 85 | FILE* OpenFile(int Fd, const char *Mode); |
| 86 | |
| 87 | int CloseFile(int Fd); |
| 88 | |
| 89 | int DuplicateFile(int Fd); |
| 90 | |
| 91 | void RemoveFile(const std::string &Path); |
| 92 | |
| 93 | void DiscardOutput(int Fd); |
| 94 | |
| 95 | intptr_t GetHandleFromFd(int fd); |
| 96 | |
kcc | 243006d | 2019-02-12 00:12:33 +0000 | [diff] [blame] | 97 | void MkDir(const std::string &Path); |
| 98 | void RmDir(const std::string &Path); |
kcc | 243006d | 2019-02-12 00:12:33 +0000 | [diff] [blame] | 99 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 100 | } // namespace fuzzer |
| 101 | |
| 102 | #endif // LLVM_FUZZER_IO_H |