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 | |
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')
-rw-r--r-- | util/folder/dirent_win.c | 42 | ||||
-rw-r--r-- | util/folder/dirent_win.h | 72 | ||||
-rw-r--r-- | util/folder/dirut.cpp | 240 | ||||
-rw-r--r-- | util/folder/dirut.h | 54 | ||||
-rw-r--r-- | util/folder/dirut_ut.cpp | 224 | ||||
-rw-r--r-- | util/folder/filelist.cpp | 28 | ||||
-rw-r--r-- | util/folder/filelist.h | 60 | ||||
-rw-r--r-- | util/folder/filelist_ut.cpp | 16 | ||||
-rw-r--r-- | util/folder/fts.cpp | 572 | ||||
-rw-r--r-- | util/folder/fts.h | 158 | ||||
-rw-r--r-- | util/folder/fts_ut.cpp | 50 | ||||
-rw-r--r-- | util/folder/iterator.cpp | 20 | ||||
-rw-r--r-- | util/folder/iterator.h | 182 | ||||
-rw-r--r-- | util/folder/iterator_ut.cpp | 298 | ||||
-rw-r--r-- | util/folder/lstat_win.c | 8 | ||||
-rw-r--r-- | util/folder/lstat_win.h | 14 | ||||
-rw-r--r-- | util/folder/path.cpp | 178 | ||||
-rw-r--r-- | util/folder/path.h | 66 | ||||
-rw-r--r-- | util/folder/path_ut.cpp | 210 | ||||
-rw-r--r-- | util/folder/pathsplit.cpp | 210 | ||||
-rw-r--r-- | util/folder/pathsplit.h | 168 | ||||
-rw-r--r-- | util/folder/pathsplit_ut.cpp | 362 | ||||
-rw-r--r-- | util/folder/tempdir.cpp | 4 | ||||
-rw-r--r-- | util/folder/ut/ya.make | 14 |
24 files changed, 1625 insertions, 1625 deletions
diff --git a/util/folder/dirent_win.c b/util/folder/dirent_win.c index 7e6db74ce5..96bff9a97a 100644 --- a/util/folder/dirent_win.c +++ b/util/folder/dirent_win.c @@ -2,25 +2,25 @@ #ifdef _win_ - #include <stdio.h> - #include "dirent_win.h" + #include <stdio.h> + #include "dirent_win.h" - #if defined(_MSC_VER) && (_MSC_VER < 1900) + #if defined(_MSC_VER) && (_MSC_VER < 1900) void __cdecl _dosmaperr(unsigned long); -static void SetErrno() { - _dosmaperr(GetLastError()); -} - #else +static void SetErrno() { + _dosmaperr(GetLastError()); +} + #else void __cdecl __acrt_errno_map_os_error(unsigned long const oserrno); static void SetErrno() { __acrt_errno_map_os_error(GetLastError()); -} - #endif - -struct DIR* opendir(const char* dirname) { - struct DIR* dir = (struct DIR*)malloc(sizeof(struct DIR)); +} + #endif + +struct DIR* opendir(const char* dirname) { + struct DIR* dir = (struct DIR*)malloc(sizeof(struct DIR)); if (!dir) { return NULL; } @@ -31,7 +31,7 @@ struct DIR* opendir(const char* dirname) { int len = strlen(dirname); //Remove trailing slashes - while (len && (dirname[len - 1] == '\\' || dirname[len - 1] == '/')) { + while (len && (dirname[len - 1] == '\\' || dirname[len - 1] == '/')) { --len; } int len_converted = MultiByteToWideChar(CP_UTF8, 0, dirname, len, 0, 0); @@ -50,7 +50,7 @@ struct DIR* opendir(const char* dirname) { memcpy(dir->fff_templ + len_converted, append, sizeof(append)); dir->sh = FindFirstFileW(dir->fff_templ, &dir->wfd); if (dir->sh == INVALID_HANDLE_VALUE) { - SetErrno(); + SetErrno(); closedir(dir); return NULL; } @@ -58,7 +58,7 @@ struct DIR* opendir(const char* dirname) { return dir; } -int closedir(struct DIR* dir) { +int closedir(struct DIR* dir) { if (dir->sh != INVALID_HANDLE_VALUE) FindClose(dir->sh); free(dir->fff_templ); @@ -67,7 +67,7 @@ int closedir(struct DIR* dir) { return 0; } -int readdir_r(struct DIR* dir, struct dirent* entry, struct dirent** result) { +int readdir_r(struct DIR* dir, struct dirent* entry, struct dirent** result) { if (!FindNextFileW(dir->sh, &dir->wfd)) { int err = GetLastError(); *result = 0; @@ -82,9 +82,9 @@ int readdir_r(struct DIR* dir, struct dirent* entry, struct dirent** result) { entry->d_reclen = sizeof(struct dirent); if (dir->wfd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && (dir->wfd.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT || dir->wfd.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) - { + { entry->d_type = DT_LNK; - } else if (dir->wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + } else if (dir->wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { entry->d_type = DT_DIR; } else { entry->d_type = DT_REG; @@ -105,8 +105,8 @@ int readdir_r(struct DIR* dir, struct dirent* entry, struct dirent** result) { return 0; } -struct dirent* readdir(struct DIR* dir) { - struct dirent* res; +struct dirent* readdir(struct DIR* dir) { + struct dirent* res; if (!dir->readdir_buf) { dir->readdir_buf = (struct dirent*)malloc(sizeof(struct dirent)); if (dir->readdir_buf == 0) @@ -116,7 +116,7 @@ struct dirent* readdir(struct DIR* dir) { return res; } -void rewinddir(struct DIR* dir) { +void rewinddir(struct DIR* dir) { FindClose(dir->sh); dir->sh = FindFirstFileW(dir->fff_templ, &dir->wfd); dir->file_no = 0; diff --git a/util/folder/dirent_win.h b/util/folder/dirent_win.h index ac11a64c04..d65c6b4128 100644 --- a/util/folder/dirent_win.h +++ b/util/folder/dirent_win.h @@ -4,43 +4,43 @@ #ifdef _win_ - #include <util/system/winint.h> + #include <util/system/winint.h> - #ifdef __cplusplus + #ifdef __cplusplus extern "C" { - #endif - - struct DIR { - HANDLE sh; - WIN32_FIND_DATAW wfd; - WCHAR* fff_templ; - int file_no; - struct dirent* readdir_buf; - }; - - #define MAXNAMLEN (MAX_PATH - 1) * 2 - #define MAXPATHLEN MAX_PATH - #define DT_DIR 4 - #define DT_REG 8 - #define DT_LNK 10 - #define DT_UNKNOWN 0 - - struct dirent { - ui32 d_fileno; /* file number of entry */ - ui16 d_reclen; /* length of this record */ - ui8 d_type; /* file type */ - ui8 d_namlen; /* length of string in d_name */ - char d_name[MAXNAMLEN + 1]; /* name must be no longer than this */ - }; - - struct DIR* opendir(const char* dirname); - int closedir(struct DIR* dir); - int readdir_r(struct DIR* dir, struct dirent* entry, struct dirent** result); - struct dirent* readdir(struct DIR* dir); - void rewinddir(struct DIR* dir); - - #ifdef __cplusplus + #endif + + struct DIR { + HANDLE sh; + WIN32_FIND_DATAW wfd; + WCHAR* fff_templ; + int file_no; + struct dirent* readdir_buf; + }; + + #define MAXNAMLEN (MAX_PATH - 1) * 2 + #define MAXPATHLEN MAX_PATH + #define DT_DIR 4 + #define DT_REG 8 + #define DT_LNK 10 + #define DT_UNKNOWN 0 + + struct dirent { + ui32 d_fileno; /* file number of entry */ + ui16 d_reclen; /* length of this record */ + ui8 d_type; /* file type */ + ui8 d_namlen; /* length of string in d_name */ + char d_name[MAXNAMLEN + 1]; /* name must be no longer than this */ + }; + + struct DIR* opendir(const char* dirname); + int closedir(struct DIR* dir); + int readdir_r(struct DIR* dir, struct dirent* entry, struct dirent** result); + struct dirent* readdir(struct DIR* dir); + void rewinddir(struct DIR* dir); + + #ifdef __cplusplus } - #endif - + #endif + #endif //_win_ diff --git a/util/folder/dirut.cpp b/util/folder/dirut.cpp index ffc9b09f96..23c3cbf9ed 100644 --- a/util/folder/dirut.cpp +++ b/util/folder/dirut.cpp @@ -1,16 +1,16 @@ -#include "dirut.h" -#include "iterator.h" -#include "filelist.h" +#include "dirut.h" +#include "iterator.h" +#include "filelist.h" #include "fts.h" -#include "pathsplit.h" +#include "pathsplit.h" #include "path.h" - + #include <util/generic/yexception.h> #include <util/system/compiler.h> -#include <util/system/fs.h> -#include <util/system/maxlen.h> -#include <util/system/yassert.h> - +#include <util/system/fs.h> +#include <util/system/maxlen.h> +#include <util/system/yassert.h> + void SlashFolderLocal(TString& folder) { if (!folder) return; @@ -35,7 +35,7 @@ bool resolvepath(TString& folder, const TString& home) { return false; } // may be from windows - char* ptr = folder.begin(); + char* ptr = folder.begin(); while ((ptr = strchr(ptr, '\\')) != nullptr) *ptr = '/'; @@ -43,7 +43,7 @@ bool resolvepath(TString& folder, const TString& home) { if (folder.length() == 1 || folder.at(1) == '/') { folder = GetHomeDir() + (folder.data() + 1); } else { - char* buf = (char*)alloca(folder.length() + 1); + char* buf = (char*)alloca(folder.length() + 1); strcpy(buf, folder.data() + 1); char* p = strchr(buf, '/'); if (p) @@ -67,10 +67,10 @@ bool resolvepath(TString& folder, const TString& home) { } else { strcpy(path, folder.data()); } - len = strlen(path) + 1; + len = strlen(path) + 1; // grabbed from url.cpp - char* newpath = (char*)alloca(len + 2); - const char** pp = (const char**)alloca(len * sizeof(char*)); + char* newpath = (char*)alloca(len + 2); + const char** pp = (const char**)alloca(len * sizeof(char*)); int i = 0; for (char* s = path; s;) { pp[i++] = s; @@ -80,31 +80,31 @@ bool resolvepath(TString& folder, const TString& home) { } for (int j = 1; j < i;) { - const char*& p = pp[j]; - if (strcmp(p, ".") == 0 || strcmp(p, "") == 0) { - if (j == i - 1) { + const char*& p = pp[j]; + if (strcmp(p, ".") == 0 || strcmp(p, "") == 0) { + if (j == i - 1) { p = ""; break; } else { - memmove(pp + j, pp + j + 1, (i - j - 1) * sizeof(p)); + memmove(pp + j, pp + j + 1, (i - j - 1) * sizeof(p)); --i; } } else if (strcmp(p, "..") == 0) { - if (j == i - 1) { + if (j == i - 1) { if (j == 1) { p = ""; } else { --i; - pp[j - 1] = ""; + pp[j - 1] = ""; } break; } else { if (j == 1) { - memmove(pp + j, pp + j + 1, (i - j - 1) * sizeof(p)); + memmove(pp + j, pp + j + 1, (i - j - 1) * sizeof(p)); --i; } else { - memmove(pp + j - 1, pp + j + 1, (i - j - 1) * sizeof(p)); - i -= 2; + memmove(pp + j - 1, pp + j + 1, (i - j - 1) * sizeof(p)); + i -= 2; --j; } } @@ -133,36 +133,36 @@ using dir_type = enum { // precondition: *ptr != '\\' || *ptr == 0 (cause dt_error) // postcondition: *ptr != '\\' -template <typename T> -static int next_dir(T*& ptr) { +template <typename T> +static int next_dir(T*& ptr) { int has_blank = 0; int has_dot = 0; int has_letter = 0; int has_ctrl = 0; - while (*ptr && *ptr != '\\') { + while (*ptr && *ptr != '\\') { int c = (unsigned char)*ptr++; - switch (c) { - case ' ': + switch (c) { + case ' ': ++has_blank; - break; - case '.': + break; + case '.': ++has_dot; - break; - case '/': - case ':': - case '*': - case '?': - case '"': - case '<': - case '>': - case '|': + break; + case '/': + case ':': + case '*': + case '?': + case '"': + case '<': + case '>': + case '|': ++has_ctrl; - break; - default: - if (c == 127 || c < ' ') + break; + default: + if (c == 127 || c < ' ') ++has_ctrl; - else + else ++has_letter; } } @@ -190,20 +190,20 @@ using disk_type = enum { }; // root slash (if any) - part of disk -template <typename T> -static int skip_disk(T*& ptr) { +template <typename T> +static int skip_disk(T*& ptr) { int result = dk_noflags; if (!*ptr) return result; if (ptr[0] == '\\' && ptr[1] == '\\') { - result |= dk_unc | dk_fromroot; + result |= dk_unc | dk_fromroot; ptr += 2; if (next_dir(ptr) != dt_dir) - return dk_error; // has no host name + return dk_error; // has no host name if (next_dir(ptr) != dt_dir) - return dk_error; // has no share name + return dk_error; // has no share name } else { - if (*ptr && *(ptr + 1) == ':') { + if (*ptr && *(ptr + 1) == ':') { result |= dk_hasdrive; ptr += 2; } @@ -215,13 +215,13 @@ static int skip_disk(T*& ptr) { return result; } -int correctpath(char* cpath, const char* path) { - if (!path || !*path) { +int correctpath(char* cpath, const char* path) { + if (!path || !*path) { *cpath = 0; return 1; } - char* ptr = (char*)path; - char* cptr = cpath; + char* ptr = (char*)path; + char* cptr = cpath; int counter = 0; while (*ptr) { char c = *ptr; @@ -244,103 +244,103 @@ int correctpath(char* cpath, const char* path) { if (dk == dk_error) return 0; - char* ptr1 = ptr = cpath; + char* ptr1 = ptr = cpath; int level = 0; - while (*ptr) { - switch (next_dir(ptr)) { - case dt_dir: + while (*ptr) { + switch (next_dir(ptr)) { + case dt_dir: ++level; - break; - case dt_empty: - memmove(ptr1, ptr, strlen(ptr) + 1); + break; + case dt_empty: + memmove(ptr1, ptr, strlen(ptr) + 1); ptr = ptr1; break; - case dt_up: + case dt_up: --level; - if (level >= 0) { - *--ptr1 = 0; - ptr1 = strrchr(cpath, '\\'); - if (!ptr1) - ptr1 = cpath; - else + if (level >= 0) { + *--ptr1 = 0; + ptr1 = strrchr(cpath, '\\'); + if (!ptr1) + ptr1 = cpath; + else ++ptr1; - memmove(ptr1, ptr, strlen(ptr) + 1); - ptr = ptr1; - break; - } else if (level == -1 && (dk & dk_hasdrive)) { - if (*ptr && *(ptr + 1) == ':' && *(cpath - 2) == ':') { - memmove(cpath - 3, ptr, strlen(ptr) + 1); - return 1; - } + memmove(ptr1, ptr, strlen(ptr) + 1); + ptr = ptr1; + break; + } else if (level == -1 && (dk & dk_hasdrive)) { + if (*ptr && *(ptr + 1) == ':' && *(cpath - 2) == ':') { + memmove(cpath - 3, ptr, strlen(ptr) + 1); + return 1; + } } - if (dk & dk_fromroot) - return 0; - break; - case dt_error: - default: + if (dk & dk_fromroot) + return 0; + break; + case dt_error: + default: return 0; } ptr1 = ptr; } - if ((ptr > cpath || ptr == cpath && dk & dk_unc) && *(ptr - 1) == '\\') - *(ptr - 1) = 0; + if ((ptr > cpath || ptr == cpath && dk & dk_unc) && *(ptr - 1) == '\\') + *(ptr - 1) = 0; return 1; } -static inline int normchar(unsigned char c) { - return (c < 'a' || c > 'z') ? c : c - 32; +static inline int normchar(unsigned char c) { + return (c < 'a' || c > 'z') ? c : c - 32; } -static inline char* strslashcat(char* a, const char* b) { +static inline char* strslashcat(char* a, const char* b) { size_t len = strlen(a); - if (len && a[len - 1] != '\\') + if (len && a[len - 1] != '\\') a[len++] = '\\'; - strcpy(a + len, b); + strcpy(a + len, b); return a; } -int resolvepath(char* apath, const char* rpath, const char* cpath) { - const char* redisk = rpath; +int resolvepath(char* apath, const char* rpath, const char* cpath) { + const char* redisk = rpath; if (!rpath || !*rpath) return 0; int rdt = skip_disk(redisk); if (rdt == dk_error) return 0; - if (rdt & dk_unc || rdt & dk_hasdrive && rdt & dk_fromroot) { + if (rdt & dk_unc || rdt & dk_hasdrive && rdt & dk_fromroot) { return correctpath(apath, rpath); } - const char* cedisk = cpath; + const char* cedisk = cpath; if (!cpath || !*cpath) return 0; int cdt = skip_disk(cedisk); if (cdt == dk_error) return 0; - char* tpath = (char*)alloca(strlen(rpath) + strlen(cpath) + 3); + char* tpath = (char*)alloca(strlen(rpath) + strlen(cpath) + 3); // rdt&dk_hasdrive && !rdt&dk_fromroot - if (rdt & dk_hasdrive) { - if (!(cdt & dk_fromroot)) + if (rdt & dk_hasdrive) { + if (!(cdt & dk_fromroot)) return 0; - if (cdt & dk_hasdrive && normchar(*rpath) != normchar(*cpath)) + if (cdt & dk_hasdrive && normchar(*rpath) != normchar(*cpath)) return 0; memcpy(tpath, rpath, 2); memcpy(tpath + 2, cedisk, strlen(cedisk) + 1); strslashcat(tpath, redisk); - // !rdt&dk_hasdrive && rdt&dk_fromroot - } else if (rdt & dk_fromroot) { - if (!(cdt & dk_hasdrive) && !(cdt & dk_unc)) + // !rdt&dk_hasdrive && rdt&dk_fromroot + } else if (rdt & dk_fromroot) { + if (!(cdt & dk_hasdrive) && !(cdt & dk_unc)) return 0; - memcpy(tpath, cpath, cedisk - cpath); - tpath[cedisk - cpath] = 0; + memcpy(tpath, cpath, cedisk - cpath); + tpath[cedisk - cpath] = 0; strslashcat(tpath, redisk); - // !rdt&dk_hasdrive && !rdt&dk_fromroot + // !rdt&dk_hasdrive && !rdt&dk_fromroot } else { - if (!(cdt & dk_fromroot) || !(cdt & dk_hasdrive) && !(cdt & dk_unc)) + if (!(cdt & dk_fromroot) || !(cdt & dk_hasdrive) && !(cdt & dk_unc)) return 0; strslashcat(strcpy(tpath, cpath), redisk); } @@ -368,11 +368,11 @@ bool resolvepath(TString& folder, const TString& home) { #endif // !defined _win32_ -char GetDirectorySeparator() { +char GetDirectorySeparator() { return LOCSLASH_C; } -const char* GetDirectorySeparatorS() { +const char* GetDirectorySeparatorS() { return LOCSLASH_S; } @@ -382,7 +382,7 @@ void RemoveDirWithContents(TString dirName) { TDirIterator dir(dirName, TDirIterator::TOptions(FTS_NOSTAT)); for (auto it = dir.begin(); it != dir.end(); ++it) { - switch (it->fts_info) { + switch (it->fts_info) { case FTS_F: case FTS_DEFAULT: case FTS_DP: @@ -391,11 +391,11 @@ void RemoveDirWithContents(TString dirName) { if (!NFs::Remove(it->fts_path)) ythrow TSystemError() << "error while removing " << it->fts_path; break; - } + } } } -int mkpath(char* path, int mode) { +int mkpath(char* path, int mode) { return NFs::MakeDirectoryRecursive(path, NFs::EFilePermission(mode)) ? 0 : -1; } @@ -476,13 +476,13 @@ TString GetHomeDir() { return s; } -void MakeDirIfNotExist(const char* path, int mode) { +void MakeDirIfNotExist(const char* path, int mode) { if (!NFs::MakeDirectory(path, NFs::EFilePermission(mode)) && !NFs::Exists(path)) { ythrow TSystemError() << "failed to create directory " << path; } } -void MakePathIfNotExist(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; @@ -557,27 +557,27 @@ static bool IsAbsolutePath(const char* str) { } int ResolvePath(const char* rel, const char* abs, char res[/*MAXPATHLEN*/], bool isdir) { - char t[MAXPATHLEN * 2 + 3]; + char t[MAXPATHLEN * 2 + 3]; size_t len; - + *res = 0; if (!rel || !*rel) return EINVAL; if (!IsAbsolutePath(rel) && IsAbsolutePath(abs)) { len = strlcpy(t, abs, sizeof(t)); - if (len >= sizeof(t) - 3) + if (len >= sizeof(t) - 3) return EINVAL; - if (t[len - 1] != LOCSLASH_C) + if (t[len - 1] != LOCSLASH_C) t[len++] = LOCSLASH_C; - len += strlcpy(t + len, rel, sizeof(t) - len); + len += strlcpy(t + len, rel, sizeof(t) - len); } else { len = strlcpy(t, rel, sizeof(t)); } - if (len >= sizeof(t) - 3) + if (len >= sizeof(t) - 3) return EINVAL; - if (isdir && t[len - 1] != LOCSLASH_C) { - t[len++] = LOCSLASH_C; - t[len] = 0; + if (isdir && t[len - 1] != LOCSLASH_C) { + t[len++] = LOCSLASH_C; + t[len] = 0; } if (!realpath(t, res)) { if (!isdir && realpath(GetDirName(t).data(), res)) { @@ -593,8 +593,8 @@ int ResolvePath(const char* rel, const char* abs, char res[/*MAXPATHLEN*/], bool } if (isdir) { len = strlen(res); - if (res[len - 1] != LOCSLASH_C) { - res[len++] = LOCSLASH_C; + if (res[len - 1] != LOCSLASH_C) { + res[len++] = LOCSLASH_C; res[len] = 0; } } diff --git a/util/folder/dirut.h b/util/folder/dirut.h index 2537027b12..ffb6c1ab99 100644 --- a/util/folder/dirut.h +++ b/util/folder/dirut.h @@ -5,52 +5,52 @@ #include <util/system/fs.h> #include <util/generic/string.h> #include <util/generic/yexception.h> - + #include <sys/types.h> -#include <cerrno> -#include <cstdlib> - +#include <cerrno> +#include <cstdlib> + #ifdef _win32_ - #include <util/system/winint.h> - #include <direct.h> - #include <malloc.h> - #include <time.h> - #include <io.h> - #include "dirent_win.h" + #include <util/system/winint.h> + #include <direct.h> + #include <malloc.h> + #include <time.h> + #include <io.h> + #include "dirent_win.h" // these live in mktemp_system.cpp -extern "C" int mkstemps(char* path, int slen); -char* mkdtemp(char* path); +extern "C" int mkstemps(char* path, int slen); +char* mkdtemp(char* path); #else - #ifdef _sun_ - #include <alloca.h> - -char* mkdtemp(char* path); - #endif - #include <unistd.h> - #include <pwd.h> - #include <dirent.h> - #ifndef DT_DIR - #include <sys/stat.h> - #endif + #ifdef _sun_ + #include <alloca.h> + +char* mkdtemp(char* path); + #endif + #include <unistd.h> + #include <pwd.h> + #include <dirent.h> + #ifndef DT_DIR + #include <sys/stat.h> + #endif #endif bool IsDir(const TString& path); -int mkpath(char* path, int mode = 0777); +int mkpath(char* path, int mode = 0777); TString GetHomeDir(); -void MakeDirIfNotExist(const char* path, int mode = 0777); +void MakeDirIfNotExist(const char* path, int mode = 0777); inline void MakeDirIfNotExist(const TString& path, int mode = 0777) { MakeDirIfNotExist(path.data(), mode); } /// Create path making parent directories as needed -void MakePathIfNotExist(const char* path, int mode = 0777); +void MakePathIfNotExist(const char* path, int mode = 0777); void SlashFolderLocal(TString& folder); bool correctpath(TString& filename); @@ -62,7 +62,7 @@ const char* GetDirectorySeparatorS(); void RemoveDirWithContents(TString dirName); const char* GetFileNameComponent(const char* f); - + inline TString GetFileNameComponent(const TString& f) { return GetFileNameComponent(f.data()); } diff --git a/util/folder/dirut_ut.cpp b/util/folder/dirut_ut.cpp index 45ebfc842c..40f7879d7a 100644 --- a/util/folder/dirut_ut.cpp +++ b/util/folder/dirut_ut.cpp @@ -1,4 +1,4 @@ -#include "dirut.h" +#include "dirut.h" #include "tempdir.h" #include <library/cpp/testing/unittest/registar.h> @@ -6,128 +6,128 @@ #include <util/generic/string.h> #include <util/memory/tempbuf.h> #include <util/stream/file.h> -#include <util/system/platform.h> +#include <util/system/platform.h> -Y_UNIT_TEST_SUITE(TDirutTest) { - Y_UNIT_TEST(TestRealPath) { +Y_UNIT_TEST_SUITE(TDirutTest) { + Y_UNIT_TEST(TestRealPath) { UNIT_ASSERT(IsDir(RealPath("."))); + } + + Y_UNIT_TEST(TestRealLocation) { + UNIT_ASSERT(IsDir(RealLocation("."))); + + TTempDir tempDir; + TString base = RealPath(tempDir()); + UNIT_ASSERT(!base.empty()); + + if (base.back() == GetDirectorySeparator()) { + base.pop_back(); + } + + TString path; + TString pathNotNorm; + + path = base + GetDirectorySeparatorS() + "no_such_file"; + UNIT_ASSERT(NFs::Exists(GetDirName(path))); + UNIT_ASSERT(!NFs::Exists(path)); + path = RealLocation(path); + UNIT_ASSERT(NFs::Exists(GetDirName(path))); + UNIT_ASSERT(!NFs::Exists(path)); + UNIT_ASSERT_EQUAL(GetDirName(path), base); + + pathNotNorm = base + GetDirectorySeparatorS() + "some_dir" + GetDirectorySeparatorS() + ".." + GetDirectorySeparatorS() + "no_such_file"; + MakeDirIfNotExist((base + GetDirectorySeparatorS() + "some_dir").data()); + pathNotNorm = RealLocation(pathNotNorm); + UNIT_ASSERT(NFs::Exists(GetDirName(pathNotNorm))); + UNIT_ASSERT(!NFs::Exists(pathNotNorm)); + UNIT_ASSERT_EQUAL(GetDirName(pathNotNorm), base); + + UNIT_ASSERT_EQUAL(path, pathNotNorm); + + path = base + GetDirectorySeparatorS() + "file"; + { + TFixedBufferFileOutput file(path); + } + UNIT_ASSERT(NFs::Exists(GetDirName(path))); + UNIT_ASSERT(NFs::Exists(path)); + UNIT_ASSERT(NFs::Exists(path)); + path = RealLocation(path); + UNIT_ASSERT(NFs::Exists(GetDirName(path))); + UNIT_ASSERT(NFs::Exists(path)); + UNIT_ASSERT_EQUAL(GetDirName(path), base); } - Y_UNIT_TEST(TestRealLocation) { - UNIT_ASSERT(IsDir(RealLocation("."))); - - TTempDir tempDir; - TString base = RealPath(tempDir()); - UNIT_ASSERT(!base.empty()); - - if (base.back() == GetDirectorySeparator()) { - base.pop_back(); - } - - TString path; - TString pathNotNorm; - - path = base + GetDirectorySeparatorS() + "no_such_file"; - UNIT_ASSERT(NFs::Exists(GetDirName(path))); - UNIT_ASSERT(!NFs::Exists(path)); - path = RealLocation(path); - UNIT_ASSERT(NFs::Exists(GetDirName(path))); - UNIT_ASSERT(!NFs::Exists(path)); - UNIT_ASSERT_EQUAL(GetDirName(path), base); - - pathNotNorm = base + GetDirectorySeparatorS() + "some_dir" + GetDirectorySeparatorS() + ".." + GetDirectorySeparatorS() + "no_such_file"; - MakeDirIfNotExist((base + GetDirectorySeparatorS() + "some_dir").data()); - pathNotNorm = RealLocation(pathNotNorm); - UNIT_ASSERT(NFs::Exists(GetDirName(pathNotNorm))); - UNIT_ASSERT(!NFs::Exists(pathNotNorm)); - UNIT_ASSERT_EQUAL(GetDirName(pathNotNorm), base); - - UNIT_ASSERT_EQUAL(path, pathNotNorm); - - path = base + GetDirectorySeparatorS() + "file"; - { - TFixedBufferFileOutput file(path); - } - UNIT_ASSERT(NFs::Exists(GetDirName(path))); - UNIT_ASSERT(NFs::Exists(path)); - UNIT_ASSERT(NFs::Exists(path)); - path = RealLocation(path); - UNIT_ASSERT(NFs::Exists(GetDirName(path))); - UNIT_ASSERT(NFs::Exists(path)); - UNIT_ASSERT_EQUAL(GetDirName(path), base); - } - - void DoTest(const char* p, const char* base, const char* canon) { - TString path(p); - UNIT_ASSERT(resolvepath(path, base)); - UNIT_ASSERT(path == canon); - } + void DoTest(const char* p, const char* base, const char* canon) { + TString path(p); + UNIT_ASSERT(resolvepath(path, base)); + UNIT_ASSERT(path == canon); + } - Y_UNIT_TEST(TestResolvePath) { + Y_UNIT_TEST(TestResolvePath) { #ifdef _win_ - DoTest("bar", "c:\\foo\\baz", "c:\\foo\\baz\\bar"); - DoTest("c:\\foo\\bar", "c:\\bar\\baz", "c:\\foo\\bar"); + DoTest("bar", "c:\\foo\\baz", "c:\\foo\\baz\\bar"); + DoTest("c:\\foo\\bar", "c:\\bar\\baz", "c:\\foo\\bar"); #else - DoTest("bar", "/foo/baz", "/foo/bar"); - DoTest("/foo/bar", "/bar/baz", "/foo/bar"); + DoTest("bar", "/foo/baz", "/foo/bar"); + DoTest("/foo/bar", "/bar/baz", "/foo/bar"); - #ifdef NDEBUG - DoTest("bar", "./baz", "./bar"); - #if 0 // should we support, for consistency, single-label dirs + #ifdef NDEBUG + DoTest("bar", "./baz", "./bar"); + #if 0 // should we support, for consistency, single-label dirs DoTest("bar", "baz", "bar"); - #endif - #endif + #endif + #endif #endif + } + + Y_UNIT_TEST(TestResolvePathRelative) { + TTempDir tempDir; + TTempBuf tempBuf; + TString base = RealPath(tempDir()); + if (base.back() == GetDirectorySeparator()) { + base.pop_back(); + } + + // File + TString path = base + GetDirectorySeparatorS() + "file"; + { + TFixedBufferFileOutput file(path); + } + ResolvePath("file", base.data(), tempBuf.Data(), false); + UNIT_ASSERT_EQUAL(tempBuf.Data(), path); + + // Dir + path = base + GetDirectorySeparatorS() + "dir"; + MakeDirIfNotExist(path.data()); + ResolvePath("dir", base.data(), tempBuf.Data(), true); + UNIT_ASSERT_EQUAL(tempBuf.Data(), path + GetDirectorySeparatorS()); + + // Absent file in existent dir + path = base + GetDirectorySeparatorS() + "nofile"; + ResolvePath("nofile", base.data(), tempBuf.Data(), false); + UNIT_ASSERT_EQUAL(tempBuf.Data(), path); } - Y_UNIT_TEST(TestResolvePathRelative) { - TTempDir tempDir; - TTempBuf tempBuf; - TString base = RealPath(tempDir()); - if (base.back() == GetDirectorySeparator()) { - base.pop_back(); - } - - // File - TString path = base + GetDirectorySeparatorS() + "file"; - { - TFixedBufferFileOutput file(path); - } - ResolvePath("file", base.data(), tempBuf.Data(), false); - UNIT_ASSERT_EQUAL(tempBuf.Data(), path); - - // Dir - path = base + GetDirectorySeparatorS() + "dir"; - MakeDirIfNotExist(path.data()); - ResolvePath("dir", base.data(), tempBuf.Data(), true); - UNIT_ASSERT_EQUAL(tempBuf.Data(), path + GetDirectorySeparatorS()); - - // Absent file in existent dir - path = base + GetDirectorySeparatorS() + "nofile"; - ResolvePath("nofile", base.data(), tempBuf.Data(), false); - UNIT_ASSERT_EQUAL(tempBuf.Data(), path); - } - - Y_UNIT_TEST(TestGetDirName) { - UNIT_ASSERT_VALUES_EQUAL(".", GetDirName("parambambam")); + Y_UNIT_TEST(TestGetDirName) { + UNIT_ASSERT_VALUES_EQUAL(".", GetDirName("parambambam")); } - Y_UNIT_TEST(TestStripFileComponent) { - static const TString tmpDir = "tmp_dir_for_tests"; - static const TString tmpSubDir = tmpDir + GetDirectorySeparatorS() + "subdir"; - static const TString tmpFile = tmpDir + GetDirectorySeparatorS() + "file"; - - // creating tmp dir and subdirs - MakeDirIfNotExist(tmpDir.data()); - MakeDirIfNotExist(tmpSubDir.data()); - { - TFixedBufferFileOutput file(tmpFile); - } - - UNIT_ASSERT_EQUAL(StripFileComponent(tmpDir), tmpDir + GetDirectorySeparatorS()); - UNIT_ASSERT_EQUAL(StripFileComponent(tmpSubDir), tmpSubDir + GetDirectorySeparatorS()); - UNIT_ASSERT_EQUAL(StripFileComponent(tmpFile), tmpDir + GetDirectorySeparatorS()); - - RemoveDirWithContents(tmpDir); - } -}; + Y_UNIT_TEST(TestStripFileComponent) { + static const TString tmpDir = "tmp_dir_for_tests"; + static const TString tmpSubDir = tmpDir + GetDirectorySeparatorS() + "subdir"; + static const TString tmpFile = tmpDir + GetDirectorySeparatorS() + "file"; + + // creating tmp dir and subdirs + MakeDirIfNotExist(tmpDir.data()); + MakeDirIfNotExist(tmpSubDir.data()); + { + TFixedBufferFileOutput file(tmpFile); + } + + UNIT_ASSERT_EQUAL(StripFileComponent(tmpDir), tmpDir + GetDirectorySeparatorS()); + UNIT_ASSERT_EQUAL(StripFileComponent(tmpSubDir), tmpSubDir + GetDirectorySeparatorS()); + UNIT_ASSERT_EQUAL(StripFileComponent(tmpFile), tmpDir + GetDirectorySeparatorS()); + + RemoveDirWithContents(tmpDir); + } +}; diff --git a/util/folder/filelist.cpp b/util/folder/filelist.cpp index b21fcdbf20..6fc07bf9d1 100644 --- a/util/folder/filelist.cpp +++ b/util/folder/filelist.cpp @@ -1,35 +1,35 @@ -#include "dirut.h" -#include "filelist.h" -#include "iterator.h" - +#include "dirut.h" +#include "filelist.h" +#include "iterator.h" + #include <util/system/defaults.h> void TFileEntitiesList::Fill(const TString& dirname, TStringBuf prefix, TStringBuf suffix, int depth, bool sort) { TDirIterator::TOptions opts; opts.SetMaxLevel(depth); - if (sort) { + if (sort) { opts.SetSortByName(); - } + } TDirIterator dir(dirname, opts); - Clear(); + Clear(); size_t dirNameLength = dirname.length(); - while (dirNameLength && (dirname[dirNameLength - 1] == '\\' || dirname[dirNameLength - 1] == '/')) { + while (dirNameLength && (dirname[dirNameLength - 1] == '\\' || dirname[dirNameLength - 1] == '/')) { --dirNameLength; - } + } for (auto file = dir.begin(); file != dir.end(); ++file) { if (file->fts_pathlen == file->fts_namelen || file->fts_pathlen <= dirNameLength) { continue; - } - + } + TStringBuf filename = file->fts_path + dirNameLength + 1; - + if (filename.empty() || !filename.StartsWith(prefix) || !filename.EndsWith(suffix)) { continue; - } - + } + if (((Mask & EM_FILES) && file->fts_info == FTS_F) || ((Mask & EM_DIRS) && file->fts_info == FTS_D) || ((Mask & EM_SLINKS) && file->fts_info == FTS_SL)) { ++FileNamesSize; FileNames.Append(filename.data(), filename.size() + 1); diff --git a/util/folder/filelist.h b/util/folder/filelist.h index 3f615fa4c2..8f2036e016 100644 --- a/util/folder/filelist.h +++ b/util/folder/filelist.h @@ -1,38 +1,38 @@ #pragma once -#include <util/generic/buffer.h> +#include <util/generic/buffer.h> #include <util/generic/string.h> -#include <util/generic/strbuf.h> +#include <util/generic/strbuf.h> #include <util/generic/flags.h> -class TFileEntitiesList { +class TFileEntitiesList { public: enum EMaskFlag { - EM_FILES = 1, - EM_DIRS = 2, - EM_SLINKS = 4, + EM_FILES = 1, + EM_DIRS = 2, + EM_SLINKS = 4, EM_FILES_DIRS = EM_FILES | EM_DIRS, EM_FILES_SLINKS = EM_FILES | EM_SLINKS, EM_DIRS_SLINKS = EM_DIRS | EM_SLINKS, EM_FILES_DIRS_SLINKS = EM_FILES | EM_DIRS | EM_SLINKS - }; + }; Y_DECLARE_FLAGS(EMask, EMaskFlag) - TFileEntitiesList(EMask mask) - : Mask(mask) - { + TFileEntitiesList(EMask mask) + : Mask(mask) + { Clear(); } void Clear() { Cur = nullptr; FileNamesSize = CurName = 0; - FileNames.Clear(); - FileNames.Append("", 1); + FileNames.Clear(); + FileNames.Append("", 1); } - const char* Next() { + const char* Next() { return Cur = (CurName++ < FileNamesSize ? strchr(Cur, 0) + 1 : nullptr); } @@ -42,40 +42,40 @@ public: inline void Fill(const TString& dirname, bool sort = false) { Fill(dirname, TStringBuf(), sort); - } + } inline void Fill(const TString& dirname, TStringBuf prefix, bool sort = false) { Fill(dirname, prefix, TStringBuf(), 1, sort); - } - + } + void Fill(const TString& dirname, TStringBuf prefix, TStringBuf suffix, int depth, bool sort = false); - + void Restart() { - Cur = FileNames.Data(); + Cur = FileNames.Data(); CurName = 0; } protected: - TBuffer FileNames; + TBuffer FileNames; size_t FileNamesSize, CurName; - const char* Cur; + const char* Cur; EMask Mask; }; Y_DECLARE_OPERATORS_FOR_FLAGS(TFileEntitiesList::EMask) -class TFileList: public TFileEntitiesList { +class TFileList: public TFileEntitiesList { public: - TFileList() - : TFileEntitiesList(TFileEntitiesList::EM_FILES) - { - } + TFileList() + : TFileEntitiesList(TFileEntitiesList::EM_FILES) + { + } }; -class TDirsList: public TFileEntitiesList { +class TDirsList: public TFileEntitiesList { public: - TDirsList() - : TFileEntitiesList(TFileEntitiesList::EM_DIRS) - { - } + TDirsList() + : TFileEntitiesList(TFileEntitiesList::EM_DIRS) + { + } }; diff --git a/util/folder/filelist_ut.cpp b/util/folder/filelist_ut.cpp index 0cdcdf3d00..2792d8f6f0 100644 --- a/util/folder/filelist_ut.cpp +++ b/util/folder/filelist_ut.cpp @@ -1,4 +1,4 @@ -#include "dirut.h" +#include "dirut.h" #include "filelist.h" #include "tempdir.h" @@ -8,13 +8,13 @@ #include <util/generic/string.h> class TFileListTest: public TTestBase { - UNIT_TEST_SUITE(TFileListTest); - UNIT_TEST(TestSimple); + UNIT_TEST_SUITE(TFileListTest); + UNIT_TEST(TestSimple); UNIT_TEST(TestPrefix); - UNIT_TEST_SUITE_END(); - -public: - void TestSimple(); + UNIT_TEST_SUITE_END(); + +public: + void TestSimple(); void TestPrefix(); }; @@ -26,7 +26,7 @@ void TFileListTest::TestSimple() { TFileList fileList; fileList.Fill(tempDir().data(), "", "", 1000); TString fileName(fileList.Next()); - UNIT_ASSERT_EQUAL(fileName, "subdir" LOCSLASH_S "file"); + UNIT_ASSERT_EQUAL(fileName, "subdir" LOCSLASH_S "file"); UNIT_ASSERT_EQUAL(fileList.Next(), nullptr); } diff --git a/util/folder/fts.cpp b/util/folder/fts.cpp index 0e6a6f86eb..1044b61187 100644 --- a/util/folder/fts.cpp +++ b/util/folder/fts.cpp @@ -41,23 +41,23 @@ #include <stdlib.h> #ifndef _win_ - #include <inttypes.h> - #include <sys/param.h> - #include <dirent.h> - #include <errno.h> - #include <string.h> - #include <unistd.h> + #include <inttypes.h> + #include <sys/param.h> + #include <dirent.h> + #include <errno.h> + #include <string.h> + #include <unistd.h> #else - #include <direct.h> - #include "dirent_win.h" - #include "lstat_win.h" + #include <direct.h> + #include "dirent_win.h" + #include "lstat_win.h" #endif #include <sys/stat.h> #include <fcntl.h> -#include "fts.h" +#include "fts.h" #include <limits.h> #ifndef _win_ @@ -90,12 +90,12 @@ int cmp_dird(dird fd1, dird fd2) { #else // ndef _win_ -int stat64UTF(const char* path, struct _stat64* _Stat) { +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; } - WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted)); + WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted)); if (buf == nullptr) { return -1; } @@ -133,7 +133,7 @@ int chdir_dird(const char* path) { if (len_converted == 0) { return -1; } - WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted)); + WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted)); if (buf == nullptr) { return -1; } @@ -152,68 +152,68 @@ dird get_dird(char* path) { if (len_converted == 0) { return nullptr; } - WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted)); + WCHAR* buf = (WCHAR*)malloc(sizeof(WCHAR) * (len_converted)); if (buf == nullptr) { return nullptr; } MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len_converted); - WCHAR* ret = _wfullpath(0, buf, 0); + WCHAR* ret = _wfullpath(0, buf, 0); free(buf); return ret; -} +} #endif // ndef _win_ -#ifdef _win_ - #define S_ISDIR(st_mode) ((st_mode & _S_IFMT) == _S_IFDIR) - #define S_ISREG(st_mode) ((st_mode & _S_IFMT) == _S_IFREG) - #define S_ISLNK(st_mode) ((st_mode & _S_IFMT) == _S_IFLNK) - #define O_RDONLY _O_RDONLY -static int fts_safe_changedir(FTS*, FTSENT*, int, dird); +#ifdef _win_ + #define S_ISDIR(st_mode) ((st_mode & _S_IFMT) == _S_IFDIR) + #define S_ISREG(st_mode) ((st_mode & _S_IFMT) == _S_IFREG) + #define S_ISLNK(st_mode) ((st_mode & _S_IFMT) == _S_IFLNK) + #define O_RDONLY _O_RDONLY +static int fts_safe_changedir(FTS*, FTSENT*, int, dird); #endif #if defined(__svr4__) || defined(__linux__) || defined(__CYGWIN__) || defined(_win_) - #ifdef MAX - #undef MAX - #endif - #define MAX(a, b) ((a) > (b) ? (a) : (b)) - #undef ALIGNBYTES - #undef ALIGN - #define ALIGNBYTES (sizeof(long long) - 1) - #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES) - #if !defined(__linux__) && !defined(__CYGWIN__) - #define dirfd(dirp) ((dirp)->dd_fd) - #endif - #define D_NAMLEN(dirp) (strlen(dirp->d_name)) + #ifdef MAX + #undef MAX + #endif + #define MAX(a, b) ((a) > (b) ? (a) : (b)) + #undef ALIGNBYTES + #undef ALIGN + #define ALIGNBYTES (sizeof(long long) - 1) + #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES) + #if !defined(__linux__) && !defined(__CYGWIN__) + #define dirfd(dirp) ((dirp)->dd_fd) + #endif + #define D_NAMLEN(dirp) (strlen(dirp->d_name)) #else - #define D_NAMLEN(dirp) (dirp->d_namlen) + #define D_NAMLEN(dirp) (dirp->d_namlen) #endif -static FTSENT* fts_alloc(FTS*, const char*, int); -static FTSENT* fts_build(FTS*, int); -static void fts_lfree(FTSENT*); -static void fts_load(FTS*, FTSENT*); -static size_t fts_maxarglen(char* const*); -static void fts_padjust(FTS*); -static int fts_palloc(FTS*, size_t); -static FTSENT* fts_sort(FTS*, FTSENT*, int); -static u_short fts_stat(FTS*, FTSENT*, int); -static int fts_safe_changedir(FTS*, FTSENT*, int, const char*); +static FTSENT* fts_alloc(FTS*, const char*, int); +static FTSENT* fts_build(FTS*, int); +static void fts_lfree(FTSENT*); +static void fts_load(FTS*, FTSENT*); +static size_t fts_maxarglen(char* const*); +static void fts_padjust(FTS*); +static int fts_palloc(FTS*, size_t); +static FTSENT* fts_sort(FTS*, FTSENT*, int); +static u_short fts_stat(FTS*, FTSENT*, int); +static int fts_safe_changedir(FTS*, FTSENT*, int, const char*); -#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) +#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) -#define CLR(opt) (sp->fts_options &= ~(opt)) -#define ISSET(opt) (sp->fts_options & (opt)) -#define SET(opt) (sp->fts_options |= (opt)) +#define CLR(opt) (sp->fts_options &= ~(opt)) +#define ISSET(opt) (sp->fts_options & (opt)) +#define SET(opt) (sp->fts_options |= (opt)) -#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && chdir_dird(fd)) +#define FCHDIR(sp, fd) (!ISSET(FTS_NOCHDIR) && chdir_dird(fd)) /* fts_build flags */ -#define BCHILD 1 /* yfts_children */ -#define BNAMES 2 /* yfts_children, names only */ -#define BREAD 3 /* yfts_read */ +#define BCHILD 1 /* yfts_children */ +#define BNAMES 2 /* yfts_children, names only */ +#define BREAD 3 /* yfts_read */ static u_short yfts_type_from_info(u_short info) { @@ -227,28 +227,28 @@ yfts_type_from_info(u_short info) { return FTS_NSOK; } -static void* -yreallocf(void* ptr, size_t size) +static void* +yreallocf(void* ptr, size_t size) { - void* nptr; + void* nptr; nptr = realloc(ptr, size); - if (!nptr && ptr) { + if (!nptr && ptr) { free(ptr); - } + } return (nptr); } - -FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, const FTSENT**)) + +FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, const FTSENT**)) { - FTS* sp; + FTS* sp; FTSENT *p, *root; int nitems; FTSENT *parent, *tmp; int len; - errno = 0; - + errno = 0; + Y_ASSERT(argv); if (!*argv) { errno = ENOENT; @@ -262,9 +262,9 @@ FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, con } /* Allocate/initialize the stream */ - if ((sp = (FTS*)malloc(sizeof(FTS))) == nullptr) { + if ((sp = (FTS*)malloc(sizeof(FTS))) == nullptr) { return nullptr; - } + } memset(sp, 0, sizeof(FTS)); sp->fts_compar = compar; sp->fts_options = options; @@ -273,22 +273,22 @@ FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, con tmp = nullptr; /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ - if (ISSET(FTS_LOGICAL)) { + if (ISSET(FTS_LOGICAL)) { SET(FTS_NOCHDIR); - } + } /* * Start out with 1K of path space, and enough, in any case, * to hold the user's paths. */ - if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) { + if (fts_palloc(sp, MAX(fts_maxarglen(argv), MAXPATHLEN))) { goto mem1; - } + } /* Allocate/initialize root's parent. */ - if ((parent = fts_alloc(sp, "", 0)) == nullptr) { + if ((parent = fts_alloc(sp, "", 0)) == nullptr) { goto mem2; - } + } parent->fts_level = FTS_ROOTPARENTLEVEL; /* Allocate/initialize root(s). */ @@ -299,7 +299,7 @@ FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, con //Any subsequent windows call will expect no trailing slashes so we will remove them here #ifdef _win_ - while (len && ((*argv)[len - 1] == '\\' || (*argv)[len - 1] == '/')) { + while (len && ((*argv)[len - 1] == '\\' || (*argv)[len - 1] == '/')) { --len; } #endif @@ -317,9 +317,9 @@ FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, con p->fts_type = yfts_type_from_info(p->fts_info); /* Command-line "." and ".." are real directories. */ - if (p->fts_info == FTS_DOT) { + if (p->fts_info == FTS_DOT) { p->fts_info = FTS_D; - } + } /* * If comparison routine supplied, traverse in sorted @@ -330,27 +330,27 @@ FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, con root = p; } else { p->fts_link = nullptr; - if (root == nullptr) { + if (root == nullptr) { tmp = root = p; - } else { + } else { tmp->fts_link = p; tmp = p; } } } - if (compar && nitems > 1) { + if (compar && nitems > 1) { root = fts_sort(sp, root, nitems); - } + } /* * Allocate a dummy pointer and make yfts_read think that we've just * finished the node before the root(s); set p->fts_info to FTS_INIT * so that everything about the "current" node is ignored. */ - if ((sp->fts_cur = fts_alloc(sp, "", 0)) == nullptr) { + if ((sp->fts_cur = fts_alloc(sp, "", 0)) == nullptr) { goto mem3; - } - sp->fts_cur->fts_level = FTS_ROOTLEVEL; + } + sp->fts_cur->fts_level = FTS_ROOTLEVEL; sp->fts_cur->fts_link = root; sp->fts_cur->fts_info = FTS_INIT; @@ -362,27 +362,27 @@ FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, con * descriptor we run anyway, just more slowly. */ - if (!ISSET(FTS_NOCHDIR) && valid_dird(sp->fts_rfd = get_cwdd())) { + if (!ISSET(FTS_NOCHDIR) && valid_dird(sp->fts_rfd = get_cwdd())) { SET(FTS_NOCHDIR); - } + } return (sp); -mem3: - fts_lfree(root); +mem3: + fts_lfree(root); free(parent); -mem2: - free(sp->fts_path); -mem1: - free(sp); +mem2: + free(sp->fts_path); +mem1: + free(sp); return nullptr; } static void -fts_load(FTS* sp, FTSENT* p) +fts_load(FTS* sp, FTSENT* p) { size_t len; - char* cp; + char* cp; /* * Load the stream structure for the next traversal. Since we don't @@ -402,7 +402,7 @@ fts_load(FTS* sp, FTSENT* p) sp->fts_dev = p->fts_dev; } -int yfts_close(FTS* sp) +int yfts_close(FTS* sp) { FTSENT *freep, *p; int saved_errno; @@ -422,12 +422,12 @@ int yfts_close(FTS* sp) } /* Free up child linked list, sort array, path buffer. */ - if (sp->fts_child) { + if (sp->fts_child) { fts_lfree(sp->fts_child); - } - if (sp->fts_array) { + } + if (sp->fts_array) { free(sp->fts_array); - } + } free(sp->fts_path); /* Return to original directory, save errno if necessary. */ @@ -453,24 +453,24 @@ int yfts_close(FTS* sp) * Special case of "/" at the end of the path so that slashes aren't * appended which would cause paths to be written as "....//foo". */ -#define NAPPEND(p) \ - (p->fts_path[p->fts_pathlen - 1] == LOCSLASH_C \ - ? p->fts_pathlen - 1 \ - : p->fts_pathlen) +#define NAPPEND(p) \ + (p->fts_path[p->fts_pathlen - 1] == LOCSLASH_C \ + ? p->fts_pathlen - 1 \ + : p->fts_pathlen) -FTSENT* -yfts_read(FTS* sp) { +FTSENT* +yfts_read(FTS* sp) { FTSENT *p, *tmp; int instr; - char* t; + char* t; int saved_errno; ClearLastSystemError(); - + /* If finished or unrecoverable error, return NULL. */ - if (sp->fts_cur == nullptr || ISSET(FTS_STOP)) { + if (sp->fts_cur == nullptr || ISSET(FTS_STOP)) { return nullptr; - } + } /* Set current node pointer. */ p = sp->fts_cur; @@ -500,9 +500,9 @@ yfts_read(FTS* sp) { if (valid_dird(p->fts_symfd = get_cwdd())) { p->fts_errno = errno; p->fts_info = FTS_ERR; - } else { + } else { p->fts_flags |= FTS_SYMFOLLOW; - } + } } return (p); } @@ -512,9 +512,9 @@ yfts_read(FTS* sp) { /* If skipped or crossed mount point, do post-order visit. */ if (instr == FTS_SKIP || (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { - if (p->fts_flags & FTS_SYMFOLLOW) { + if (p->fts_flags & FTS_SYMFOLLOW) { close_dird(p->fts_symfd); - } + } if (sp->fts_child) { fts_lfree(sp->fts_child); sp->fts_child = nullptr; @@ -546,15 +546,15 @@ yfts_read(FTS* sp) { if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { p->fts_errno = errno; p->fts_flags |= FTS_DONTCHDIR; - for (p = sp->fts_child; p; p = p->fts_link) { + for (p = sp->fts_child; p; p = p->fts_link) { p->fts_accpath = p->fts_parent->fts_accpath; - } + } } } else if ((sp->fts_child = fts_build(sp, BREAD)) == nullptr) { - if (ISSET(FTS_STOP)) { + if (ISSET(FTS_STOP)) { return nullptr; - } + } return (p); } p = sp->fts_child; @@ -563,8 +563,8 @@ yfts_read(FTS* sp) { } /* Move to the next node on this level. */ -next: - tmp = p; +next: + tmp = p; if ((p = p->fts_link) != nullptr) { free(tmp); @@ -586,26 +586,26 @@ next: * ignore. If followed, get a file descriptor so we can * get back if necessary. */ - if (p->fts_instr == FTS_SKIP) { + if (p->fts_instr == FTS_SKIP) { goto next; - } + } if (p->fts_instr == FTS_FOLLOW) { p->fts_info = fts_stat(sp, p, 1); p->fts_type = yfts_type_from_info(p->fts_info); if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { if (valid_dird(p->fts_symfd = - get_cwdd())) { + get_cwdd())) { p->fts_errno = errno; p->fts_info = FTS_ERR; - } else { + } else { p->fts_flags |= FTS_SYMFOLLOW; - } + } } p->fts_instr = FTS_NOINSTR; } - name: - t = sp->fts_path + NAPPEND(p->fts_parent); + name: + t = sp->fts_path + NAPPEND(p->fts_parent); *t++ = LOCSLASH_C; memmove(t, p->fts_name, (size_t)p->fts_namelen + 1); return (sp->fts_cur = p); @@ -648,9 +648,9 @@ next: } close_dird(p->fts_symfd); } else if (!(p->fts_flags & FTS_DONTCHDIR) && - fts_safe_changedir(sp, p->fts_parent, -1, "..")) { - SET(FTS_STOP); - return nullptr; + fts_safe_changedir(sp, p->fts_parent, -1, "..")) { + SET(FTS_STOP); + return nullptr; } p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; return (sp->fts_cur = p); @@ -663,9 +663,9 @@ next: * reasons. */ /* ARGSUSED */ -int yfts_set(FTS* sp, FTSENT* p, int instr) +int yfts_set(FTS* sp, FTSENT* p, int instr) { - (void)sp; //Unused + (void)sp; //Unused if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW && instr != FTS_NOINSTR && instr != FTS_SKIP) { errno = EINVAL; @@ -675,10 +675,10 @@ int yfts_set(FTS* sp, FTSENT* p, int instr) return (0); } -FTSENT* -yfts_children(FTS* sp, int instr) +FTSENT* +yfts_children(FTS* sp, int instr) { - FTSENT* p; + FTSENT* p; dird fd; if (instr && instr != FTS_NAMEONLY) { errno = EINVAL; @@ -695,35 +695,35 @@ yfts_children(FTS* sp, int instr) errno = 0; /* Fatal errors stop here. */ - if (ISSET(FTS_STOP)) { + if (ISSET(FTS_STOP)) { return nullptr; - } + } /* Return logical hierarchy of user's arguments. */ - if (p->fts_info == FTS_INIT) { + if (p->fts_info == FTS_INIT) { return (p->fts_link); - } + } /* * If not a directory being visited in pre-order, stop here. Could * allow FTS_DNR, assuming the user has fixed the problem, but the * same effect is available with FTS_AGAIN. */ - if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) { + if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */) { return nullptr; - } + } /* Free up any previous child list. */ - if (sp->fts_child) { + if (sp->fts_child) { fts_lfree(sp->fts_child); - } + } if (instr == FTS_NAMEONLY) { SET(FTS_NAMEONLY); instr = BNAMES; - } else { + } else { instr = BCHILD; - } + } /* * If using chdir on a relative path and called BEFORE yfts_read does @@ -733,35 +733,35 @@ yfts_children(FTS* sp, int instr) * yfts_read will work. */ if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == LOCSLASH_C || - ISSET(FTS_NOCHDIR)) { + ISSET(FTS_NOCHDIR)) { return (sp->fts_child = fts_build(sp, instr)); - } + } - if (valid_dird(fd = get_cwdd())) { + if (valid_dird(fd = get_cwdd())) { return nullptr; - } + } sp->fts_child = fts_build(sp, instr); if (chdir_dird(fd)) { - close_dird(fd); + close_dird(fd); return nullptr; - } + } close_dird(fd); return (sp->fts_child); } -static inline struct dirent* yreaddir(DIR* dir, struct dirent* de) { +static inline struct dirent* yreaddir(DIR* dir, struct dirent* de) { // TODO(yazevnul|IGNIETFERRO-1070): remove these macroses by replacing `readdir_r` with proper // alternative Y_PRAGMA_DIAGNOSTIC_PUSH Y_PRAGMA_NO_DEPRECATED - if (readdir_r(dir, de, &de) == 0) { - Y_PRAGMA_DIAGNOSTIC_POP - return de; - } - + if (readdir_r(dir, de, &de) == 0) { + Y_PRAGMA_DIAGNOSTIC_POP + return de; + } + return nullptr; -} - +} + /* * This is the tricky part -- do not casually change *anything* in here. The * idea is to build the linked list of entries that are used by yfts_children @@ -776,25 +776,25 @@ static inline struct dirent* yreaddir(DIR* dir, struct dirent* de) { * directories and for any files after the subdirectories in the directory have * been found, cutting the stat calls by about 2/3. */ -static FTSENT* -fts_build(FTS* sp, int type) +static FTSENT* +fts_build(FTS* sp, int type) { - struct dirent* dp; + struct dirent* dp; FTSENT *p, *head; int nitems; FTSENT *cur, *tail; - + #ifdef _win_ dird dirpd; - struct DIR* dirp; + struct DIR* dirp; #else - DIR* dirp; + DIR* dirp; #endif - - void* oldaddr; + + void* oldaddr; int cderrno, descend, len, level, maxlen, nlinks, saved_errno, nostat, doadjust; - char* cp; + char* cp; /* Set current node pointer. */ cur = sp->fts_cur; @@ -805,11 +805,11 @@ fts_build(FTS* sp, int type) */ #ifdef FTS_WHITEOUT if (ISSET(FTS_WHITEOUT)) - oflag = DTF_NODUP | DTF_REWIND; + oflag = DTF_NODUP | DTF_REWIND; else - oflag = DTF_HIDEW | DTF_NODUP | DTF_REWIND; + oflag = DTF_HIDEW | DTF_NODUP | DTF_REWIND; #else - #define __opendir2(path, flag) opendir(path) + #define __opendir2(path, flag) opendir(path) #endif if ((dirp = __opendir2(cur->fts_accpath, oflag)) == nullptr) { if (type == BREAD) { @@ -862,10 +862,10 @@ fts_build(FTS* sp, int type) #else if (fts_safe_changedir(sp, cur, -1, dirpd)) { #endif - - if (nlinks && type == BREAD) { + + if (nlinks && type == BREAD) { cur->fts_errno = errno; - } + } cur->fts_flags |= FTS_DONTCHDIR; descend = 0; cderrno = errno; @@ -877,12 +877,12 @@ fts_build(FTS* sp, int type) #else Y_UNUSED(invalidDirD); #endif - } else { + } else { descend = 1; - } - } else { + } + } else { descend = 0; - } + } /* * Figure out the max file name length that can be stored in the @@ -909,31 +909,31 @@ fts_build(FTS* sp, int type) /* Read the directory, attaching each entry to the `link' pointer. */ doadjust = 0; - - //to ensure enough buffer - TTempBuf dpe; - + + //to ensure enough buffer + TTempBuf dpe; + for (head = tail = nullptr, nitems = 0; dirp && (dp = yreaddir(dirp, (struct dirent*)dpe.Data())) != nullptr;) { - if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) { + if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) { continue; - } + } - if ((p = fts_alloc(sp, dp->d_name, (int)strlen(dp->d_name))) == nullptr) { + if ((p = fts_alloc(sp, dp->d_name, (int)strlen(dp->d_name))) == nullptr) { goto mem1; - } - if (strlen(dp->d_name) >= (size_t)maxlen) { /* include space for NUL */ + } + if (strlen(dp->d_name) >= (size_t)maxlen) { /* include space for NUL */ oldaddr = sp->fts_path; - if (fts_palloc(sp, strlen(dp->d_name) + len + 1)) { + if (fts_palloc(sp, strlen(dp->d_name) + len + 1)) { /* * No more memory for path or structures. Save * errno, free up the current structure and the * structures already allocated. */ - mem1: - saved_errno = errno; - if (p) { + mem1: + saved_errno = errno; + if (p) { free(p); - } + } fts_lfree(head); (void)closedir(dirp); #ifdef _win_ @@ -947,9 +947,9 @@ fts_build(FTS* sp, int type) /* Did realloc() change the pointer? */ if (oldaddr != sp->fts_path) { doadjust = 1; - if (ISSET(FTS_NOCHDIR)) { + if (ISSET(FTS_NOCHDIR)) { cp = sp->fts_path + len; - } + } } maxlen = sp->fts_pathlen - len; } @@ -996,16 +996,16 @@ fts_build(FTS* sp, int type) if (nlinks) { p->fts_info = FTS_NS; p->fts_errno = cderrno; - } else { + } else { p->fts_info = FTS_NSOK; - } + } p->fts_accpath = cur->fts_accpath; } else if (nlinks == 0 #ifdef DT_DIR - || (nostat && - dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) + || (nostat && + dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN) #endif - ) { + ) { p->fts_accpath = ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; p->fts_info = FTS_NSOK; @@ -1014,25 +1014,25 @@ fts_build(FTS* sp, int type) if (ISSET(FTS_NOCHDIR)) { p->fts_accpath = p->fts_path; memmove((void*)cp, (void*)p->fts_name, (size_t)p->fts_namelen + 1); - } else { + } else { p->fts_accpath = p->fts_name; - } + } /* Stat it. */ p->fts_info = fts_stat(sp, p, 0); p->fts_type = yfts_type_from_info(p->fts_info); /* Decrement link count if applicable. */ if (nlinks > 0 && (p->fts_info == FTS_D || - p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) { + p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) { --nlinks; - } + } } /* We walk in directory order so "ls -f" doesn't get upset. */ p->fts_link = nullptr; - if (head == nullptr) { + if (head == nullptr) { head = tail = p; - } else { + } else { tail->fts_link = p; tail = p; } @@ -1049,18 +1049,18 @@ fts_build(FTS* sp, int type) * If realloc() changed the address of the path, adjust the * addresses for the rest of the tree and the dir list. */ - if (doadjust) { + if (doadjust) { fts_padjust(sp); - } + } /* * If not changing directories, reset the path back to original * state. */ if (ISSET(FTS_NOCHDIR)) { - if (len == sp->fts_pathlen || nitems == 0) { + if (len == sp->fts_pathlen || nitems == 0) { --cp; - } + } *cp = '\0'; } @@ -1072,7 +1072,7 @@ fts_build(FTS* sp, int type) * can't get back, we're done. */ if (descend && (type == BCHILD || !nitems) && - (cur->fts_level == FTS_ROOTLEVEL ? FCHDIR(sp, sp->fts_rfd) : fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { + (cur->fts_level == FTS_ROOTLEVEL ? FCHDIR(sp, sp->fts_rfd) : fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) { cur->fts_info = FTS_ERR; SET(FTS_STOP); fts_lfree(head); @@ -1081,22 +1081,22 @@ fts_build(FTS* sp, int type) /* If didn't find anything, return NULL. */ if (!nitems) { - if (type == BREAD) { + if (type == BREAD) { cur->fts_info = FTS_DP; - } + } fts_lfree(head); return nullptr; } /* Sort the entries. */ - if (sp->fts_compar && nitems > 1) { + if (sp->fts_compar && nitems > 1) { head = fts_sort(sp, head, nitems); - } + } return (head); } static u_short -fts_stat(FTS* sp, FTSENT* p, int follow) +fts_stat(FTS* sp, FTSENT* p, int follow) { dev_t dev; ino_t ino; @@ -1109,7 +1109,7 @@ fts_stat(FTS* sp, FTSENT* p, int follow) /* check for whiteout */ if (p->fts_flags & FTS_ISW) { if (sbp != &sb) { - memset(sbp, '\0', sizeof(*sbp)); + memset(sbp, '\0', sizeof(*sbp)); sbp->st_mode = S_IFWHT; } return (FTS_W); @@ -1132,7 +1132,7 @@ fts_stat(FTS* sp, FTSENT* p, int follow) memset(sbp, 0, sizeof(stat_struct)); return (FTS_NS); } - } else if (lstat(p->fts_accpath, sbp)) { + } else if (lstat(p->fts_accpath, sbp)) { p->fts_errno = errno; memset(sbp, 0, sizeof(stat_struct)); return (FTS_NS); @@ -1150,43 +1150,43 @@ fts_stat(FTS* sp, FTSENT* p, int follow) ino = p->fts_ino = sbp->st_ino; p->fts_nlink = sbp->st_nlink; - const char* fts_name_x = p->fts_name; - if (ISDOT(fts_name_x)) { + const char* fts_name_x = p->fts_name; + if (ISDOT(fts_name_x)) { return (FTS_DOT); - } + } - /* + /* * Cycle detection is done by brute force when the directory * is first encountered. If the tree gets deep enough or the * number of symbolic links to directories is high enough, * something faster might be worthwhile. */ - //There is no way to detect symlink or mount cycles on win32 + //There is no way to detect symlink or mount cycles on win32 #ifndef _win_ - FTSENT* t; + FTSENT* t; for (t = p->fts_parent; - t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) { + t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent) { if (ino == t->fts_ino && dev == t->fts_dev) { p->fts_cycle = t; return (FTS_DC); } - } + } #endif /*_win_*/ return (FTS_D); } - if (S_ISLNK(sbp->st_mode)) { + if (S_ISLNK(sbp->st_mode)) { return (FTS_SL); - } - if (S_ISREG(sbp->st_mode)) { + } + if (S_ISREG(sbp->st_mode)) { return (FTS_F); - } + } return (FTS_DEFAULT); } -static FTSENT* -fts_sort(FTS* sp, FTSENT* head, int nitems) +static FTSENT* +fts_sort(FTS* sp, FTSENT* head, int nitems) { FTSENT **ap, *p; @@ -1198,35 +1198,35 @@ fts_sort(FTS* sp, FTSENT* head, int nitems) * 40 so don't realloc one entry at a time. */ if (nitems > sp->fts_nitems) { - struct _ftsent** a; + struct _ftsent** a; sp->fts_nitems = nitems + 40; - if ((a = (struct _ftsent**)realloc(sp->fts_array, - sp->fts_nitems * sizeof(FTSENT*))) == nullptr) { - if (sp->fts_array) { + if ((a = (struct _ftsent**)realloc(sp->fts_array, + sp->fts_nitems * sizeof(FTSENT*))) == nullptr) { + if (sp->fts_array) { free(sp->fts_array); - } + } sp->fts_array = nullptr; sp->fts_nitems = 0; return (head); } sp->fts_array = a; } - for (ap = sp->fts_array, p = head; p; p = p->fts_link) { + for (ap = sp->fts_array, p = head; p; p = p->fts_link) { *ap++ = p; - } - qsort((void*)sp->fts_array, (size_t)nitems, sizeof(FTSENT*), (int (*)(const void*, const void*))sp->fts_compar); - for (head = *(ap = sp->fts_array); --nitems; ++ap) { + } + qsort((void*)sp->fts_array, (size_t)nitems, sizeof(FTSENT*), (int (*)(const void*, const void*))sp->fts_compar); + for (head = *(ap = sp->fts_array); --nitems; ++ap) { ap[0]->fts_link = ap[1]; - } + } ap[0]->fts_link = nullptr; return (head); } -static FTSENT* -fts_alloc(FTS* sp, const char* name, int namelen) +static FTSENT* +fts_alloc(FTS* sp, const char* name, int namelen) { - FTSENT* p; + FTSENT* p; size_t len; /* @@ -1238,20 +1238,20 @@ fts_alloc(FTS* sp, const char* name, int namelen) * namelen + 2 before the first possible address of the stat structure. */ len = sizeof(FTSENT) + namelen; - if (!ISSET(FTS_NOSTAT)) { + if (!ISSET(FTS_NOSTAT)) { len += sizeof(stat_struct) + ALIGNBYTES; - } - if ((p = (FTSENT*)malloc(len)) == nullptr) { + } + if ((p = (FTSENT*)malloc(len)) == nullptr) { return nullptr; - } + } /* Copy the name and guarantee NUL termination. */ memmove((void*)p->fts_name, (void*)name, (size_t)namelen); p->fts_name[namelen] = '\0'; - if (!ISSET(FTS_NOSTAT)) { - p->fts_statp = (stat_struct*)ALIGN(p->fts_name + namelen + 2); - } + if (!ISSET(FTS_NOSTAT)) { + p->fts_statp = (stat_struct*)ALIGN(p->fts_name + namelen + 2); + } p->fts_namelen = (u_short)namelen; p->fts_path = sp->fts_path; p->fts_errno = 0; @@ -1264,9 +1264,9 @@ fts_alloc(FTS* sp, const char* name, int namelen) } static void -fts_lfree(FTSENT* head) +fts_lfree(FTSENT* head) { - FTSENT* p; + FTSENT* p; /* Free a linked list of structures. */ while ((p = head) != nullptr) { @@ -1282,27 +1282,27 @@ fts_lfree(FTSENT* head) * plus 256 bytes so don't realloc the path 2 bytes at a time. */ static int -fts_palloc(FTS* sp, size_t more) +fts_palloc(FTS* sp, size_t more) { sp->fts_pathlen += more + 256; - sp->fts_path = (char*)yreallocf(sp->fts_path, (size_t)sp->fts_pathlen); + sp->fts_path = (char*)yreallocf(sp->fts_path, (size_t)sp->fts_pathlen); return (sp->fts_path == nullptr); } static void -ADJUST(FTSENT* p, void* addr) +ADJUST(FTSENT* p, void* addr) { if ((p)->fts_accpath >= (p)->fts_path && (p)->fts_accpath < (p)->fts_path + (p)->fts_pathlen) { - if (p->fts_accpath != p->fts_path) { + if (p->fts_accpath != p->fts_path) { errx(1, "fts ADJUST: accpath %p path %p", - p->fts_accpath, p->fts_path); - } - if (p->fts_level != 0) { + p->fts_accpath, p->fts_path); + } + if (p->fts_level != 0) { errx(1, "fts ADJUST: level %d not 0", p->fts_level); - } + } (p)->fts_accpath = - (char*)addr + ((p)->fts_accpath - (p)->fts_path); + (char*)addr + ((p)->fts_accpath - (p)->fts_path); } (p)->fts_path = (char*)addr; } @@ -1312,21 +1312,21 @@ ADJUST(FTSENT* p, void* addr) * already returned. */ static void -fts_padjust(FTS* sp) +fts_padjust(FTS* sp) { - FTSENT* p; - char* addr = sp->fts_path; - -#define ADJUST1(p) \ - { \ - if ((p)->fts_accpath == (p)->fts_path) \ - (p)->fts_accpath = (addr); \ - (p)->fts_path = addr; \ - } + FTSENT* p; + char* addr = sp->fts_path; + +#define ADJUST1(p) \ + { \ + if ((p)->fts_accpath == (p)->fts_path) \ + (p)->fts_accpath = (addr); \ + (p)->fts_path = addr; \ + } /* Adjust the current set of children. */ - for (p = sp->fts_child; p; p = p->fts_link) { + for (p = sp->fts_child; p; p = p->fts_link) { ADJUST(p, addr); - } + } /* Adjust the rest of the tree. */ for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { @@ -1336,15 +1336,15 @@ fts_padjust(FTS* sp) } static size_t -fts_maxarglen(char* const* argv) +fts_maxarglen(char* const* argv) { size_t len, max; - for (max = 0; *argv; ++argv) { - if ((len = strlen(*argv)) > max) { + for (max = 0; *argv; ++argv) { + if ((len = strlen(*argv)) > max) { max = len; - } - } + } + } return (max + 1); } @@ -1356,39 +1356,39 @@ fts_maxarglen(char* const* argv) #ifndef _win_ static int -fts_safe_changedir(FTS* sp, FTSENT* p, int fd, const char* path) +fts_safe_changedir(FTS* sp, FTSENT* p, int fd, const char* path) { int ret, oerrno, newfd; stat_struct sb; newfd = fd; - if (ISSET(FTS_NOCHDIR)) { + if (ISSET(FTS_NOCHDIR)) { return (0); - } - if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0) { + } + if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0) { return (-1); - } + } if (fstat(newfd, &sb)) { ret = -1; goto bail; } if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) { - errno = ENOENT; /* disinformation */ + errno = ENOENT; /* disinformation */ ret = -1; goto bail; } ret = fchdir(newfd); bail: oerrno = errno; - if (fd < 0) { + if (fd < 0) { (void)close(newfd); - } + } errno = oerrno; return (ret); } #else static int -fts_safe_changedir(FTS* sp, FTSENT* p, int /*fd*/, const char* path) +fts_safe_changedir(FTS* sp, FTSENT* p, int /*fd*/, const char* path) { int ret; stat_struct sb; @@ -1400,7 +1400,7 @@ fts_safe_changedir(FTS* sp, FTSENT* p, int /*fd*/, const char* path) goto bail; } if (p->fts_dev != sb.st_dev) { - errno = ENOENT; /* disinformation */ + errno = ENOENT; /* disinformation */ ret = -1; goto bail; } @@ -1421,7 +1421,7 @@ fts_safe_changedir(FTS* sp, FTSENT* p, int /*fd*/, dird path) { goto bail; } if (p->fts_dev != sb.st_dev) { - errno = ENOENT; /* disinformation */ + errno = ENOENT; /* disinformation */ ret = -1; goto bail; } diff --git a/util/folder/fts.h b/util/folder/fts.h index f3c799e8c8..3427fbc4e6 100644 --- a/util/folder/fts.h +++ b/util/folder/fts.h @@ -1,108 +1,108 @@ -#pragma once +#pragma once #include <sys/types.h> - + #include <util/system/defaults.h> #ifndef _win32_ -typedef int dird; +typedef int dird; typedef struct stat stat_struct; - #define STAT_FUNC stat + #define STAT_FUNC stat #else - #include <util/folder/dirent_win.h> + #include <util/folder/dirent_win.h> typedef WCHAR* dird; -typedef unsigned short u_short; -typedef unsigned int nlink_t; +typedef unsigned short u_short; +typedef unsigned int nlink_t; typedef struct _stat64 stat_struct; - #define STAT_FUNC stat64UTF - //TODO: remove from global scope stat64UTF stat64UTF - #ifdef __cplusplus + #define STAT_FUNC stat64UTF + //TODO: remove from global scope stat64UTF stat64UTF + #ifdef __cplusplus int stat64UTF(const char* path, struct _stat64* _Stat); int stat64UTF(dird path, struct _stat64* _Stat); - #endif + #endif #endif typedef struct { - struct _ftsent* fts_cur; /* current node */ - struct _ftsent* fts_child; /* linked list of children */ - struct _ftsent** fts_array; /* sort array */ - dev_t fts_dev; /* starting device # */ - char* fts_path; /* path for this descent */ - dird fts_rfd; /* fd for root */ - int fts_pathlen; /* sizeof(path) */ - int fts_nitems; /* elements in the sort array */ - int(*fts_compar) /* compare function */ - (const struct _ftsent**, const struct _ftsent**); + struct _ftsent* fts_cur; /* current node */ + struct _ftsent* fts_child; /* linked list of children */ + struct _ftsent** fts_array; /* sort array */ + dev_t fts_dev; /* starting device # */ + char* fts_path; /* path for this descent */ + dird fts_rfd; /* fd for root */ + int fts_pathlen; /* sizeof(path) */ + int fts_nitems; /* elements in the sort array */ + int(*fts_compar) /* compare function */ + (const struct _ftsent**, const struct _ftsent**); -#define FTS_COMFOLLOW 0x001 /* follow command line symlinks */ -#define FTS_LOGICAL 0x002 /* logical walk */ -#define FTS_NOCHDIR 0x004 /* don't change directories */ -#define FTS_NOSTAT 0x008 /* don't get stat info */ -#define FTS_PHYSICAL 0x010 /* physical walk */ -#define FTS_SEEDOT 0x020 /* return dot and dot-dot */ -#define FTS_XDEV 0x040 /* don't cross devices */ -#define FTS_OPTIONMASK 0x0ff /* valid user option mask */ +#define FTS_COMFOLLOW 0x001 /* follow command line symlinks */ +#define FTS_LOGICAL 0x002 /* logical walk */ +#define FTS_NOCHDIR 0x004 /* don't change directories */ +#define FTS_NOSTAT 0x008 /* don't get stat info */ +#define FTS_PHYSICAL 0x010 /* physical walk */ +#define FTS_SEEDOT 0x020 /* return dot and dot-dot */ +#define FTS_XDEV 0x040 /* don't cross devices */ +#define FTS_OPTIONMASK 0x0ff /* valid user option mask */ -#define FTS_NAMEONLY 0x100 /* (private) child names only */ -#define FTS_STOP 0x200 /* (private) unrecoverable error */ - int fts_options; /* yfts_open options, global flags */ +#define FTS_NAMEONLY 0x100 /* (private) child names only */ +#define FTS_STOP 0x200 /* (private) unrecoverable error */ + int fts_options; /* yfts_open options, global flags */ } FTS; typedef struct _ftsent { - struct _ftsent* fts_cycle; /* cycle node */ - struct _ftsent* fts_parent; /* parent directory */ - struct _ftsent* fts_link; /* next file in directory */ - long fts_number; /* local numeric value */ - void* fts_pointer; /* local address value */ - char* fts_accpath; /* access path */ - char* fts_path; /* root path */ - int fts_errno; /* errno for this node */ - dird fts_symfd; /* fd for symlink */ - u_short fts_pathlen; /* strlen(fts_path) */ - u_short fts_namelen; /* strlen(fts_name) */ + struct _ftsent* fts_cycle; /* cycle node */ + struct _ftsent* fts_parent; /* parent directory */ + struct _ftsent* fts_link; /* next file in directory */ + long fts_number; /* local numeric value */ + void* fts_pointer; /* local address value */ + char* fts_accpath; /* access path */ + char* fts_path; /* root path */ + int fts_errno; /* errno for this node */ + dird fts_symfd; /* fd for symlink */ + u_short fts_pathlen; /* strlen(fts_path) */ + u_short fts_namelen; /* strlen(fts_name) */ - ino_t fts_ino; /* inode */ - dev_t fts_dev; /* device */ - nlink_t fts_nlink; /* link count */ + ino_t fts_ino; /* inode */ + dev_t fts_dev; /* device */ + nlink_t fts_nlink; /* link count */ -#define FTS_ROOTPARENTLEVEL -1 -#define FTS_ROOTLEVEL 0 - short fts_level; /* depth (-1 to N) */ +#define FTS_ROOTPARENTLEVEL -1 +#define FTS_ROOTLEVEL 0 + short fts_level; /* depth (-1 to N) */ -#define FTS_D 1 /* preorder directory */ -#define FTS_DC 2 /* directory that causes cycles */ -#define FTS_DEFAULT 3 /* none of the above */ -#define FTS_DNR 4 /* unreadable directory */ -#define FTS_DOT 5 /* dot or dot-dot */ -#define FTS_DP 6 /* postorder directory */ -#define FTS_ERR 7 /* error; errno is set */ -#define FTS_F 8 /* regular file */ -#define FTS_INIT 9 /* initialized only */ -#define FTS_NS 10 /* stat(2) failed */ -#define FTS_NSOK 11 /* no stat(2) requested */ -#define FTS_SL 12 /* symbolic link */ -#define FTS_SLNONE 13 /* symbolic link without target */ -#define FTS_W 14 /* whiteout object */ - u_short fts_info; /* user flags for FTSENT structure */ +#define FTS_D 1 /* preorder directory */ +#define FTS_DC 2 /* directory that causes cycles */ +#define FTS_DEFAULT 3 /* none of the above */ +#define FTS_DNR 4 /* unreadable directory */ +#define FTS_DOT 5 /* dot or dot-dot */ +#define FTS_DP 6 /* postorder directory */ +#define FTS_ERR 7 /* error; errno is set */ +#define FTS_F 8 /* regular file */ +#define FTS_INIT 9 /* initialized only */ +#define FTS_NS 10 /* stat(2) failed */ +#define FTS_NSOK 11 /* no stat(2) requested */ +#define FTS_SL 12 /* symbolic link */ +#define FTS_SLNONE 13 /* symbolic link without target */ +#define FTS_W 14 /* whiteout object */ + u_short fts_info; /* user flags for FTSENT structure */ u_short fts_type; /* type of fs node; one of FTS_D, FTS_F, FTS_SL */ -#define FTS_DONTCHDIR 0x01 /* don't chdir .. to the parent */ -#define FTS_SYMFOLLOW 0x02 /* followed a symlink to get here */ -#define FTS_ISW 0x04 /* this is a whiteout object */ - u_short fts_flags; /* private flags for FTSENT structure */ +#define FTS_DONTCHDIR 0x01 /* don't chdir .. to the parent */ +#define FTS_SYMFOLLOW 0x02 /* followed a symlink to get here */ +#define FTS_ISW 0x04 /* this is a whiteout object */ + u_short fts_flags; /* private flags for FTSENT structure */ -#define FTS_AGAIN 1 /* read node again */ -#define FTS_FOLLOW 2 /* follow symbolic link */ -#define FTS_NOINSTR 3 /* no instructions */ -#define FTS_SKIP 4 /* discard node */ - u_short fts_instr; /* yfts_set() instructions */ +#define FTS_AGAIN 1 /* read node again */ +#define FTS_FOLLOW 2 /* follow symbolic link */ +#define FTS_NOINSTR 3 /* no instructions */ +#define FTS_SKIP 4 /* discard node */ + u_short fts_instr; /* yfts_set() instructions */ stat_struct* fts_statp; /* stat(2) information */ - char fts_name[1]; /* file name */ + char fts_name[1]; /* file name */ } FTSENT; -FTSENT* yfts_children(FTS*, int); -int yfts_close(FTS*); -FTS* yfts_open(char* const*, int, int (*)(const FTSENT**, const FTSENT**)); -FTSENT* yfts_read(FTS*); -int yfts_set(FTS*, FTSENT*, int); +FTSENT* yfts_children(FTS*, int); +int yfts_close(FTS*); +FTS* yfts_open(char* const*, int, int (*)(const FTSENT**, const FTSENT**)); +FTSENT* yfts_read(FTS*); +int yfts_set(FTS*, FTSENT*, int); diff --git a/util/folder/fts_ut.cpp b/util/folder/fts_ut.cpp index c5d59e35f4..35c6d68863 100644 --- a/util/folder/fts_ut.cpp +++ b/util/folder/fts_ut.cpp @@ -1,61 +1,61 @@ -#include "fts.h" -#include "dirut.h" +#include "fts.h" +#include "dirut.h" #include "tempdir.h" - + #include <library/cpp/testing/unittest/registar.h> #include <library/cpp/threading/future/async.h> -#include <util/system/file.h> +#include <util/system/file.h> #include <util/system/tempfile.h> #include <util/generic/string.h> class TFtsTest: public TTestBase { - UNIT_TEST_SUITE(TFtsTest); - UNIT_TEST(TestSimple); + UNIT_TEST_SUITE(TFtsTest); + UNIT_TEST(TestSimple); UNIT_TEST(TestNoLeakChangingAccessToFolder); - UNIT_TEST_SUITE_END(); + UNIT_TEST_SUITE_END(); -public: - void TestSimple(); +public: + void TestSimple(); void TestNoLeakChangingAccessToFolder(); }; -void MakeFile(const char* path) { - TFile(path, CreateAlways); +void MakeFile(const char* path) { + TFile(path, CreateAlways); } //There potentially could be problems in listing order on different platforms -int FtsCmp(const FTSENT** ent1, const FTSENT** ent2) { +int FtsCmp(const FTSENT** ent1, const FTSENT** ent2) { return strcmp((*ent1)->fts_accpath, (*ent2)->fts_accpath); } -void CheckEnt(FTSENT* ent, const char* name, int type) { +void CheckEnt(FTSENT* ent, const char* name, int type) { UNIT_ASSERT(ent); UNIT_ASSERT_STRINGS_EQUAL(ent->fts_path, name); UNIT_ASSERT_EQUAL(ent->fts_info, type); } -class TFileTree { +class TFileTree { public: - TFileTree(char* const* argv, int options, int (*compar)(const FTSENT**, const FTSENT**)) { - Fts_ = yfts_open(argv, options, compar); + TFileTree(char* const* argv, int options, int (*compar)(const FTSENT**, const FTSENT**)) { + Fts_ = yfts_open(argv, options, compar); } - ~TFileTree() { - yfts_close(Fts_); + ~TFileTree() { + yfts_close(Fts_); } FTS* operator()() { - return Fts_; + return Fts_; } - + private: - FTS* Fts_; + FTS* Fts_; }; -void TFtsTest::TestSimple() { +void TFtsTest::TestSimple() { const char* dotPath[2] = {"." LOCSLASH_S, nullptr}; - TFileTree currentDirTree((char* const*)dotPath, 0, FtsCmp); + TFileTree currentDirTree((char* const*)dotPath, 0, FtsCmp); UNIT_ASSERT(currentDirTree()); TTempDir tempDir = MakeTempName(yfts_read(currentDirTree())->fts_path); MakeDirIfNotExist(tempDir().data()); @@ -67,7 +67,7 @@ void TFtsTest::TestSimple() { MakeFile((tempDir() + LOCSLASH_S "dir2" LOCSLASH_S "file4").data()); const char* path[2] = {tempDir().data(), nullptr}; - TFileTree fileTree((char* const*)path, 0, FtsCmp); + TFileTree fileTree((char* const*)path, 0, FtsCmp); UNIT_ASSERT(fileTree()); CheckEnt(yfts_read(fileTree()), tempDir().data(), FTS_D); CheckEnt(yfts_read(fileTree()), (tempDir() + LOCSLASH_S "dir1").data(), FTS_D); @@ -120,4 +120,4 @@ void TFtsTest::TestNoLeakChangingAccessToFolder() { chmodFuture.Wait(); } -UNIT_TEST_SUITE_REGISTRATION(TFtsTest); +UNIT_TEST_SUITE_REGISTRATION(TFtsTest); diff --git a/util/folder/iterator.cpp b/util/folder/iterator.cpp index 73703d31f9..b5969f7d0f 100644 --- a/util/folder/iterator.cpp +++ b/util/folder/iterator.cpp @@ -1,11 +1,11 @@ -#include "iterator.h" - -#include <cstring> - -static int SortFTSENTByName(const FTSENT** a, const FTSENT** b) { - return strcmp((*a)->fts_name, (*b)->fts_name); -} - +#include "iterator.h" + +#include <cstring> + +static int SortFTSENTByName(const FTSENT** a, const FTSENT** b) { + return strcmp((*a)->fts_name, (*b)->fts_name); +} + TDirIterator::TOptions& TDirIterator::TOptions::SetSortByName() noexcept { - return SetSortFunctor(SortFTSENTByName); -} + return SetSortFunctor(SortFTSENTByName); +} diff --git a/util/folder/iterator.h b/util/folder/iterator.h index 69e025b9c4..35c6538e31 100644 --- a/util/folder/iterator.h +++ b/util/folder/iterator.h @@ -1,109 +1,109 @@ #pragma once - -#include "fts.h" - -#include <util/system/error.h> -#include <util/generic/ptr.h> -#include <util/generic/iterator.h> -#include <util/generic/yexception.h> - + +#include "fts.h" + +#include <util/system/error.h> +#include <util/generic/ptr.h> +#include <util/generic/iterator.h> +#include <util/generic/yexception.h> + /// Note this magic API traverses directory hierarchy class TDirIterator: public TInputRangeAdaptor<TDirIterator> { - struct TFtsDestroy { + struct TFtsDestroy { static inline void Destroy(FTS* f) noexcept { - yfts_close(f); - } - }; - -public: - class TError: public TSystemError { - public: - inline TError(int err) - : TSystemError(err) - { - } - }; - + yfts_close(f); + } + }; + +public: + class TError: public TSystemError { + public: + inline TError(int err) + : TSystemError(err) + { + } + }; + using TCompare = int (*)(const FTSENT**, const FTSENT**); - - struct TOptions { - inline TOptions() { - Init(FTS_PHYSICAL); - } - - inline TOptions(int opts) { - Init(opts); - } - + + struct TOptions { + inline TOptions() { + Init(FTS_PHYSICAL); + } + + inline TOptions(int opts) { + Init(opts); + } + inline TOptions& SetMaxLevel(size_t level) noexcept { - MaxLevel = level; - - return *this; - } - + MaxLevel = level; + + return *this; + } + inline TOptions& SetSortFunctor(TCompare cmp) noexcept { - Cmp = cmp; - - return *this; - } - + Cmp = cmp; + + return *this; + } + TOptions& SetSortByName() noexcept; - - int FtsOptions; - size_t MaxLevel; - TCompare Cmp; - - private: + + int FtsOptions; + size_t MaxLevel; + TCompare Cmp; + + private: inline void Init(int opts) noexcept { - FtsOptions = opts | FTS_NOCHDIR; - MaxLevel = Max<size_t>(); + FtsOptions = opts | FTS_NOCHDIR; + MaxLevel = Max<size_t>(); Cmp = nullptr; - } - }; - + } + }; + inline TDirIterator(const TString& path, const TOptions& options = TOptions()) - : Options_(options) - , Path_(path) - { - Trees_[0] = Path_.begin(); + : Options_(options) + , Path_(path) + { + Trees_[0] = Path_.begin(); Trees_[1] = nullptr; - - ClearLastSystemError(); - FileTree_.Reset(yfts_open(Trees_, Options_.FtsOptions, Options_.Cmp)); - - const int err = LastSystemError(); - - if (err) { - ythrow TError(err) << "can not open '" << Path_ << "'"; - } - } - - inline FTSENT* Next() { - FTSENT* ret = yfts_read(FileTree_.Get()); - - if (ret) { - if ((size_t)(ret->fts_level + 1) > Options_.MaxLevel) { - yfts_set(FileTree_.Get(), ret, FTS_SKIP); - } - } else { - const int err = LastSystemError(); - - if (err) { - ythrow TError(err) << "error while iterating " << Path_; - } - } - - return ret; - } - + + ClearLastSystemError(); + FileTree_.Reset(yfts_open(Trees_, Options_.FtsOptions, Options_.Cmp)); + + const int err = LastSystemError(); + + if (err) { + ythrow TError(err) << "can not open '" << Path_ << "'"; + } + } + + inline FTSENT* Next() { + FTSENT* ret = yfts_read(FileTree_.Get()); + + if (ret) { + if ((size_t)(ret->fts_level + 1) > Options_.MaxLevel) { + yfts_set(FileTree_.Get(), ret, FTS_SKIP); + } + } else { + const int err = LastSystemError(); + + if (err) { + ythrow TError(err) << "error while iterating " << Path_; + } + } + + return ret; + } + inline void Skip(FTSENT* ent) { yfts_set(FileTree_.Get(), ent, FTS_SKIP); } -private: - TOptions Options_; +private: + TOptions Options_; TString Path_; - char* Trees_[2]; - THolder<FTS, TFtsDestroy> FileTree_; -}; + char* Trees_[2]; + THolder<FTS, TFtsDestroy> FileTree_; +}; diff --git a/util/folder/iterator_ut.cpp b/util/folder/iterator_ut.cpp index 936becd139..f774f528e2 100644 --- a/util/folder/iterator_ut.cpp +++ b/util/folder/iterator_ut.cpp @@ -1,14 +1,14 @@ -#include "dirut.h" -#include "iterator.h" - +#include "dirut.h" +#include "iterator.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/system/fs.h> -#include <util/system/file.h> -#include <util/generic/hash.h> -#include <util/generic/algorithm.h> -#include <util/random/mersenne.h> - + +#include <util/system/fs.h> +#include <util/system/file.h> +#include <util/generic/hash.h> +#include <util/generic/algorithm.h> +#include <util/random/mersenne.h> + static TString JoinWithNewline(const TVector<TString>& strings) { TStringStream ss; for (const auto& string : strings) { @@ -17,123 +17,123 @@ static TString JoinWithNewline(const TVector<TString>& strings) { return ss.Str(); } -class TDirIteratorTest: public TTestBase { - UNIT_TEST_SUITE(TDirIteratorTest); - UNIT_TEST(TestIt) - UNIT_TEST(TestError) - UNIT_TEST(TestLocal) +class TDirIteratorTest: public TTestBase { + UNIT_TEST_SUITE(TDirIteratorTest); + UNIT_TEST(TestIt) + UNIT_TEST(TestError) + UNIT_TEST(TestLocal) UNIT_TEST(TestSkip) - UNIT_TEST(TestSort) - UNIT_TEST_SUITE_END(); - -private: - class TDirHier { - public: - struct TPath { + UNIT_TEST(TestSort) + UNIT_TEST_SUITE_END(); + +private: + class TDirHier { + public: + struct TPath { TString Path; - int Type; - }; - + int Type; + }; + inline void AddFile(const TString& path) { - Add(path, 0); - } - + Add(path, 0); + } + inline void AddDir(const TString& path) { - Add(path, 1); - } - + Add(path, 1); + } + inline void Add(const TString& path, int type) { - const TPath p = { - path, type}; - - Add(p); - } - - inline void Add(const TPath& path) { - switch (path.Type) { - case 0: - TFile(path.Path, CreateAlways | RdWr); - break; - - case 1: + const TPath p = { + path, type}; + + Add(p); + } + + inline void Add(const TPath& path) { + switch (path.Type) { + case 0: + TFile(path.Path, CreateAlways | RdWr); + break; + + case 1: MakeDirIfNotExist(path.Path.data()); - break; - - case 2: + break; + + case 2: ythrow yexception() << "unknown path type"; - } - - Paths_.push_back(path); - Srch_[path.Path] = path; - } - + } + + Paths_.push_back(path); + Srch_[path.Path] = path; + } + inline int Type(const TString& path) { THashMap<TString, TPath>::const_iterator it = Srch_.find(path); - - UNIT_ASSERT(it != Srch_.end()); - - return it->second.Type; - } - + + UNIT_ASSERT(it != Srch_.end()); + + return it->second.Type; + } + inline bool Have(const TString& path, int type) { - return Type(path) == type; - } - - inline ~TDirHier() { - for (size_t i = 0; i < Paths_.size(); ++i) { + return Type(path) == type; + } + + inline ~TDirHier() { + for (size_t i = 0; i < Paths_.size(); ++i) { NFs::Remove(Paths_[Paths_.size() - i - 1].Path); } } - private: + private: TVector<TPath> Paths_; THashMap<TString, TPath> Srch_; - }; - - inline void TestLocal() { + }; + + inline void TestLocal() { TString dirname("." LOCSLASH_S); - TDirIterator d(dirname, FTS_NOCHDIR); + TDirIterator d(dirname, FTS_NOCHDIR); for (auto it = d.begin(); it != d.end(); ++it) { - } - } - - inline void TestIt() { - TDirHier hier; - + } + } + + inline void TestIt() { + TDirHier hier; + const TString dir = "tmpdir"; - const TDirHier::TPath path = {dir, 1}; - - hier.Add(path); - - for (size_t i = 0; i < 10; ++i) { + const TDirHier::TPath path = {dir, 1}; + + hier.Add(path); + + for (size_t i = 0; i < 10; ++i) { const TString dir1 = dir + LOCSLASH_C + ToString(i); - const TDirHier::TPath path1 = {dir1, 1}; - - hier.Add(path1); - - for (size_t j = 0; j < 10; ++j) { + const TDirHier::TPath path1 = {dir1, 1}; + + hier.Add(path1); + + for (size_t j = 0; j < 10; ++j) { const TString subdir2 = ToString(j); const TString dir2 = dir1 + LOCSLASH_C + subdir2; - const TDirHier::TPath path2 = {dir2, 1}; - - hier.Add(path2); - - for (size_t k = 0; k < 3; ++k) { + const TDirHier::TPath path2 = {dir2, 1}; + + hier.Add(path2); + + for (size_t k = 0; k < 3; ++k) { const TString file = dir2 + LOCSLASH_C + "file" + ToString(k); - const TDirHier::TPath fpath = {file, 0}; - - hier.Add(fpath); - } - } - } - - TDirIterator d(dir); - + const TDirHier::TPath fpath = {file, 0}; + + hier.Add(fpath); + } + } + } + + TDirIterator d(dir); + for (auto it = d.begin(); it != d.end(); ++it) { - UNIT_ASSERT(hier.Have(it->fts_path, it->fts_info != FTS_F)); - } - } - + UNIT_ASSERT(hier.Have(it->fts_path, it->fts_info != FTS_F)); + } + } + inline void TestSkip() { TDirHier hier; @@ -173,55 +173,55 @@ private: } } - inline void TestSort() { - TDirHier dh; + inline void TestSort() { + TDirHier dh; const TString dir("tmpdir"); - - //prepare fs - { - TMersenne<ui32> rnd; + + //prepare fs + { + TMersenne<ui32> rnd; const TString prefixes[] = { - "a", "b", "xxx", "111", ""}; - - dh.AddDir(dir); - - for (size_t i = 0; i < 100; ++i) { + "a", "b", "xxx", "111", ""}; + + dh.AddDir(dir); + + for (size_t i = 0; i < 100; ++i) { const TString fname = dir + LOCSLASH_C + prefixes[i % Y_ARRAY_SIZE(prefixes)] + ToString(rnd.GenRand()); - - dh.AddFile(fname); - } - } - + + dh.AddFile(fname); + } + } + TVector<TString> fnames; - - { - TDirIterator d(dir, TDirIterator::TOptions().SetSortByName()); - + + { + TDirIterator d(dir, TDirIterator::TOptions().SetSortByName()); + for (auto it = d.begin(); it != d.end(); ++it) { - if (it->fts_info == FTS_F) { - fnames.push_back(it->fts_name); - } - } - } - + if (it->fts_info == FTS_F) { + fnames.push_back(it->fts_name); + } + } + } + TVector<TString> sorted(fnames); - Sort(sorted.begin(), sorted.end()); - - UNIT_ASSERT_VALUES_EQUAL(JoinWithNewline(fnames), JoinWithNewline(sorted)); - } - - inline void TestError() { - try { - TDirIterator d("./notexistingfilename"); - - UNIT_ASSERT(false); - } catch (const TDirIterator::TError&) { - } catch (...) { - UNIT_ASSERT(false); - } - - UNIT_ASSERT(true); - } -}; - -UNIT_TEST_SUITE_REGISTRATION(TDirIteratorTest); + Sort(sorted.begin(), sorted.end()); + + UNIT_ASSERT_VALUES_EQUAL(JoinWithNewline(fnames), JoinWithNewline(sorted)); + } + + inline void TestError() { + try { + TDirIterator d("./notexistingfilename"); + + UNIT_ASSERT(false); + } catch (const TDirIterator::TError&) { + } catch (...) { + UNIT_ASSERT(false); + } + + UNIT_ASSERT(true); + } +}; + +UNIT_TEST_SUITE_REGISTRATION(TDirIteratorTest); diff --git a/util/folder/lstat_win.c b/util/folder/lstat_win.c index cf94cec01a..0c5334327e 100644 --- a/util/folder/lstat_win.c +++ b/util/folder/lstat_win.c @@ -1,10 +1,10 @@ #include <util/system/defaults.h> #ifdef _win_ - #include <util/system/winint.h> - #include "lstat_win.h" + #include <util/system/winint.h> + #include "lstat_win.h" -int lstat(const char* fileName, stat_struct* fileStat) { +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) { @@ -23,7 +23,7 @@ int lstat(const char* fileName, stat_struct* fileStat) { findHandle = FindFirstFileW(buf, &findBuf); if (findBuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT && (findBuf.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT || findBuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) - { + { fileStat->st_mode = fileStat->st_mode & ~_S_IFMT | _S_IFLNK; } FindClose(findHandle); diff --git a/util/folder/lstat_win.h b/util/folder/lstat_win.h index 0bf7c19055..d234ed7cc8 100644 --- a/util/folder/lstat_win.h +++ b/util/folder/lstat_win.h @@ -4,17 +4,17 @@ #include "fts.h" #ifdef _win_ - #include <sys/stat.h> + #include <sys/stat.h> - #ifdef __cplusplus + #ifdef __cplusplus extern "C" { - #endif + #endif - #define _S_IFLNK 0xA000 - int lstat(const char* fileName, stat_struct* fileStat); + #define _S_IFLNK 0xA000 + int lstat(const char* fileName, stat_struct* fileStat); - #ifdef __cplusplus + #ifdef __cplusplus } - #endif + #endif #endif //_win_ 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; } } diff --git a/util/folder/path.h b/util/folder/path.h index 2fb4d6b4ef..8d85eb77ee 100644 --- a/util/folder/path.h +++ b/util/folder/path.h @@ -1,18 +1,18 @@ #pragma once #include "fwd.h" -#include "pathsplit.h" - +#include "pathsplit.h" + #include <util/generic/ptr.h> #include <util/generic/strbuf.h> #include <util/generic/string.h> #include <util/generic/vector.h> #include <util/string/cast.h> -#include <util/system/fstat.h> +#include <util/system/fstat.h> #include <util/system/platform.h> #include <util/system/sysstat.h> #include <util/system/yassert.h> - + #include <utility> /** @@ -20,7 +20,7 @@ * It uses platform-dependent separators for path-reconstructing operations. */ class TFsPath { -private: +private: struct TSplit; public: @@ -29,15 +29,15 @@ public: TFsPath(const TStringBuf path); TFsPath(const char* path); - TFsPath(const std::string& path) - : TFsPath(TStringBuf(path)) - { - } - - void CheckDefined() const; + TFsPath(const std::string& path) + : TFsPath(TStringBuf(path)) + { + } + + void CheckDefined() const; inline bool IsDefined() const { - return Path_.length() > 0; + return Path_.length() > 0; } inline explicit operator bool() const { @@ -47,32 +47,32 @@ public: inline const char* c_str() const { return Path_.c_str(); } - + inline operator const TString&() const { - return Path_; + return Path_; } - + inline bool operator==(const TFsPath& that) const { - return Path_ == that.Path_; + return Path_ == that.Path_; } - + inline bool operator!=(const TFsPath& that) const { return Path_ != that.Path_; } TFsPath& operator/=(const TFsPath& that); - + friend TFsPath operator/(const TFsPath& s, const TFsPath& p) { TFsPath ret(s); return ret /= p; } - const TPathSplit& PathSplit() const; - + const TPathSplit& PathSplit() const; + TFsPath& Fix(); inline const TString& GetPath() const { - return Path_; + return Path_; } /// last component of path, or "/" if root @@ -109,12 +109,12 @@ public: * @return True if this is a subpath of that or they are equivalent and false otherwise. */ bool IsNonStrictSubpathOf(const TFsPath& that) const; - + bool IsContainerOf(const TFsPath& that) const { 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. @@ -165,12 +165,12 @@ public: // XXX: ino - inline bool Stat(TFileStat& stat) const { + inline bool Stat(TFileStat& stat) const { stat = TFileStat(Path_.data()); - + return stat.Mode; } - + bool Exists() const; /// false if not exists bool IsDirectory() const; @@ -198,18 +198,18 @@ public: static TFsPath Cwd(); inline void Swap(TFsPath& p) noexcept { - DoSwap(Path_, p.Path_); - Split_.Swap(p.Split_); - } - + DoSwap(Path_, p.Path_); + Split_.Swap(p.Split_); + } + private: void InitSplit() const; TSplit& GetSplit() const; - -private: + +private: TString Path_; /// cache - mutable TSimpleIntrusivePtr<TSplit> Split_; + mutable TSimpleIntrusivePtr<TSplit> Split_; }; namespace NPrivate { diff --git a/util/folder/path_ut.cpp b/util/folder/path_ut.cpp index e6a3451016..fe802707ad 100644 --- a/util/folder/path_ut.cpp +++ b/util/folder/path_ut.cpp @@ -1,59 +1,59 @@ -#include "path.h" +#include "path.h" #include "pathsplit.h" #include "dirut.h" #include "tempdir.h" - + #include <library/cpp/testing/unittest/registar.h> - + #include <util/generic/scope.h> #include <util/system/platform.h> -#include <util/system/yassert.h> +#include <util/system/yassert.h> #include <util/stream/output.h> #include <util/stream/file.h> #include <util/system/fs.h> -#include <algorithm> - +#include <algorithm> + #ifdef _win_ #include <aclapi.h> #endif -namespace { - /// empty directory for test that needs filesystem - /// recreates directory in constructor and removes directory in destructor - class TTestDirectory { - private: - TFsPath Path_; - - public: +namespace { + /// empty directory for test that needs filesystem + /// recreates directory in constructor and removes directory in destructor + class TTestDirectory { + private: + TFsPath Path_; + + public: TTestDirectory(const TString& name); - ~TTestDirectory(); - - TFsPath GetFsPath() const { - return Path_; - } - + ~TTestDirectory(); + + TFsPath GetFsPath() const { + return Path_; + } + TFsPath Child(const TString& name) const { - return Path_.Child(name); - } - }; - + return Path_.Child(name); + } + }; + TTestDirectory::TTestDirectory(const TString& name) { Y_VERIFY(name.length() > 0, "have to specify name"); Y_VERIFY(name.find('.') == TString::npos, "must be simple name"); Y_VERIFY(name.find('/') == TString::npos, "must be simple name"); Y_VERIFY(name.find('\\') == TString::npos, "must be simple name"); - Path_ = TFsPath(name); - - Path_.ForceDelete(); - Path_.MkDir(); - } - - TTestDirectory::~TTestDirectory() { - Path_.ForceDelete(); - } -} - + Path_ = TFsPath(name); + + Path_.ForceDelete(); + Path_.MkDir(); + } + + TTestDirectory::~TTestDirectory() { + Path_.ForceDelete(); + } +} + Y_UNIT_TEST_SUITE(TFsPathTests) { Y_UNIT_TEST(TestMkDirs) { const TFsPath path = "a/b/c/d/e/f"; @@ -115,38 +115,38 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { UNIT_ASSERT_VALUES_EQUAL(TFsPath(".").Parent(), TFsPath("..")); UNIT_ASSERT_VALUES_EQUAL(TFsPath("..").Parent(), TFsPath("../..")); #endif - } + } Y_UNIT_TEST(GetName) { - TTestDirectory d("GetName"); + TTestDirectory d("GetName"); UNIT_ASSERT_VALUES_EQUAL(TString("dfgh"), d.Child("dfgh").GetName()); - // check does not fail - TFsPath(".").GetName(); + // check does not fail + TFsPath(".").GetName(); #ifdef _unix_ UNIT_ASSERT_VALUES_EQUAL(TString("/"), TFsPath("/").GetName()); #endif - } + } Y_UNIT_TEST(GetExtension) { - TTestDirectory d("GetExtension"); + TTestDirectory d("GetExtension"); UNIT_ASSERT_VALUES_EQUAL("", d.Child("a").GetExtension()); UNIT_ASSERT_VALUES_EQUAL("", d.Child(".a").GetExtension()); UNIT_ASSERT_VALUES_EQUAL("", d.Child("zlib").GetExtension()); UNIT_ASSERT_VALUES_EQUAL("zlib", d.Child("file.zlib").GetExtension()); UNIT_ASSERT_VALUES_EQUAL("zlib", d.Child("file.ylib.zlib").GetExtension()); - } + } Y_UNIT_TEST(TestRename) { - TTestDirectory xx("TestRename"); - TFsPath f1 = xx.Child("f1"); - TFsPath f2 = xx.Child("f2"); - f1.Touch(); - f1.RenameTo(f2); - UNIT_ASSERT(!f1.Exists()); - UNIT_ASSERT(f2.Exists()); - } + TTestDirectory xx("TestRename"); + TFsPath f1 = xx.Child("f1"); + TFsPath f2 = xx.Child("f2"); + f1.Touch(); + f1.RenameTo(f2); + UNIT_ASSERT(!f1.Exists()); + UNIT_ASSERT(f2.Exists()); + } Y_UNIT_TEST(TestForceRename) { TTestDirectory xx("TestForceRename"); @@ -170,11 +170,11 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { Y_UNIT_TEST(TestRenameFail) { UNIT_ASSERT_EXCEPTION(TFsPath("sfsfsfsdfsfsdfdf").RenameTo("sdfsdf"), TIoException); - } + } #ifndef _win_ Y_UNIT_TEST(TestRealPath) { - UNIT_ASSERT(TFsPath(".").RealPath().IsDirectory()); + UNIT_ASSERT(TFsPath(".").RealPath().IsDirectory()); TTestDirectory td("TestRealPath"); TFsPath link = td.Child("link"); @@ -187,19 +187,19 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { UNIT_ASSERT(NFs::Remove(link.GetPath())); UNIT_ASSERT(NFs::SymLink(target2.RealPath(), link.GetPath())); UNIT_ASSERT_VALUES_EQUAL(link.RealPath(), target2.RealPath()); // must not cache old value - } + } #endif Y_UNIT_TEST(TestSlashesAndBasename) { - TFsPath p("/db/BASE/primus121-025-1380131338//"); + TFsPath p("/db/BASE/primus121-025-1380131338//"); UNIT_ASSERT_VALUES_EQUAL(p.Basename(), TString("primus121-025-1380131338")); - TFsPath testP = p / "test"; + TFsPath testP = p / "test"; #ifdef _win_ UNIT_ASSERT_VALUES_EQUAL(testP.GetPath(), "\\db\\BASE\\primus121-025-1380131338\\test"); #else UNIT_ASSERT_VALUES_EQUAL(testP.GetPath(), "/db/BASE/primus121-025-1380131338/test"); #endif - } + } Y_UNIT_TEST(TestSlashesAndBasenameWin) { TFsPath p("\\db\\BASE\\primus121-025-1380131338\\\\"); @@ -226,27 +226,27 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { } Y_UNIT_TEST(TestList) { - TTestDirectory td("TestList-dir"); + TTestDirectory td("TestList-dir"); - TFsPath dir = td.GetFsPath(); - dir.Child("a").Touch(); - dir.Child("b").MkDir(); - dir.Child("b").Child("b-1").Touch(); - dir.Child("c").MkDir(); - dir.Child("d").Touch(); + TFsPath dir = td.GetFsPath(); + dir.Child("a").Touch(); + dir.Child("b").MkDir(); + dir.Child("b").Child("b-1").Touch(); + dir.Child("c").MkDir(); + dir.Child("d").Touch(); TVector<TString> children; - dir.ListNames(children); - std::sort(children.begin(), children.end()); + dir.ListNames(children); + std::sort(children.begin(), children.end()); TVector<TString> expected; - expected.push_back("a"); - expected.push_back("b"); - expected.push_back("c"); - expected.push_back("d"); + expected.push_back("a"); + expected.push_back("b"); + expected.push_back("c"); + expected.push_back("d"); UNIT_ASSERT_VALUES_EQUAL(expected, children); - } + } #ifdef _unix_ Y_UNIT_TEST(MkDirMode) { @@ -259,39 +259,39 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { // mkdir(2) places umask(2) on mode argument. const int mask = Umask(0); Umask(mask); - UNIT_ASSERT_VALUES_EQUAL(stat.Mode& MODE0777, mode & ~mask); + UNIT_ASSERT_VALUES_EQUAL(stat.Mode& MODE0777, mode & ~mask); } #endif Y_UNIT_TEST(Cwd) { UNIT_ASSERT_VALUES_EQUAL(TFsPath::Cwd().RealPath(), TFsPath(".").RealPath()); - } + } Y_UNIT_TEST(TestSubpathOf) { - UNIT_ASSERT(TFsPath("/a/b/c/d").IsSubpathOf("/a/b")); + UNIT_ASSERT(TFsPath("/a/b/c/d").IsSubpathOf("/a/b")); - UNIT_ASSERT(TFsPath("/a").IsSubpathOf("/")); - UNIT_ASSERT(!TFsPath("/").IsSubpathOf("/a")); - UNIT_ASSERT(!TFsPath("/a").IsSubpathOf("/a")); + UNIT_ASSERT(TFsPath("/a").IsSubpathOf("/")); + UNIT_ASSERT(!TFsPath("/").IsSubpathOf("/a")); + UNIT_ASSERT(!TFsPath("/a").IsSubpathOf("/a")); - UNIT_ASSERT(TFsPath("/a/b").IsSubpathOf("/a")); - UNIT_ASSERT(TFsPath("a/b").IsSubpathOf("a")); - UNIT_ASSERT(!TFsPath("/a/b").IsSubpathOf("/b")); - UNIT_ASSERT(!TFsPath("a/b").IsSubpathOf("b")); + UNIT_ASSERT(TFsPath("/a/b").IsSubpathOf("/a")); + UNIT_ASSERT(TFsPath("a/b").IsSubpathOf("a")); + UNIT_ASSERT(!TFsPath("/a/b").IsSubpathOf("/b")); + UNIT_ASSERT(!TFsPath("a/b").IsSubpathOf("b")); - // mixing absolute/relative - UNIT_ASSERT(!TFsPath("a").IsSubpathOf("/")); - UNIT_ASSERT(!TFsPath("a").IsSubpathOf("/a")); - UNIT_ASSERT(!TFsPath("/a").IsSubpathOf("a")); - UNIT_ASSERT(!TFsPath("a/b").IsSubpathOf("/a")); - UNIT_ASSERT(!TFsPath("/a/b").IsSubpathOf("a")); + // mixing absolute/relative + UNIT_ASSERT(!TFsPath("a").IsSubpathOf("/")); + UNIT_ASSERT(!TFsPath("a").IsSubpathOf("/a")); + UNIT_ASSERT(!TFsPath("/a").IsSubpathOf("a")); + UNIT_ASSERT(!TFsPath("a/b").IsSubpathOf("/a")); + UNIT_ASSERT(!TFsPath("/a/b").IsSubpathOf("a")); #ifdef _win_ - UNIT_ASSERT(TFsPath("x:/a/b").IsSubpathOf("x:/a")); - UNIT_ASSERT(!TFsPath("x:/a/b").IsSubpathOf("y:/a")); - UNIT_ASSERT(!TFsPath("x:/a/b").IsSubpathOf("a")); + UNIT_ASSERT(TFsPath("x:/a/b").IsSubpathOf("x:/a")); + UNIT_ASSERT(!TFsPath("x:/a/b").IsSubpathOf("y:/a")); + UNIT_ASSERT(!TFsPath("x:/a/b").IsSubpathOf("a")); #endif - } + } Y_UNIT_TEST(TestNonStrictSubpathOf) { UNIT_ASSERT(TFsPath("/a/b/c/d").IsNonStrictSubpathOf("/a/b")); @@ -343,7 +343,7 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { UNIT_ASSERT_VALUES_EQUAL(TFsPath("a/.././b").RelativePath(TFsPath("b/c")), TFsPath("..")); UNIT_ASSERT_EXCEPTION(TFsPath("a/b/c").RelativePath(TFsPath("d/e")), TIoException); - } + } Y_UNIT_TEST(TestUndefined) { UNIT_ASSERT_VALUES_EQUAL(TFsPath(), TFsPath("")); @@ -364,9 +364,9 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { UNIT_ASSERT_VALUES_EQUAL(TFsPath("."), TFsPath() / "."); UNIT_ASSERT_VALUES_EQUAL(TFsPath("."), "." / TFsPath()); - UNIT_ASSERT(TFsPath().PathSplit().empty()); - UNIT_ASSERT(!TFsPath().PathSplit().IsAbsolute); - UNIT_ASSERT(TFsPath().IsRelative()); // undefined path is relative + UNIT_ASSERT(TFsPath().PathSplit().empty()); + UNIT_ASSERT(!TFsPath().PathSplit().IsAbsolute); + UNIT_ASSERT(TFsPath().IsRelative()); // undefined path is relative UNIT_ASSERT_VALUES_EQUAL(TFsPath().GetPath(), ""); UNIT_ASSERT_VALUES_EQUAL(TFsPath().GetName(), ""); @@ -377,21 +377,21 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { UNIT_ASSERT_VALUES_EQUAL(TFsPath().Basename(), ""); UNIT_ASSERT_VALUES_EQUAL(TFsPath().Dirname(), ""); - UNIT_ASSERT(!TFsPath().IsSubpathOf("a/b")); - UNIT_ASSERT(TFsPath().IsContainerOf("a/b")); - UNIT_ASSERT(!TFsPath().IsContainerOf("/a/b")); + UNIT_ASSERT(!TFsPath().IsSubpathOf("a/b")); + UNIT_ASSERT(TFsPath().IsContainerOf("a/b")); + UNIT_ASSERT(!TFsPath().IsContainerOf("/a/b")); #ifdef _win_ UNIT_ASSERT_VALUES_EQUAL(TFsPath("a\\b").RelativeTo(TFsPath()), TFsPath("a\\b")); #else UNIT_ASSERT_VALUES_EQUAL(TFsPath("a/b").RelativeTo(TFsPath()), TFsPath("a/b")); #endif - UNIT_ASSERT(!TFsPath().Exists()); - UNIT_ASSERT(!TFsPath().IsFile()); - UNIT_ASSERT(!TFsPath().IsDirectory()); - TFileStat stat; - UNIT_ASSERT(!TFsPath().Stat(stat)); - } + UNIT_ASSERT(!TFsPath().Exists()); + UNIT_ASSERT(!TFsPath().IsFile()); + UNIT_ASSERT(!TFsPath().IsDirectory()); + TFileStat stat; + UNIT_ASSERT(!TFsPath().Stat(stat)); + } Y_UNIT_TEST(TestJoinFsPaths) { #ifdef _win_ @@ -402,14 +402,14 @@ Y_UNIT_TEST_SUITE(TFsPathTests) { UNIT_ASSERT_VALUES_EQUAL(JoinFsPaths("a\\b\\..\\c"), "a\\b\\..\\c"); UNIT_ASSERT_VALUES_EQUAL(JoinFsPaths("a\\b", ""), "a\\b"); #else - UNIT_ASSERT_VALUES_EQUAL(JoinFsPaths("a/b", "c/d"), "a/b/c/d"); + UNIT_ASSERT_VALUES_EQUAL(JoinFsPaths("a/b", "c/d"), "a/b/c/d"); UNIT_ASSERT_VALUES_EQUAL(JoinFsPaths("a/b", "../c"), "a/b/../c"); UNIT_ASSERT_VALUES_EQUAL(JoinFsPaths("a/b/../c", "d"), "a/c/d"); UNIT_ASSERT_VALUES_EQUAL(JoinFsPaths("a", "b", "c", "d"), "a/b/c/d"); UNIT_ASSERT_VALUES_EQUAL(JoinFsPaths("a/b/../c"), "a/b/../c"); UNIT_ASSERT_VALUES_EQUAL(JoinFsPaths("a/b", ""), "a/b"); #endif - } + } Y_UNIT_TEST(TestStringCast) { TFsPath pathOne; diff --git a/util/folder/pathsplit.cpp b/util/folder/pathsplit.cpp index 81d439a727..15d5b8e616 100644 --- a/util/folder/pathsplit.cpp +++ b/util/folder/pathsplit.cpp @@ -1,138 +1,138 @@ -#include "pathsplit.h" - +#include "pathsplit.h" + #include "dirut.h" -#include <util/stream/output.h> -#include <util/generic/yexception.h> - -template <class T> -static inline size_t ToReserve(const T& t) { - size_t ret = t.size() + 5; - - for (auto it = t.begin(); it != t.end(); ++it) { - ret += it->size(); - } - - return ret; -} - +#include <util/stream/output.h> +#include <util/generic/yexception.h> + +template <class T> +static inline size_t ToReserve(const T& t) { + size_t ret = t.size() + 5; + + for (auto it = t.begin(); it != t.end(); ++it) { + ret += it->size(); + } + + return ret; +} + void TPathSplitTraitsUnix::DoParseFirstPart(const TStringBuf part) { if (part == TStringBuf(".")) { push_back(TStringBuf(".")); - - return; - } - - if (IsAbsolutePath(part)) { + + return; + } + + if (IsAbsolutePath(part)) { IsAbsolute = true; - } - - DoParsePart(part); -} - + } + + DoParsePart(part); +} + void TPathSplitTraitsUnix::DoParsePart(const TStringBuf part0) { DoAppendHint(part0.size() / 8); - - TStringBuf next(part0); - TStringBuf part; - + + TStringBuf next(part0); + TStringBuf part; + while (TStringBuf(next).TrySplit('/', part, next)) { - AppendComponent(part); - } - - AppendComponent(next); -} - + AppendComponent(part); + } + + AppendComponent(next); +} + void TPathSplitTraitsWindows::DoParseFirstPart(const TStringBuf part0) { - TStringBuf part(part0); - + TStringBuf part(part0); + if (part == TStringBuf(".")) { push_back(TStringBuf(".")); - - return; - } - - if (IsAbsolutePath(part)) { - IsAbsolute = true; - + + return; + } + + if (IsAbsolutePath(part)) { + IsAbsolute = true; + if (part.size() > 1 && part[1] == ':') { - Drive = part.SubStr(0, 2); - part = part.SubStr(2); - } - } - - DoParsePart(part); -} - + Drive = part.SubStr(0, 2); + part = part.SubStr(2); + } + } + + DoParsePart(part); +} + void TPathSplitTraitsWindows::DoParsePart(const TStringBuf part0) { DoAppendHint(part0.size() / 8); - - size_t pos = 0; - TStringBuf part(part0); - + + size_t pos = 0; + TStringBuf part(part0); + while (pos < part.size()) { while (pos < part.size() && this->IsPathSep(part[pos])) { - ++pos; - } - + ++pos; + } + const char* begin = part.data() + pos; - + while (pos < part.size() && !this->IsPathSep(part[pos])) { - ++pos; - } - + ++pos; + } + AppendComponent(TStringBuf(begin, part.data() + pos)); - } -} - + } +} + TString TPathSplitStore::DoReconstruct(const TStringBuf slash) const { TString r; - - r.reserve(ToReserve(*this)); - - if (IsAbsolute) { - r.AppendNoAlias(Drive); - r.AppendNoAlias(slash); - } - - for (auto i = begin(); i != end(); ++i) { - if (i != begin()) { - r.AppendNoAlias(slash); - } - - r.AppendNoAlias(*i); - } - - return r; -} - + + r.reserve(ToReserve(*this)); + + if (IsAbsolute) { + r.AppendNoAlias(Drive); + r.AppendNoAlias(slash); + } + + for (auto i = begin(); i != end(); ++i) { + if (i != begin()) { + r.AppendNoAlias(slash); + } + + r.AppendNoAlias(*i); + } + + return r; +} + void TPathSplitStore::AppendComponent(const TStringBuf comp) { if (!comp || comp == TStringBuf(".")) { - ; // ignore + ; // ignore } else if (comp == TStringBuf("..") && !empty() && back() != TStringBuf("..")) { - pop_back(); - } else { - // push back first .. also - push_back(comp); - } -} - -TStringBuf TPathSplitStore::Extension() const { + pop_back(); + } else { + // push back first .. also + push_back(comp); + } +} + +TStringBuf TPathSplitStore::Extension() const { return size() > 0 ? CutExtension(back()) : TStringBuf(); } -template <> +template <> void Out<TPathSplit>(IOutputStream& o, const TPathSplit& ps) { - o << ps.Reconstruct(); -} - + o << ps.Reconstruct(); +} + TString JoinPaths(const TPathSplit& p1, const TPathSplit& p2) { - if (p2.IsAbsolute) { - ythrow yexception() << "can not join " << p1 << " and " << p2; - } - - return TPathSplit(p1).AppendMany(p2.begin(), p2.end()).Reconstruct(); -} + if (p2.IsAbsolute) { + ythrow yexception() << "can not join " << p1 << " and " << p2; + } + + return TPathSplit(p1).AppendMany(p2.begin(), p2.end()).Reconstruct(); +} TStringBuf CutExtension(const TStringBuf fileName) { if (fileName.empty()) { diff --git a/util/folder/pathsplit.h b/util/folder/pathsplit.h index d134338e35..c6c0458398 100644 --- a/util/folder/pathsplit.h +++ b/util/folder/pathsplit.h @@ -1,113 +1,113 @@ -#pragma once - -#include <util/generic/vector.h> -#include <util/generic/strbuf.h> +#pragma once + +#include <util/generic/vector.h> +#include <util/generic/strbuf.h> #include <util/generic/string.h> #include <util/string/ascii.h> - -//do not own any data + +//do not own any data struct TPathSplitStore: public TVector<TStringBuf> { - TStringBuf Drive; - bool IsAbsolute = false; - + TStringBuf Drive; + bool IsAbsolute = false; + void AppendComponent(const TStringBuf comp); - TStringBuf Extension() const; - -protected: + TStringBuf Extension() const; + +protected: TString DoReconstruct(const TStringBuf slash) const; - - inline void DoAppendHint(size_t hint) { - reserve(size() + hint); - } -}; - -struct TPathSplitTraitsUnix: public TPathSplitStore { + + inline void DoAppendHint(size_t hint) { + reserve(size() + hint); + } +}; + +struct TPathSplitTraitsUnix: public TPathSplitStore { static constexpr char MainPathSep = '/'; inline TString Reconstruct() const { return DoReconstruct(TStringBuf("/")); - } - + } + static constexpr bool IsPathSep(const char c) noexcept { - return c == '/'; - } - + return c == '/'; + } + static inline bool IsAbsolutePath(const TStringBuf path) noexcept { - return path && IsPathSep(path[0]); - } - + return path && IsPathSep(path[0]); + } + void DoParseFirstPart(const TStringBuf part); void DoParsePart(const TStringBuf part); -}; - -struct TPathSplitTraitsWindows: public TPathSplitStore { +}; + +struct TPathSplitTraitsWindows: public TPathSplitStore { static constexpr char MainPathSep = '\\'; inline TString Reconstruct() const { return DoReconstruct(TStringBuf("\\")); - } - + } + static constexpr bool IsPathSep(char c) noexcept { - return c == '/' || c == '\\'; - } - + return c == '/' || c == '\\'; + } + static inline bool IsAbsolutePath(const TStringBuf path) noexcept { return path && (IsPathSep(path[0]) || (path.size() > 1 && path[1] == ':' && IsAsciiAlpha(path[0]) && (path.size() == 2 || IsPathSep(path[2])))); - } - + } + void DoParseFirstPart(const TStringBuf part); void DoParsePart(const TStringBuf part); -}; - -#if defined(_unix_) -using TPathSplitTraitsLocal = TPathSplitTraitsUnix; -#else -using TPathSplitTraitsLocal = TPathSplitTraitsWindows; -#endif - -template <class TTraits> -class TPathSplitBase: public TTraits { -public: +}; + +#if defined(_unix_) +using TPathSplitTraitsLocal = TPathSplitTraitsUnix; +#else +using TPathSplitTraitsLocal = TPathSplitTraitsWindows; +#endif + +template <class TTraits> +class TPathSplitBase: public TTraits { +public: inline TPathSplitBase() = default; - + inline TPathSplitBase(const TStringBuf part) { - this->ParseFirstPart(part); - } - - inline TPathSplitBase& AppendHint(size_t hint) { - this->DoAppendHint(hint); - - return *this; - } - + this->ParseFirstPart(part); + } + + inline TPathSplitBase& AppendHint(size_t hint) { + this->DoAppendHint(hint); + + return *this; + } + inline TPathSplitBase& ParseFirstPart(const TStringBuf part) { - this->DoParseFirstPart(part); - - return *this; - } - + this->DoParseFirstPart(part); + + return *this; + } + inline TPathSplitBase& ParsePart(const TStringBuf part) { - this->DoParsePart(part); - - return *this; - } - - template <class It> - inline TPathSplitBase& AppendMany(It b, It e) { - this->AppendHint(e - b); - - while (b != e) { - this->AppendComponent(*b++); - } - - return *this; - } -}; - -using TPathSplit = TPathSplitBase<TPathSplitTraitsLocal>; -using TPathSplitUnix = TPathSplitBase<TPathSplitTraitsUnix>; -using TPathSplitWindows = TPathSplitBase<TPathSplitTraitsWindows>; - + this->DoParsePart(part); + + return *this; + } + + template <class It> + inline TPathSplitBase& AppendMany(It b, It e) { + this->AppendHint(e - b); + + while (b != e) { + this->AppendComponent(*b++); + } + + return *this; + } +}; + +using TPathSplit = TPathSplitBase<TPathSplitTraitsLocal>; +using TPathSplitUnix = TPathSplitBase<TPathSplitTraitsUnix>; +using TPathSplitWindows = TPathSplitBase<TPathSplitTraitsWindows>; + TString JoinPaths(const TPathSplit& p1, const TPathSplit& p2); TStringBuf CutExtension(const TStringBuf fileName); diff --git a/util/folder/pathsplit_ut.cpp b/util/folder/pathsplit_ut.cpp index 0e97afd0d0..ef014be7a9 100644 --- a/util/folder/pathsplit_ut.cpp +++ b/util/folder/pathsplit_ut.cpp @@ -3,129 +3,129 @@ #ifndef PS_INCLUDED // Outer part - #include "pathsplit.h" - - #include <library/cpp/testing/unittest/registar.h> - - #define VAR(NAME) Y_CAT(NAME, __LINE__) - - #define PS_CHECK(input, ...) \ - const char* VAR(model)[] = {"", __VA_ARGS__}; \ - UNIT_ASSERT_EQUAL(input.size(), sizeof(VAR(model)) / sizeof(const char*) - 1); \ - for (size_t n = 0; n < input.size(); ++n) { \ - UNIT_ASSERT_STRINGS_EQUAL(input[n], VAR(model)[n + 1]); \ - } - - #define PS_INCLUDED - - #define PSUF(NAME) NAME - #define PSUF_LOCAL(NAME) NAME##Local - #include __FILE__ - #undef PSUF - #undef PSUF_LOCAL - - #define PSUF(NAME) NAME##Unix - #define PSUF_LOCAL(NAME) PSUF(NAME) - #ifdef _win_ - #undef _win_ - #define REVERT_WIN - #endif - #include __FILE__ - #ifdef REVERT_WIN - #define _win_ - #undef REVERT_WIN - #endif - #undef PSUF - #undef PSUF_LOCAL - - #define PSUF(NAME) NAME##Windows - #define PSUF_LOCAL(NAME) PSUF(NAME) - #ifndef _win_ - #define _win_ - #define REVERT_WIN - #endif - #include __FILE__ - #ifdef REVERT_WIN - #undef _win_ - #undef REVERT_WIN - #endif - #undef PSUF - #undef PSUF_LOCAL - - #undef PS_INCLUDED + #include "pathsplit.h" + + #include <library/cpp/testing/unittest/registar.h> + + #define VAR(NAME) Y_CAT(NAME, __LINE__) + + #define PS_CHECK(input, ...) \ + const char* VAR(model)[] = {"", __VA_ARGS__}; \ + UNIT_ASSERT_EQUAL(input.size(), sizeof(VAR(model)) / sizeof(const char*) - 1); \ + for (size_t n = 0; n < input.size(); ++n) { \ + UNIT_ASSERT_STRINGS_EQUAL(input[n], VAR(model)[n + 1]); \ + } + + #define PS_INCLUDED + + #define PSUF(NAME) NAME + #define PSUF_LOCAL(NAME) NAME##Local + #include __FILE__ + #undef PSUF + #undef PSUF_LOCAL + + #define PSUF(NAME) NAME##Unix + #define PSUF_LOCAL(NAME) PSUF(NAME) + #ifdef _win_ + #undef _win_ + #define REVERT_WIN + #endif + #include __FILE__ + #ifdef REVERT_WIN + #define _win_ + #undef REVERT_WIN + #endif + #undef PSUF + #undef PSUF_LOCAL + + #define PSUF(NAME) NAME##Windows + #define PSUF_LOCAL(NAME) PSUF(NAME) + #ifndef _win_ + #define _win_ + #define REVERT_WIN + #endif + #include __FILE__ + #ifdef REVERT_WIN + #undef _win_ + #undef REVERT_WIN + #endif + #undef PSUF + #undef PSUF_LOCAL + + #undef PS_INCLUDED #else // Inner part - #ifdef _win_ - #define TRUE_ONLY_WIN true - #else - #define TRUE_ONLY_WIN false - #endif + #ifdef _win_ + #define TRUE_ONLY_WIN true + #else + #define TRUE_ONLY_WIN false + #endif Y_UNIT_TEST_SUITE(PSUF(PathSplit)) { Y_UNIT_TEST(Empty) { - PSUF(TPathSplit) - ps; + PSUF(TPathSplit) + ps; PS_CHECK(ps); UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); } Y_UNIT_TEST(Relative) { - PSUF(TPathSplit) - ps("some/usual/path"); + PSUF(TPathSplit) + ps("some/usual/path"); PS_CHECK(ps, "some", "usual", "path"); UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); } Y_UNIT_TEST(Absolute) { - PSUF(TPathSplit) - ps("/some/usual/path"); + PSUF(TPathSplit) + ps("/some/usual/path"); PS_CHECK(ps, "some", "usual", "path"); UNIT_ASSERT_EQUAL(ps.IsAbsolute, true); } Y_UNIT_TEST(Self) { - PSUF(TPathSplit) - ps("."); + PSUF(TPathSplit) + ps("."); PS_CHECK(ps, "."); UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); } Y_UNIT_TEST(Parent) { - PSUF(TPathSplit) - ps(".."); + PSUF(TPathSplit) + ps(".."); PS_CHECK(ps, ".."); UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); } Y_UNIT_TEST(Root) { - PSUF(TPathSplit) - ps("/"); + PSUF(TPathSplit) + ps("/"); PS_CHECK(ps); UNIT_ASSERT_EQUAL(ps.IsAbsolute, true); } Y_UNIT_TEST(Reconstruct) { - PSUF(TPathSplit) - ps("some/usual/path/../../other/././//path"); - #ifdef _win_ - UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some\\other\\path"); - #else - UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some/other/path"); - #endif - - ps = PSUF(TPathSplit)("/some/usual/path/../../other/././//path"); - #ifdef _win_ - UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "\\some\\other\\path"); - #else - UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "/some/other/path"); - #endif + PSUF(TPathSplit) + ps("some/usual/path/../../other/././//path"); + #ifdef _win_ + UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some\\other\\path"); + #else + UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some/other/path"); + #endif + + ps = PSUF(TPathSplit)("/some/usual/path/../../other/././//path"); + #ifdef _win_ + UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "\\some\\other\\path"); + #else + UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "/some/other/path"); + #endif } Y_UNIT_TEST(ParseFirstPart) { - PSUF(TPathSplit) - ps; + PSUF(TPathSplit) + ps; ps.ParseFirstPart("some/usual/path"); PS_CHECK(ps, "some", "usual", "path"); UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); @@ -137,8 +137,8 @@ Y_UNIT_TEST_SUITE(PSUF(PathSplit)) { } Y_UNIT_TEST(ParsePart) { - PSUF(TPathSplit) - ps("some/usual/path"); + PSUF(TPathSplit) + ps("some/usual/path"); ps.ParsePart("sub/path"); PS_CHECK(ps, "some", "usual", "path", "sub", "path"); UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); @@ -150,8 +150,8 @@ Y_UNIT_TEST_SUITE(PSUF(PathSplit)) { } Y_UNIT_TEST(ParsePartSelf) { - PSUF(TPathSplit) - ps("some/usual/path"); + PSUF(TPathSplit) + ps("some/usual/path"); ps.ParsePart("."); PS_CHECK(ps, "some", "usual", "path"); @@ -161,8 +161,8 @@ Y_UNIT_TEST_SUITE(PSUF(PathSplit)) { } Y_UNIT_TEST(ParsePartParent) { - PSUF(TPathSplit) - ps("some/usual/path"); + PSUF(TPathSplit) + ps("some/usual/path"); ps.ParsePart(".."); PS_CHECK(ps, "some", "usual"); @@ -182,8 +182,8 @@ Y_UNIT_TEST_SUITE(PSUF(PathSplit)) { } Y_UNIT_TEST(ParsePartOverflow) { - PSUF(TPathSplit) - ps("some/usual/path"); + PSUF(TPathSplit) + ps("some/usual/path"); ps.ParsePart("../../../../.."); PS_CHECK(ps, "..", ".."); UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); @@ -195,131 +195,131 @@ Y_UNIT_TEST_SUITE(PSUF(PathSplit)) { } Y_UNIT_TEST(WinRelative) { - PSUF(TPathSplit) - ps("some\\usual\\path"); - #ifdef _win_ + PSUF(TPathSplit) + ps("some\\usual\\path"); + #ifdef _win_ PS_CHECK(ps, "some", "usual", "path"); - #else + #else PS_CHECK(ps, "some\\usual\\path"); - #endif + #endif UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); } Y_UNIT_TEST(WinAbsolute) { - PSUF(TPathSplit) - ps("\\some\\usual\\path"); - #ifdef _win_ + PSUF(TPathSplit) + ps("\\some\\usual\\path"); + #ifdef _win_ PS_CHECK(ps, "some", "usual", "path"); - #else + #else PS_CHECK(ps, "\\some\\usual\\path"); - #endif + #endif UNIT_ASSERT_EQUAL(ps.IsAbsolute, TRUE_ONLY_WIN); - PSUF(TPathSplit) - psDrive("C:\\some\\usual\\path"); - #ifdef _win_ + PSUF(TPathSplit) + psDrive("C:\\some\\usual\\path"); + #ifdef _win_ PS_CHECK(psDrive, "some", "usual", "path"); UNIT_ASSERT_EQUAL(psDrive.Drive, "C:"); - #else + #else PS_CHECK(psDrive, "C:\\some\\usual\\path"); - #endif + #endif UNIT_ASSERT_EQUAL(psDrive.IsAbsolute, TRUE_ONLY_WIN); - PSUF(TPathSplit) - psDrive2("C:/some/usual/path"); - #ifdef _win_ + PSUF(TPathSplit) + psDrive2("C:/some/usual/path"); + #ifdef _win_ PS_CHECK(psDrive2, "some", "usual", "path"); UNIT_ASSERT_EQUAL(psDrive2.Drive, "C:"); - #else + #else PS_CHECK(psDrive2, "C:", "some", "usual", "path"); - #endif + #endif UNIT_ASSERT_EQUAL(psDrive2.IsAbsolute, TRUE_ONLY_WIN); } Y_UNIT_TEST(WinRoot) { - PSUF(TPathSplit) - ps("\\"); - #ifdef _win_ + PSUF(TPathSplit) + ps("\\"); + #ifdef _win_ PS_CHECK(ps); - #else + #else PS_CHECK(ps, "\\"); - #endif + #endif UNIT_ASSERT_EQUAL(ps.IsAbsolute, TRUE_ONLY_WIN); - PSUF(TPathSplit) - psDrive("C:"); - #ifdef _win_ + PSUF(TPathSplit) + psDrive("C:"); + #ifdef _win_ PS_CHECK(psDrive); UNIT_ASSERT_EQUAL(psDrive.Drive, "C:"); - #else + #else PS_CHECK(psDrive, "C:"); - #endif + #endif UNIT_ASSERT_EQUAL(psDrive.IsAbsolute, TRUE_ONLY_WIN); } Y_UNIT_TEST(WinReconstruct) { - PSUF(TPathSplit) - ps("some\\usual\\path\\..\\..\\other\\.\\.\\\\\\path"); - #ifdef _win_ + PSUF(TPathSplit) + ps("some\\usual\\path\\..\\..\\other\\.\\.\\\\\\path"); + #ifdef _win_ UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some\\other\\path"); - #else + #else UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some\\usual\\path\\..\\..\\other\\.\\.\\\\\\path"); - #endif + #endif ps = PSUF(TPathSplit)("\\some\\usual\\path\\..\\..\\other\\.\\.\\\\\\path"); - #ifdef _win_ + #ifdef _win_ UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "\\some\\other\\path"); - #else + #else UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "\\some\\usual\\path\\..\\..\\other\\.\\.\\\\\\path"); - #endif + #endif } Y_UNIT_TEST(WinParseFirstPart) { - PSUF(TPathSplit) - ps; + PSUF(TPathSplit) + ps; ps.ParseFirstPart("some\\usual\\path"); - #ifdef _win_ + #ifdef _win_ PS_CHECK(ps, "some", "usual", "path"); - #else + #else PS_CHECK(ps, "some\\usual\\path"); - #endif + #endif UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); ps = PSUF(TPathSplit)(); ps.ParseFirstPart("\\some\\usual\\path"); - #ifdef _win_ + #ifdef _win_ PS_CHECK(ps, "some", "usual", "path"); - #else + #else PS_CHECK(ps, "\\some\\usual\\path"); - #endif + #endif UNIT_ASSERT_EQUAL(ps.IsAbsolute, TRUE_ONLY_WIN); } Y_UNIT_TEST(WinParsePart) { - PSUF(TPathSplit) - ps("some\\usual\\path"); + PSUF(TPathSplit) + ps("some\\usual\\path"); ps.ParsePart("sub\\path"); - #ifdef _win_ + #ifdef _win_ PS_CHECK(ps, "some", "usual", "path", "sub", "path"); - #else + #else PS_CHECK(ps, "some\\usual\\path", "sub\\path"); - #endif + #endif UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); ps = PSUF(TPathSplit)("some\\usual\\path"); ps.ParsePart("\\sub\\path"); - #ifdef _win_ + #ifdef _win_ PS_CHECK(ps, "some", "usual", "path", "sub", "path"); - #else + #else PS_CHECK(ps, "some\\usual\\path", "\\sub\\path"); - #endif + #endif UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); } - #ifdef _win_ + #ifdef _win_ Y_UNIT_TEST(WinParsePartSelf) { - PSUF(TPathSplit) - ps("some\\usual\\path"); + PSUF(TPathSplit) + ps("some\\usual\\path"); ps.ParsePart("."); PS_CHECK(ps, "some", "usual", "path"); @@ -329,8 +329,8 @@ Y_UNIT_TEST_SUITE(PSUF(PathSplit)) { } Y_UNIT_TEST(WinParsePartParent) { - PSUF(TPathSplit) - ps("some\\usual\\path"); + PSUF(TPathSplit) + ps("some\\usual\\path"); ps.ParsePart(".."); PS_CHECK(ps, "some", "usual"); @@ -356,8 +356,8 @@ Y_UNIT_TEST_SUITE(PSUF(PathSplit)) { } Y_UNIT_TEST(WinParsePartOverflow) { - PSUF(TPathSplit) - ps("some\\usual\\path"); + PSUF(TPathSplit) + ps("some\\usual\\path"); ps.ParsePart("..\\..\\..\\..\\.."); PS_CHECK(ps, "..", ".."); UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); @@ -373,69 +373,69 @@ Y_UNIT_TEST_SUITE(PSUF(PathSplit)) { UNIT_ASSERT_EQUAL(ps.IsAbsolute, true); UNIT_ASSERT_EQUAL(ps.Drive, "C:"); } - #endif + #endif Y_UNIT_TEST(WinMixed) { - PSUF(TPathSplit) - ps("some\\usual/path"); - #ifdef _win_ + PSUF(TPathSplit) + ps("some\\usual/path"); + #ifdef _win_ PS_CHECK(ps, "some", "usual", "path"); - #else + #else PS_CHECK(ps, "some\\usual", "path"); - #endif + #endif UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); } Y_UNIT_TEST(WinParsePartMixed) { - PSUF(TPathSplit) - ps("some\\usual/path"); + PSUF(TPathSplit) + ps("some\\usual/path"); ps.ParsePart("sub/sub\\path"); - #ifdef _win_ + #ifdef _win_ PS_CHECK(ps, "some", "usual", "path", "sub", "sub", "path"); - #else + #else PS_CHECK(ps, "some\\usual", "path", "sub", "sub\\path"); - #endif + #endif UNIT_ASSERT_EQUAL(ps.IsAbsolute, false); } Y_UNIT_TEST(BeginWithSelf) { - PSUF(TPathSplit) - ps("./some/usual/path"); + PSUF(TPathSplit) + ps("./some/usual/path"); PS_CHECK(ps, "some", "usual", "path"); - #ifdef _win_ + #ifdef _win_ UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some\\usual\\path"); - #else + #else UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "some/usual/path"); - #endif + #endif } Y_UNIT_TEST(BeginWithParent) { - PSUF(TPathSplit) - ps("../some/usual/path"); + PSUF(TPathSplit) + ps("../some/usual/path"); PS_CHECK(ps, "..", "some", "usual", "path"); - #ifdef _win_ + #ifdef _win_ UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "..\\some\\usual\\path"); - #else + #else UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "../some/usual/path"); - #endif + #endif } Y_UNIT_TEST(InOut) { - PSUF(TPathSplit) - ps("path/.."); + PSUF(TPathSplit) + ps("path/.."); PS_CHECK(ps); UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), ""); } Y_UNIT_TEST(OutIn) { - PSUF(TPathSplit) - ps("../path"); + PSUF(TPathSplit) + ps("../path"); PS_CHECK(ps, "..", "path"); - #ifdef _win_ + #ifdef _win_ UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "..\\path"); - #else + #else UNIT_ASSERT_STRINGS_EQUAL(ps.Reconstruct(), "../path"); - #endif + #endif } } @@ -477,6 +477,6 @@ Y_UNIT_TEST_SUITE(PSUF(PathSplitTraits)) { } } - #undef TRUE_ONLY_WIN + #undef TRUE_ONLY_WIN #endif diff --git a/util/folder/tempdir.cpp b/util/folder/tempdir.cpp index 6fdf8f753c..a877f9da63 100644 --- a/util/folder/tempdir.cpp +++ b/util/folder/tempdir.cpp @@ -15,9 +15,9 @@ TTempDir::TTempDir(const char* prefix, TCreationToken) , Remove(true) { char tempDir[MAX_PATH]; - if (MakeTempDir(tempDir, prefix) != 0) { + if (MakeTempDir(tempDir, prefix) != 0) { ythrow TSystemError() << "Can't create temporary directory"; - } + } TempDir = tempDir; } diff --git a/util/folder/ut/ya.make b/util/folder/ut/ya.make index 64877d9b58..bea9866134 100644 --- a/util/folder/ut/ya.make +++ b/util/folder/ut/ya.make @@ -1,15 +1,15 @@ -UNITTEST_FOR(util) +UNITTEST_FOR(util) OWNER(g:util) SUBSCRIBER(g:util-subscribers) SRCS( - folder/dirut_ut.cpp - folder/filelist_ut.cpp - folder/fts_ut.cpp - folder/iterator_ut.cpp - folder/path_ut.cpp - folder/pathsplit_ut.cpp + folder/dirut_ut.cpp + folder/filelist_ut.cpp + folder/fts_ut.cpp + folder/iterator_ut.cpp + folder/path_ut.cpp + folder/pathsplit_ut.cpp ) INCLUDE(${ARCADIA_ROOT}/util/tests/ya_util_tests.inc) |