1 //===- FuzzerIO.cpp - IO utils. -------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
11 #include "FuzzerInternal.h"
16 #include <sys/types.h>
22 static long GetEpoch(const std::string &Path) {
24 if (stat(Path.c_str(), &St)) return 0;
28 static std::vector<std::string> ListFilesInDir(const std::string &Dir,
30 std::vector<std::string> V;
32 auto E = GetEpoch(Dir.c_str());
33 if (*Epoch >= E) return V;
36 DIR *D = opendir(Dir.c_str());
38 while (auto E = readdir(D)) {
39 if (E->d_type == DT_REG || E->d_type == DT_LNK)
40 V.push_back(E->d_name);
46 Unit FileToVector(const std::string &Path) {
47 std::ifstream T(Path);
48 return Unit((std::istreambuf_iterator<char>(T)),
49 std::istreambuf_iterator<char>());
52 std::string FileToString(const std::string &Path) {
53 std::ifstream T(Path);
54 return std::string((std::istreambuf_iterator<char>(T)),
55 std::istreambuf_iterator<char>());
58 void CopyFileToErr(const std::string &Path) {
59 std::ifstream T(Path);
60 std::copy(std::istreambuf_iterator<char>(T), std::istreambuf_iterator<char>(),
61 std::ostream_iterator<char>(std::cerr, ""));
64 void WriteToFile(const Unit &U, const std::string &Path) {
65 std::ofstream OF(Path);
66 OF.write((const char*)U.data(), U.size());
69 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
71 long E = Epoch ? *Epoch : 0;
72 for (auto &X : ListFilesInDir(Path, Epoch)) {
73 auto FilePath = DirPlusFile(Path, X);
74 if (Epoch && GetEpoch(FilePath) < E) continue;
75 V->push_back(FileToVector(FilePath));
79 std::string DirPlusFile(const std::string &DirPath,
80 const std::string &FileName) {
81 return DirPath + "/" + FileName;
84 void PrintFileAsBase64(const std::string &Path) {
85 std::string Cmd = "base64 -w 0 < " + Path + "; echo";