119 lines · c
1//===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===//2//3// 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-exception6//7//===----------------------------------------------------------------------===//8// IO interface.9//===----------------------------------------------------------------------===//10 11#ifndef LLVM_FUZZER_IO_H12#define LLVM_FUZZER_IO_H13 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 uint8_t *Data, size_t Size, const std::string &Path);28// Write Data.c_str() to the file without terminating null character.29void WriteToFile(const std::string &Data, const std::string &Path);30void WriteToFile(const Unit &U, const std::string &Path);31 32void AppendToFile(const uint8_t *Data, size_t Size, const std::string &Path);33void AppendToFile(const std::string &Data, const std::string &Path);34 35void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, long *Epoch,36 size_t MaxSize, bool ExitOnError,37 std::vector<std::string> *VPaths = 0);38 39// Returns "Dir/FileName" or equivalent for the current OS.40std::string DirPlusFile(const std::string &DirPath,41 const std::string &FileName);42 43// Returns the name of the dir, similar to the 'dirname' utility.44std::string DirName(const std::string &FileName);45 46// Returns path to a TmpDir.47std::string TmpDir();48 49std::string TempPath(const char *Prefix, const char *Extension);50 51bool IsInterestingCoverageFile(const std::string &FileName);52 53void DupAndCloseStderr();54 55void CloseStdout();56 57// For testing.58FILE *GetOutputFile();59void SetOutputFile(FILE *NewOutputFile);60 61void Puts(const char *Str);62void Printf(const char *Fmt, ...);63void VPrintf(bool Verbose, const char *Fmt, ...);64 65// Print using raw syscalls, useful when printing at early init stages.66void RawPrint(const char *Str);67 68// Platform specific functions:69bool IsFile(const std::string &Path);70bool IsDirectory(const std::string &Path);71size_t FileSize(const std::string &Path);72 73void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,74 std::vector<std::string> *V, bool TopDir);75 76bool MkDirRecursive(const std::string &Dir);77void RmDirRecursive(const std::string &Dir);78 79// Iterate files and dirs inside Dir, recursively.80// Call DirPreCallback/DirPostCallback on dirs before/after81// calling FileCallback on files.82void IterateDirRecursive(const std::string &Dir,83 void (*DirPreCallback)(const std::string &Dir),84 void (*DirPostCallback)(const std::string &Dir),85 void (*FileCallback)(const std::string &Dir));86 87struct SizedFile {88 std::string File;89 size_t Size;90 bool operator<(const SizedFile &B) const { return Size < B.Size; }91};92 93void GetSizedFilesFromDir(const std::string &Dir, std::vector<SizedFile> *V);94 95char GetSeparator();96bool IsSeparator(char C);97// Similar to the basename utility: returns the file name w/o the dir prefix.98std::string Basename(const std::string &Path);99 100FILE* OpenFile(int Fd, const char *Mode);101 102int CloseFile(int Fd);103 104int DuplicateFile(int Fd);105 106void RemoveFile(const std::string &Path);107void RenameFile(const std::string &OldPath, const std::string &NewPath);108 109intptr_t GetHandleFromFd(int fd);110 111void MkDir(const std::string &Path);112void RmDir(const std::string &Path);113 114const std::string &getDevNull();115 116} // namespace fuzzer117 118#endif // LLVM_FUZZER_IO_H119