diff options
author | Ilnur Khuziev <ilnur.khuziev@yandex.ru> | 2022-02-10 16:46:14 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:14 +0300 |
commit | 60040c91ffe701a84689b2c6310ff845e65cff42 (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /util/folder | |
parent | 736dcd8ca259457a136f2f9f9168c44643914323 (diff) | |
download | ydb-60040c91ffe701a84689b2c6310ff845e65cff42.tar.gz |
Restoring authorship annotation for Ilnur Khuziev <ilnur.khuziev@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'util/folder')
-rw-r--r-- | util/folder/dirent_win.c | 58 | ||||
-rw-r--r-- | util/folder/dirut.cpp | 30 | ||||
-rw-r--r-- | util/folder/dirut.h | 6 | ||||
-rw-r--r-- | util/folder/dirut_ut.cpp | 2 | ||||
-rw-r--r-- | util/folder/filelist_ut.cpp | 2 | ||||
-rw-r--r-- | util/folder/fts.cpp | 134 | ||||
-rw-r--r-- | util/folder/fts.h | 4 | ||||
-rw-r--r-- | util/folder/fts_ut.cpp | 2 | ||||
-rw-r--r-- | util/folder/iterator_ut.cpp | 2 | ||||
-rw-r--r-- | util/folder/lstat_win.c | 26 | ||||
-rw-r--r-- | util/folder/path.cpp | 12 | ||||
-rw-r--r-- | util/folder/path_ut.cpp | 30 |
12 files changed, 154 insertions, 154 deletions
diff --git a/util/folder/dirent_win.c b/util/folder/dirent_win.c index 6b8d5c9462..7e6db74ce5 100644 --- a/util/folder/dirent_win.c +++ b/util/folder/dirent_win.c @@ -23,32 +23,32 @@ struct DIR* opendir(const char* dirname) { struct DIR* dir = (struct DIR*)malloc(sizeof(struct DIR)); if (!dir) { return NULL; - } + } dir->sh = INVALID_HANDLE_VALUE; dir->fff_templ = NULL; dir->file_no = 0; dir->readdir_buf = NULL; - + int len = strlen(dirname); //Remove trailing slashes while (len && (dirname[len - 1] == '\\' || dirname[len - 1] == '/')) { --len; } - int len_converted = MultiByteToWideChar(CP_UTF8, 0, dirname, len, 0, 0); - if (len_converted == 0) { + int len_converted = MultiByteToWideChar(CP_UTF8, 0, dirname, len, 0, 0); + if (len_converted == 0) { closedir(dir); return NULL; - } + } dir->fff_templ = (WCHAR*)malloc((len_converted + 5) * sizeof(WCHAR)); if (!dir->fff_templ) { closedir(dir); return NULL; - } - MultiByteToWideChar(CP_UTF8, 0, dirname, len, dir->fff_templ, len_converted); - - WCHAR append[] = {'\\', '*', '.', '*', 0}; - memcpy(dir->fff_templ + len_converted, append, sizeof(append)); - dir->sh = FindFirstFileW(dir->fff_templ, &dir->wfd); + } + MultiByteToWideChar(CP_UTF8, 0, dirname, len, dir->fff_templ, len_converted); + + WCHAR append[] = {'\\', '*', '.', '*', 0}; + memcpy(dir->fff_templ + len_converted, append, sizeof(append)); + dir->sh = FindFirstFileW(dir->fff_templ, &dir->wfd); if (dir->sh == INVALID_HANDLE_VALUE) { SetErrno(); closedir(dir); @@ -68,7 +68,7 @@ int closedir(struct DIR* dir) { } int readdir_r(struct DIR* dir, struct dirent* entry, struct dirent** result) { - if (!FindNextFileW(dir->sh, &dir->wfd)) { + if (!FindNextFileW(dir->sh, &dir->wfd)) { int err = GetLastError(); *result = 0; if (err == ERROR_NO_MORE_FILES) { @@ -89,36 +89,36 @@ int readdir_r(struct DIR* dir, struct dirent* entry, struct dirent** result) { } else { entry->d_type = DT_REG; } - int len = lstrlenW(dir->wfd.cFileName); - int conv_len = WideCharToMultiByte(CP_UTF8, 0, dir->wfd.cFileName, len, 0, 0, 0, 0); - if (conv_len == 0) { - return -1; - } - if (conv_len > sizeof(entry->d_name) - 1) { - SetLastError(ERROR_INSUFFICIENT_BUFFER); - return ERROR_INSUFFICIENT_BUFFER; - } - entry->d_namlen = conv_len; - WideCharToMultiByte(CP_UTF8, 0, dir->wfd.cFileName, len, entry->d_name, conv_len, 0, 0); - entry->d_name[conv_len] = 0; + int len = lstrlenW(dir->wfd.cFileName); + int conv_len = WideCharToMultiByte(CP_UTF8, 0, dir->wfd.cFileName, len, 0, 0, 0, 0); + if (conv_len == 0) { + return -1; + } + if (conv_len > sizeof(entry->d_name) - 1) { + SetLastError(ERROR_INSUFFICIENT_BUFFER); + return ERROR_INSUFFICIENT_BUFFER; + } + entry->d_namlen = conv_len; + WideCharToMultiByte(CP_UTF8, 0, dir->wfd.cFileName, len, entry->d_name, conv_len, 0, 0); + entry->d_name[conv_len] = 0; *result = entry; return 0; } struct dirent* readdir(struct DIR* dir) { struct dirent* res; - if (!dir->readdir_buf) { + if (!dir->readdir_buf) { dir->readdir_buf = (struct dirent*)malloc(sizeof(struct dirent)); - if (dir->readdir_buf == 0) - return 0; - } + if (dir->readdir_buf == 0) + return 0; + } readdir_r(dir, dir->readdir_buf, &res); return res; } void rewinddir(struct DIR* dir) { FindClose(dir->sh); - dir->sh = FindFirstFileW(dir->fff_templ, &dir->wfd); + dir->sh = FindFirstFileW(dir->fff_templ, &dir->wfd); dir->file_no = 0; } diff --git a/util/folder/dirut.cpp b/util/folder/dirut.cpp index 096cf3fa00..ffc9b09f96 100644 --- a/util/folder/dirut.cpp +++ b/util/folder/dirut.cpp @@ -3,7 +3,7 @@ #include "filelist.h" #include "fts.h" #include "pathsplit.h" -#include "path.h" +#include "path.h" #include <util/generic/yexception.h> #include <util/system/compiler.h> @@ -396,7 +396,7 @@ void RemoveDirWithContents(TString dirName) { } int mkpath(char* path, int mode) { - return NFs::MakeDirectoryRecursive(path, NFs::EFilePermission(mode)) ? 0 : -1; + return NFs::MakeDirectoryRecursive(path, NFs::EFilePermission(mode)) ? 0 : -1; } // Implementation of realpath in FreeBSD (version 9.0 and less) and GetFullPathName in Windows @@ -415,10 +415,10 @@ TString RealPath(const TString& path) { } TString RealLocation(const TString& path) { - if (NFs::Exists(path)) + if (NFs::Exists(path)) return RealPath(path); TString dirpath = GetDirName(path); - if (NFs::Exists(dirpath)) + if (NFs::Exists(dirpath)) return RealPath(dirpath) + GetDirectorySeparatorS() + GetFileNameComponent(path.data()); ythrow TFileError() << "RealLocation failed \"" << path << "\""; } @@ -439,8 +439,8 @@ int MakeTempDir(char path[/*FILENAME_MAX*/], const char* prefix) { if ((ret = ResolvePath(prefix, nullptr, path, 1)) != 0) return ret; - if (!TFileStat(path).IsDir()) - return ENOENT; + if (!TFileStat(path).IsDir()) + return ENOENT; if ((strlcat(path, "tmpXXXXXX", FILENAME_MAX) > FILENAME_MAX - 100)) return EINVAL; if (!(mkdtemp(path))) @@ -450,7 +450,7 @@ int MakeTempDir(char path[/*FILENAME_MAX*/], const char* prefix) { } bool IsDir(const TString& path) { - return TFileStat(path).IsDir(); + return TFileStat(path).IsDir(); } TString GetHomeDir() { @@ -483,9 +483,9 @@ void MakeDirIfNotExist(const char* path, int mode) { } void MakePathIfNotExist(const char* path, int mode) { - NFs::MakeDirectoryRecursive(path, NFs::EFilePermission(mode)); - if (!NFs::Exists(path) || !TFileStat(path).IsDir()) { - ythrow TSystemError() << "failed to create directory " << path; + NFs::MakeDirectoryRecursive(path, NFs::EFilePermission(mode)); + if (!NFs::Exists(path) || !TFileStat(path).IsDir()) { + ythrow TSystemError() << "failed to create directory " << path; } } @@ -536,7 +536,7 @@ bool SafeResolveDir(const char* path, TString& result) { } TString GetDirName(const TString& path) { - return TFsPath(path).Dirname(); + return TFsPath(path).Dirname(); } #ifdef _win32_ @@ -549,13 +549,13 @@ char* realpath(const char* pathname, char resolved_path[MAXPATHLEN]) { #endif TString GetBaseName(const TString& path) { - return TFsPath(path).Basename(); + return TFsPath(path).Basename(); } -static bool IsAbsolutePath(const char* str) { +static bool IsAbsolutePath(const char* str) { return str && TPathSplitTraitsLocal::IsAbsolutePath(TStringBuf(str, NStringPrivate::GetStringLengthWithLimit(str, 3))); -} - +} + int ResolvePath(const char* rel, const char* abs, char res[/*MAXPATHLEN*/], bool isdir) { char t[MAXPATHLEN * 2 + 3]; size_t len; diff --git a/util/folder/dirut.h b/util/folder/dirut.h index 03de8ff143..2537027b12 100644 --- a/util/folder/dirut.h +++ b/util/folder/dirut.h @@ -2,7 +2,7 @@ #include <util/system/defaults.h> #include <util/system/sysstat.h> -#include <util/system/fs.h> +#include <util/system/fs.h> #include <util/generic/string.h> #include <util/generic/yexception.h> @@ -108,8 +108,8 @@ public: if (!fname || !*fname) return nullptr; if (Strict) { - NFs::EnsureExists(fname); - } else if (!NFs::Exists(fname)) + NFs::EnsureExists(fname); + } else if (!NFs::Exists(fname)) fname = nullptr; return fname; } diff --git a/util/folder/dirut_ut.cpp b/util/folder/dirut_ut.cpp index e355038bf5..45ebfc842c 100644 --- a/util/folder/dirut_ut.cpp +++ b/util/folder/dirut_ut.cpp @@ -1,7 +1,7 @@ #include "dirut.h" #include "tempdir.h" -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> #include <util/generic/string.h> #include <util/memory/tempbuf.h> diff --git a/util/folder/filelist_ut.cpp b/util/folder/filelist_ut.cpp index 94c75f884e..0cdcdf3d00 100644 --- a/util/folder/filelist_ut.cpp +++ b/util/folder/filelist_ut.cpp @@ -2,7 +2,7 @@ #include "filelist.h" #include "tempdir.h" -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> #include <util/system/file.h> #include <util/generic/string.h> diff --git a/util/folder/fts.cpp b/util/folder/fts.cpp index 86d6420536..0e6a6f86eb 100644 --- a/util/folder/fts.cpp +++ b/util/folder/fts.cpp @@ -91,29 +91,29 @@ int cmp_dird(dird fd1, dird fd2) { #else // ndef _win_ int stat64UTF(const char* path, struct _stat64* _Stat) { - int len_converted = MultiByteToWideChar(CP_UTF8, 0, path, -1, 0, 0); - if (len_converted == 0) { - return -1; - } + int len_converted = MultiByteToWideChar(CP_UTF8, 0, path, -1, 0, 0); + if (len_converted == 0) { + return -1; + } WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted)); - if (buf == nullptr) { - return -1; - } - MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len_converted); - - int ret = _wstat64(buf, _Stat); - free(buf); - return ret; -} - -int stat64UTF(dird path, struct _stat64* _Stat) { - return _wstat64(path, _Stat); -} - + if (buf == nullptr) { + return -1; + } + MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len_converted); + + int ret = _wstat64(buf, _Stat); + free(buf); + return ret; +} + +int stat64UTF(dird path, struct _stat64* _Stat) { + return _wstat64(path, _Stat); +} + const dird invalidDirD = nullptr; dird get_cwdd() { - return _wgetcwd(nullptr, 0); + return _wgetcwd(nullptr, 0); } int valid_dird(dird fd) { @@ -125,42 +125,42 @@ void close_dird(dird fd) { } int chdir_dird(dird fd) { - return _wchdir(fd); + return _wchdir(fd); } -int chdir_dird(const char* path) { - int len_converted = MultiByteToWideChar(CP_UTF8, 0, path, -1, 0, 0); - if (len_converted == 0) { - return -1; - } +int chdir_dird(const char* path) { + int len_converted = MultiByteToWideChar(CP_UTF8, 0, path, -1, 0, 0); + if (len_converted == 0) { + return -1; + } WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted)); - if (buf == nullptr) { - return -1; - } - MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len_converted); - int ret = _wchdir(buf); - free(buf); - return ret; -} - + if (buf == nullptr) { + return -1; + } + MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len_converted); + int ret = _wchdir(buf); + free(buf); + return ret; +} + int cmp_dird(dird fd1, dird fd2) { - return lstrcmpW(fd1, fd2); + return lstrcmpW(fd1, fd2); } dird get_dird(char* path) { - int len_converted = MultiByteToWideChar(CP_UTF8, 0, path, -1, 0, 0); - if (len_converted == 0) { - return nullptr; - } + int len_converted = MultiByteToWideChar(CP_UTF8, 0, path, -1, 0, 0); + if (len_converted == 0) { + return nullptr; + } WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted)); - if (buf == nullptr) { - return nullptr; - } - MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len_converted); - + if (buf == nullptr) { + return nullptr; + } + MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len_converted); + WCHAR* ret = _wfullpath(0, buf, 0); - - free(buf); + + free(buf); return ret; } @@ -1404,29 +1404,29 @@ fts_safe_changedir(FTS* sp, FTSENT* p, int /*fd*/, const char* path) ret = -1; goto bail; } - ret = chdir_dird(path); + ret = chdir_dird(path); bail: return (ret); } - -static int -fts_safe_changedir(FTS* sp, FTSENT* p, int /*fd*/, dird path) { - int ret; - stat_struct sb; - - if (ISSET(FTS_NOCHDIR)) - return (0); - if (STAT_FUNC(path, &sb)) { - ret = -1; - goto bail; - } - if (p->fts_dev != sb.st_dev) { + +static int +fts_safe_changedir(FTS* sp, FTSENT* p, int /*fd*/, dird path) { + int ret; + stat_struct sb; + + if (ISSET(FTS_NOCHDIR)) + return (0); + if (STAT_FUNC(path, &sb)) { + ret = -1; + goto bail; + } + if (p->fts_dev != sb.st_dev) { errno = ENOENT; /* disinformation */ - ret = -1; - goto bail; - } - ret = chdir_dird(path); -bail: - return (ret); -} + ret = -1; + goto bail; + } + ret = chdir_dird(path); +bail: + return (ret); +} #endif diff --git a/util/folder/fts.h b/util/folder/fts.h index 7da796bb62..f3c799e8c8 100644 --- a/util/folder/fts.h +++ b/util/folder/fts.h @@ -10,7 +10,7 @@ typedef struct stat stat_struct; #define STAT_FUNC stat #else #include <util/folder/dirent_win.h> -typedef WCHAR* dird; +typedef WCHAR* dird; typedef unsigned short u_short; typedef unsigned int nlink_t; typedef struct _stat64 stat_struct; @@ -18,7 +18,7 @@ typedef struct _stat64 stat_struct; //TODO: remove from global scope stat64UTF stat64UTF #ifdef __cplusplus int stat64UTF(const char* path, struct _stat64* _Stat); -int stat64UTF(dird path, struct _stat64* _Stat); +int stat64UTF(dird path, struct _stat64* _Stat); #endif #endif diff --git a/util/folder/fts_ut.cpp b/util/folder/fts_ut.cpp index d6a882fa60..c5d59e35f4 100644 --- a/util/folder/fts_ut.cpp +++ b/util/folder/fts_ut.cpp @@ -2,7 +2,7 @@ #include "dirut.h" #include "tempdir.h" -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> #include <library/cpp/threading/future/async.h> #include <util/system/file.h> diff --git a/util/folder/iterator_ut.cpp b/util/folder/iterator_ut.cpp index 98e28ba639..936becd139 100644 --- a/util/folder/iterator_ut.cpp +++ b/util/folder/iterator_ut.cpp @@ -1,7 +1,7 @@ #include "dirut.h" #include "iterator.h" -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> #include <util/system/fs.h> #include <util/system/file.h> diff --git a/util/folder/lstat_win.c b/util/folder/lstat_win.c index 43951ecd32..cf94cec01a 100644 --- a/util/folder/lstat_win.c +++ b/util/folder/lstat_win.c @@ -5,22 +5,22 @@ #include "lstat_win.h" int lstat(const char* fileName, stat_struct* fileStat) { - int len = strlen(fileName); - int convRes = MultiByteToWideChar(CP_UTF8, 0, fileName, len, 0, 0); - if (convRes == 0) { - return -1; - } - WCHAR* buf = malloc(sizeof(WCHAR) * (convRes + 1)); - MultiByteToWideChar(CP_UTF8, 0, fileName, len, buf, convRes); - buf[convRes] = 0; - + int len = strlen(fileName); + int convRes = MultiByteToWideChar(CP_UTF8, 0, fileName, len, 0, 0); + if (convRes == 0) { + return -1; + } + WCHAR* buf = malloc(sizeof(WCHAR) * (convRes + 1)); + MultiByteToWideChar(CP_UTF8, 0, fileName, len, buf, convRes); + buf[convRes] = 0; + HANDLE findHandle; - WIN32_FIND_DATAW findBuf; + WIN32_FIND_DATAW findBuf; int result; - result = _wstat64(buf, fileStat); + result = _wstat64(buf, fileStat); if (result == 0) { SetLastError(0); - findHandle = FindFirstFileW(buf, &findBuf); + findHandle = FindFirstFileW(buf, &findBuf); if (findBuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && (findBuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT || findBuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) { @@ -28,7 +28,7 @@ int lstat(const char* fileName, stat_struct* fileStat) { } FindClose(findHandle); } - free(buf); + free(buf); return result; } diff --git a/util/folder/path.cpp b/util/folder/path.cpp index cdb1d6cd6f..bfe0c67d68 100644 --- a/util/folder/path.cpp +++ b/util/folder/path.cpp @@ -188,21 +188,21 @@ TFsPath::TSplit& TFsPath::GetSplit() const { static Y_FORCE_INLINE void VerifyPath(const TStringBuf path) { Y_VERIFY(!path.Contains('\0'), "wrong format of TFsPath"); -} - +} + TFsPath::TFsPath() { } TFsPath::TFsPath(const TString& path) : Path_(path) { - VerifyPath(Path_); + VerifyPath(Path_); } TFsPath::TFsPath(const TStringBuf path) : Path_(ToString(path)) { - VerifyPath(Path_); + VerifyPath(Path_); } TFsPath::TFsPath(const char* path) @@ -330,7 +330,7 @@ TFsPath TFsPath::ReadLink() const { } bool TFsPath::Exists() const { - return IsDefined() && NFs::Exists(*this); + return IsDefined() && NFs::Exists(*this); } void TFsPath::CheckExists() const { @@ -459,7 +459,7 @@ void TFsPath::ForceRenameTo(const TString& newPath) const { } TFsPath TFsPath::Cwd() { - return TFsPath(::NFs::CurrentWorkingDirectory()); + return TFsPath(::NFs::CurrentWorkingDirectory()); } const TPathSplit& TFsPath::PathSplit() const { diff --git a/util/folder/path_ut.cpp b/util/folder/path_ut.cpp index d621554769..e6a3451016 100644 --- a/util/folder/path_ut.cpp +++ b/util/folder/path_ut.cpp @@ -3,7 +3,7 @@ #include "dirut.h" #include "tempdir.h" -#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/testing/unittest/registar.h> #include <util/generic/scope.h> #include <util/system/platform.h> @@ -444,9 +444,9 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { TFsPath(link).ForceDelete(); - UNIT_ASSERT(!NFs::Exists(link)); - UNIT_ASSERT(NFs::Exists(originFile)); - UNIT_ASSERT(NFs::Exists(originDir)); + UNIT_ASSERT(!NFs::Exists(link)); + UNIT_ASSERT(NFs::Exists(originFile)); + UNIT_ASSERT(NFs::Exists(originDir)); } Y_UNIT_TEST(TestRemoveSymlinkToFile) { @@ -467,9 +467,9 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { TFsPath(link).ForceDelete(); - UNIT_ASSERT(!NFs::Exists(link)); - UNIT_ASSERT(NFs::Exists(originFile)); - UNIT_ASSERT(NFs::Exists(originDir)); + UNIT_ASSERT(!NFs::Exists(link)); + UNIT_ASSERT(NFs::Exists(originFile)); + UNIT_ASSERT(NFs::Exists(originDir)); } Y_UNIT_TEST(TestRemoveDirWithSymlinkToDir) { @@ -493,10 +493,10 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { TFsPath(symlinkedDir).ForceDelete(); - UNIT_ASSERT(!NFs::Exists(symlinkedFile)); - UNIT_ASSERT(!NFs::Exists(symlinkedDir)); - UNIT_ASSERT(NFs::Exists(originFile)); - UNIT_ASSERT(NFs::Exists(originDir)); + UNIT_ASSERT(!NFs::Exists(symlinkedFile)); + UNIT_ASSERT(!NFs::Exists(symlinkedDir)); + UNIT_ASSERT(NFs::Exists(originFile)); + UNIT_ASSERT(NFs::Exists(originDir)); } Y_UNIT_TEST(TestRemoveDirWithSymlinkToFile) { @@ -520,10 +520,10 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { TFsPath(symlinkedDir).ForceDelete(); - UNIT_ASSERT(!NFs::Exists(symlinkedFile)); - UNIT_ASSERT(!NFs::Exists(symlinkedDir)); - UNIT_ASSERT(NFs::Exists(originFile)); - UNIT_ASSERT(NFs::Exists(originDir)); + UNIT_ASSERT(!NFs::Exists(symlinkedFile)); + UNIT_ASSERT(!NFs::Exists(symlinkedDir)); + UNIT_ASSERT(NFs::Exists(originFile)); + UNIT_ASSERT(NFs::Exists(originDir)); } #endif |