f0295e9a730875aa545c50fb361bf6879631d554
[oota-llvm.git] / lib / Fuzzer / FuzzerIO.cpp
1 //===- 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"
12 #include <iterator>
13 #include <fstream>
14 #include <dirent.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <cstdio>
19
20 namespace fuzzer {
21
22 static long GetEpoch(const std::string &Path) {
23   struct stat St;
24   if (stat(Path.c_str(), &St)) {
25     Printf("Can not stat: %s; exiting\n", Path.c_str());
26     exit(1);
27   }
28   return St.st_mtime;
29 }
30
31 static std::vector<std::string> ListFilesInDir(const std::string &Dir,
32                                                long *Epoch) {
33   std::vector<std::string> V;
34   if (Epoch) {
35     auto E = GetEpoch(Dir.c_str());
36     if (*Epoch >= E) return V;
37     *Epoch = E;
38   }
39   DIR *D = opendir(Dir.c_str());
40   if (!D) {
41     Printf("No such directory: %s; exiting\n", Dir.c_str());
42     exit(1);
43   }
44   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
52 Unit 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
58 std::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
64 void CopyFileToErr(const std::string &Path) {
65   Printf("%s", FileToString(Path).c_str());
66 }
67
68 void WriteToFile(const Unit &U, const std::string &Path) {
69   // Use raw C interface because this function may be called from a sig handler.
70   FILE *Out = fopen(Path.c_str(), "w");
71   if (!Out) return;
72   fwrite(U.data(), sizeof(U[0]), U.size(), Out);
73   fclose(Out);
74 }
75
76 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
77                             long *Epoch) {
78   long E = Epoch ? *Epoch : 0;
79   for (auto &X : ListFilesInDir(Path, Epoch)) {
80     auto FilePath = DirPlusFile(Path, X);
81     if (Epoch && GetEpoch(FilePath) < E) continue;
82     V->push_back(FileToVector(FilePath));
83   }
84 }
85
86 std::string DirPlusFile(const std::string &DirPath,
87                         const std::string &FileName) {
88   return DirPath + "/" + FileName;
89 }
90
91 void PrintFileAsBase64(const std::string &Path) {
92   std::string Cmd = "base64 -w 0 < " + Path + "; echo";
93   ExecuteCommand(Cmd);
94 }
95
96 void Printf(const char *Fmt, ...) {
97   va_list ap;
98   va_start(ap, Fmt);
99   vfprintf(stderr, Fmt, ap);
100   va_end(ap);
101 }
102
103 }  // namespace fuzzer