Skip to content

Commit

Permalink
[fix]curvefs/client: fix check filetype
Browse files Browse the repository at this point in the history
Signed-off-by: Cyber-SiKu <[email protected]>
  • Loading branch information
Cyber-SiKu committed Sep 19, 2023
1 parent 14d4b07 commit 44f928f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 26 deletions.
71 changes: 51 additions & 20 deletions curvefs/src/client/s3/disk_cache_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
* Author: hzwuhongsong
*/

#include <sys/stat.h>
#include <unistd.h>
#include "curvefs/src/client/s3/disk_cache_base.h"

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <fmt/format.h>
#include <glog/logging.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <functional>
#include <unistd.h>

#include "curvefs/src/client/s3/disk_cache_base.h"
#include <functional>
#include <string>

namespace curvefs {

Expand Down Expand Up @@ -69,7 +73,7 @@ int DiskCacheBase::CreateIoDir(bool writreDir) {
return 0;
}

bool DiskCacheBase::IsFileExist(const std::string file) {
bool DiskCacheBase::IsFileExist(const std::string& file) {
struct stat statFile;
int ret;
ret = posixWrapper_->stat(file.c_str(), &statFile);
Expand All @@ -86,7 +90,7 @@ std::string DiskCacheBase::GetCacheIoFullDir() {
return fullPath;
}

int DiskCacheBase::CreateDir(const std::string dir) {
int DiskCacheBase::CreateDir(const std::string& dir) {
size_t p = dir.find_last_of('/');
std::string dirPath = dir;
if (p != -1ULL) {
Expand Down Expand Up @@ -119,6 +123,7 @@ int DiskCacheBase::CreateDir(const std::string dir) {

int DiskCacheBase::LoadAllCacheFile(std::set<std::string> *cachedObj) {
std::string cachePath = GetCacheIoFullDir();
LOG(ERROR) << "cachePath dir: " << cachePath;
bool ret = IsFileExist(cachePath);
if (!ret) {
LOG(ERROR) << "LoadAllCacheFile, cache read dir is not exist.";
Expand All @@ -133,23 +138,33 @@ int DiskCacheBase::LoadAllCacheFile(std::set<std::string> *cachedObj) {
std::set<std::string> *cacheObj) -> bool {
DIR *dir;
struct dirent *ent;
std::string fileName, nextdir;
if ((dir = posixWrapper_->opendir(path.c_str())) != NULL) {
while ((ent = posixWrapper_->readdir(dir)) != NULL) {
dir = posixWrapper_->opendir(path.c_str());
if (dir != nullptr) {
while ((ent = posixWrapper_->readdir(dir)) != nullptr) {
VLOG(9) << "LoadAllCacheFile obj, name = " << ent->d_name;
if (strncmp(ent->d_name, ".", 1) == 0 ||
strncmp(ent->d_name, "..", 2) == 0) {
continue;
} else if (ent->d_type == 8) {
fileName = std::string(ent->d_name);
VLOG(9) << "LoadAllCacheFile obj, name = " << fileName;
cacheObj->emplace(fileName);
} else {
nextdir = std::string(ent->d_name);
nextdir = path + '/' + nextdir;
if (!listDir(nextdir, cacheObj)) {
return false;
}
}
std::string fileName, nextdir;
switch (GetPathType(path + '/' + ent->d_name)) {
case PathType::kError:
// Generally, the path has been deleted,
// and it is temporarily considered to be a file.
case PathType::kFile:
fileName = std::string(ent->d_name);
VLOG(9) << "LoadAllCacheFile obj, name = " << fileName;
cacheObj->emplace(fileName);
break;
case PathType::kDir:
nextdir = std::string(ent->d_name);
nextdir = path + '/' + nextdir;
if (!listDir(nextdir, cacheObj)) {
return false;
}
break;
default:
break;
}
}
int ret = posixWrapper_->closedir(dir);
Expand All @@ -169,5 +184,21 @@ int DiskCacheBase::LoadAllCacheFile(std::set<std::string> *cachedObj) {
return 0;
}

PathType DiskCacheBase::GetPathType(const std::string& path) {
struct stat statbuf;
if (stat(path.c_str(), &statbuf) != 0) {
// stat path error or path not exist
// Maybe it was deleted
return PathType::kError;
}
if (S_ISREG(statbuf.st_mode)) {
return PathType::kFile;
}
if (S_ISDIR(statbuf.st_mode)) {
return PathType::kDir;
}
return PathType::kOther;
}

} // namespace client
} // namespace curvefs
21 changes: 19 additions & 2 deletions curvefs/src/client/s3/disk_cache_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
#include "curvefs/src/common/wrap_posix.h"
#include "curvefs/src/client/metric/client_metric.h"

enum class PathType {
kError = -1,
kOther = 0,
kFile = 1,
kDir = 2,
};
namespace curvefs {
namespace client {

Expand All @@ -51,8 +57,19 @@ class DiskCacheBase {
* @brief Create Read/Write Cache Dir.
*/
virtual int CreateIoDir(bool writreDir);
virtual bool IsFileExist(const std::string file);
virtual int CreateDir(const std::string name);
virtual bool IsFileExist(const std::string& file);
virtual int CreateDir(const std::string& name);
/**
* @brief
*
* @param path
* @return PathType
* kError = -1,
* kOther = 0,
* kFile = 1,
* kDir = 2,
*/
virtual PathType GetPathType(const std::string& path);
/**
* @brief Get Read/Write Cache full Dir(include CacheDir_).
*/
Expand Down
10 changes: 7 additions & 3 deletions curvefs/test/client/test_disk_cache_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,16 @@ TEST_F(TestDiskCacheRead, LoadAllCacheFile) {
strcpy(fake.d_name, "fake"); // NOLINT

EXPECT_CALL(*wrapper_, stat(NotNull(), NotNull())).WillOnce(Return(0));
EXPECT_CALL(*wrapper_, opendir(NotNull())).WillOnce(Return(dir));
EXPECT_CALL(*wrapper_, opendir(NotNull()))
.WillOnce(Return(dir))
.WillOnce(Return(dir));
EXPECT_CALL(*wrapper_, readdir(NotNull()))
.Times(2)
.Times(3)
.WillOnce(Return(&fake))
.WillOnce(ReturnNull());
EXPECT_CALL(*wrapper_, closedir(NotNull())).WillOnce(Return(0));
EXPECT_CALL(*wrapper_, closedir(NotNull()))
.WillOnce(Return(0))
.WillOnce(Return(0));
ret = diskCacheRead_->LoadAllCacheReadFile(cachedObj);
ASSERT_EQ(0, ret);
}
Expand Down
2 changes: 1 addition & 1 deletion curvefs/test/client/test_disk_cache_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ TEST_F(TestDiskCacheWrite, UploadFileByInode) {
fake.d_type = 8;
strcpy(fake.d_name, obj1.c_str()); // NOLINT
EXPECT_CALL(*wrapper_, stat(NotNull(), NotNull()))
.Times(3)
.Times(2)
.WillRepeatedly(Return(0));
EXPECT_CALL(*wrapper_, opendir(NotNull()))
.Times(2)
Expand Down

0 comments on commit 44f928f

Please sign in to comment.