Path: blob/main/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerIO.h
35262 views
//===- FuzzerIO.h - Internal header for IO utils ----------------*- C++ -* ===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// IO interface.8//===----------------------------------------------------------------------===//910#ifndef LLVM_FUZZER_IO_H11#define LLVM_FUZZER_IO_H1213#include "FuzzerDefs.h"1415namespace fuzzer {1617long GetEpoch(const std::string &Path);1819Unit FileToVector(const std::string &Path, size_t MaxSize = 0,20bool ExitOnError = true);2122std::string FileToString(const std::string &Path);2324void CopyFileToErr(const std::string &Path);2526void WriteToFile(const uint8_t *Data, size_t Size, const std::string &Path);27// Write Data.c_str() to the file without terminating null character.28void WriteToFile(const std::string &Data, const std::string &Path);29void WriteToFile(const Unit &U, const std::string &Path);3031void AppendToFile(const uint8_t *Data, size_t Size, const std::string &Path);32void AppendToFile(const std::string &Data, const std::string &Path);3334void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, long *Epoch,35size_t MaxSize, bool ExitOnError,36std::vector<std::string> *VPaths = 0);3738// Returns "Dir/FileName" or equivalent for the current OS.39std::string DirPlusFile(const std::string &DirPath,40const std::string &FileName);4142// Returns the name of the dir, similar to the 'dirname' utility.43std::string DirName(const std::string &FileName);4445// Returns path to a TmpDir.46std::string TmpDir();4748std::string TempPath(const char *Prefix, const char *Extension);4950bool IsInterestingCoverageFile(const std::string &FileName);5152void DupAndCloseStderr();5354void CloseStdout();5556// For testing.57FILE *GetOutputFile();58void SetOutputFile(FILE *NewOutputFile);5960void Puts(const char *Str);61void Printf(const char *Fmt, ...);62void VPrintf(bool Verbose, const char *Fmt, ...);6364// Print using raw syscalls, useful when printing at early init stages.65void RawPrint(const char *Str);6667// Platform specific functions:68bool IsFile(const std::string &Path);69bool IsDirectory(const std::string &Path);70size_t FileSize(const std::string &Path);7172void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,73std::vector<std::string> *V, bool TopDir);7475bool MkDirRecursive(const std::string &Dir);76void RmDirRecursive(const std::string &Dir);7778// Iterate files and dirs inside Dir, recursively.79// Call DirPreCallback/DirPostCallback on dirs before/after80// calling FileCallback on files.81void IterateDirRecursive(const std::string &Dir,82void (*DirPreCallback)(const std::string &Dir),83void (*DirPostCallback)(const std::string &Dir),84void (*FileCallback)(const std::string &Dir));8586struct SizedFile {87std::string File;88size_t Size;89bool operator<(const SizedFile &B) const { return Size < B.Size; }90};9192void GetSizedFilesFromDir(const std::string &Dir, std::vector<SizedFile> *V);9394char GetSeparator();95bool IsSeparator(char C);96// Similar to the basename utility: returns the file name w/o the dir prefix.97std::string Basename(const std::string &Path);9899FILE* OpenFile(int Fd, const char *Mode);100101int CloseFile(int Fd);102103int DuplicateFile(int Fd);104105void RemoveFile(const std::string &Path);106void RenameFile(const std::string &OldPath, const std::string &NewPath);107108intptr_t GetHandleFromFd(int fd);109110void MkDir(const std::string &Path);111void RmDir(const std::string &Path);112113const std::string &getDevNull();114115} // namespace fuzzer116117#endif // LLVM_FUZZER_IO_H118119120