blob: b31aea9ded7365f71ea8ccb95730cd5dde95b9f5 [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===//
2//
chandlerc40284492019-01-19 08:50:56 +00003// 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.karpenkov29efa6d2017-08-21 23:25:50 +00006//
7//===----------------------------------------------------------------------===//
8// IO interface.
9//===----------------------------------------------------------------------===//
10
11#ifndef LLVM_FUZZER_IO_H
12#define LLVM_FUZZER_IO_H
13
14#include "FuzzerDefs.h"
15
16namespace fuzzer {
17
18long GetEpoch(const std::string &Path);
19
20Unit FileToVector(const std::string &Path, size_t MaxSize = 0,
21 bool ExitOnError = true);
22
23std::string FileToString(const std::string &Path);
24
25void CopyFileToErr(const std::string &Path);
26
27void WriteToFile(const Unit &U, const std::string &Path);
28
george.karpenkovfbfa45c2017-08-27 23:20:09 +000029void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V,
george.karpenkov29efa6d2017-08-21 23:25:50 +000030 long *Epoch, size_t MaxSize, bool ExitOnError);
31
32// Returns "Dir/FileName" or equivalent for the current OS.
33std::string DirPlusFile(const std::string &DirPath,
34 const std::string &FileName);
35
36// Returns the name of the dir, similar to the 'dirname' utility.
37std::string DirName(const std::string &FileName);
38
39// Returns path to a TmpDir.
40std::string TmpDir();
41
42bool IsInterestingCoverageFile(const std::string &FileName);
43
44void DupAndCloseStderr();
45
46void CloseStdout();
47
48void Printf(const char *Fmt, ...);
49
50// Print using raw syscalls, useful when printing at early init stages.
51void RawPrint(const char *Str);
52
53// Platform specific functions:
54bool IsFile(const std::string &Path);
kccdc00cd32017-08-29 20:51:24 +000055size_t FileSize(const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000056
57void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
george.karpenkovfbfa45c2017-08-27 23:20:09 +000058 Vector<std::string> *V, bool TopDir);
george.karpenkov29efa6d2017-08-21 23:25:50 +000059
kcc7f5f2222017-09-12 21:58:07 +000060struct SizedFile {
61 std::string File;
62 size_t Size;
63 bool operator<(const SizedFile &B) const { return Size < B.Size; }
64};
65
66void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V);
67
george.karpenkov29efa6d2017-08-21 23:25:50 +000068char GetSeparator();
kcc86e43882018-06-06 01:23:29 +000069// Similar to the basename utility: returns the file name w/o the dir prefix.
morehouse68f46432018-08-30 15:54:44 +000070std::string Basename(const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000071
72FILE* OpenFile(int Fd, const char *Mode);
73
74int CloseFile(int Fd);
75
76int DuplicateFile(int Fd);
77
78void RemoveFile(const std::string &Path);
79
80void DiscardOutput(int Fd);
81
82intptr_t GetHandleFromFd(int fd);
83
kcc243006d2019-02-12 00:12:33 +000084void MkDir(const std::string &Path);
85void RmDir(const std::string &Path);
86void RmFilesInDir(const std::string &Path);
87
george.karpenkov29efa6d2017-08-21 23:25:50 +000088} // namespace fuzzer
89
90#endif // LLVM_FUZZER_IO_H