Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 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" |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 12 | #include <iterator> |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 13 | #include <fstream> |
| 14 | #include <dirent.h> |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <unistd.h> |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame^] | 18 | #include <cstdio> |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 19 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 20 | namespace fuzzer { |
| 21 | |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 22 | static 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 | |
| 28 | static std::vector<std::string> ListFilesInDir(const std::string &Dir, |
| 29 | long *Epoch) { |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 30 | std::vector<std::string> V; |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 31 | if (Epoch) { |
| 32 | auto E = GetEpoch(Dir.c_str()); |
| 33 | if (*Epoch >= E) return V; |
| 34 | *Epoch = E; |
| 35 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 36 | 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 | |
| 46 | Unit 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 Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 52 | std::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 Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 58 | void CopyFileToErr(const std::string &Path) { |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame^] | 59 | Printf("%s", FileToString(Path).c_str()); |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 62 | void WriteToFile(const Unit &U, const std::string &Path) { |
| 63 | std::ofstream OF(Path); |
| 64 | OF.write((const char*)U.data(), U.size()); |
| 65 | } |
| 66 | |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 67 | void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, |
| 68 | long *Epoch) { |
| 69 | long E = Epoch ? *Epoch : 0; |
| 70 | for (auto &X : ListFilesInDir(Path, Epoch)) { |
| 71 | auto FilePath = DirPlusFile(Path, X); |
| 72 | if (Epoch && GetEpoch(FilePath) < E) continue; |
| 73 | V->push_back(FileToVector(FilePath)); |
| 74 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | std::string DirPlusFile(const std::string &DirPath, |
| 78 | const std::string &FileName) { |
| 79 | return DirPath + "/" + FileName; |
| 80 | } |
| 81 | |
Kostya Serebryany | ca6a2a2 | 2015-05-05 21:59:51 +0000 | [diff] [blame] | 82 | void PrintFileAsBase64(const std::string &Path) { |
| 83 | std::string Cmd = "base64 -w 0 < " + Path + "; echo"; |
Kostya Serebryany | 2da7b84 | 2015-05-18 21:34:20 +0000 | [diff] [blame] | 84 | ExecuteCommand(Cmd); |
Kostya Serebryany | ca6a2a2 | 2015-05-05 21:59:51 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame^] | 87 | void Printf(const char *Fmt, ...) { |
| 88 | va_list ap; |
| 89 | va_start(ap, Fmt); |
| 90 | vfprintf(stderr, Fmt, ap); |
| 91 | va_end(ap); |
| 92 | } |
| 93 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 94 | } // namespace fuzzer |