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 | |
| 42 | bool IsInterestingCoverageFile(const std::string &FileName); |
| 43 | |
| 44 | void DupAndCloseStderr(); |
| 45 | |
| 46 | void CloseStdout(); |
| 47 | |
| 48 | void Printf(const char *Fmt, ...); |
| 49 | |
| 50 | // Print using raw syscalls, useful when printing at early init stages. |
| 51 | void RawPrint(const char *Str); |
| 52 | |
| 53 | // Platform specific functions: |
| 54 | bool IsFile(const std::string &Path); |
kcc | dc00cd3 | 2017-08-29 20:51:24 +0000 | [diff] [blame] | 55 | size_t FileSize(const std::string &Path); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 56 | |
| 57 | void ListFilesInDirRecursive(const std::string &Dir, long *Epoch, |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 58 | Vector<std::string> *V, bool TopDir); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 59 | |
kcc | 7f5f222 | 2017-09-12 21:58:07 +0000 | [diff] [blame] | 60 | struct SizedFile { |
| 61 | std::string File; |
| 62 | size_t Size; |
| 63 | bool operator<(const SizedFile &B) const { return Size < B.Size; } |
| 64 | }; |
| 65 | |
| 66 | void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V); |
| 67 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 68 | char GetSeparator(); |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 69 | // 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] | 70 | std::string Basename(const std::string &Path); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 71 | |
| 72 | FILE* OpenFile(int Fd, const char *Mode); |
| 73 | |
| 74 | int CloseFile(int Fd); |
| 75 | |
| 76 | int DuplicateFile(int Fd); |
| 77 | |
| 78 | void RemoveFile(const std::string &Path); |
| 79 | |
| 80 | void DiscardOutput(int Fd); |
| 81 | |
| 82 | intptr_t GetHandleFromFd(int fd); |
| 83 | |
kcc | 243006d | 2019-02-12 00:12:33 +0000 | [diff] [blame] | 84 | void MkDir(const std::string &Path); |
| 85 | void RmDir(const std::string &Path); |
| 86 | void RmFilesInDir(const std::string &Path); |
| 87 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 88 | } // namespace fuzzer |
| 89 | |
| 90 | #endif // LLVM_FUZZER_IO_H |