1 //===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===//
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 // 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 uint8_t *Data, size_t Size, const std::string &Path);
28 // Write Data.c_str() to the file without terminating null character.
29 void WriteToFile(const std::string &Data, const std::string &Path);
30 void WriteToFile(const Unit &U, const std::string &Path);
31 
32 void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V,
33                             long *Epoch, size_t MaxSize, bool ExitOnError);
34 
35 // Returns "Dir/FileName" or equivalent for the current OS.
36 std::string DirPlusFile(const std::string &DirPath,
37                         const std::string &FileName);
38 
39 // Returns the name of the dir, similar to the 'dirname' utility.
40 std::string DirName(const std::string &FileName);
41 
42 // Returns path to a TmpDir.
43 std::string TmpDir();
44 
45 std::string TempPath(const char *Extension);
46 
47 bool IsInterestingCoverageFile(const std::string &FileName);
48 
49 void DupAndCloseStderr();
50 
51 void CloseStdout();
52 
53 void Printf(const char *Fmt, ...);
54 void VPrintf(bool Verbose, const char *Fmt, ...);
55 
56 // Print using raw syscalls, useful when printing at early init stages.
57 void RawPrint(const char *Str);
58 
59 // Platform specific functions:
60 bool IsFile(const std::string &Path);
61 size_t FileSize(const std::string &Path);
62 
63 void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
64                              Vector<std::string> *V, bool TopDir);
65 
66 void RmDirRecursive(const std::string &Dir);
67 
68 // Iterate files and dirs inside Dir, recursively.
69 // Call DirPreCallback/DirPostCallback on dirs before/after
70 // calling FileCallback on files.
71 void IterateDirRecursive(const std::string &Dir,
72                          void (*DirPreCallback)(const std::string &Dir),
73                          void (*DirPostCallback)(const std::string &Dir),
74                          void (*FileCallback)(const std::string &Dir));
75 
76 struct SizedFile {
77   std::string File;
78   size_t Size;
79   bool operator<(const SizedFile &B) const { return Size < B.Size; }
80 };
81 
82 void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V);
83 
84 char GetSeparator();
85 // Similar to the basename utility: returns the file name w/o the dir prefix.
86 std::string Basename(const std::string &Path);
87 
88 FILE* OpenFile(int Fd, const char *Mode);
89 
90 int CloseFile(int Fd);
91 
92 int DuplicateFile(int Fd);
93 
94 void RemoveFile(const std::string &Path);
95 void RenameFile(const std::string &OldPath, const std::string &NewPath);
96 
97 intptr_t GetHandleFromFd(int fd);
98 
99 void MkDir(const std::string &Path);
100 void RmDir(const std::string &Path);
101 
102 const std::string &getDevNull();
103 
104 }  // namespace fuzzer
105 
106 #endif  // LLVM_FUZZER_IO_H
107