blob: c6636c8681832fc318877871e089fc77fb5ba936 [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 <iostream>
13#include <iterator>
Aaron Ballmanef116982015-01-29 16:58:29 +000014#include <fstream>
15#include <dirent.h>
Kostya Serebryany1ac80552015-05-08 21:30:55 +000016#include <sys/types.h>
17#include <sys/stat.h>
18#include <unistd.h>
19
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;
24 if (stat(Path.c_str(), &St)) return 0;
25 return St.st_mtime;
26}
27
28static std::vector<std::string> ListFilesInDir(const std::string &Dir,
29 long *Epoch) {
Aaron Ballmanef116982015-01-29 16:58:29 +000030 std::vector<std::string> V;
Kostya Serebryany1ac80552015-05-08 21:30:55 +000031 if (Epoch) {
32 auto E = GetEpoch(Dir.c_str());
33 if (*Epoch >= E) return V;
34 *Epoch = E;
35 }
Aaron Ballmanef116982015-01-29 16:58:29 +000036 DIR *D = opendir(Dir.c_str());
37 if (!D) return V;
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);
41 }
42 closedir(D);
43 return V;
44}
45
46Unit FileToVector(const std::string &Path) {
47 std::ifstream T(Path);
48 return Unit((std::istreambuf_iterator<char>(T)),
49 std::istreambuf_iterator<char>());
50}
51
Kostya Serebryany52a788e2015-03-31 20:13:20 +000052std::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>());
56}
57
Kostya Serebryany5b266a82015-02-04 19:10:20 +000058void 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, ""));
62}
63
Aaron Ballmanef116982015-01-29 16:58:29 +000064void WriteToFile(const Unit &U, const std::string &Path) {
65 std::ofstream OF(Path);
66 OF.write((const char*)U.data(), U.size());
67}
68
Kostya Serebryany1ac80552015-05-08 21:30:55 +000069void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
70 long *Epoch) {
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));
76 }
Aaron Ballmanef116982015-01-29 16:58:29 +000077}
78
79std::string DirPlusFile(const std::string &DirPath,
80 const std::string &FileName) {
81 return DirPath + "/" + FileName;
82}
83
Kostya Serebryanyca6a2a22015-05-05 21:59:51 +000084void PrintFileAsBase64(const std::string &Path) {
85 std::string Cmd = "base64 -w 0 < " + Path + "; echo";
Kostya Serebryany2da7b842015-05-18 21:34:20 +000086 ExecuteCommand(Cmd);
Kostya Serebryanyca6a2a22015-05-05 21:59:51 +000087}
88
Aaron Ballmanef116982015-01-29 16:58:29 +000089} // namespace fuzzer