blob: 9d849bed69339e463892ccfebd83e72faf3dc4fb [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
kcc2e6ca5c2019-02-12 22:48:55 +000042std::string TempPath(const char *Extension);
43
george.karpenkov29efa6d2017-08-21 23:25:50 +000044bool IsInterestingCoverageFile(const std::string &FileName);
45
46void DupAndCloseStderr();
47
48void CloseStdout();
49
50void Printf(const char *Fmt, ...);
kccbfb59752019-02-12 03:12:40 +000051void VPrintf(bool Verbose, const char *Fmt, ...);
george.karpenkov29efa6d2017-08-21 23:25:50 +000052
53// Print using raw syscalls, useful when printing at early init stages.
54void RawPrint(const char *Str);
55
56// Platform specific functions:
57bool IsFile(const std::string &Path);
kccdc00cd32017-08-29 20:51:24 +000058size_t FileSize(const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000059
60void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
george.karpenkovfbfa45c2017-08-27 23:20:09 +000061 Vector<std::string> *V, bool TopDir);
george.karpenkov29efa6d2017-08-21 23:25:50 +000062
kcc7f5f2222017-09-12 21:58:07 +000063struct SizedFile {
64 std::string File;
65 size_t Size;
66 bool operator<(const SizedFile &B) const { return Size < B.Size; }
67};
68
69void GetSizedFilesFromDir(const std::string &Dir, Vector<SizedFile> *V);
70
george.karpenkov29efa6d2017-08-21 23:25:50 +000071char GetSeparator();
kcc86e43882018-06-06 01:23:29 +000072// Similar to the basename utility: returns the file name w/o the dir prefix.
morehouse68f46432018-08-30 15:54:44 +000073std::string Basename(const std::string &Path);
george.karpenkov29efa6d2017-08-21 23:25:50 +000074
75FILE* OpenFile(int Fd, const char *Mode);
76
77int CloseFile(int Fd);
78
79int DuplicateFile(int Fd);
80
81void RemoveFile(const std::string &Path);
82
83void DiscardOutput(int Fd);
84
85intptr_t GetHandleFromFd(int fd);
86
kcc243006d2019-02-12 00:12:33 +000087void MkDir(const std::string &Path);
88void RmDir(const std::string &Path);
89void RmFilesInDir(const std::string &Path);
90
george.karpenkov29efa6d2017-08-21 23:25:50 +000091} // namespace fuzzer
92
93#endif // LLVM_FUZZER_IO_H