diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/folder/path.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/folder/path.cpp')
-rw-r--r-- | util/folder/path.cpp | 178 |
1 files changed, 89 insertions, 89 deletions
diff --git a/util/folder/path.cpp b/util/folder/path.cpp index bfe0c67d68..594473445e 100644 --- a/util/folder/path.cpp +++ b/util/folder/path.cpp @@ -1,43 +1,43 @@ #include "dirut.h" -#include "path.h" -#include "pathsplit.h" - +#include "path.h" +#include "pathsplit.h" + #include <util/generic/yexception.h> #include <util/string/cast.h> #include <util/system/compiler.h> #include <util/system/file.h> -#include <util/system/fs.h> +#include <util/system/fs.h> -struct TFsPath::TSplit: public TAtomicRefCount<TSplit>, public TPathSplit { +struct TFsPath::TSplit: public TAtomicRefCount<TSplit>, public TPathSplit { inline TSplit(const TStringBuf path) - : TPathSplit(path) - { - } + : TPathSplit(path) + { + } }; -void TFsPath::CheckDefined() const { - if (!IsDefined()) { +void TFsPath::CheckDefined() const { + if (!IsDefined()) { ythrow TIoException() << TStringBuf("must be defined"); - } -} - + } +} + bool TFsPath::IsSubpathOf(const TFsPath& that) const { const TSplit& split = GetSplit(); const TSplit& rsplit = that.GetSplit(); - if (rsplit.IsAbsolute != split.IsAbsolute) { + if (rsplit.IsAbsolute != split.IsAbsolute) { return false; - } + } - if (rsplit.Drive != split.Drive) { + if (rsplit.Drive != split.Drive) { return false; - } + } - if (rsplit.size() >= split.size()) { + if (rsplit.size() >= split.size()) { return false; - } + } - return std::equal(rsplit.begin(), rsplit.end(), split.begin()); + return std::equal(rsplit.begin(), rsplit.end(), split.begin()); } bool TFsPath::IsNonStrictSubpathOf(const TFsPath& that) const { @@ -62,18 +62,18 @@ bool TFsPath::IsNonStrictSubpathOf(const TFsPath& that) const { TFsPath TFsPath::RelativeTo(const TFsPath& root) const { TSplit split = GetSplit(); const TSplit& rsplit = root.GetSplit(); - - if (split.Reconstruct() == rsplit.Reconstruct()) { + + if (split.Reconstruct() == rsplit.Reconstruct()) { return TFsPath(); - } - - if (!this->IsSubpathOf(root)) { + } + + if (!this->IsSubpathOf(root)) { ythrow TIoException() << "path " << *this << " is not subpath of " << root; - } - - split.erase(split.begin(), split.begin() + rsplit.size()); - split.IsAbsolute = false; + } + split.erase(split.begin(), split.begin() + rsplit.size()); + split.IsAbsolute = false; + return TFsPath(split.Reconstruct()); } @@ -81,37 +81,37 @@ TFsPath TFsPath::RelativePath(const TFsPath& root) const { TSplit split = GetSplit(); const TSplit& rsplit = root.GetSplit(); size_t cnt = 0; - - while (split.size() > cnt && rsplit.size() > cnt && split[cnt] == rsplit[cnt]) { + + while (split.size() > cnt && rsplit.size() > cnt && split[cnt] == rsplit[cnt]) { ++cnt; - } - bool absboth = split.IsAbsolute && rsplit.IsAbsolute; - if (cnt == 0 && !absboth) { + } + bool absboth = split.IsAbsolute && rsplit.IsAbsolute; + if (cnt == 0 && !absboth) { ythrow TIoException() << "No common parts in " << *this << " and " << root; - } + } TString r; - for (size_t i = 0; i < rsplit.size() - cnt; i++) { + for (size_t i = 0; i < rsplit.size() - cnt; i++) { r += i == 0 ? ".." : "/.."; - } + } for (size_t i = cnt; i < split.size(); i++) { r += (i == 0 || i == cnt && rsplit.size() - cnt == 0 ? "" : "/"); - r += split[i]; - } + r += split[i]; + } return r.size() ? TFsPath(r) : TFsPath(); } TFsPath TFsPath::Parent() const { - if (!IsDefined()) { + if (!IsDefined()) { return TFsPath(); - } + } TSplit split = GetSplit(); - if (split.size()) { - split.pop_back(); - } - if (!split.size() && !split.IsAbsolute) { + if (split.size()) { + split.pop_back(); + } + if (!split.size() && !split.IsAbsolute) { return TFsPath("."); - } + } return TFsPath(split.Reconstruct()); } @@ -120,13 +120,13 @@ TFsPath& TFsPath::operator/=(const TFsPath& that) { *this = that; } else if (that.IsDefined() && that.GetPath() != ".") { - if (!that.IsRelative()) { + if (!that.IsRelative()) { ythrow TIoException() << "path should be relative: " << that.GetPath(); - } + } TSplit split = GetSplit(); const TSplit& rsplit = that.GetSplit(); - split.insert(split.end(), rsplit.begin(), rsplit.end()); + split.insert(split.end(), rsplit.begin(), rsplit.end()); *this = TFsPath(split.Reconstruct()); } return *this; @@ -134,27 +134,27 @@ TFsPath& TFsPath::operator/=(const TFsPath& that) { TFsPath& TFsPath::Fix() { // just normalize via reconstruction - TFsPath(GetSplit().Reconstruct()).Swap(*this); - + TFsPath(GetSplit().Reconstruct()).Swap(*this); + return *this; } TString TFsPath::GetName() const { - if (!IsDefined()) { + if (!IsDefined()) { return TString(); - } + } const TSplit& split = GetSplit(); - if (split.size() > 0) { - if (split.back() != "..") { + if (split.size() > 0) { + if (split.back() != "..") { return TString(split.back()); } else { // cannot just drop last component, because path itself may be a symlink return RealPath().GetName(); } } else { - if (split.IsAbsolute) { + if (split.IsAbsolute) { return split.Reconstruct(); } else { return Cwd().GetName(); @@ -167,7 +167,7 @@ TString TFsPath::GetExtension() const { } bool TFsPath::IsAbsolute() const { - return GetSplit().IsAbsolute; + return GetSplit().IsAbsolute; } bool TFsPath::IsRelative() const { @@ -175,15 +175,15 @@ bool TFsPath::IsRelative() const { } void TFsPath::InitSplit() const { - Split_ = new TSplit(Path_); + Split_ = new TSplit(Path_); } TFsPath::TSplit& TFsPath::GetSplit() const { // XXX: race condition here - if (!Split_) { + if (!Split_) { InitSplit(); - } - return *Split_; + } + return *Split_; } static Y_FORCE_INLINE void VerifyPath(const TStringBuf path) { @@ -211,20 +211,20 @@ TFsPath::TFsPath(const char* path) } TFsPath TFsPath::Child(const TString& name) const { - if (!name) { + if (!name) { ythrow TIoException() << "child name must not be empty"; - } + } return *this / name; } struct TClosedir { static void Destroy(DIR* dir) { - if (dir) { - if (0 != closedir(dir)) { + if (dir) { + if (0 != closedir(dir)) { ythrow TIoSystemError() << "failed to closedir"; - } - } + } + } } }; @@ -244,16 +244,16 @@ void TFsPath::ListNames(TVector<TString>& children) const { Y_PRAGMA_NO_DEPRECATED int r = readdir_r(dir.Get(), &de, &ok); Y_PRAGMA_DIAGNOSTIC_POP - if (r != 0) { + if (r != 0) { ythrow TIoSystemError() << "failed to readdir " << Path_; - } - if (ok == nullptr) { + } + if (ok == nullptr) { return; - } + } TString name(de.d_name); - if (name == "." || name == "..") { + if (name == "." || name == "..") { continue; - } + } children.push_back(name); } } @@ -285,12 +285,12 @@ void TFsPath::List(TVector<TFsPath>& files) const { void TFsPath::RenameTo(const TString& newPath) const { CheckDefined(); - if (!newPath) { + if (!newPath) { ythrow TIoException() << "bad new file name"; - } - if (!NFs::Rename(Path_, newPath)) { + } + if (!NFs::Rename(Path_, newPath)) { ythrow TIoSystemError() << "failed to rename " << Path_ << " to " << newPath; - } + } } void TFsPath::RenameTo(const char* newPath) const { @@ -322,9 +322,9 @@ TFsPath TFsPath::RealLocation() const { TFsPath TFsPath::ReadLink() const { CheckDefined(); - if (!IsSymlink()) { + if (!IsSymlink()) { ythrow TIoException() << "not a symlink " << *this; - } + } return NFs::ReadLink(*this); } @@ -352,14 +352,14 @@ bool TFsPath::IsSymlink() const { } void TFsPath::DeleteIfExists() const { - if (!IsDefined()) { + if (!IsDefined()) { return; - } + } ::unlink(this->c_str()); ::rmdir(this->c_str()); if (Exists()) { - ythrow TIoException() << "failed to delete " << Path_; + ythrow TIoException() << "failed to delete " << Path_; } } @@ -462,16 +462,16 @@ TFsPath TFsPath::Cwd() { return TFsPath(::NFs::CurrentWorkingDirectory()); } -const TPathSplit& TFsPath::PathSplit() const { - return GetSplit(); -} - -template <> +const TPathSplit& TFsPath::PathSplit() const { + return GetSplit(); +} + +template <> void Out<TFsPath>(IOutputStream& os, const TFsPath& f) { os << f.GetPath(); } -template <> +template <> TFsPath FromStringImpl<TFsPath>(const char* s, size_t len) { return TFsPath{TStringBuf{s, len}}; } @@ -481,7 +481,7 @@ bool TryFromStringImpl(const char* s, size_t len, TFsPath& result) { try { result = TStringBuf{s, len}; return true; - } catch (std::exception&) { + } catch (std::exception&) { return false; } } |