blob: 1d25389770fa27f38735fad7894d2189e494fa72 [file] [log] [blame]
Aaron Ballmanef116982015-01-29 16:58:29 +00001//===- FuzzerIO.cpp - IO utils. -------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// IO functions.
10//===----------------------------------------------------------------------===//
11#include "FuzzerInternal.h"
Kostya Serebryany5b266a82015-02-04 19:10:20 +000012#include <iterator>
Aaron Ballmanef116982015-01-29 16:58:29 +000013#include <fstream>
14#include <dirent.h>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000015#include <sys/types.h>
16#include <sys/stat.h>
17#include <unistd.h>
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000018#include <cstdio>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000019
Aaron Ballmanef116982015-01-29 16:58:29 +000020namespace fuzzer {
21
Kostya Serebryany1ac80552015-05-08 21:30:55 +000022static long GetEpoch(const std::string &Path) {
23 struct stat St;
Kostya Serebryany86e4a3e2015-07-18 00:03:37 +000024 if (stat(Path.c_str(), &St)) {
25 Printf("Can not stat: %s; exiting\n", Path.c_str());
26 exit(1);
27 }
Kostya Serebryany1ac80552015-05-08 21:30:55 +000028 return St.st_mtime;
29}
30
31static std::vector<std::string> ListFilesInDir(const std::string &Dir,
32 long *Epoch) {
Aaron Ballmanef116982015-01-29 16:58:29 +000033 std::vector<std::string> V;
Kostya Serebryany1ac80552015-05-08 21:30:55 +000034 if (Epoch) {
35 auto E = GetEpoch(Dir.c_str());
36 if (*Epoch >= E) return V;
37 *Epoch = E;
38 }
Aaron Ballmanef116982015-01-29 16:58:29 +000039 DIR *D = opendir(Dir.c_str());
Kostya Serebryany86e4a3e2015-07-18 00:03:37 +000040 if (!D) {
41 Printf("No such directory: %s; exiting\n", Dir.c_str());
42 exit(1);
43 }
Aaron Ballmanef116982015-01-29 16:58:29 +000044 while (auto E = readdir(D)) {
45 if (E->d_type == DT_REG || E->d_type == DT_LNK)
46 V.push_back(E->d_name);
47 }
48 closedir(D);
49 return V;
50}
51
52Unit FileToVector(const std::string &Path) {
53 std::ifstream T(Path);
54 return Unit((std::istreambuf_iterator<char>(T)),
55 std::istreambuf_iterator<char>());
56}
57
Kostya Serebryany52a788e2015-03-31 20:13:20 +000058std::string FileToString(const std::string &Path) {
59 std::ifstream T(Path);
60 return std::string((std::istreambuf_iterator<char>(T)),
61 std::istreambuf_iterator<char>());
62}
63
Kostya Serebryany5b266a82015-02-04 19:10:20 +000064void CopyFileToErr(const std::string &Path) {
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000065 Printf("%s", FileToString(Path).c_str());
Kostya Serebryany5b266a82015-02-04 19:10:20 +000066}
67
Aaron Ballmanef116982015-01-29 16:58:29 +000068void WriteToFile(const Unit &U, const std::string &Path) {
69 std::ofstream OF(Path);
70 OF.write((const char*)U.data(), U.size());
71}
72
Kostya Serebryany1ac80552015-05-08 21:30:55 +000073void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
74 long *Epoch) {
75 long E = Epoch ? *Epoch : 0;
76 for (auto &X : ListFilesInDir(Path, Epoch)) {
77 auto FilePath = DirPlusFile(Path, X);
78 if (Epoch && GetEpoch(FilePath) < E) continue;
79 V->push_back(FileToVector(FilePath));
80 }
Aaron Ballmanef116982015-01-29 16:58:29 +000081}
82
83std::string DirPlusFile(const std::string &DirPath,
84 const std::string &FileName) {
85 return DirPath + "/" + FileName;
86}
87
Kostya Serebryanyca6a2a22015-05-05 21:59:51 +000088void PrintFileAsBase64(const std::string &Path) {
89 std::string Cmd = "base64 -w 0 < " + Path + "; echo";
Kostya Serebryany2da7b842015-05-18 21:34:20 +000090 ExecuteCommand(Cmd);
Kostya Serebryanyca6a2a22015-05-05 21:59:51 +000091}
92
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000093void Printf(const char *Fmt, ...) {
94 va_list ap;
95 va_start(ap, Fmt);
96 vfprintf(stderr, Fmt, ap);
97 va_end(ap);
98}
99
Aaron Ballmanef116982015-01-29 16:58:29 +0000100} // namespace fuzzer