Small refactor and 'CreateDir' implementation

This commit is contained in:
payemo 2023-04-19 00:16:10 +03:00
parent 8305d32eea
commit 7cfbcb7aef
3 changed files with 37 additions and 33 deletions

View File

@ -302,7 +302,7 @@ Status DBImpl::Recover(VersionEdit* edit, bool* save_manifest) {
// committed only when the descriptor is created, and this directory // committed only when the descriptor is created, and this directory
// may already exist from a previous failed creation attempt. // may already exist from a previous failed creation attempt.
if (path_->IsDirectory()) { if (path_->IsDirectory()) {
path_->CreateDirectories(); path_->CreateDirs();
} }
env_->CreateDir(dbname_); env_->CreateDir(dbname_);

View File

@ -5,6 +5,7 @@
#include <string> #include <string>
#include "leveldb/export.h" #include "leveldb/export.h"
#include "leveldb/status.h"
namespace leveldb { namespace leveldb {
@ -19,9 +20,9 @@ public:
static const char kAltDirecttorySeparator = '/'; static const char kAltDirecttorySeparator = '/';
static const char kVolumeSeparatorChar = ':'; static const char kVolumeSeparatorChar = ':';
Path() : isDir_{false}, path_("") {} Path() : is_dir_{false}, path_("") {}
Path(const std::string& path) : path_(path) { Path(const std::string& path) : path_(path) {
isDir_ = !IsEmpty() && IsDirectorySeparator(path_[Size() - 1]); is_dir_ = !IsEmpty() && IsDirectorySeparator(path_[Size() - 1]);
} }
virtual ~Path() {} virtual ~Path() {}
@ -31,17 +32,17 @@ public:
virtual bool IsAbsolute() const = 0; virtual bool IsAbsolute() const = 0;
virtual bool IsRelative() const = 0; virtual bool IsRelative() const = 0;
virtual bool CreateDirectories() = 0; virtual Status CreateDirs() = 0;
virtual bool CreateDirectory() = 0; virtual Status CreateDir() = 0;
inline size_t Size() const { return path_.size(); } inline size_t Size() const { return path_.size(); }
inline bool IsEmpty() const { return path_.empty(); } inline bool IsEmpty() const { return path_.empty(); }
inline bool IsDirectory() const { return isDir_; } inline bool IsDirectory() const { return is_dir_; }
// Utility functions // Utility functions
inline bool HasExtension() { inline bool HasExtension() {
if (!IsEmpty()) { if (!IsEmpty() && !is_dir_) {
std::string::reverse_iterator& path_iter = path_.rbegin(); std::string::reverse_iterator& path_iter = path_.rbegin();
while (path_iter != path_.rend()) { while (path_iter != path_.rend()) {
@ -60,14 +61,12 @@ public:
} }
protected: protected:
bool isDir_; bool is_dir_;
std::string path_; std::string path_;
virtual void Normalize() = 0; virtual void Normalize() = 0;
}; };
#ifdef LEVELDB_PLATFORM_WINDOWS
class WindowsFilePath : public Path { class WindowsFilePath : public Path {
public: public:
explicit WindowsFilePath(const std::string& path) : Path(path) { explicit WindowsFilePath(const std::string& path) : Path(path) {
@ -79,8 +78,8 @@ public:
bool IsAbsolute() const override; bool IsAbsolute() const override;
bool IsRelative() const override; bool IsRelative() const override;
bool CreateDirectories() override; Status CreateDirs() override;
bool CreateDirectory() override; Status CreateDir() override;
protected: protected:
void Normalize() override; void Normalize() override;
@ -91,16 +90,13 @@ inline bool IsValidDriveChar(const char c) {
return drive_char >= 'A' && drive_char <= 'Z'; return drive_char >= 'A' && drive_char <= 'Z';
} }
#endif
// Factory // Factory
class PathFactory { class PathFactory {
public: public:
static Path* Create(const std::string& path); PathFactory() = delete;
~PathFactory() = delete;
private: static Path* Create(const std::string& path);
PathFactory() {}
~PathFactory() {}
}; };
} // namespace filesystem } // namespace filesystem

View File

@ -7,18 +7,10 @@ bool IsDirectorySeparator(const char c) {
return (c == Path::kDirectorySeparator || c == Path::kAltDirecttorySeparator); return (c == Path::kDirectorySeparator || c == Path::kAltDirecttorySeparator);
} }
Path* PathFactory::Create(const std::string& path)
{
#ifdef LEVELDB_PLATFORM_WINDOWS
return new WindowsFilePath(path);
#elif LEVELDB_PLATFORM_POSIX
return nullptr;
#endif
return nullptr;
}
#ifdef LEVELDB_PLATFORM_WINDOWS #ifdef LEVELDB_PLATFORM_WINDOWS
#include <windows.h>
bool WindowsFilePath::IsAbsolute() const { bool WindowsFilePath::IsAbsolute() const {
return path_.size() >= 3 && IsValidDriveChar(path_[0]) && return path_.size() >= 3 && IsValidDriveChar(path_[0]) &&
path_[1] == Path::kVolumeSeparatorChar; path_[1] == Path::kVolumeSeparatorChar;
@ -39,7 +31,6 @@ bool WindowsFilePath::IsRelative() const {
IsDirectorySeparator(path_[2])) { IsDirectorySeparator(path_[2])) {
return IsValidDriveChar(path_[0]); return IsValidDriveChar(path_[0]);
} }
return true; return true;
}; };
@ -49,7 +40,7 @@ void WindowsFilePath::Normalize() {
for (const char c : path_) { for (const char c : path_) {
if (!IsDirectorySeparator(c)) { if (!IsDirectorySeparator(c)) {
*(out++) = c; *(out++) = c;
} else if (out == path_.begin() || IsDirectorySeparator(*std::prev(out))) { } else if (out == path_.begin() || !IsDirectorySeparator(*std::prev(out))) {
*(out++) = Path::kDirectorySeparator; *(out++) = Path::kDirectorySeparator;
} else { } else {
continue; continue;
@ -59,12 +50,29 @@ void WindowsFilePath::Normalize() {
path_.erase(out, path_.end()); path_.erase(out, path_.end());
} }
bool WindowsFilePath::CreateDirectories() { return true; } Status WindowsFilePath::CreateDirs() { return Status::OK(); }
bool WindowsFilePath::CreateDirectory() { return true; } Status WindowsFilePath::CreateDir() {
if (!CreateDirectoryA(Path::ToCString(), nullptr)) {
DWORD error_code = GetLastError();
if (error_code == ERROR_FILE_NOT_FOUND || error_code == ERROR_PATH_NOT_FOUND)
return Status::NotFound(path_ + "not foud.");
return Status::IOError("I/O error occured during " + path_ + " creation");
}
return Status::OK();
}
#endif #endif
Path* PathFactory::Create(const std::string& path) {
#ifdef LEVELDB_PLATFORM_WINDOWS
return new WindowsFilePath(path);
#elif LEVELDB_PLATFORM_POSIX
return nullptr;
#endif
return nullptr;
}
} }
} }