Zachary Turner | 5abac17 | 2016-11-30 21:44:26 +0000 | [diff] [blame^] | 1 | //===- FuzzerIOPosix.cpp - IO utils for Posix. ----------------------------===// |
| 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 implementation using Posix API. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include "FuzzerDefs.h" |
| 13 | #if LIBFUZZER_POSIX |
| 14 | #include "FuzzerExtFunctions.h" |
| 15 | #include "FuzzerIO.h" |
| 16 | #include <cstdarg> |
| 17 | #include <cstdio> |
| 18 | #include <dirent.h> |
| 19 | #include <fstream> |
| 20 | #include <iterator> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | namespace fuzzer { |
| 26 | |
| 27 | bool IsFile(const std::string &Path) { |
| 28 | struct stat St; |
| 29 | if (stat(Path.c_str(), &St)) |
| 30 | return false; |
| 31 | return S_ISREG(St.st_mode); |
| 32 | } |
| 33 | |
| 34 | void ListFilesInDirRecursive(const std::string &Dir, long *Epoch, |
| 35 | std::vector<std::string> *V, bool TopDir) { |
| 36 | auto E = GetEpoch(Dir); |
| 37 | if (Epoch) |
| 38 | if (E && *Epoch >= E) return; |
| 39 | |
| 40 | DIR *D = opendir(Dir.c_str()); |
| 41 | if (!D) { |
| 42 | Printf("No such directory: %s; exiting\n", Dir.c_str()); |
| 43 | exit(1); |
| 44 | } |
| 45 | while (auto E = readdir(D)) { |
| 46 | std::string Path = DirPlusFile(Dir, E->d_name); |
| 47 | if (E->d_type == DT_REG || E->d_type == DT_LNK) |
| 48 | V->push_back(Path); |
| 49 | else if (E->d_type == DT_DIR && *E->d_name != '.') |
| 50 | ListFilesInDirRecursive(Path, Epoch, V, false); |
| 51 | } |
| 52 | closedir(D); |
| 53 | if (Epoch && TopDir) |
| 54 | *Epoch = E; |
| 55 | } |
| 56 | |
| 57 | char GetSeparator() { |
| 58 | return '/'; |
| 59 | } |
| 60 | |
| 61 | FILE* OpenFile(int Fd, const char* Mode) { |
| 62 | return fdopen(Fd, Mode); |
| 63 | } |
| 64 | |
| 65 | int CloseFile(int fd) { |
| 66 | return close(fd); |
| 67 | } |
| 68 | |
| 69 | int DuplicateFile(int Fd) { |
| 70 | return dup(Fd); |
| 71 | } |
| 72 | |
| 73 | void DeleteFile(const std::string &Path) { |
| 74 | unlink(Path.c_str()); |
| 75 | } |
| 76 | |
| 77 | } // namespace fuzzer |
| 78 | #endif // LIBFUZZER_POSIX |