blob: cbfafa5c38b32c3e73f68719c532d165a09d8fc0 [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
kcc6f1e9bc2019-04-13 00:20:31 +000027void WriteToFile(const uint8_t *Data, size_t Size, const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000028void 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
kcc2e6ca5c2019-02-12 22:48:55 +000043std::string TempPath(const char *Extension);
44
george.karpenkov29efa6d2017-08-21 23:25:50 +000045bool IsInterestingCoverageFile(const std::string &FileName);
46
47void DupAndCloseStderr();
48
49void CloseStdout();
50
51void Printf(const char *Fmt, ...);
kccbfb59752019-02-12 03:12:40 +000052void VPrintf(bool Verbose, const char *Fmt, ...);
george.karpenkov29efa6d2017-08-21 23:25:50 +000053
54// Print using raw syscalls, useful when printing at early init stages.
55void RawPrint(const char *Str);
56
57// Platform specific functions:
58bool IsFile(const std::string &Path);
kccdc00cd32017-08-29 20:51:24 +000059size_t FileSize(const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000060
61void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
george.karpenkovfbfa45c2017-08-27 23:20:09 +000062 Vector<std::string> *V, bool TopDir);
george.karpenkov29efa6d2017-08-21 23:25:50 +000063
kcc64bcb922019-02-13 04:04:45 +000064void RmDirRecursive(const std::string &Dir);
65
66// Iterate files and dirs inside Dir, recursively.
67// Call DirPreCallback/DirPostCallback on dirs before/after
68// calling FileCallback on files.
kcc55e54ed2019-02-15 21:51:15 +000069void IterateDirRecursive(const std::string &Dir,
kcc64bcb922019-02-13 04:04:45 +000070 void (*DirPreCallback)(const std::string &Dir),
71 void (*DirPostCallback)(const std::string &Dir),
72 void (*FileCallback)(const std::string &Dir));
73
kcc7f5f2222017-09-12 21:58:07 +000074struct SizedFile {
75 std::string File;
76 size_t Size;
77 bool operator<(const SizedFile &B) const { return Size < B.Size; }
78};
79
80void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V);
81
george.karpenkov29efa6d2017-08-21 23:25:50 +000082char GetSeparator();
kcc86e43882018-06-06 01:23:29 +000083// Similar to the basename utility: returns the file name w/o the dir prefix.
morehouse68f46432018-08-30 15:54:44 +000084std::string Basename(const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000085
86FILE* OpenFile(int Fd, const char *Mode);
87
88int CloseFile(int Fd);
89
90int DuplicateFile(int Fd);
91
92void RemoveFile(const std::string &Path);
kcc6f1e9bc2019-04-13 00:20:31 +000093void RenameFile(const std::string &OldPath, const std::string &NewPath);
george.karpenkov29efa6d2017-08-21 23:25:50 +000094
95void DiscardOutput(int Fd);
96
97intptr_t GetHandleFromFd(int fd);
98
kcc243006d2019-02-12 00:12:33 +000099void MkDir(const std::string &Path);
100void RmDir(const std::string &Path);
kcc243006d2019-02-12 00:12:33 +0000101
metzmane847d8a2019-02-27 19:27:16 +0000102const std::string &getDevNull();
103
george.karpenkov29efa6d2017-08-21 23:25:50 +0000104} // namespace fuzzer
105
106#endif // LLVM_FUZZER_IO_H