1 //===- FuzzerIOPosix.cpp - IO utils for Posix. ----------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 // IO functions implementation using Posix API.
9 //===----------------------------------------------------------------------===//
10 #include "FuzzerDefs.h"
11 #if LIBFUZZER_POSIX || LIBFUZZER_FUCHSIA
13 #include "FuzzerExtFunctions.h"
22 #include <sys/types.h>
27 bool IsFile(const std::string &Path) {
29 if (stat(Path.c_str(), &St))
31 return S_ISREG(St.st_mode);
34 static bool IsDirectory(const std::string &Path) {
36 if (stat(Path.c_str(), &St))
38 return S_ISDIR(St.st_mode);
41 size_t FileSize(const std::string &Path) {
43 if (stat(Path.c_str(), &St))
48 std::string Basename(const std::string &Path) {
49 size_t Pos = Path.rfind(GetSeparator());
50 if (Pos == std::string::npos) return Path;
51 assert(Pos < Path.size());
52 return Path.substr(Pos + 1);
55 void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
56 Vector<std::string> *V, bool TopDir) {
57 auto E = GetEpoch(Dir);
59 if (E && *Epoch >= E) return;
61 DIR *D = opendir(Dir.c_str());
63 Printf("%s: %s; exiting\n", strerror(errno), Dir.c_str());
66 while (auto E = readdir(D)) {
67 std::string Path = DirPlusFile(Dir, E->d_name);
68 if (E->d_type == DT_REG || E->d_type == DT_LNK ||
69 (E->d_type == DT_UNKNOWN && IsFile(Path)))
71 else if ((E->d_type == DT_DIR ||
72 (E->d_type == DT_UNKNOWN && IsDirectory(Path))) &&
74 ListFilesInDirRecursive(Path, Epoch, V, false);
82 void IterateDirRecursive(const std::string &Dir,
83 void (*DirPreCallback)(const std::string &Dir),
84 void (*DirPostCallback)(const std::string &Dir),
85 void (*FileCallback)(const std::string &Dir)) {
87 DIR *D = opendir(Dir.c_str());
89 while (auto E = readdir(D)) {
90 std::string Path = DirPlusFile(Dir, E->d_name);
91 if (E->d_type == DT_REG || E->d_type == DT_LNK ||
92 (E->d_type == DT_UNKNOWN && IsFile(Path)))
94 else if ((E->d_type == DT_DIR ||
95 (E->d_type == DT_UNKNOWN && IsDirectory(Path))) &&
97 IterateDirRecursive(Path, DirPreCallback, DirPostCallback, FileCallback);
100 DirPostCallback(Dir);
103 char GetSeparator() {
107 FILE* OpenFile(int Fd, const char* Mode) {
108 return fdopen(Fd, Mode);
111 int CloseFile(int fd) {
115 int DuplicateFile(int Fd) {
119 void RemoveFile(const std::string &Path) {
120 unlink(Path.c_str());
123 void RenameFile(const std::string &OldPath, const std::string &NewPath) {
124 rename(OldPath.c_str(), NewPath.c_str());
127 intptr_t GetHandleFromFd(int fd) {
128 return static_cast<intptr_t>(fd);
131 std::string DirName(const std::string &FileName) {
132 char *Tmp = new char[FileName.size() + 1];
133 memcpy(Tmp, FileName.c_str(), FileName.size() + 1);
134 std::string Res = dirname(Tmp);
139 std::string TmpDir() {
140 if (auto Env = getenv("TMPDIR"))
145 bool IsInterestingCoverageFile(const std::string &FileName) {
146 if (FileName.find("compiler-rt/lib/") != std::string::npos)
147 return false; // sanitizer internal.
148 if (FileName.find("/usr/lib/") != std::string::npos)
150 if (FileName.find("/usr/include/") != std::string::npos)
152 if (FileName == "<null>")
157 void RawPrint(const char *Str) {
158 write(2, Str, strlen(Str));
161 void MkDir(const std::string &Path) {
162 mkdir(Path.c_str(), 0700);
165 void RmDir(const std::string &Path) {
169 const std::string &getDevNull() {
170 static const std::string devNull = "/dev/null";
174 } // namespace fuzzer
176 #endif // LIBFUZZER_POSIX