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 6728c29
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 24 deletions.
79 changes: 59 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,36 @@ 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::kNotExist:
// Generally, the path has been deleted or moved,
// and it is temporarily considered to be a file.
// In most cases it is from write to read
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;
case PathType::kOther:
case PathType::kError:
default:
break;
}
}
int ret = posixWrapper_->closedir(dir);
Expand All @@ -169,5 +187,26 @@ 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
if (errno == ENOENT) {
return PathType::kNotExist;
}
LOG(ERROR) << "stat path error, path = " << path
<< ", errno = " << errno;
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
23 changes: 21 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,13 @@
#include "curvefs/src/common/wrap_posix.h"
#include "curvefs/src/client/metric/client_metric.h"

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

Expand All @@ -51,8 +58,20 @@ 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,
* kNotExist = 0,
* kOther = 1,
* kFile = 2,
* kDir = 3,
*/
virtual PathType GetPathType(const std::string& path);
/**
* @brief Get Read/Write Cache full Dir(include CacheDir_).
*/
Expand Down
6 changes: 4 additions & 2 deletions curvefs/test/client/test_disk_cache_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,14 @@ 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));
EXPECT_CALL(*wrapper_, readdir(NotNull()))
.Times(2)
.WillOnce(Return(&fake))
.WillOnce(ReturnNull());
EXPECT_CALL(*wrapper_, closedir(NotNull())).WillOnce(Return(0));
EXPECT_CALL(*wrapper_, closedir(NotNull()))
.WillOnce(Return(0));
ret = diskCacheRead_->LoadAllCacheReadFile(cachedObj);
ASSERT_EQ(0, ret);
}
Expand Down

0 comments on commit 6728c29

Please sign in to comment.