2015-01-30 00:58:29 +08:00
|
|
|
//===- FuzzerIO.cpp - IO utils. -------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IO functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "FuzzerInternal.h"
|
2015-02-05 03:10:20 +08:00
|
|
|
#include <iterator>
|
2015-01-30 00:58:29 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <dirent.h>
|
2015-05-09 05:30:55 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2015-05-23 09:07:46 +08:00
|
|
|
#include <cstdio>
|
2015-05-09 05:30:55 +08:00
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
namespace fuzzer {
|
|
|
|
|
2015-05-09 05:30:55 +08:00
|
|
|
static long GetEpoch(const std::string &Path) {
|
|
|
|
struct stat St;
|
2015-07-18 08:03:37 +08:00
|
|
|
if (stat(Path.c_str(), &St)) {
|
|
|
|
Printf("Can not stat: %s; exiting\n", Path.c_str());
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-05-09 05:30:55 +08:00
|
|
|
return St.st_mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<std::string> ListFilesInDir(const std::string &Dir,
|
|
|
|
long *Epoch) {
|
2015-01-30 00:58:29 +08:00
|
|
|
std::vector<std::string> V;
|
2015-05-09 05:30:55 +08:00
|
|
|
if (Epoch) {
|
|
|
|
auto E = GetEpoch(Dir.c_str());
|
|
|
|
if (*Epoch >= E) return V;
|
|
|
|
*Epoch = E;
|
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
DIR *D = opendir(Dir.c_str());
|
2015-07-18 08:03:37 +08:00
|
|
|
if (!D) {
|
|
|
|
Printf("No such directory: %s; exiting\n", Dir.c_str());
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
while (auto E = readdir(D)) {
|
|
|
|
if (E->d_type == DT_REG || E->d_type == DT_LNK)
|
|
|
|
V.push_back(E->d_name);
|
|
|
|
}
|
|
|
|
closedir(D);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unit FileToVector(const std::string &Path) {
|
|
|
|
std::ifstream T(Path);
|
|
|
|
return Unit((std::istreambuf_iterator<char>(T)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
}
|
|
|
|
|
2015-04-01 04:13:20 +08:00
|
|
|
std::string FileToString(const std::string &Path) {
|
|
|
|
std::ifstream T(Path);
|
|
|
|
return std::string((std::istreambuf_iterator<char>(T)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
}
|
|
|
|
|
2015-02-05 03:10:20 +08:00
|
|
|
void CopyFileToErr(const std::string &Path) {
|
2015-05-23 09:07:46 +08:00
|
|
|
Printf("%s", FileToString(Path).c_str());
|
2015-02-05 03:10:20 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
void WriteToFile(const Unit &U, const std::string &Path) {
|
|
|
|
std::ofstream OF(Path);
|
|
|
|
OF.write((const char*)U.data(), U.size());
|
|
|
|
}
|
|
|
|
|
2015-05-09 05:30:55 +08:00
|
|
|
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
|
|
|
|
long *Epoch) {
|
|
|
|
long E = Epoch ? *Epoch : 0;
|
|
|
|
for (auto &X : ListFilesInDir(Path, Epoch)) {
|
|
|
|
auto FilePath = DirPlusFile(Path, X);
|
|
|
|
if (Epoch && GetEpoch(FilePath) < E) continue;
|
|
|
|
V->push_back(FileToVector(FilePath));
|
|
|
|
}
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DirPlusFile(const std::string &DirPath,
|
|
|
|
const std::string &FileName) {
|
|
|
|
return DirPath + "/" + FileName;
|
|
|
|
}
|
|
|
|
|
2015-05-06 05:59:51 +08:00
|
|
|
void PrintFileAsBase64(const std::string &Path) {
|
|
|
|
std::string Cmd = "base64 -w 0 < " + Path + "; echo";
|
2015-05-19 05:34:20 +08:00
|
|
|
ExecuteCommand(Cmd);
|
2015-05-06 05:59:51 +08:00
|
|
|
}
|
|
|
|
|
2015-05-23 09:07:46 +08:00
|
|
|
void Printf(const char *Fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, Fmt);
|
|
|
|
vfprintf(stderr, Fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
} // namespace fuzzer
|