blob: fe0d7b451758d029eb94060d618eacb33afc6afc [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);
metzmane9b95bc2019-04-30 20:56:18 +000028// Write Data.c_str() to the file without terminating null character.
29void WriteToFile(const std::string &Data, const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000030void WriteToFile(const Unit &U, const std::string &Path);
31
george.karpenkovfbfa45c2017-08-27 23:20:09 +000032void ReadDirToVectorOfUnits(const char *Path, Vector<Unit> *V,
george.karpenkov29efa6d2017-08-21 23:25:50 +000033 long *Epoch, size_t MaxSize, bool ExitOnError);
34
35// Returns "Dir/FileName" or equivalent for the current OS.
36std::string DirPlusFile(const std::string &DirPath,
37 const std::string &FileName);
38
39// Returns the name of the dir, similar to the 'dirname' utility.
40std::string DirName(const std::string &FileName);
41
42// Returns path to a TmpDir.
43std::string TmpDir();
44
kcc2e6ca5c2019-02-12 22:48:55 +000045std::string TempPath(const char *Extension);
46
george.karpenkov29efa6d2017-08-21 23:25:50 +000047bool IsInterestingCoverageFile(const std::string &FileName);
48
49void DupAndCloseStderr();
50
51void CloseStdout();
52
53void Printf(const char *Fmt, ...);
kccbfb59752019-02-12 03:12:40 +000054void VPrintf(bool Verbose, const char *Fmt, ...);
george.karpenkov29efa6d2017-08-21 23:25:50 +000055
56// Print using raw syscalls, useful when printing at early init stages.
57void RawPrint(const char *Str);
58
59// Platform specific functions:
60bool IsFile(const std::string &Path);
kccdc00cd32017-08-29 20:51:24 +000061size_t FileSize(const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000062
63void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
george.karpenkovfbfa45c2017-08-27 23:20:09 +000064 Vector<std::string> *V, bool TopDir);
george.karpenkov29efa6d2017-08-21 23:25:50 +000065
kcc64bcb922019-02-13 04:04:45 +000066void 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.
kcc55e54ed2019-02-15 21:51:15 +000071void IterateDirRecursive(const std::string &Dir,
kcc64bcb922019-02-13 04:04:45 +000072 void (*DirPreCallback)(const std::string &Dir),
73 void (*DirPostCallback)(const std::string &Dir),
74 void (*FileCallback)(const std::string &Dir));
75
kcc7f5f2222017-09-12 21:58:07 +000076struct SizedFile {
77 std::string File;
78 size_t Size;
79 bool operator<(const SizedFile &B) const { return Size < B.Size; }
80};
81
82void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V);
83
george.karpenkov29efa6d2017-08-21 23:25:50 +000084char GetSeparator();
kcc86e43882018-06-06 01:23:29 +000085// Similar to the basename utility: returns the file name w/o the dir prefix.
morehouse68f46432018-08-30 15:54:44 +000086std::string Basename(const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000087
88FILE* OpenFile(int Fd, const char *Mode);
89
90int CloseFile(int Fd);
91
92int DuplicateFile(int Fd);
93
94void RemoveFile(const std::string &Path);
kcc6f1e9bc2019-04-13 00:20:31 +000095void RenameFile(const std::string &OldPath, const std::string &NewPath);
george.karpenkov29efa6d2017-08-21 23:25:50 +000096
97void DiscardOutput(int Fd);
98
99intptr_t GetHandleFromFd(int fd);
100
kcc243006d2019-02-12 00:12:33 +0000101void MkDir(const std::string &Path);
102void RmDir(const std::string &Path);
kcc243006d2019-02-12 00:12:33 +0000103
metzmane847d8a2019-02-27 19:27:16 +0000104const std::string &getDevNull();
105
george.karpenkov29efa6d2017-08-21 23:25:50 +0000106} // namespace fuzzer
107
108#endif // LLVM_FUZZER_IO_H