aboutsummaryrefslogtreecommitdiffstats
path: root/util/folder
diff options
context:
space:
mode:
authorxinull <xinull@yandex-team.com>2022-08-01 10:56:15 +0300
committerxinull <xinull@yandex-team.com>2022-08-01 10:56:15 +0300
commit47a1bc66262692f81b677e6be91b7e5381dba7e4 (patch)
tree7f01e1e3654cad099426a49590e11da557a210eb /util/folder
parent5927c2c274fd15f9132b7cdb3069f6eaa9bdb752 (diff)
downloadydb-47a1bc66262692f81b677e6be91b7e5381dba7e4.tar.gz
TString -> std::string fix TFsPath copy
Add copy operator and constructor Create copy with initialized split test
Diffstat (limited to 'util/folder')
-rw-r--r--util/folder/path.cpp27
-rw-r--r--util/folder/path.h11
-rw-r--r--util/folder/path_ut.cpp21
3 files changed, 58 insertions, 1 deletions
diff --git a/util/folder/path.cpp b/util/folder/path.cpp
index 38945d0bda..7f920db8f0 100644
--- a/util/folder/path.cpp
+++ b/util/folder/path.cpp
@@ -14,6 +14,13 @@ struct TFsPath::TSplit: public TAtomicRefCount<TSplit>, public TPathSplit {
: TPathSplit(path)
{
}
+ inline TSplit(const TSplit& that, const TString& path, const TString& other) {
+ for (const auto& part : that) {
+ emplace_back(path.begin() + (part.data() - other.begin()), part.size());
+ }
+ Drive = TStringBuf(path.begin() + (that.Drive.data() - other.begin()), that.Drive.size());
+ IsAbsolute = that.IsAbsolute;
+ }
};
void TFsPath::CheckDefined() const {
@@ -187,6 +194,14 @@ TFsPath::TSplit& TFsPath::GetSplit() const {
return *Split_;
}
+void TFsPath::CopySplitFrom(const TFsPath& that) const {
+ if (that.Split_) {
+ Split_ = new TSplit(*that.Split_, Path_, that.Path_);
+ } else {
+ Split_ = that.Split_;
+ }
+}
+
static Y_FORCE_INLINE void VerifyPath(const TStringBuf path) {
Y_VERIFY(!path.Contains('\0'), "wrong format of TFsPath: %s", EscapeC(path).c_str());
}
@@ -211,6 +226,18 @@ TFsPath::TFsPath(const char* path)
{
}
+TFsPath::TFsPath(const TFsPath& that)
+ : Path_(that.Path_)
+{
+ CopySplitFrom(that);
+}
+
+TFsPath& TFsPath::operator=(const TFsPath& that) {
+ Path_ = that.Path_;
+ CopySplitFrom(that);
+ return *this;
+}
+
TFsPath TFsPath::Child(const TString& name) const {
if (!name) {
ythrow TIoException() << "child name must not be empty";
diff --git a/util/folder/path.h b/util/folder/path.h
index 2fb4d6b4ef..e1615a947a 100644
--- a/util/folder/path.h
+++ b/util/folder/path.h
@@ -34,6 +34,14 @@ public:
{
}
+ TFsPath(const TFsPath& that);
+ TFsPath(TFsPath&& that) = default;
+
+ TFsPath& operator=(const TFsPath& that);
+ TFsPath& operator=(TFsPath&& that) = default;
+
+ ~TFsPath() = default;
+
void CheckDefined() const;
inline bool IsDefined() const {
@@ -114,7 +122,7 @@ public:
return that.IsSubpathOf(*this);
}
- TFsPath RelativeTo(const TFsPath& root) const; //must be subpath of root
+ TFsPath RelativeTo(const TFsPath& root) const; // must be subpath of root
/**
* @returns relative path or empty path if root equals to this.
@@ -205,6 +213,7 @@ public:
private:
void InitSplit() const;
TSplit& GetSplit() const;
+ void CopySplitFrom(const TFsPath& that) const;
private:
TString Path_;
diff --git a/util/folder/path_ut.cpp b/util/folder/path_ut.cpp
index e6a3451016..f06fec78ee 100644
--- a/util/folder/path_ut.cpp
+++ b/util/folder/path_ut.cpp
@@ -809,4 +809,25 @@ Y_UNIT_TEST_SUITE(TFsPathTests) {
UNIT_ASSERT_EXCEPTION_CONTAINS(testSymlink.ForceDelete(), TIoException, "failed to delete");
}
#endif
+
+ Y_UNIT_TEST(TestCopyWithInitializedSplit) {
+ const TFsPath path1 = TFsPath("some_folder_with_file") / TFsPath("file_in_folder");
+ path1.PathSplit();
+
+ const TFsPath path2 = path1;
+ const TPathSplit& split2 = path2.PathSplit();
+
+ for (const auto& it : split2) {
+ UNIT_ASSERT(path2.GetPath().begin() <= it.begin());
+ UNIT_ASSERT(it.end() <= path2.GetPath().end());
+ }
+ }
+
+ Y_UNIT_TEST(TestAssignmentWithInitializedSplit) {
+ TFsPath path1 = TFsPath("some_folder_with_file_1") / TFsPath("file_in_folder_1");
+ TFsPath path2 = TFsPath("some_folder_with_file_2") / TFsPath("file_in_folder_2");
+ path1.PathSplit();
+ path1 = path2;
+ UNIT_ASSERT_VALUES_EQUAL(path1.PathSplit().at(1), "file_in_folder_2");
+ }
}