Zachary Turner | 5abac17 | 2016-11-30 21:44:26 +0000 | [diff] [blame] | 1 | //===- FuzzerIOWindows.cpp - IO utils for Windows. ------------------------===// |
| 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 for Windows. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include "FuzzerDefs.h" |
| 13 | #if LIBFUZZER_WINDOWS |
| 14 | #include "FuzzerExtFunctions.h" |
| 15 | #include "FuzzerIO.h" |
| 16 | #include <cstdarg> |
| 17 | #include <cstdio> |
| 18 | #include <fstream> |
| 19 | #include <io.h> |
| 20 | #include <iterator> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <windows.h> |
| 24 | |
| 25 | namespace fuzzer { |
| 26 | |
| 27 | static bool IsFile(const std::string &Path, const DWORD &FileAttributes) { |
| 28 | |
| 29 | if (FileAttributes & FILE_ATTRIBUTE_NORMAL) |
| 30 | return true; |
| 31 | |
| 32 | if (FileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 33 | return false; |
| 34 | |
| 35 | HANDLE FileHandle( |
| 36 | CreateFileA(Path.c_str(), 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 37 | FILE_FLAG_BACKUP_SEMANTICS, 0)); |
| 38 | |
| 39 | if (FileHandle == INVALID_HANDLE_VALUE) { |
| 40 | Printf("CreateFileA() failed for \"%s\" (Error code: %lu).\n", Path.c_str(), |
| 41 | GetLastError()); |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | DWORD FileType = GetFileType(FileHandle); |
| 46 | |
| 47 | if (FileType == FILE_TYPE_UNKNOWN) { |
| 48 | Printf("GetFileType() failed for \"%s\" (Error code: %lu).\n", Path.c_str(), |
| 49 | GetLastError()); |
| 50 | CloseHandle(FileHandle); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | if (FileType != FILE_TYPE_DISK) { |
| 55 | CloseHandle(FileHandle); |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | CloseHandle(FileHandle); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool IsFile(const std::string &Path) { |
| 64 | DWORD Att = GetFileAttributesA(Path.c_str()); |
| 65 | |
| 66 | if (Att == INVALID_FILE_ATTRIBUTES) { |
| 67 | Printf("GetFileAttributesA() failed for \"%s\" (Error code: %lu).\n", |
| 68 | Path.c_str(), GetLastError()); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | return IsFile(Path, Att); |
| 73 | } |
| 74 | |
| 75 | void ListFilesInDirRecursive(const std::string &Dir, long *Epoch, |
| 76 | std::vector<std::string> *V, bool TopDir) { |
| 77 | auto E = GetEpoch(Dir); |
| 78 | if (Epoch) |
| 79 | if (E && *Epoch >= E) return; |
| 80 | |
| 81 | std::string Path(Dir); |
| 82 | assert(!Path.empty()); |
| 83 | if (Path.back() != '\\') |
| 84 | Path.push_back('\\'); |
| 85 | Path.push_back('*'); |
| 86 | |
| 87 | // Get the first directory entry. |
| 88 | WIN32_FIND_DATAA FindInfo; |
| 89 | HANDLE FindHandle(FindFirstFileA(Path.c_str(), &FindInfo)); |
| 90 | if (FindHandle == INVALID_HANDLE_VALUE) |
| 91 | { |
| 92 | Printf("No file found in: %s.\n", Dir.c_str()); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | do { |
| 97 | std::string FileName = DirPlusFile(Dir, FindInfo.cFileName); |
| 98 | |
| 99 | if (FindInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 100 | size_t FilenameLen = strlen(FindInfo.cFileName); |
| 101 | if ((FilenameLen == 1 && FindInfo.cFileName[0] == '.') || |
| 102 | (FilenameLen == 2 && FindInfo.cFileName[0] == '.' && |
| 103 | FindInfo.cFileName[1] == '.')) |
| 104 | continue; |
| 105 | |
| 106 | ListFilesInDirRecursive(FileName, Epoch, V, false); |
| 107 | } |
| 108 | else if (IsFile(FileName, FindInfo.dwFileAttributes)) |
| 109 | V->push_back(FileName); |
| 110 | } while (FindNextFileA(FindHandle, &FindInfo)); |
| 111 | |
| 112 | DWORD LastError = GetLastError(); |
| 113 | if (LastError != ERROR_NO_MORE_FILES) |
| 114 | Printf("FindNextFileA failed (Error code: %lu).\n", LastError); |
| 115 | |
| 116 | FindClose(FindHandle); |
| 117 | |
| 118 | if (Epoch && TopDir) |
| 119 | *Epoch = E; |
| 120 | } |
| 121 | |
| 122 | char GetSeparator() { |
| 123 | return '\\'; |
| 124 | } |
| 125 | |
| 126 | FILE* OpenFile(int Fd, const char* Mode) { |
| 127 | return _fdopen(Fd, Mode); |
| 128 | } |
| 129 | |
| 130 | int CloseFile(int Fd) { |
| 131 | return _close(Fd); |
| 132 | } |
| 133 | |
| 134 | int DuplicateFile(int Fd) { |
| 135 | return _dup(Fd); |
| 136 | } |
| 137 | |
| 138 | void DeleteFile(const std::string &Path) { |
| 139 | _unlink(Path.c_str()); |
| 140 | } |
| 141 | |
Kostya Serebryany | 1cba0a9 | 2016-11-30 21:53:32 +0000 | [diff] [blame^] | 142 | std::string DirName(const std::string &FileName) { |
| 143 | assert(0 && "Unimplemented"); |
| 144 | } |
| 145 | |
Zachary Turner | 5abac17 | 2016-11-30 21:44:26 +0000 | [diff] [blame] | 146 | } // namespace fuzzer |
| 147 | #endif // LIBFUZZER_WINDOWS |