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