blob: b4a68190e78083f72914c3f92686ca431c85d5fe [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// IO interface.
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_FUZZER_IO_H
13#define LLVM_FUZZER_IO_H
14
15#include "FuzzerDefs.h"
16
17namespace fuzzer {
18
19long GetEpoch(const std::string &Path);
20
21Unit FileToVector(const std::string &Path, size_t MaxSize = 0,
22 bool ExitOnError = true);
23
24std::string FileToString(const std::string &Path);
25
26void CopyFileToErr(const std::string &Path);
27
28void WriteToFile(const Unit &U, const std::string &Path);
29
george.karpenkovfbfa45c2017-08-27 23:20:09 +000030void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V,
george.karpenkov29efa6d2017-08-21 23:25:50 +000031 long *Epoch, size_t MaxSize, bool ExitOnError);
32
33// Returns "Dir/FileName" or equivalent for the current OS.
34std::string DirPlusFile(const std::string &DirPath,
35 const std::string &FileName);
36
37// Returns the name of the dir, similar to the 'dirname' utility.
38std::string DirName(const std::string &FileName);
39
40// Returns path to a TmpDir.
41std::string TmpDir();
42
43bool IsInterestingCoverageFile(const std::string &FileName);
44
45void DupAndCloseStderr();
46
47void CloseStdout();
48
49void Printf(const char *Fmt, ...);
50
51// Print using raw syscalls, useful when printing at early init stages.
52void RawPrint(const char *Str);
53
54// Platform specific functions:
55bool IsFile(const std::string &Path);
kccdc00cd32017-08-29 20:51:24 +000056size_t FileSize(const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000057
58void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
george.karpenkovfbfa45c2017-08-27 23:20:09 +000059 Vector<std::string> *V, bool TopDir);
george.karpenkov29efa6d2017-08-21 23:25:50 +000060
kcc7f5f2222017-09-12 21:58:07 +000061struct SizedFile {
62 std::string File;
63 size_t Size;
64 bool operator<(const SizedFile &B) const { return Size < B.Size; }
65};
66
67void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V);
68
george.karpenkov29efa6d2017-08-21 23:25:50 +000069char GetSeparator();
kcc86e43882018-06-06 01:23:29 +000070// Similar to the basename utility: returns the file name w/o the dir prefix.
morehouse40c623a2018-08-29 18:08:34 +000071std::string Basename(const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000072
73FILE* OpenFile(int Fd, const char *Mode);
74
75int CloseFile(int Fd);
76
77int DuplicateFile(int Fd);
78
79void RemoveFile(const std::string &Path);
80
81void DiscardOutput(int Fd);
82
83intptr_t GetHandleFromFd(int fd);
84
85} // namespace fuzzer
86
87#endif // LLVM_FUZZER_IO_H