blob: 043fad396d51e958f7021fc7df2694c623948597 [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 Serebryany2a48c242015-11-13 01:54:40 +000018#include <cstdarg>
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000019#include <cstdio>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000020
Aaron Ballmanef116982015-01-29 16:58:29 +000021namespace fuzzer {
22
Kostya Serebryany1ac80552015-05-08 21:30:55 +000023static long GetEpoch(const std::string &Path) {
24 struct stat St;
Kostya Serebryany9cdea942015-09-08 20:36:33 +000025 if (stat(Path.c_str(), &St))
26 return 0; // Can't stat, be conservative.
Kostya Serebryany1ac80552015-05-08 21:30:55 +000027 return St.st_mtime;
28}
29
30static std::vector<std::string> ListFilesInDir(const std::string &Dir,
31 long *Epoch) {
Aaron Ballmanef116982015-01-29 16:58:29 +000032 std::vector<std::string> V;
Kostya Serebryany1ac80552015-05-08 21:30:55 +000033 if (Epoch) {
Kostya Serebryany06c199a2015-08-26 21:55:19 +000034 auto E = GetEpoch(Dir);
Kostya Serebryany1ac80552015-05-08 21:30:55 +000035 if (*Epoch >= E) return V;
36 *Epoch = E;
37 }
Aaron Ballmanef116982015-01-29 16:58:29 +000038 DIR *D = opendir(Dir.c_str());
Kostya Serebryany86e4a3e2015-07-18 00:03:37 +000039 if (!D) {
40 Printf("No such directory: %s; exiting\n", Dir.c_str());
41 exit(1);
42 }
Aaron Ballmanef116982015-01-29 16:58:29 +000043 while (auto E = readdir(D)) {
44 if (E->d_type == DT_REG || E->d_type == DT_LNK)
45 V.push_back(E->d_name);
46 }
47 closedir(D);
48 return V;
49}
50
51Unit FileToVector(const std::string &Path) {
52 std::ifstream T(Path);
Kostya Serebryanyb91c62b2015-10-16 22:41:47 +000053 if (!T) {
54 Printf("No such directory: %s; exiting\n", Path.c_str());
55 exit(1);
56 }
Aaron Ballmanef116982015-01-29 16:58:29 +000057 return Unit((std::istreambuf_iterator<char>(T)),
58 std::istreambuf_iterator<char>());
59}
60
Kostya Serebryany52a788e2015-03-31 20:13:20 +000061std::string FileToString(const std::string &Path) {
62 std::ifstream T(Path);
63 return std::string((std::istreambuf_iterator<char>(T)),
64 std::istreambuf_iterator<char>());
65}
66
Kostya Serebryany5b266a82015-02-04 19:10:20 +000067void CopyFileToErr(const std::string &Path) {
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000068 Printf("%s", FileToString(Path).c_str());
Kostya Serebryany5b266a82015-02-04 19:10:20 +000069}
70
Aaron Ballmanef116982015-01-29 16:58:29 +000071void WriteToFile(const Unit &U, const std::string &Path) {
Kostya Serebryanyac25eeb2015-08-12 00:55:09 +000072 // Use raw C interface because this function may be called from a sig handler.
73 FILE *Out = fopen(Path.c_str(), "w");
74 if (!Out) return;
75 fwrite(U.data(), sizeof(U[0]), U.size(), Out);
76 fclose(Out);
Aaron Ballmanef116982015-01-29 16:58:29 +000077}
78
Kostya Serebryany1ac80552015-05-08 21:30:55 +000079void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
80 long *Epoch) {
81 long E = Epoch ? *Epoch : 0;
82 for (auto &X : ListFilesInDir(Path, Epoch)) {
83 auto FilePath = DirPlusFile(Path, X);
84 if (Epoch && GetEpoch(FilePath) < E) continue;
85 V->push_back(FileToVector(FilePath));
86 }
Aaron Ballmanef116982015-01-29 16:58:29 +000087}
88
89std::string DirPlusFile(const std::string &DirPath,
90 const std::string &FileName) {
91 return DirPath + "/" + FileName;
92}
93
Kostya Serebryany20e9bcb2015-05-23 01:07:46 +000094void Printf(const char *Fmt, ...) {
95 va_list ap;
96 va_start(ap, Fmt);
97 vfprintf(stderr, Fmt, ap);
98 va_end(ap);
99}
100
Aaron Ballmanef116982015-01-29 16:58:29 +0000101} // namespace fuzzer