2014-12-02 22:16:48 +01:00
|
|
|
#include "FuseReadDirTest.h"
|
|
|
|
|
2015-06-18 19:30:52 +02:00
|
|
|
using cpputils::make_unique_ref;
|
2014-12-02 22:16:48 +01:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
using ::testing::Action;
|
|
|
|
using ::testing::Return;
|
|
|
|
|
2019-10-13 17:49:57 +02:00
|
|
|
vector<string> FuseReadDirTest::ReadDir(const char *dirname) {
|
2014-12-02 22:16:48 +01:00
|
|
|
auto fs = TestFS();
|
|
|
|
|
|
|
|
DIR *dir = openDir(fs.get(), dirname);
|
|
|
|
|
2019-10-13 17:49:57 +02:00
|
|
|
vector<string> result;
|
|
|
|
readDirEntries(dir, &result);
|
2014-12-02 22:16:48 +01:00
|
|
|
closeDir(dir);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FuseReadDirTest::ReadDirReturnError(const char *dirname) {
|
|
|
|
auto fs = TestFS();
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
DIR *dir = openDirAllowError(fs.get(), dirname);
|
|
|
|
EXPECT_EQ(errno!=0, dir==nullptr) << "errno should exactly be != 0 if opendir returned nullptr";
|
|
|
|
if (errno != 0) {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2015-06-18 19:30:52 +02:00
|
|
|
auto result = make_unique_ref<vector<string>>();
|
2014-12-02 22:16:48 +01:00
|
|
|
int error = readDirEntriesAllowError(dir, result.get());
|
|
|
|
closeDir(dir);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR *FuseReadDirTest::openDir(TempTestFS *fs, const char *dirname) {
|
|
|
|
DIR *dir = openDirAllowError(fs, dirname);
|
|
|
|
EXPECT_NE(nullptr, dir) << "Opening directory failed";
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR *FuseReadDirTest::openDirAllowError(TempTestFS *fs, const char *dirname) {
|
|
|
|
auto realpath = fs->mountDir() / dirname;
|
2018-05-21 01:20:38 +02:00
|
|
|
return ::opendir(realpath.string().c_str());
|
2014-12-02 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FuseReadDirTest::readDirEntries(DIR *dir, vector<string> *result) {
|
|
|
|
int error = readDirEntriesAllowError(dir, result);
|
|
|
|
EXPECT_EQ(0, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int FuseReadDirTest::readDirEntriesAllowError(DIR *dir, vector<string> *result) {
|
|
|
|
struct dirent *entry = nullptr;
|
|
|
|
int error = readNextDirEntryAllowError(dir, &entry);
|
|
|
|
if (error != 0) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
while(entry != nullptr) {
|
|
|
|
result->push_back(entry->d_name);
|
|
|
|
int error = readNextDirEntryAllowError(dir, &entry);
|
|
|
|
if (error != 0) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FuseReadDirTest::readNextDirEntryAllowError(DIR *dir, struct dirent **result) {
|
|
|
|
errno = 0;
|
|
|
|
*result = ::readdir(dir);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FuseReadDirTest::closeDir(DIR *dir) {
|
|
|
|
int retval = ::closedir(dir);
|
|
|
|
EXPECT_EQ(0, retval) << "Closing dir failed";
|
|
|
|
}
|
|
|
|
|
2019-10-13 17:49:57 +02:00
|
|
|
Action<vector<fspp::Dir::Entry>(const boost::filesystem::path&)> FuseReadDirTest::ReturnDirEntries(vector<std::string> entries) {
|
|
|
|
vector<fspp::Dir::Entry> direntries(entries.size(), fspp::Dir::Entry(fspp::Dir::EntryType::FILE, ""));
|
2019-06-08 22:06:17 +02:00
|
|
|
for(size_t i = 0; i < entries.size(); ++i) {
|
2019-10-13 17:49:57 +02:00
|
|
|
direntries[i].name = entries[i];
|
2015-03-10 21:51:12 +01:00
|
|
|
}
|
2019-10-13 17:49:57 +02:00
|
|
|
return Return(std::move(direntries));
|
2014-12-02 22:16:48 +01:00
|
|
|
}
|