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