diff options
author | leo <leo@yandex-team.ru> | 2022-02-10 16:46:40 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:40 +0300 |
commit | 980edcd3304699edf9d4e4d6a656e585028e2a72 (patch) | |
tree | 139f47f3911484ae9af0eb347b1a88bd6c4bb35f /util | |
parent | b036a557f285146e5e35d4213e29a094ab907bcf (diff) | |
download | ydb-980edcd3304699edf9d4e4d6a656e585028e2a72.tar.gz |
Restoring authorship annotation for <leo@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util')
110 files changed, 2298 insertions, 2298 deletions
diff --git a/util/datetime/cputimer.cpp b/util/datetime/cputimer.cpp index 516d372c37..24e40ff8bb 100644 --- a/util/datetime/cputimer.cpp +++ b/util/datetime/cputimer.cpp @@ -5,7 +5,7 @@ #include <util/string/printf.h> #include <util/stream/output.h> #include <util/generic/singleton.h> - + #if defined(_unix_) #include <unistd.h> #include <sched.h> @@ -14,7 +14,7 @@ #include <sys/param.h> #elif defined(_win_) #include <util/system/winint.h> -#endif +#endif TTimer::TTimer(const TStringBuf message) { static const int SMALL_DURATION_CHAR_LENGTH = 9; // strlen("0.123456s") @@ -90,7 +90,7 @@ TFormattedPrecisionTimer::~TFormattedPrecisionTimer() { *Out << Message << ": " << diff << " ticks " << FormatCycles(diff) << Endl; } - + TFuncTimer::TFuncTimer(const char* func) : Start_(TInstant::Now()) , Func_(func) @@ -103,26 +103,26 @@ TFuncTimer::~TFuncTimer() { } TTimeLogger::TTimeLogger(const TString& message, bool verbose) - : Message(message) + : Message(message) , Verbose(verbose) - , OK(false) + , OK(false) , Begin(time(nullptr)) , BeginCycles(GetCycleCount()) -{ +{ if (Verbose) { fprintf(stderr, "=========================================================\n"); fprintf(stderr, "%s started: %.24s (%lu) (%d)\n", Message.data(), ctime(&Begin), (unsigned long)Begin, (int)getpid()); } -} - +} + double TTimeLogger::ElapsedTime() const { return time(nullptr) - Begin; } void TTimeLogger::SetOK() { - OK = true; -} - + OK = true; +} + TTimeLogger::~TTimeLogger() { time_t tim = time(nullptr); ui64 endCycles = GetCycleCount(); @@ -133,4 +133,4 @@ TTimeLogger::~TTimeLogger() { (unsigned long)tim - (unsigned long)Begin, FormatCycles(endCycles - BeginCycles).data()); fprintf(stderr, "%s=========================================================\n", prefix); } -} +} diff --git a/util/datetime/cputimer.h b/util/datetime/cputimer.h index 7d38d5bdb3..b7849cf735 100644 --- a/util/datetime/cputimer.h +++ b/util/datetime/cputimer.h @@ -7,7 +7,7 @@ #include <util/stream/str.h> class TTimer { -private: +private: TInstant Start_; TStringStream Message_; @@ -108,17 +108,17 @@ public: #endif class TTimeLogger { -private: +private: TString Message; bool Verbose; - bool OK; + bool OK; time_t Begin; ui64 BeginCycles; - + public: TTimeLogger(const TString& message, bool verbose = true); - ~TTimeLogger(); + ~TTimeLogger(); - void SetOK(); + void SetOK(); double ElapsedTime() const; -}; +}; diff --git a/util/datetime/parser.rl6 b/util/datetime/parser.rl6 index 931f09eae1..8e47b66179 100644 --- a/util/datetime/parser.rl6 +++ b/util/datetime/parser.rl6 @@ -7,7 +7,7 @@ #include <util/datetime/parser.h> #include <util/generic/ymath.h> - + %%{ @@ -119,18 +119,18 @@ action set_mil_offset { if (c <= 'M') { // ['A'..'M'] \ 'J' if (c < 'J') - DateTimeFields.ZoneOffsetMinutes = (i32)TDuration::Hours(c - 'A' + 1).Minutes(); + DateTimeFields.ZoneOffsetMinutes = (i32)TDuration::Hours(c - 'A' + 1).Minutes(); else - DateTimeFields.ZoneOffsetMinutes = (i32)TDuration::Hours(c - 'A').Minutes(); + DateTimeFields.ZoneOffsetMinutes = (i32)TDuration::Hours(c - 'A').Minutes(); } else { // ['N'..'Y'] - DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(c - 'N' + 1).Minutes(); + DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(c - 'N' + 1).Minutes(); } } } action set_digit_offset { - DateTimeFields.ZoneOffsetMinutes = Sign * (i32)(TDuration::Hours(I / 100) + TDuration::Minutes(I % 100)).Minutes(); + DateTimeFields.ZoneOffsetMinutes = Sign * (i32)(TDuration::Hours(I / 100) + TDuration::Minutes(I % 100)).Minutes(); } mil_zone = /[A-IK-Za-ik-z]/ $set_mil_offset; @@ -142,14 +142,14 @@ mil_zone = /[A-IK-Za-ik-z]/ $set_mil_offset; zone = 'UT' @{ DateTimeFields.ZoneOffsetMinutes = 0; } | 'GMT' @{ DateTimeFields.ZoneOffsetMinutes = 0; } - | 'EST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(5).Minutes();} - | 'EDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(4).Minutes(); } - | 'CST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(6).Minutes();} - | 'CDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(5).Minutes(); } - | 'MST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(7).Minutes();} - | 'MDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(6).Minutes(); } - | 'PST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(8).Minutes();} - | 'PDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(7).Minutes(); }; + | 'EST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(5).Minutes();} + | 'EDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(4).Minutes(); } + | 'CST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(6).Minutes();} + | 'CDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(5).Minutes(); } + | 'MST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(7).Minutes();} + | 'MDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(6).Minutes(); } + | 'PST' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(8).Minutes();} + | 'PDT' @{ DateTimeFields.ZoneOffsetMinutes = -(i32)TDuration::Hours(7).Minutes(); }; digit_offset = ('+' | '-') > { Sign = fc == '+' ? 1 : -1; } . int4 @set_digit_offset; diff --git a/util/datetime/strptime.cpp b/util/datetime/strptime.cpp index f0d4ec333e..fb6b038bf0 100644 --- a/util/datetime/strptime.cpp +++ b/util/datetime/strptime.cpp @@ -54,14 +54,14 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <util/system/compat.h> -#include "systime.h" +#include <util/system/compat.h> +#include "systime.h" #ifdef _win32_ #ifndef lint #ifndef NOID static char copyright[] = "@(#) Copyright (c) 1994 Powerdog Industries. All rights reserved."; -static char sccsid[] = "@(#)strptime.c 0.1 (Powerdog) 94/03/27"; +static char sccsid[] = "@(#)strptime.c 0.1 (Powerdog) 94/03/27"; #endif /* !defined NOID */ #endif /* not lint */ //__FBSDID("$FreeBSD: src/lib/libc/stdtime/strptime.c,v 1.35 2003/11/17 04:19:15 nectar Exp $"); @@ -176,31 +176,31 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) char c; const char* ptr; int i; - size_t len = 0; - int Ealternative, Oalternative; + size_t len = 0; + int Ealternative, Oalternative; struct lc_time_T* tptr = __get_current_time_locale(); - ptr = fmt; - while (*ptr != 0) { - if (*buf == 0) - break; + ptr = fmt; + while (*ptr != 0) { + if (*buf == 0) + break; - c = *ptr++; + c = *ptr++; - if (c != '%') { - if (isspace((unsigned char)c)) - while (*buf != 0 && isspace((unsigned char)*buf)) + if (c != '%') { + if (isspace((unsigned char)c)) + while (*buf != 0 && isspace((unsigned char)*buf)) ++buf; - else if (c != *buf++) - return 0; - continue; - } + else if (c != *buf++) + return 0; + continue; + } - Ealternative = 0; - Oalternative = 0; + Ealternative = 0; + Oalternative = 0; label: - c = *ptr++; - switch (c) { + c = *ptr++; + switch (c) { case 0: case '%': if (*buf++ != '%') @@ -234,13 +234,13 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) buf = _strptime(buf, tptr->c_fmt, tm, GMTp); if (buf == 0) return 0; - break; + break; case 'D': buf = _strptime(buf, "%m/%d/%y", tm, GMTp); if (buf == 0) return 0; - break; + break; case 'E': if (Ealternative || Oalternative) @@ -304,7 +304,7 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) return 0; tm->tm_yday = i - 1; - break; + break; case 'M': case 'S': @@ -312,7 +312,7 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) break; if (!isdigit((unsigned char)*buf)) - return 0; + return 0; len = 2; for (i = 0; len && *buf != 0 && isdigit((unsigned char)*buf); buf++) { @@ -341,13 +341,13 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) case 'k': case 'l': /* - * Of these, %l is the only specifier explicitly - * documented as not being zero-padded. However, - * there is no harm in allowing zero-padding. - * - * XXX The %l specifier may gobble one too many - * digits if used incorrectly. - */ + * Of these, %l is the only specifier explicitly + * documented as not being zero-padded. However, + * there is no harm in allowing zero-padding. + * + * XXX The %l specifier may gobble one too many + * digits if used incorrectly. + */ if (!isdigit((unsigned char)*buf)) return 0; @@ -361,7 +361,7 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) if (i > 23) return 0; } else if (i > 12) - return 0; + return 0; tm->tm_hour = i; @@ -372,9 +372,9 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) case 'p': /* - * XXX This is bogus if parsed before hour-related - * specifiers. - */ + * XXX This is bogus if parsed before hour-related + * specifiers. + */ len = strlen(tptr->am); if (strnicmp(buf, tptr->am, len) == 0) { if (tm->tm_hour > 12) @@ -410,20 +410,20 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) break; } if (i == asizeof(tptr->weekday)) - return 0; + return 0; tm->tm_wday = i; - buf += len; - break; + buf += len; + break; case 'U': case 'W': /* - * XXX This is bogus, as we can not assume any valid - * information present in the tm structure at this - * point to calculate a real value, so just check the - * range for now. - */ + * XXX This is bogus, as we can not assume any valid + * information present in the tm structure at this + * point to calculate a real value, so just check the + * range for now. + */ if (!isdigit((unsigned char)*buf)) return 0; @@ -459,13 +459,13 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) case 'd': case 'e': /* - * The %e specifier is explicitly documented as not - * being zero-padded but there is no harm in allowing - * such padding. - * - * XXX The %e specifier may gobble one too many - * digits if used incorrectly. - */ + * The %e specifier is explicitly documented as not + * being zero-padded but there is no harm in allowing + * such padding. + * + * XXX The %e specifier may gobble one too many + * digits if used incorrectly. + */ if (!isdigit((unsigned char)*buf)) return 0; @@ -501,13 +501,13 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) len = strlen(tptr->month[i]); if (strnicmp(buf, tptr->month[i], len) == 0) - break; + break; len = strlen(tptr->mon[i]); if (strnicmp(buf, tptr->mon[i], len) == 0) break; - } - } + } + } if (i == asizeof(tptr->month)) return 0; @@ -548,7 +548,7 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) errno = sverrno; return 0; } - errno = sverrno; + errno = sverrno; buf = cp; GmTimeR(&t, tm); *GMTp = 1; @@ -587,7 +587,7 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) char* zonestr; for (cp = buf; *cp && isupper((unsigned char)*cp); ++cp) { /*empty*/ - } + } if (cp - buf) { zonestr = (char*)alloca(cp - buf + 1); strncpy(zonestr, buf, cp - buf); @@ -605,23 +605,23 @@ _strptime(const char* buf, const char* fmt, struct tm* tm, int* GMTp) buf += cp - buf; } } break; - } - } + } + } return (char*)buf; } char* strptime(const char* buf, const char* fmt, struct tm* tm) { char* ret; - int gmt; + int gmt; - gmt = 0; - ret = _strptime(buf, fmt, tm, &gmt); - if (ret && gmt) { - time_t t = timegm(tm); - localtime_r(&t, tm); - } + gmt = 0; + ret = _strptime(buf, fmt, tm, &gmt); + if (ret && gmt) { + time_t t = timegm(tm); + localtime_r(&t, tm); + } - return (ret); + return (ret); } #endif //_win32_ diff --git a/util/datetime/systime.cpp b/util/datetime/systime.cpp index 6ee7e8fc6e..b4e9c48eed 100644 --- a/util/datetime/systime.cpp +++ b/util/datetime/systime.cpp @@ -1,4 +1,4 @@ -#include "systime.h" +#include "systime.h" #include <util/system/yassert.h> #include <util/system/defaults.h> @@ -11,32 +11,32 @@ void FileTimeToTimeval(const FILETIME* ft, timeval* tv) { ui64 ft_scalar; FILETIME ft_struct; } nt_time; - nt_time.ft_struct = *ft; - tv->tv_sec = (long)((nt_time.ft_scalar - NANOINTERVAL) / LL(10000000)); - tv->tv_usec = (i32)((nt_time.ft_scalar / LL(10)) % LL(1000000)); -} - + nt_time.ft_struct = *ft; + tv->tv_sec = (long)((nt_time.ft_scalar - NANOINTERVAL) / LL(10000000)); + tv->tv_usec = (i32)((nt_time.ft_scalar / LL(10)) % LL(1000000)); +} + int gettimeofday(timeval* tp, void*) { - FILETIME ft; - GetSystemTimeAsFileTime(&ft); - FileTimeToTimeval(&ft, tp); - return 0; -} - + FILETIME ft; + GetSystemTimeAsFileTime(&ft); + FileTimeToTimeval(&ft, tp); + return 0; +} + tm* localtime_r(const time_t* clock, tm* result) { - tzset(); - tm* res = localtime(clock); - if (res) { - memcpy(result, res, sizeof(tm)); - return result; - } - return 0; -} - + tzset(); + tm* res = localtime(clock); + if (res) { + memcpy(result, res, sizeof(tm)); + return result; + } + return 0; +} + tm* gmtime_r(const time_t* clock, tm* result) { return gmtime_s(result, clock) == 0 ? result : 0; -} - +} + char* ctime_r(const time_t* clock, char* buf) { char* res = ctime(clock); if (res) { @@ -54,7 +54,7 @@ char* ctime_r(const time_t* clock, char* buf) { #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400))) #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365) #define FOURCENTURIES (400 * 365 + 100 - 3) - + //! Inverse of gmtime: converts struct tm to time_t, assuming the data //! in tm is UTC rather than local timezone. This implementation //! returns the number of seconds since 1970-01-01, converted to time_t. @@ -83,7 +83,7 @@ time_t TimeGM(const struct tm* t) { unsigned long secs = days * 86400ul + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec; return (time_t)secs; -} +} struct tm* GmTimeR(const time_t* timer, struct tm* tmbuf) { static const int _ytab[2][12] = { diff --git a/util/datetime/systime.h b/util/datetime/systime.h index 491d36e802..167c1dab86 100644 --- a/util/datetime/systime.h +++ b/util/datetime/systime.h @@ -3,7 +3,7 @@ #include <util/system/platform.h> #include <util/generic/string.h> -#include <ctime> +#include <ctime> // timegm and gmtime_r versions that don't need access to filesystem or a big stack time_t TimeGM(const struct tm* t); @@ -14,12 +14,12 @@ TString CTimeR(const time_t* timer); #ifdef _win_ #include <util/system/winint.h> #include <winsock2.h> - + void FileTimeToTimeval(const FILETIME* ft, struct timeval* tv); - + // obtains the current time, expressed as seconds and microseconds since 00:00 UTC, January 1, 1970 int gettimeofday(struct timeval* tp, void*); - + // thou should not mix these with non-_r functions tm* localtime_r(const time_t* clock, tm* result); tm* gmtime_r(const time_t* clock, tm* result); @@ -30,11 +30,11 @@ inline time_t timegm(struct tm* t) { } char* strptime(const char* buf, const char* fmt, struct tm* tm); // strptime.cpp -#else +#else #include <sys/time.h> -#endif +#endif -#ifndef timersub +#ifndef timersub #define timersub(tvp, uvp, vvp) \ do { \ (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ @@ -44,4 +44,4 @@ char* strptime(const char* buf, const char* fmt, struct tm* tm); // strptime.cpp (vvp)->tv_usec += 1000000; \ } \ } while (0) -#endif +#endif diff --git a/util/draft/ya.make b/util/draft/ya.make index e00674b682..f7129447e1 100644 --- a/util/draft/ya.make +++ b/util/draft/ya.make @@ -2,14 +2,14 @@ LIBRARY() OWNER(g:util) SUBSCRIBER(g:util-subscribers) - + NO_UTIL() - + IF (TSTRING_IS_STD_STRING) CFLAGS(GLOBAL -DTSTRING_IS_STD_STRING) ENDIF() -SRCS( +SRCS( date.cpp datetime.cpp enum.cpp @@ -17,9 +17,9 @@ SRCS( ip.cpp matrix.cpp memory.cpp -) - -END() +) + +END() RECURSE_FOR_TESTS( ut diff --git a/util/folder/dirut.cpp b/util/folder/dirut.cpp index ffc9b09f96..9a38a89ea0 100644 --- a/util/folder/dirut.cpp +++ b/util/folder/dirut.cpp @@ -12,118 +12,118 @@ #include <util/system/yassert.h> void SlashFolderLocal(TString& folder) { - if (!folder) - return; -#ifdef _win32_ - size_t pos; + if (!folder) + return; +#ifdef _win32_ + size_t pos; while ((pos = folder.find('/')) != TString::npos) folder.replace(pos, 1, LOCSLASH_S); -#endif +#endif if (folder[folder.size() - 1] != LOCSLASH_C) - folder.append(LOCSLASH_S); -} - -#ifndef _win32_ - + folder.append(LOCSLASH_S); +} + +#ifndef _win32_ + bool correctpath(TString& folder) { - return resolvepath(folder, "/"); -} - + return resolvepath(folder, "/"); +} + bool resolvepath(TString& folder, const TString& home) { Y_ASSERT(home && home.at(0) == '/'); - if (!folder) { - return false; - } - // may be from windows + if (!folder) { + return false; + } + // may be from windows char* ptr = folder.begin(); while ((ptr = strchr(ptr, '\\')) != nullptr) - *ptr = '/'; - - if (folder.at(0) == '~') { - if (folder.length() == 1 || folder.at(1) == '/') { + *ptr = '/'; + + if (folder.at(0) == '~') { + if (folder.length() == 1 || folder.at(1) == '/') { folder = GetHomeDir() + (folder.data() + 1); - } else { + } else { char* buf = (char*)alloca(folder.length() + 1); strcpy(buf, folder.data() + 1); - char* p = strchr(buf, '/'); - if (p) - *p++ = 0; - passwd* pw = getpwnam(buf); - if (pw) { - folder = pw->pw_dir; - folder += "/"; - if (p) - folder += p; - } else { - return false; // unknown user - } - } - } - int len = folder.length() + home.length() + 1; - char* path = (char*)alloca(len); - if (folder.at(0) != '/') { + char* p = strchr(buf, '/'); + if (p) + *p++ = 0; + passwd* pw = getpwnam(buf); + if (pw) { + folder = pw->pw_dir; + folder += "/"; + if (p) + folder += p; + } else { + return false; // unknown user + } + } + } + int len = folder.length() + home.length() + 1; + char* path = (char*)alloca(len); + if (folder.at(0) != '/') { strcpy(path, home.data()); strcpy(strrchr(path, '/') + 1, folder.data()); // the last char must be '/' if it's a dir - } else { + } else { strcpy(path, folder.data()); - } + } len = strlen(path) + 1; - // grabbed from url.cpp + // grabbed from url.cpp 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; - s = strchr(s, '/'); - if (s) - *s++ = 0; - } - - for (int j = 1; j < i;) { + int i = 0; + for (char* s = path; s;) { + pp[i++] = s; + s = strchr(s, '/'); + if (s) + *s++ = 0; + } + + for (int j = 1; j < i;) { const char*& p = pp[j]; if (strcmp(p, ".") == 0 || strcmp(p, "") == 0) { if (j == i - 1) { - p = ""; - break; - } else { + p = ""; + break; + } else { memmove(pp + j, pp + j + 1, (i - j - 1) * sizeof(p)); --i; - } - } else if (strcmp(p, "..") == 0) { + } + } else if (strcmp(p, "..") == 0) { if (j == i - 1) { - if (j == 1) { - p = ""; - } else { + if (j == 1) { + p = ""; + } else { --i; pp[j - 1] = ""; - } - break; - } else { - if (j == 1) { + } + break; + } else { + if (j == 1) { memmove(pp + j, pp + j + 1, (i - j - 1) * sizeof(p)); --i; - } else { + } else { memmove(pp + j - 1, pp + j + 1, (i - j - 1) * sizeof(p)); i -= 2; --j; - } - } - } else + } + } + } else ++j; - } - - char* s = newpath; - for (int k = 0; k < i; k++) { - s = strchr(strcpy(s, pp[k]), 0); - *s++ = '/'; - } - *(--s) = 0; - folder = newpath; - return true; -} - -#else - + } + + char* s = newpath; + for (int k = 0; k < i; k++) { + s = strchr(strcpy(s, pp[k]), 0); + *s++ = '/'; + } + *(--s) = 0; + folder = newpath; + return true; +} + +#else + using dir_type = enum { dt_empty, dt_error, @@ -131,17 +131,17 @@ using dir_type = enum { dt_dir }; -// precondition: *ptr != '\\' || *ptr == 0 (cause dt_error) -// postcondition: *ptr != '\\' +// precondition: *ptr != '\\' || *ptr == 0 (cause dt_error) +// postcondition: *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; - + int has_blank = 0; + int has_dot = 0; + int has_letter = 0; + int has_ctrl = 0; + while (*ptr && *ptr != '\\') { - int c = (unsigned char)*ptr++; + int c = (unsigned char)*ptr++; switch (c) { case ' ': ++has_blank; @@ -164,23 +164,23 @@ static int next_dir(T*& ptr) { ++has_ctrl; else ++has_letter; - } - } - if (*ptr) + } + } + if (*ptr) ++ptr; - if (has_ctrl) - return dt_error; - if (has_letter) - return dt_dir; - if (has_dot && has_blank) - return dt_error; - if (has_dot == 1) - return dt_empty; - if (has_dot == 2) - return dt_up; - return dt_error; -} - + if (has_ctrl) + return dt_error; + if (has_letter) + return dt_dir; + if (has_dot && has_blank) + return dt_error; + if (has_dot == 1) + return dt_empty; + if (has_dot == 2) + return dt_up; + return dt_error; +} + using disk_type = enum { dk_noflags = 0, dk_unc = 1, @@ -192,34 +192,34 @@ using disk_type = enum { // root slash (if any) - part of disk template <typename T> static int skip_disk(T*& ptr) { - int result = dk_noflags; - if (!*ptr) - return result; - if (ptr[0] == '\\' && ptr[1] == '\\') { + int result = dk_noflags; + if (!*ptr) + return result; + if (ptr[0] == '\\' && ptr[1] == '\\') { result |= dk_unc | dk_fromroot; - ptr += 2; - if (next_dir(ptr) != dt_dir) + ptr += 2; + if (next_dir(ptr) != dt_dir) return dk_error; // has no host name - if (next_dir(ptr) != dt_dir) + if (next_dir(ptr) != dt_dir) return dk_error; // has no share name - } else { + } else { if (*ptr && *(ptr + 1) == ':') { - result |= dk_hasdrive; - ptr += 2; - } - if (*ptr == '\\' || *ptr == '/') { + result |= dk_hasdrive; + ptr += 2; + } + if (*ptr == '\\' || *ptr == '/') { ++ptr; - result |= dk_fromroot; - } - } - return result; -} - + result |= dk_fromroot; + } + } + return result; +} + int correctpath(char* cpath, const char* path) { if (!path || !*path) { - *cpath = 0; - return 1; - } + *cpath = 0; + return 1; + } char* ptr = (char*)path; char* cptr = cpath; int counter = 0; @@ -238,14 +238,14 @@ int correctpath(char* cpath, const char* path) { ++ptr; } *cptr = 0; - // replace '/' by '\' - int dk = skip_disk(cpath); - - if (dk == dk_error) - return 0; + // replace '/' by '\' + int dk = skip_disk(cpath); + if (dk == dk_error) + return 0; + char* ptr1 = ptr = cpath; - int level = 0; + int level = 0; while (*ptr) { switch (next_dir(ptr)) { case dt_dir: @@ -253,8 +253,8 @@ int correctpath(char* cpath, const char* path) { break; case dt_empty: memmove(ptr1, ptr, strlen(ptr) + 1); - ptr = ptr1; - break; + ptr = ptr1; + break; case dt_up: --level; if (level >= 0) { @@ -272,100 +272,100 @@ int correctpath(char* cpath, const char* path) { memmove(cpath - 3, ptr, strlen(ptr) + 1); return 1; } - } + } if (dk & dk_fromroot) return 0; break; case dt_error: default: - return 0; - } - ptr1 = ptr; - } - + return 0; + } + ptr1 = ptr; + } + if ((ptr > cpath || ptr == cpath && dk & dk_unc) && *(ptr - 1) == '\\') *(ptr - 1) = 0; - return 1; -} - + return 1; +} + static inline int normchar(unsigned char c) { return (c < 'a' || c > 'z') ? c : c - 32; -} - +} + static inline char* strslashcat(char* a, const char* b) { - size_t len = strlen(a); + size_t len = strlen(a); if (len && a[len - 1] != '\\') - a[len++] = '\\'; + a[len++] = '\\'; strcpy(a + len, b); - return a; -} - + return a; +} + 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 (!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) { - return correctpath(apath, rpath); - } - + return correctpath(apath, rpath); + } + const char* cedisk = cpath; - if (!cpath || !*cpath) - return 0; - int cdt = skip_disk(cedisk); - if (cdt == dk_error) - return 0; - + 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); - - // rdt&dk_hasdrive && !rdt&dk_fromroot + + // rdt&dk_hasdrive && !rdt&dk_fromroot if (rdt & dk_hasdrive) { if (!(cdt & dk_fromroot)) - return 0; + return 0; if (cdt & dk_hasdrive && normchar(*rpath) != normchar(*cpath)) - return 0; - memcpy(tpath, rpath, 2); - memcpy(tpath + 2, cedisk, strlen(cedisk) + 1); - strslashcat(tpath, redisk); - + 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)) - return 0; + return 0; memcpy(tpath, cpath, cedisk - cpath); tpath[cedisk - cpath] = 0; - strslashcat(tpath, redisk); - + strslashcat(tpath, redisk); + // !rdt&dk_hasdrive && !rdt&dk_fromroot - } else { + } else { if (!(cdt & dk_fromroot) || !(cdt & dk_hasdrive) && !(cdt & dk_unc)) - return 0; - strslashcat(strcpy(tpath, cpath), redisk); - } - - return correctpath(apath, tpath); -} - + return 0; + strslashcat(strcpy(tpath, cpath), redisk); + } + + return correctpath(apath, tpath); +} + bool correctpath(TString& filename) { char* ptr = (char*)alloca(filename.size() + 2); if (correctpath(ptr, filename.data())) { - filename = ptr; - return true; - } - return false; -} - + filename = ptr; + return true; + } + return false; +} + bool resolvepath(TString& folder, const TString& home) { char* ptr = (char*)alloca(folder.size() + 3 + home.size()); if (resolvepath(ptr, folder.data(), home.data())) { - folder = ptr; - return true; - } - return false; -} - + folder = ptr; + return true; + } + return false; +} + #endif // !defined _win32_ char GetDirectorySeparator() { diff --git a/util/folder/dirut.h b/util/folder/dirut.h index 2537027b12..95e265dcd1 100644 --- a/util/folder/dirut.h +++ b/util/folder/dirut.h @@ -1,17 +1,17 @@ #pragma once #include <util/system/defaults.h> -#include <util/system/sysstat.h> +#include <util/system/sysstat.h> #include <util/system/fs.h> #include <util/generic/string.h> #include <util/generic/yexception.h> -#include <sys/types.h> - +#include <sys/types.h> + #include <cerrno> #include <cstdlib> -#ifdef _win32_ +#ifdef _win32_ #include <util/system/winint.h> #include <direct.h> #include <malloc.h> @@ -35,14 +35,14 @@ char* mkdtemp(char* path); #ifndef DT_DIR #include <sys/stat.h> #endif -#endif +#endif bool IsDir(const TString& path); int mkpath(char* path, int mode = 0777); TString GetHomeDir(); - + void MakeDirIfNotExist(const char* path, int mode = 0777); inline void MakeDirIfNotExist(const TString& path, int mode = 0777) { diff --git a/util/folder/dirut_ut.cpp b/util/folder/dirut_ut.cpp index 45ebfc842c..fd1cafcb7a 100644 --- a/util/folder/dirut_ut.cpp +++ b/util/folder/dirut_ut.cpp @@ -62,19 +62,19 @@ Y_UNIT_TEST_SUITE(TDirutTest) { UNIT_ASSERT(resolvepath(path, base)); UNIT_ASSERT(path == canon); } - + Y_UNIT_TEST(TestResolvePath) { -#ifdef _win_ +#ifdef _win_ DoTest("bar", "c:\\foo\\baz", "c:\\foo\\baz\\bar"); DoTest("c:\\foo\\bar", "c:\\bar\\baz", "c:\\foo\\bar"); -#else +#else 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 - DoTest("bar", "baz", "bar"); + DoTest("bar", "baz", "bar"); #endif #endif #endif diff --git a/util/folder/filelist.cpp b/util/folder/filelist.cpp index b21fcdbf20..848b7af96f 100644 --- a/util/folder/filelist.cpp +++ b/util/folder/filelist.cpp @@ -36,5 +36,5 @@ void TFileEntitiesList::Fill(const TString& dirname, TStringBuf prefix, TStringB } } - Restart(); + Restart(); } diff --git a/util/folder/filelist.h b/util/folder/filelist.h index 3f615fa4c2..3dea0bb913 100644 --- a/util/folder/filelist.h +++ b/util/folder/filelist.h @@ -18,7 +18,7 @@ public: EM_FILES_DIRS_SLINKS = EM_FILES | EM_DIRS | EM_SLINKS }; Y_DECLARE_FLAGS(EMask, EMaskFlag) - + TFileEntitiesList(EMask mask) : Mask(mask) { @@ -50,11 +50,11 @@ public: void Fill(const TString& dirname, TStringBuf prefix, TStringBuf suffix, int depth, bool sort = false); - void Restart() { + void Restart() { Cur = FileNames.Data(); - CurName = 0; - } - + CurName = 0; + } + protected: TBuffer FileNames; size_t FileNamesSize, CurName; diff --git a/util/folder/fts.cpp b/util/folder/fts.cpp index 0e6a6f86eb..809a589032 100644 --- a/util/folder/fts.cpp +++ b/util/folder/fts.cpp @@ -1,6 +1,6 @@ /*- * Copyright (c) 1990, 1993, 1994 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -12,8 +12,8 @@ * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. + * This product includes software developed by the University of + * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -232,20 +232,20 @@ yreallocf(void* ptr, size_t size) { void* nptr; - nptr = realloc(ptr, size); + nptr = realloc(ptr, size); if (!nptr && ptr) { - free(ptr); + free(ptr); } - return (nptr); + return (nptr); } FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, const FTSENT**)) { FTS* sp; - FTSENT *p, *root; - int nitems; - FTSENT *parent, *tmp; - int len; + FTSENT *p, *root; + int nitems; + FTSENT *parent, *tmp; + int len; errno = 0; @@ -255,122 +255,122 @@ FTS* yfts_open(char* const* argv, int options, int (*compar)(const FTSENT**, con return nullptr; } - /* Options check. */ - if (options & ~FTS_OPTIONMASK) { - errno = EINVAL; + /* Options check. */ + if (options & ~FTS_OPTIONMASK) { + errno = EINVAL; return nullptr; - } + } - /* Allocate/initialize the stream */ + /* Allocate/initialize the stream */ if ((sp = (FTS*)malloc(sizeof(FTS))) == nullptr) { return nullptr; } - memset(sp, 0, sizeof(FTS)); - sp->fts_compar = compar; - sp->fts_options = options; + memset(sp, 0, sizeof(FTS)); + sp->fts_compar = compar; + sp->fts_options = options; - /* Shush, GCC. */ + /* Shush, GCC. */ tmp = nullptr; - /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ + /* Logical walks turn on NOCHDIR; symbolic links are too hard. */ if (ISSET(FTS_LOGICAL)) { - SET(FTS_NOCHDIR); + SET(FTS_NOCHDIR); } - /* - * Start out with 1K of path space, and enough, in any case, - * to hold the user's paths. - */ + /* + * 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))) { - goto mem1; + goto mem1; } - /* Allocate/initialize root's parent. */ + /* Allocate/initialize root's parent. */ if ((parent = fts_alloc(sp, "", 0)) == nullptr) { - goto mem2; + goto mem2; } - parent->fts_level = FTS_ROOTPARENTLEVEL; + parent->fts_level = FTS_ROOTPARENTLEVEL; - /* Allocate/initialize root(s). */ + /* Allocate/initialize root(s). */ for (root = nullptr, nitems = 0; *argv; ++argv, ++nitems) { - /* Don't allow zero-length paths. */ + /* Don't allow zero-length paths. */ len = strlen(*argv); //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] == '/')) { - --len; - } + --len; + } #endif - if (len == 0) { - errno = ENOENT; - goto mem3; - } + if (len == 0) { + errno = ENOENT; + goto mem3; + } - p = fts_alloc(sp, *argv, len); - p->fts_level = FTS_ROOTLEVEL; - p->fts_parent = parent; - p->fts_accpath = p->fts_name; - p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); + p = fts_alloc(sp, *argv, len); + p->fts_level = FTS_ROOTLEVEL; + p->fts_parent = parent; + p->fts_accpath = p->fts_name; + p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW)); p->fts_type = yfts_type_from_info(p->fts_info); - /* Command-line "." and ".." are real directories. */ + /* Command-line "." and ".." are real directories. */ if (p->fts_info == FTS_DOT) { - p->fts_info = FTS_D; + p->fts_info = FTS_D; } - /* - * If comparison routine supplied, traverse in sorted - * order; otherwise traverse in the order specified. - */ - if (compar) { - p->fts_link = root; - root = p; - } else { + /* + * If comparison routine supplied, traverse in sorted + * order; otherwise traverse in the order specified. + */ + if (compar) { + p->fts_link = root; + root = p; + } else { p->fts_link = nullptr; if (root == nullptr) { - tmp = root = p; + tmp = root = p; } else { - tmp->fts_link = p; - tmp = p; - } - } - } + tmp->fts_link = p; + tmp = p; + } + } + } if (compar && nitems > 1) { - root = fts_sort(sp, root, nitems); + 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. - */ + /* + * 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) { - goto mem3; + goto mem3; } sp->fts_cur->fts_level = FTS_ROOTLEVEL; - sp->fts_cur->fts_link = root; - sp->fts_cur->fts_info = FTS_INIT; + sp->fts_cur->fts_link = root; + sp->fts_cur->fts_info = FTS_INIT; - /* - * If using chdir(2), grab a file descriptor pointing to dot to ensure - * that we can get back here; this could be avoided for some paths, - * but almost certainly not worth the effort. Slashes, symbolic links, - * and ".." are all fairly nasty problems. Note, if we can't get the - * descriptor we run anyway, just more slowly. - */ + /* + * If using chdir(2), grab a file descriptor pointing to dot to ensure + * that we can get back here; this could be avoided for some paths, + * but almost certainly not worth the effort. Slashes, symbolic links, + * and ".." are all fairly nasty problems. Note, if we can't get the + * descriptor we run anyway, just more slowly. + */ if (!ISSET(FTS_NOCHDIR) && valid_dird(sp->fts_rfd = get_cwdd())) { - SET(FTS_NOCHDIR); + SET(FTS_NOCHDIR); } - return (sp); + return (sp); mem3: fts_lfree(root); - free(parent); + free(parent); mem2: free(sp->fts_path); mem1: @@ -381,72 +381,72 @@ mem1: static void fts_load(FTS* sp, FTSENT* p) { - size_t len; + size_t len; char* cp; - /* - * Load the stream structure for the next traversal. Since we don't - * actually enter the directory until after the preorder visit, set - * the fts_accpath field specially so the chdir gets done to the right - * place and the user can access the first node. From yfts_open it's - * known that the path will fit. - */ - len = p->fts_pathlen = p->fts_namelen; - memmove((void*)sp->fts_path, (void*)p->fts_name, len + 1); + /* + * Load the stream structure for the next traversal. Since we don't + * actually enter the directory until after the preorder visit, set + * the fts_accpath field specially so the chdir gets done to the right + * place and the user can access the first node. From yfts_open it's + * known that the path will fit. + */ + len = p->fts_pathlen = p->fts_namelen; + memmove((void*)sp->fts_path, (void*)p->fts_name, len + 1); if ((cp = strrchr(p->fts_name, LOCSLASH_C)) != nullptr && (cp != p->fts_name || cp[1])) { - len = strlen(++cp); - memmove((void*)p->fts_name, (void*)cp, len + 1); - p->fts_namelen = (u_short)len; - } - p->fts_accpath = p->fts_path = sp->fts_path; - sp->fts_dev = p->fts_dev; + len = strlen(++cp); + memmove((void*)p->fts_name, (void*)cp, len + 1); + p->fts_namelen = (u_short)len; + } + p->fts_accpath = p->fts_path = sp->fts_path; + sp->fts_dev = p->fts_dev; } int yfts_close(FTS* sp) { - FTSENT *freep, *p; - int saved_errno; - - /* - * This still works if we haven't read anything -- the dummy structure - * points to the root list, so we step through to the end of the root - * list which has a valid parent pointer. - */ - if (sp->fts_cur) { - for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { - freep = p; - p = p->fts_link ? p->fts_link : p->fts_parent; - free(freep); - } - free(p); - } - - /* Free up child linked list, sort array, path buffer. */ + FTSENT *freep, *p; + int saved_errno; + + /* + * This still works if we haven't read anything -- the dummy structure + * points to the root list, so we step through to the end of the root + * list which has a valid parent pointer. + */ + if (sp->fts_cur) { + for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { + freep = p; + p = p->fts_link ? p->fts_link : p->fts_parent; + free(freep); + } + free(p); + } + + /* Free up child linked list, sort array, path buffer. */ if (sp->fts_child) { - fts_lfree(sp->fts_child); + fts_lfree(sp->fts_child); } if (sp->fts_array) { - free(sp->fts_array); + free(sp->fts_array); } - free(sp->fts_path); + free(sp->fts_path); - /* Return to original directory, save errno if necessary. */ - if (!ISSET(FTS_NOCHDIR)) { + /* Return to original directory, save errno if necessary. */ + if (!ISSET(FTS_NOCHDIR)) { saved_errno = chdir_dird(sp->fts_rfd) ? errno : 0; close_dird(sp->fts_rfd); - /* Set errno and return. */ - if (saved_errno != 0) { - /* Free up the stream pointer. */ - free(sp); - errno = saved_errno; - return (-1); - } - } - - /* Free up the stream pointer. */ - free(sp); - return (0); + /* Set errno and return. */ + if (saved_errno != 0) { + /* Free up the stream pointer. */ + free(sp); + errno = saved_errno; + return (-1); + } + } + + /* Free up the stream pointer. */ + free(sp); + return (0); } /* @@ -460,200 +460,200 @@ int yfts_close(FTS* sp) FTSENT* yfts_read(FTS* sp) { - FTSENT *p, *tmp; - int instr; + FTSENT *p, *tmp; + int instr; char* t; - int saved_errno; + int saved_errno; ClearLastSystemError(); - /* If finished or unrecoverable error, return NULL. */ + /* If finished or unrecoverable error, return NULL. */ if (sp->fts_cur == nullptr || ISSET(FTS_STOP)) { return nullptr; } - /* Set current node pointer. */ - p = sp->fts_cur; + /* Set current node pointer. */ + p = sp->fts_cur; - /* Save and zero out user instructions. */ - instr = p->fts_instr; - p->fts_instr = FTS_NOINSTR; + /* Save and zero out user instructions. */ + instr = p->fts_instr; + p->fts_instr = FTS_NOINSTR; - /* Any type of file may be re-visited; re-stat and re-turn. */ - if (instr == FTS_AGAIN) { - p->fts_info = fts_stat(sp, p, 0); + /* Any type of file may be re-visited; re-stat and re-turn. */ + if (instr == FTS_AGAIN) { + p->fts_info = fts_stat(sp, p, 0); p->fts_type = yfts_type_from_info(p->fts_info); - return (p); - } - - /* - * Following a symlink -- SLNONE test allows application to see - * SLNONE and recover. If indirecting through a symlink, have - * keep a pointer to current location. If unable to get that - * pointer, follow fails. - */ - if (instr == FTS_FOLLOW && - (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { - p->fts_info = fts_stat(sp, p, 1); + return (p); + } + + /* + * Following a symlink -- SLNONE test allows application to see + * SLNONE and recover. If indirecting through a symlink, have + * keep a pointer to current location. If unable to get that + * pointer, follow fails. + */ + if (instr == FTS_FOLLOW && + (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { + 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())) { - p->fts_errno = errno; - p->fts_info = FTS_ERR; + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if (valid_dird(p->fts_symfd = get_cwdd())) { + p->fts_errno = errno; + p->fts_info = FTS_ERR; } else { - p->fts_flags |= FTS_SYMFOLLOW; + p->fts_flags |= FTS_SYMFOLLOW; } - } - return (p); - } - - /* Directory in pre-order. */ - if (p->fts_info == FTS_D) { - /* If skipped or crossed mount point, do post-order visit. */ - if (instr == FTS_SKIP || - (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) { + } + return (p); + } + + /* Directory in pre-order. */ + if (p->fts_info == FTS_D) { + /* 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) { - close_dird(p->fts_symfd); + close_dird(p->fts_symfd); } - if (sp->fts_child) { - fts_lfree(sp->fts_child); + if (sp->fts_child) { + fts_lfree(sp->fts_child); sp->fts_child = nullptr; - } - p->fts_info = FTS_DP; - return (p); - } - - /* Rebuild if only read the names and now traversing. */ - if (sp->fts_child && ISSET(FTS_NAMEONLY)) { - CLR(FTS_NAMEONLY); - fts_lfree(sp->fts_child); + } + p->fts_info = FTS_DP; + return (p); + } + + /* Rebuild if only read the names and now traversing. */ + if (sp->fts_child && ISSET(FTS_NAMEONLY)) { + CLR(FTS_NAMEONLY); + fts_lfree(sp->fts_child); sp->fts_child = nullptr; - } - - /* - * Cd to the subdirectory. - * - * If have already read and now fail to chdir, whack the list - * to make the names come out right, and set the parent errno - * so the application will eventually get an error condition. - * Set the FTS_DONTCHDIR flag so that when we logically change - * directories back to the parent we don't do a chdir. - * - * If haven't read do so. If the read fails, fts_build sets - * FTS_STOP or the fts_info field of the node. - */ - if (sp->fts_child) { - if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) { - p->fts_errno = errno; - p->fts_flags |= FTS_DONTCHDIR; + } + + /* + * Cd to the subdirectory. + * + * If have already read and now fail to chdir, whack the list + * to make the names come out right, and set the parent errno + * so the application will eventually get an error condition. + * Set the FTS_DONTCHDIR flag so that when we logically change + * directories back to the parent we don't do a chdir. + * + * If haven't read do so. If the read fails, fts_build sets + * FTS_STOP or the fts_info field of the node. + */ + if (sp->fts_child) { + 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) { - p->fts_accpath = - p->fts_parent->fts_accpath; + p->fts_accpath = + p->fts_parent->fts_accpath; } - } + } } else if ((sp->fts_child = fts_build(sp, BREAD)) == nullptr) { if (ISSET(FTS_STOP)) { return nullptr; } - return (p); - } - p = sp->fts_child; + return (p); + } + p = sp->fts_child; sp->fts_child = nullptr; - goto name; - } + goto name; + } - /* Move to the next node on this level. */ + /* Move to the next node on this level. */ next: tmp = p; if ((p = p->fts_link) != nullptr) { - free(tmp); - - /* - * If reached the top, return to the original directory (or - * the root of the tree), and load the paths for the next root. - */ - if (p->fts_level == FTS_ROOTLEVEL) { - if (FCHDIR(sp, sp->fts_rfd)) { - SET(FTS_STOP); + free(tmp); + + /* + * If reached the top, return to the original directory (or + * the root of the tree), and load the paths for the next root. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); return nullptr; - } - fts_load(sp, p); - return (sp->fts_cur = p); - } - - /* - * User may have called yfts_set on the node. If skipped, - * ignore. If followed, get a file descriptor so we can - * get back if necessary. - */ + } + fts_load(sp, p); + return (sp->fts_cur = p); + } + + /* + * User may have called yfts_set on the node. If skipped, + * ignore. If followed, get a file descriptor so we can + * get back if necessary. + */ if (p->fts_instr == FTS_SKIP) { - goto next; + goto next; } - if (p->fts_instr == FTS_FOLLOW) { - p->fts_info = fts_stat(sp, p, 1); + 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 = + if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) { + if (valid_dird(p->fts_symfd = get_cwdd())) { - p->fts_errno = errno; - p->fts_info = FTS_ERR; + p->fts_errno = errno; + p->fts_info = FTS_ERR; } else { - p->fts_flags |= FTS_SYMFOLLOW; + p->fts_flags |= FTS_SYMFOLLOW; } - } - p->fts_instr = FTS_NOINSTR; - } + } + p->fts_instr = FTS_NOINSTR; + } 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); - } - - /* Move up to the parent node. */ - p = tmp->fts_parent; - free(tmp); - - if (p->fts_level == FTS_ROOTPARENTLEVEL) { - /* - * Done; free everything up and set errno to 0 so the user - * can distinguish between error and EOF. - */ - free(p); - errno = 0; + *t++ = LOCSLASH_C; + memmove(t, p->fts_name, (size_t)p->fts_namelen + 1); + return (sp->fts_cur = p); + } + + /* Move up to the parent node. */ + p = tmp->fts_parent; + free(tmp); + + if (p->fts_level == FTS_ROOTPARENTLEVEL) { + /* + * Done; free everything up and set errno to 0 so the user + * can distinguish between error and EOF. + */ + free(p); + errno = 0; return (sp->fts_cur = nullptr); - } - - /* NUL terminate the pathname. */ - sp->fts_path[p->fts_pathlen] = '\0'; - - /* - * Return to the parent directory. If at a root node or came through - * a symlink, go back through the file descriptor. Otherwise, cd up - * one directory. - */ - if (p->fts_level == FTS_ROOTLEVEL) { - if (FCHDIR(sp, sp->fts_rfd)) { - SET(FTS_STOP); + } + + /* NUL terminate the pathname. */ + sp->fts_path[p->fts_pathlen] = '\0'; + + /* + * Return to the parent directory. If at a root node or came through + * a symlink, go back through the file descriptor. Otherwise, cd up + * one directory. + */ + if (p->fts_level == FTS_ROOTLEVEL) { + if (FCHDIR(sp, sp->fts_rfd)) { + SET(FTS_STOP); return nullptr; - } - } else if (p->fts_flags & FTS_SYMFOLLOW) { - if (FCHDIR(sp, p->fts_symfd)) { - saved_errno = errno; - close_dird(p->fts_symfd); - errno = saved_errno; - SET(FTS_STOP); + } + } else if (p->fts_flags & FTS_SYMFOLLOW) { + if (FCHDIR(sp, p->fts_symfd)) { + saved_errno = errno; + close_dird(p->fts_symfd); + errno = saved_errno; + SET(FTS_STOP); return nullptr; - } - close_dird(p->fts_symfd); - } else if (!(p->fts_flags & FTS_DONTCHDIR) && + } + 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; - } - p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; - return (sp->fts_cur = p); + } + p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP; + return (sp->fts_cur = p); } /* @@ -666,87 +666,87 @@ next: int yfts_set(FTS* sp, FTSENT* p, int instr) { (void)sp; //Unused - if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW && - instr != FTS_NOINSTR && instr != FTS_SKIP) { - errno = EINVAL; - return (1); - } - p->fts_instr = (u_short)instr; - return (0); + if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW && + instr != FTS_NOINSTR && instr != FTS_SKIP) { + errno = EINVAL; + return (1); + } + p->fts_instr = (u_short)instr; + return (0); } FTSENT* yfts_children(FTS* sp, int instr) { FTSENT* p; - dird fd; - if (instr && instr != FTS_NAMEONLY) { - errno = EINVAL; + dird fd; + if (instr && instr != FTS_NAMEONLY) { + errno = EINVAL; return nullptr; - } + } - /* Set current node pointer. */ - p = sp->fts_cur; + /* Set current node pointer. */ + p = sp->fts_cur; - /* - * Errno set to 0 so user can distinguish empty directory from - * an error. - */ - errno = 0; + /* + * Errno set to 0 so user can distinguish empty directory from + * an error. + */ + errno = 0; - /* Fatal errors stop here. */ + /* Fatal errors stop here. */ if (ISSET(FTS_STOP)) { return nullptr; } - /* Return logical hierarchy of user's arguments. */ + /* Return logical hierarchy of user's arguments. */ if (p->fts_info == FTS_INIT) { - return (p->fts_link); + 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 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 */) { return nullptr; } - /* Free up any previous child list. */ + /* Free up any previous child list. */ if (sp->fts_child) { - fts_lfree(sp->fts_child); + fts_lfree(sp->fts_child); } - if (instr == FTS_NAMEONLY) { - SET(FTS_NAMEONLY); - instr = BNAMES; + if (instr == FTS_NAMEONLY) { + SET(FTS_NAMEONLY); + instr = BNAMES; } else { - instr = BCHILD; + instr = BCHILD; } - /* - * If using chdir on a relative path and called BEFORE yfts_read does - * its chdir to the root of a traversal, we can lose -- we need to - * chdir into the subdirectory, and we don't know where the current - * directory is, so we can't get back so that the upcoming chdir by - * yfts_read will work. - */ - if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == LOCSLASH_C || + /* + * If using chdir on a relative path and called BEFORE yfts_read does + * its chdir to the root of a traversal, we can lose -- we need to + * chdir into the subdirectory, and we don't know where the current + * directory is, so we can't get back so that the upcoming chdir by + * yfts_read will work. + */ + if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == LOCSLASH_C || ISSET(FTS_NOCHDIR)) { - return (sp->fts_child = fts_build(sp, instr)); + return (sp->fts_child = fts_build(sp, instr)); } if (valid_dird(fd = get_cwdd())) { return nullptr; } - sp->fts_child = fts_build(sp, instr); - if (chdir_dird(fd)) { + sp->fts_child = fts_build(sp, instr); + if (chdir_dird(fd)) { close_dird(fd); return nullptr; } - close_dird(fd); - return (sp->fts_child); + close_dird(fd); + return (sp->fts_child); } static inline struct dirent* yreaddir(DIR* dir, struct dirent* de) { @@ -780,9 +780,9 @@ static FTSENT* fts_build(FTS* sp, int type) { struct dirent* dp; - FTSENT *p, *head; - int nitems; - FTSENT *cur, *tail; + FTSENT *p, *head; + int nitems; + FTSENT *cur, *tail; #ifdef _win_ dird dirpd; @@ -792,71 +792,71 @@ fts_build(FTS* sp, int type) #endif void* oldaddr; - int cderrno, descend, len, level, maxlen, nlinks, saved_errno, - nostat, doadjust; + int cderrno, descend, len, level, maxlen, nlinks, saved_errno, + nostat, doadjust; char* cp; - /* Set current node pointer. */ - cur = sp->fts_cur; + /* Set current node pointer. */ + cur = sp->fts_cur; - /* - * Open the directory for reading. If this fails, we're done. - * If being called from yfts_read, set the fts_info field. - */ + /* + * Open the directory for reading. If this fails, we're done. + * If being called from yfts_read, set the fts_info field. + */ #ifdef FTS_WHITEOUT - if (ISSET(FTS_WHITEOUT)) + if (ISSET(FTS_WHITEOUT)) oflag = DTF_NODUP | DTF_REWIND; - else + else oflag = DTF_HIDEW | DTF_NODUP | DTF_REWIND; #else #define __opendir2(path, flag) opendir(path) #endif if ((dirp = __opendir2(cur->fts_accpath, oflag)) == nullptr) { - if (type == BREAD) { - cur->fts_info = FTS_DNR; - cur->fts_errno = errno; - } + if (type == BREAD) { + cur->fts_info = FTS_DNR; + cur->fts_errno = errno; + } return nullptr; - } + } #ifdef _win_ dirpd = get_dird(cur->fts_accpath); #endif - /* - * Nlinks is the number of possible entries of type directory in the - * directory if we're cheating on stat calls, 0 if we're not doing - * any stat calls at all, -1 if we're doing stats on everything. - */ - if (type == BNAMES) { - nlinks = 0; - /* Be quiet about nostat, GCC. */ - nostat = 0; - } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { - nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); - nostat = 1; - } else { - nlinks = -1; - nostat = 0; - } - - /* - * If we're going to need to stat anything or we want to descend - * and stay in the directory, chdir. If this fails we keep going, - * but set a flag so we don't chdir after the post-order visit. - * We won't be able to stat anything, but we can still return the - * names themselves. Note, that since yfts_read won't be able to - * chdir into the directory, it will have to return different path - * names than before, i.e. "a/b" instead of "b". Since the node - * has already been visited in pre-order, have to wait until the - * post-order visit to return the error. There is a special case - * here, if there was nothing to stat then it's not an error to - * not be able to stat. This is all fairly nasty. If a program - * needed sorted entries or stat information, they had better be - * checking FTS_NS on the returned nodes. - */ - cderrno = 0; - if (nlinks || type == BREAD) { + /* + * Nlinks is the number of possible entries of type directory in the + * directory if we're cheating on stat calls, 0 if we're not doing + * any stat calls at all, -1 if we're doing stats on everything. + */ + if (type == BNAMES) { + nlinks = 0; + /* Be quiet about nostat, GCC. */ + nostat = 0; + } else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) { + nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2); + nostat = 1; + } else { + nlinks = -1; + nostat = 0; + } + + /* + * If we're going to need to stat anything or we want to descend + * and stay in the directory, chdir. If this fails we keep going, + * but set a flag so we don't chdir after the post-order visit. + * We won't be able to stat anything, but we can still return the + * names themselves. Note, that since yfts_read won't be able to + * chdir into the directory, it will have to return different path + * names than before, i.e. "a/b" instead of "b". Since the node + * has already been visited in pre-order, have to wait until the + * post-order visit to return the error. There is a special case + * here, if there was nothing to stat then it's not an error to + * not be able to stat. This is all fairly nasty. If a program + * needed sorted entries or stat information, they had better be + * checking FTS_NS on the returned nodes. + */ + cderrno = 0; + if (nlinks || type == BREAD) { #ifndef _win_ if (fts_safe_changedir(sp, cur, dirfd(dirp), nullptr)) { #else @@ -864,12 +864,12 @@ fts_build(FTS* sp, int type) #endif if (nlinks && type == BREAD) { - cur->fts_errno = errno; + cur->fts_errno = errno; } - cur->fts_flags |= FTS_DONTCHDIR; - descend = 0; - cderrno = errno; - (void)closedir(dirp); + cur->fts_flags |= FTS_DONTCHDIR; + descend = 0; + cderrno = errno; + (void)closedir(dirp); dirp = nullptr; #ifdef _win_ close_dird(dirpd); @@ -878,107 +878,107 @@ fts_build(FTS* sp, int type) Y_UNUSED(invalidDirD); #endif } else { - descend = 1; + descend = 1; } } else { - descend = 0; - } - - /* - * Figure out the max file name length that can be stored in the - * current path -- the inner loop allocates more path as necessary. - * We really wouldn't have to do the maxlen calculations here, we - * could do them in yfts_read before returning the path, but it's a - * lot easier here since the length is part of the dirent structure. - * - * If not changing directories set a pointer so that can just append - * each new name into the path. - */ - len = NAPPEND(cur); - if (ISSET(FTS_NOCHDIR)) { - cp = sp->fts_path + len; - *cp++ = LOCSLASH_C; - } else { - /* GCC, you're too verbose. */ + descend = 0; + } + + /* + * Figure out the max file name length that can be stored in the + * current path -- the inner loop allocates more path as necessary. + * We really wouldn't have to do the maxlen calculations here, we + * could do them in yfts_read before returning the path, but it's a + * lot easier here since the length is part of the dirent structure. + * + * If not changing directories set a pointer so that can just append + * each new name into the path. + */ + len = NAPPEND(cur); + if (ISSET(FTS_NOCHDIR)) { + cp = sp->fts_path + len; + *cp++ = LOCSLASH_C; + } else { + /* GCC, you're too verbose. */ cp = nullptr; - } + } ++len; - maxlen = sp->fts_pathlen - len; + maxlen = sp->fts_pathlen - len; - level = cur->fts_level + 1; + level = cur->fts_level + 1; - /* Read the directory, attaching each entry to the `link' pointer. */ - doadjust = 0; + /* Read the directory, attaching each entry to the `link' pointer. */ + doadjust = 0; //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)) { - continue; + continue; } if ((p = fts_alloc(sp, dp->d_name, (int)strlen(dp->d_name))) == nullptr) { - goto mem1; + goto mem1; } if (strlen(dp->d_name) >= (size_t)maxlen) { /* include space for NUL */ - oldaddr = sp->fts_path; + oldaddr = sp->fts_path; 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. - */ + /* + * 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) { - free(p); + free(p); } - fts_lfree(head); - (void)closedir(dirp); + fts_lfree(head); + (void)closedir(dirp); #ifdef _win_ close_dird(dirpd); #endif - cur->fts_info = FTS_ERR; - SET(FTS_STOP); - errno = saved_errno; + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + errno = saved_errno; return nullptr; - } - /* Did realloc() change the pointer? */ - if (oldaddr != sp->fts_path) { - doadjust = 1; + } + /* Did realloc() change the pointer? */ + if (oldaddr != sp->fts_path) { + doadjust = 1; if (ISSET(FTS_NOCHDIR)) { - cp = sp->fts_path + len; + cp = sp->fts_path + len; } - } - maxlen = sp->fts_pathlen - len; - } - - if (len + strlen(dp->d_name) >= USHRT_MAX) { - /* - * In an FTSENT, fts_pathlen is a u_short so it is - * possible to wraparound here. If we do, free up - * the current structure and the structures already - * allocated, then error out with ENAMETOOLONG. - */ - free(p); - fts_lfree(head); - (void)closedir(dirp); + } + maxlen = sp->fts_pathlen - len; + } + + if (len + strlen(dp->d_name) >= USHRT_MAX) { + /* + * In an FTSENT, fts_pathlen is a u_short so it is + * possible to wraparound here. If we do, free up + * the current structure and the structures already + * allocated, then error out with ENAMETOOLONG. + */ + free(p); + fts_lfree(head); + (void)closedir(dirp); #ifdef _win_ close_dird(dirpd); #endif - cur->fts_info = FTS_ERR; - SET(FTS_STOP); - errno = ENAMETOOLONG; + cur->fts_info = FTS_ERR; + SET(FTS_STOP); + errno = ENAMETOOLONG; return nullptr; - } - p->fts_level = (short)level; - p->fts_parent = sp->fts_cur; - p->fts_pathlen = u_short(len + strlen(dp->d_name)); + } + p->fts_level = (short)level; + p->fts_parent = sp->fts_cur; + p->fts_pathlen = u_short(len + strlen(dp->d_name)); #ifdef FTS_WHITEOUT - if (dp->d_type == DT_WHT) - p->fts_flags |= FTS_ISW; + if (dp->d_type == DT_WHT) + p->fts_flags |= FTS_ISW; #endif #ifdef _DIRENT_HAVE_D_TYPE @@ -992,252 +992,252 @@ fts_build(FTS* sp, int type) #endif // coverity[dead_error_line]: false positive - if (cderrno) { - if (nlinks) { - p->fts_info = FTS_NS; - p->fts_errno = cderrno; + if (cderrno) { + if (nlinks) { + p->fts_info = FTS_NS; + p->fts_errno = cderrno; } else { - p->fts_info = FTS_NSOK; + p->fts_info = FTS_NSOK; } - p->fts_accpath = cur->fts_accpath; - } else if (nlinks == 0 + p->fts_accpath = cur->fts_accpath; + } else if (nlinks == 0 #ifdef DT_DIR || (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; - } else { - /* Build a file name for fts_stat to stat. */ - if (ISSET(FTS_NOCHDIR)) { - p->fts_accpath = p->fts_path; - memmove((void*)cp, (void*)p->fts_name, (size_t)p->fts_namelen + 1); + p->fts_accpath = + ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name; + p->fts_info = FTS_NSOK; + } else { + /* Build a file name for fts_stat to stat. */ + if (ISSET(FTS_NOCHDIR)) { + p->fts_accpath = p->fts_path; + memmove((void*)cp, (void*)p->fts_name, (size_t)p->fts_namelen + 1); } else { - p->fts_accpath = p->fts_name; + p->fts_accpath = p->fts_name; } - /* Stat it. */ - p->fts_info = fts_stat(sp, p, 0); + /* 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 || + /* Decrement link count if applicable. */ + if (nlinks > 0 && (p->fts_info == FTS_D || p->fts_info == FTS_DC || p->fts_info == FTS_DOT)) { - --nlinks; + --nlinks; } - } + } - /* We walk in directory order so "ls -f" doesn't get upset. */ + /* We walk in directory order so "ls -f" doesn't get upset. */ p->fts_link = nullptr; if (head == nullptr) { - head = tail = p; + head = tail = p; } else { - tail->fts_link = p; - tail = p; - } - ++nitems; - } + tail->fts_link = p; + tail = p; + } + ++nitems; + } if (dirp) { - (void)closedir(dirp); + (void)closedir(dirp); #ifdef _win_ close_dird(dirpd); #endif } - /* - * If realloc() changed the address of the path, adjust the - * addresses for the rest of the tree and the dir list. - */ + /* + * If realloc() changed the address of the path, adjust the + * addresses for the rest of the tree and the dir list. + */ if (doadjust) { - fts_padjust(sp); + fts_padjust(sp); } - /* - * If not changing directories, reset the path back to original - * state. - */ - if (ISSET(FTS_NOCHDIR)) { + /* + * If not changing directories, reset the path back to original + * state. + */ + if (ISSET(FTS_NOCHDIR)) { if (len == sp->fts_pathlen || nitems == 0) { - --cp; + --cp; } - *cp = '\0'; - } - - /* - * If descended after called from yfts_children or after called from - * yfts_read and nothing found, get back. At the root level we use - * the saved fd; if one of yfts_open()'s arguments is a relative path - * to an empty directory, we wind up here with no other way back. If - * can't get back, we're done. - */ - if (descend && (type == BCHILD || !nitems) && + *cp = '\0'; + } + + /* + * If descended after called from yfts_children or after called from + * yfts_read and nothing found, get back. At the root level we use + * the saved fd; if one of yfts_open()'s arguments is a relative path + * to an empty directory, we wind up here with no other way back. If + * 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_info = FTS_ERR; - SET(FTS_STOP); + cur->fts_info = FTS_ERR; + SET(FTS_STOP); fts_lfree(head); return nullptr; - } + } - /* If didn't find anything, return NULL. */ - if (!nitems) { + /* If didn't find anything, return NULL. */ + if (!nitems) { if (type == BREAD) { - cur->fts_info = FTS_DP; + cur->fts_info = FTS_DP; } fts_lfree(head); return nullptr; - } + } - /* Sort the entries. */ + /* Sort the entries. */ if (sp->fts_compar && nitems > 1) { - head = fts_sort(sp, head, nitems); + head = fts_sort(sp, head, nitems); } - return (head); + return (head); } static u_short fts_stat(FTS* sp, FTSENT* p, int follow) { - dev_t dev; - ino_t ino; + dev_t dev; + ino_t ino; stat_struct *sbp, sb; - int saved_errno; - /* If user needs stat info, stat buffer already allocated. */ - sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; + int saved_errno; + /* If user needs stat info, stat buffer already allocated. */ + sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp; #ifdef FTS_WHITEOUT - /* check for whiteout */ - if (p->fts_flags & FTS_ISW) { - if (sbp != &sb) { + /* check for whiteout */ + if (p->fts_flags & FTS_ISW) { + if (sbp != &sb) { memset(sbp, '\0', sizeof(*sbp)); - sbp->st_mode = S_IFWHT; - } - return (FTS_W); - } + sbp->st_mode = S_IFWHT; + } + return (FTS_W); + } #endif - /* - * If doing a logical walk, or application requested FTS_FOLLOW, do - * a stat(2). If that fails, check for a non-existent symlink. If - * fail, set the errno from the stat call. - */ - if (ISSET(FTS_LOGICAL) || follow) { + /* + * If doing a logical walk, or application requested FTS_FOLLOW, do + * a stat(2). If that fails, check for a non-existent symlink. If + * fail, set the errno from the stat call. + */ + if (ISSET(FTS_LOGICAL) || follow) { if (STAT_FUNC(p->fts_accpath, sbp)) { - saved_errno = errno; - if (!lstat(p->fts_accpath, sbp)) { - errno = 0; - return (FTS_SLNONE); - } - p->fts_errno = saved_errno; + saved_errno = errno; + if (!lstat(p->fts_accpath, sbp)) { + errno = 0; + return (FTS_SLNONE); + } + p->fts_errno = saved_errno; memset(sbp, 0, sizeof(stat_struct)); return (FTS_NS); - } + } } else if (lstat(p->fts_accpath, sbp)) { - p->fts_errno = errno; + p->fts_errno = errno; memset(sbp, 0, sizeof(stat_struct)); - return (FTS_NS); - } + return (FTS_NS); + } if (S_ISDIR(sbp->st_mode)) { - /* - * Set the device/inode. Used to find cycles and check for - * crossing mount points. Also remember the link count, used - * in fts_build to limit the number of stat calls. It is - * understood that these fields are only referenced if fts_info - * is set to FTS_D. - */ - dev = p->fts_dev = sbp->st_dev; - ino = p->fts_ino = sbp->st_ino; - p->fts_nlink = sbp->st_nlink; + /* + * Set the device/inode. Used to find cycles and check for + * crossing mount points. Also remember the link count, used + * in fts_build to limit the number of stat calls. It is + * understood that these fields are only referenced if fts_info + * is set to FTS_D. + */ + dev = p->fts_dev = sbp->st_dev; + 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)) { - return (FTS_DOT); + 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. - */ + * 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 #ifndef _win_ FTSENT* t; - for (t = p->fts_parent; + for (t = p->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); - } + if (ino == t->fts_ino && dev == t->fts_dev) { + p->fts_cycle = t; + return (FTS_DC); + } } #endif /*_win_*/ - return (FTS_D); - } + return (FTS_D); + } if (S_ISLNK(sbp->st_mode)) { - return (FTS_SL); + return (FTS_SL); } if (S_ISREG(sbp->st_mode)) { - return (FTS_F); + return (FTS_F); } - return (FTS_DEFAULT); + return (FTS_DEFAULT); } static FTSENT* fts_sort(FTS* sp, FTSENT* head, int nitems) { - FTSENT **ap, *p; - - /* - * Construct an array of pointers to the structures and call qsort(3). - * Reassemble the array in the order returned by qsort. If unable to - * sort for memory reasons, return the directory entries in their - * current order. Allocate enough space for the current needs plus - * 40 so don't realloc one entry at a time. - */ - if (nitems > sp->fts_nitems) { + FTSENT **ap, *p; + + /* + * Construct an array of pointers to the structures and call qsort(3). + * Reassemble the array in the order returned by qsort. If unable to + * sort for memory reasons, return the directory entries in their + * current order. Allocate enough space for the current needs plus + * 40 so don't realloc one entry at a time. + */ + if (nitems > sp->fts_nitems) { struct _ftsent** a; - sp->fts_nitems = nitems + 40; + sp->fts_nitems = nitems + 40; if ((a = (struct _ftsent**)realloc(sp->fts_array, sp->fts_nitems * sizeof(FTSENT*))) == nullptr) { if (sp->fts_array) { - free(sp->fts_array); + free(sp->fts_array); } sp->fts_array = nullptr; - sp->fts_nitems = 0; - return (head); - } - sp->fts_array = a; - } + sp->fts_nitems = 0; + return (head); + } + sp->fts_array = a; + } for (ap = sp->fts_array, p = head; p; p = p->fts_link) { - *ap++ = p; + *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) { - ap[0]->fts_link = ap[1]; + ap[0]->fts_link = ap[1]; } ap[0]->fts_link = nullptr; - return (head); + return (head); } static FTSENT* fts_alloc(FTS* sp, const char* name, int namelen) { FTSENT* p; - size_t len; - - /* - * The file name is a variable length array and no stat structure is - * necessary if the user has set the nostat bit. Allocate the FTSENT - * structure, the file name and the stat structure in one chunk, but - * be careful that the stat structure is reasonably aligned. Since the - * fts_name field is declared to be of size 1, the fts_name pointer is - * namelen + 2 before the first possible address of the stat structure. - */ - len = sizeof(FTSENT) + namelen; + size_t len; + + /* + * The file name is a variable length array and no stat structure is + * necessary if the user has set the nostat bit. Allocate the FTSENT + * structure, the file name and the stat structure in one chunk, but + * be careful that the stat structure is reasonably aligned. Since the + * fts_name field is declared to be of size 1, the fts_name pointer is + * namelen + 2 before the first possible address of the stat structure. + */ + len = sizeof(FTSENT) + namelen; if (!ISSET(FTS_NOSTAT)) { len += sizeof(stat_struct) + ALIGNBYTES; } @@ -1245,22 +1245,22 @@ fts_alloc(FTS* sp, const char* name, int namelen) return nullptr; } - /* Copy the name and guarantee NUL termination. */ - memmove((void*)p->fts_name, (void*)name, (size_t)namelen); - p->fts_name[namelen] = '\0'; + /* 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); } - p->fts_namelen = (u_short)namelen; - p->fts_path = sp->fts_path; - p->fts_errno = 0; - p->fts_flags = 0; - p->fts_instr = FTS_NOINSTR; - p->fts_number = 0; + p->fts_namelen = (u_short)namelen; + p->fts_path = sp->fts_path; + p->fts_errno = 0; + p->fts_flags = 0; + p->fts_instr = FTS_NOINSTR; + p->fts_number = 0; p->fts_pointer = nullptr; p->fts_type = FTS_NSOK; - return (p); + return (p); } static void @@ -1268,11 +1268,11 @@ fts_lfree(FTSENT* head) { FTSENT* p; - /* Free a linked list of structures. */ + /* Free a linked list of structures. */ while ((p = head) != nullptr) { - head = head->fts_link; - free(p); - } + head = head->fts_link; + free(p); + } } /* @@ -1284,7 +1284,7 @@ fts_lfree(FTSENT* head) static int fts_palloc(FTS* sp, size_t more) { - sp->fts_pathlen += more + 256; + sp->fts_pathlen += more + 256; sp->fts_path = (char*)yreallocf(sp->fts_path, (size_t)sp->fts_pathlen); return (sp->fts_path == nullptr); } @@ -1292,19 +1292,19 @@ fts_palloc(FTS* sp, size_t more) static void 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 && + (p)->fts_accpath < (p)->fts_path + (p)->fts_pathlen) { if (p->fts_accpath != p->fts_path) { - errx(1, "fts ADJUST: accpath %p path %p", + errx(1, "fts ADJUST: accpath %p path %p", p->fts_accpath, p->fts_path); } if (p->fts_level != 0) { - errx(1, "fts ADJUST: level %d not 0", p->fts_level); + errx(1, "fts ADJUST: level %d not 0", p->fts_level); } - (p)->fts_accpath = + (p)->fts_accpath = (char*)addr + ((p)->fts_accpath - (p)->fts_path); - } - (p)->fts_path = (char*)addr; + } + (p)->fts_path = (char*)addr; } /* @@ -1323,29 +1323,29 @@ fts_padjust(FTS* sp) (p)->fts_accpath = (addr); \ (p)->fts_path = addr; \ } - /* Adjust the current set of children. */ + /* Adjust the current set of children. */ for (p = sp->fts_child; p; p = p->fts_link) { - ADJUST(p, addr); + ADJUST(p, addr); } - /* Adjust the rest of the tree. */ - for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { - ADJUST(p, addr); - p = p->fts_link ? p->fts_link : p->fts_parent; - } + /* Adjust the rest of the tree. */ + for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) { + ADJUST(p, addr); + p = p->fts_link ? p->fts_link : p->fts_parent; + } } static size_t fts_maxarglen(char* const* argv) { - size_t len, max; + size_t len, max; for (max = 0; *argv; ++argv) { if ((len = strlen(*argv)) > max) { - max = len; + max = len; } } - return (max + 1); + return (max + 1); } /* @@ -1358,33 +1358,33 @@ fts_maxarglen(char* const* argv) static int fts_safe_changedir(FTS* sp, FTSENT* p, int fd, const char* path) { - int ret, oerrno, newfd; + int ret, oerrno, newfd; stat_struct sb; - newfd = fd; + newfd = fd; if (ISSET(FTS_NOCHDIR)) { - return (0); + return (0); } if (fd < 0 && (newfd = open(path, O_RDONLY, 0)) < 0) { - return (-1); - } - if (fstat(newfd, &sb)) { - ret = -1; - goto bail; + return (-1); } - if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) { + if (fstat(newfd, &sb)) { + ret = -1; + goto bail; + } + if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) { errno = ENOENT; /* disinformation */ - ret = -1; - goto bail; - } - ret = fchdir(newfd); + ret = -1; + goto bail; + } + ret = fchdir(newfd); bail: - oerrno = errno; + oerrno = errno; if (fd < 0) { - (void)close(newfd); + (void)close(newfd); } - errno = oerrno; - return (ret); + errno = oerrno; + return (ret); } #else static int diff --git a/util/generic/buffer.cpp b/util/generic/buffer.cpp index b92697e1d0..cc39f809f3 100644 --- a/util/generic/buffer.cpp +++ b/util/generic/buffer.cpp @@ -1,7 +1,7 @@ #include "buffer.h" #include "mem_copy.h" #include "string.h" -#include "ymath.h" +#include "ymath.h" #include <util/system/sys_alloc.h> #include <util/system/sanitizers.h> diff --git a/util/generic/buffer.h b/util/generic/buffer.h index 9576467404..e7811a2fc9 100644 --- a/util/generic/buffer.h +++ b/util/generic/buffer.h @@ -6,7 +6,7 @@ #include <util/system/align.h> #include <util/system/yassert.h> -#include <cstring> +#include <cstring> class TBuffer { public: diff --git a/util/generic/hash.h b/util/generic/hash.h index e46db21fa9..8d5e01bcaf 100644 --- a/util/generic/hash.h +++ b/util/generic/hash.h @@ -958,12 +958,12 @@ template <class V> __yhashtable_iterator<V>& __yhashtable_iterator<V>::operator++() { Y_ASSERT(cur); cur = cur->next; - if ((uintptr_t)cur & 1) { + if ((uintptr_t)cur & 1) { node** bucket = (node**)((uintptr_t)cur & ~1); while (*bucket == nullptr) ++bucket; Y_ASSERT(*bucket != nullptr); - cur = (node*)((uintptr_t)*bucket & ~1); + cur = (node*)((uintptr_t)*bucket & ~1); } return *this; } @@ -979,12 +979,12 @@ template <class V> __yhashtable_const_iterator<V>& __yhashtable_const_iterator<V>::operator++() { Y_ASSERT(cur); cur = cur->next; - if ((uintptr_t)cur & 1) { + if ((uintptr_t)cur & 1) { node** bucket = (node**)((uintptr_t)cur & ~1); while (*bucket == nullptr) ++bucket; Y_ASSERT(*bucket != nullptr); - cur = (node*)((uintptr_t)*bucket & ~1); + cur = (node*)((uintptr_t)*bucket & ~1); } return *this; } diff --git a/util/generic/intrlist_ut.cpp b/util/generic/intrlist_ut.cpp index eff7cdf2ee..2d4a1a44df 100644 --- a/util/generic/intrlist_ut.cpp +++ b/util/generic/intrlist_ut.cpp @@ -236,12 +236,12 @@ static void CheckIterationAfterCut(const TMyList& l, const TMyList& l2, size_t N UNIT_ASSERT_EQUAL(c, 0); } -static void TestCutFront(int N, int M) { +static void TestCutFront(int N, int M) { TMyList l(N); TMyList l2(0); TMyList::TIterator it = l.Begin(); - for (int i = 0; i < M; ++i) { + for (int i = 0; i < M; ++i) { ++it; } @@ -249,12 +249,12 @@ static void TestCutFront(int N, int M) { CheckIterationAfterCut(l2, l, N, M); } -static void TestCutBack(int N, int M) { +static void TestCutBack(int N, int M) { TMyList l(N); TMyList l2(0); TMyList::TIterator it = l.Begin(); - for (int i = 0; i < M; ++i) { + for (int i = 0; i < M; ++i) { ++it; } @@ -291,7 +291,7 @@ static void CheckIterationAfterAppend(const TMyList& l, size_t N, size_t M) { UNIT_ASSERT_EQUAL(it, l.End()); } -static void TestAppend(int N, int M) { +static void TestAppend(int N, int M) { TMyList l(N); TMyList l2(M); l.Append(l2); diff --git a/util/generic/ptr.h b/util/generic/ptr.h index 19db0e3ec5..355ea932f8 100644 --- a/util/generic/ptr.h +++ b/util/generic/ptr.h @@ -398,15 +398,15 @@ public: Y_ASSERT(resultCount >= 0); (void)resultCount; } - + TRefCounted(const TRefCounted&) : Counter_(0) { } - + void operator=(const TRefCounted&) { } - + private: C Counter_; }; @@ -459,10 +459,10 @@ public: t->UnRef(); } - + static inline void DecRef(T* t) noexcept { Y_ASSERT(t); - + t->DecRef(); } @@ -571,10 +571,10 @@ public: if (T_) { Ops::DecRef(T_); T_ = nullptr; - } + } return res; } - + inline long RefCount() const noexcept { return T_ ? Ops::RefCount(T_) : 0; } diff --git a/util/generic/ptr_ut.cpp b/util/generic/ptr_ut.cpp index c2dcff23f6..0fdfb0d67e 100644 --- a/util/generic/ptr_ut.cpp +++ b/util/generic/ptr_ut.cpp @@ -293,76 +293,76 @@ void TPointerTest::TestCopyPtr() { UNIT_ASSERT_VALUES_EQUAL(cnt, 0); } - + class TOp: public TSimpleRefCount<TOp>, public TNonCopyable { -public: - static int Cnt; +public: + static int Cnt; -public: - TOp() { - ++Cnt; - } +public: + TOp() { + ++Cnt; + } virtual ~TOp() { - --Cnt; - } -}; - -int TOp::Cnt = 0; - + --Cnt; + } +}; + +int TOp::Cnt = 0; + class TOp2: public TOp { -public: - TIntrusivePtr<TOp> Op; - -public: - TOp2(const TIntrusivePtr<TOp>& op) - : Op(op) - { - ++Cnt; - } +public: + TIntrusivePtr<TOp> Op; + +public: + TOp2(const TIntrusivePtr<TOp>& op) + : Op(op) + { + ++Cnt; + } ~TOp2() override { - --Cnt; - } -}; - -class TOp3 { -public: - TIntrusivePtr<TOp2> Op2; -}; - -void Attach(TOp3* op3, TIntrusivePtr<TOp>* op) { - TIntrusivePtr<TOp2> op2 = new TOp2(*op); - op3->Op2 = op2.Get(); - *op = op2.Get(); -} - + --Cnt; + } +}; + +class TOp3 { +public: + TIntrusivePtr<TOp2> Op2; +}; + +void Attach(TOp3* op3, TIntrusivePtr<TOp>* op) { + TIntrusivePtr<TOp2> op2 = new TOp2(*op); + op3->Op2 = op2.Get(); + *op = op2.Get(); +} + void TPointerTest::TestIntrPtr() { - { - TIntrusivePtr<TOp> p, p2; - TOp3 op3; - { + { + TIntrusivePtr<TOp> p, p2; + TOp3 op3; + { TVector<TIntrusivePtr<TOp>> f1; - { + { TVector<TIntrusivePtr<TOp>> f2; - f2.push_back(new TOp); - p = new TOp; - f2.push_back(p); - Attach(&op3, &f2[1]); - f1 = f2; + f2.push_back(new TOp); + p = new TOp; + f2.push_back(p); + Attach(&op3, &f2[1]); + f1 = f2; UNIT_ASSERT_VALUES_EQUAL(f1[0]->RefCount(), 2); UNIT_ASSERT_VALUES_EQUAL(f1[1]->RefCount(), 3); - UNIT_ASSERT_EQUAL(f1[1].Get(), op3.Op2.Get()); + UNIT_ASSERT_EQUAL(f1[1].Get(), op3.Op2.Get()); UNIT_ASSERT_VALUES_EQUAL(op3.Op2->RefCount(), 3); UNIT_ASSERT_VALUES_EQUAL(op3.Op2->Op->RefCount(), 2); UNIT_ASSERT_VALUES_EQUAL(TOp::Cnt, 4); - } - p2 = p; - } + } + p2 = p; + } UNIT_ASSERT_VALUES_EQUAL(op3.Op2->RefCount(), 1); UNIT_ASSERT_VALUES_EQUAL(op3.Op2->Op->RefCount(), 3); UNIT_ASSERT_VALUES_EQUAL(TOp::Cnt, 3); - } + } UNIT_ASSERT_VALUES_EQUAL(TOp::Cnt, 0); -} +} namespace NTestIntrusiveConvertion { struct TA: public TSimpleRefCount<TA> { diff --git a/util/generic/refcount.h b/util/generic/refcount.h index 966e853b77..538f295e58 100644 --- a/util/generic/refcount.h +++ b/util/generic/refcount.h @@ -2,7 +2,7 @@ #include <util/system/guard.h> #include <util/system/atomic.h> -#include <util/system/defaults.h> +#include <util/system/defaults.h> #include <util/system/yassert.h> template <class TCounterCheckPolicy> diff --git a/util/generic/strbase.h b/util/generic/strbase.h index ab39fc7537..dc8ce69dbc 100644 --- a/util/generic/strbase.h +++ b/util/generic/strbase.h @@ -9,7 +9,7 @@ #include <util/charset/unidata.h> #include <util/system/platform.h> -#include <util/system/yassert.h> +#include <util/system/yassert.h> #include <contrib/libs/libc_compat/string.h> diff --git a/util/generic/string.cpp b/util/generic/string.cpp index 3c655f1f66..279fc4f865 100644 --- a/util/generic/string.cpp +++ b/util/generic/string.cpp @@ -69,17 +69,17 @@ TBasicString<wchar16, std::char_traits<wchar16>>::AppendUtf8(const ::TStringBuf& template <> bool TBasicString<wchar16, std::char_traits<wchar16>>::to_lower(size_t pos, size_t n) { return ToLower(*this, pos, n); -} - +} + template <> bool TBasicString<wchar16, std::char_traits<wchar16>>::to_upper(size_t pos, size_t n) { return ToUpper(*this, pos, n); -} - +} + template <> bool TBasicString<wchar16, std::char_traits<wchar16>>::to_title(size_t pos, size_t n) { return ToTitle(*this, pos, n); -} +} template <> TUtf32String& diff --git a/util/generic/string.h b/util/generic/string.h index 8cd8aa6917..7bff6fc224 100644 --- a/util/generic/string.h +++ b/util/generic/string.h @@ -6,12 +6,12 @@ #include <stdexcept> #include <string> #include <string_view> - -#include <util/system/yassert.h> + +#include <util/system/yassert.h> #include <util/system/atomic.h> #include "ptr.h" -#include "utility.h" +#include "utility.h" #include "bitops.h" #include "explicit_type.h" #include "reserve.h" diff --git a/util/generic/string_ut.h b/util/generic/string_ut.h index 44bb10bdeb..0701a6d6d8 100644 --- a/util/generic/string_ut.h +++ b/util/generic/string_ut.h @@ -578,23 +578,23 @@ public: // append family s.append(Data.x()); UNIT_ASSERT(s == Data._0123456x()); - + #ifdef TSTRING_IS_STD_STRING s.append(Data.xyz() + 1, 1); #else s.append(Data.xyz(), 1, 1); #endif UNIT_ASSERT(s == Data._0123456xy()); - + s.append(TStringType(Data.z())); UNIT_ASSERT(s == Data._0123456xyz()); - + s.append(TStringType(Data.XYZ()), 2, 1); UNIT_ASSERT(s == Data._0123456xyzZ()); - + s.append(*Data._0()); UNIT_ASSERT(s == Data._0123456xyzZ0()); - + // prepend family s = Data._0123456xyz(); s.prepend(TStringType(Data.abc())); @@ -649,7 +649,7 @@ public: UNIT_ASSERT(s.at(s.size()) == 0); UNIT_ASSERT(s[s.size()] == 0); } - + #ifndef TSTRING_IS_STD_STRING void TestRefCount() { using TStr = TStringType; @@ -682,12 +682,12 @@ public: UNIT_ASSERT(s.find(Data._345()) == 3); UNIT_ASSERT(s.find(Data._345(), 5) == 10); - + UNIT_ASSERT(s.find(Data._345(), 20) == TStringType::npos); UNIT_ASSERT(s.find(*Data._3()) == 3); UNIT_ASSERT(s.find(TStringType(Data._345())) == 3); UNIT_ASSERT(s.find(TStringType(Data._345()), 2) == 3); - + UNIT_ASSERT(s.find_first_of(TStringType(Data._389())) == 3); UNIT_ASSERT(s.find_first_of(Data._389()) == 3); UNIT_ASSERT(s.find_first_of(Data._389(), s.size()) == TStringType::npos); @@ -696,7 +696,7 @@ public: UNIT_ASSERT(s.find_first_of('1', 2) == 8); UNIT_ASSERT(s.find_first_not_of('0') == 1); UNIT_ASSERT(s.find_first_not_of('1', 1) == 2); - + const TStringType rs = Data._0123401234(); UNIT_ASSERT(rs.rfind(*Data._3()) == 8); @@ -720,8 +720,8 @@ public: UNIT_ASSERT(s.rfind(TStringType(Data._345()), 6) == 3); UNIT_ASSERT(s.rfind(TStringType(Data._345()), 3) == 3); UNIT_ASSERT(s.rfind(TStringType(Data._345()), 2) == TStringType::npos); - } - + } + void TestContains() { const TStringType s(Data._0123456_12345()); const TStringType s2(Data._0123()); @@ -743,7 +743,7 @@ public: void TestOperators() { TStringType s(Data._0123456()); - + // operator += s += TStringType(Data.x()); UNIT_ASSERT(s == Data._0123456x()); diff --git a/util/generic/vector.h b/util/generic/vector.h index a5b258955a..92ec4488e9 100644 --- a/util/generic/vector.h +++ b/util/generic/vector.h @@ -1,5 +1,5 @@ #pragma once - + #include "fwd.h" #include "reserve.h" @@ -129,4 +129,4 @@ public: this->erase(this->begin() + size, this->end()); } } -}; +}; diff --git a/util/generic/yexception_ut.cpp b/util/generic/yexception_ut.cpp index cb3e29fed8..a0a4e1c06c 100644 --- a/util/generic/yexception_ut.cpp +++ b/util/generic/yexception_ut.cpp @@ -21,8 +21,8 @@ static inline void Throw2DontMove() { #if defined(_MSC_VER) #pragma warning(disable : 4702) /*unreachable code*/ -#endif - +#endif + static void CallbackFun(int i) { throw i; } diff --git a/util/generic/ymath.cpp b/util/generic/ymath.cpp index 31270728f4..d713267537 100644 --- a/util/generic/ymath.cpp +++ b/util/generic/ymath.cpp @@ -8,7 +8,7 @@ float Exp2f(float x) { return powf(2.0f, x); } -#ifdef _MSC_VER +#ifdef _MSC_VER double Erf(double x) { static constexpr double _M_2_SQRTPI = 1.12837916709551257390; diff --git a/util/generic/ymath.h b/util/generic/ymath.h index 9ff9ae2abe..9ed3110211 100644 --- a/util/generic/ymath.h +++ b/util/generic/ymath.h @@ -68,7 +68,7 @@ static inline bool IsFinite(double f) { #if defined(isfinite) return isfinite(f); #elif defined(_win_) - return _finite(f) != 0; + return _finite(f) != 0; #elif defined(_darwin_) return isfinite(f); #elif defined(__GNUC__) @@ -82,7 +82,7 @@ static inline bool IsFinite(double f) { static inline bool IsNan(double f) { #if defined(_win_) - return _isnan(f) != 0; + return _isnan(f) != 0; #else return std::isnan(f); #endif @@ -92,7 +92,7 @@ inline bool IsValidFloat(double f) { return IsFinite(f) && !IsNan(f); } -#ifdef _MSC_VER +#ifdef _MSC_VER double Erf(double x); #else inline double Erf(double x) { diff --git a/util/memory/blob.cpp b/util/memory/blob.cpp index 91da5cadca..ab5bb88fc1 100644 --- a/util/memory/blob.cpp +++ b/util/memory/blob.cpp @@ -166,7 +166,7 @@ TBlob TBlob::SubBlob(size_t begin, size_t end) const { } TBlob TBlob::DeepCopy() const { - return TBlob::Copy(Data(), Length()); + return TBlob::Copy(Data(), Length()); } template <class TCounter> @@ -184,11 +184,11 @@ static inline TBlob CopyConstruct(const void* data, size_t len) { return ret; } -TBlob TBlob::CopySingleThreaded(const void* data, size_t length) { +TBlob TBlob::CopySingleThreaded(const void* data, size_t length) { return CopyConstruct<TSimpleCounter>(data, length); } -TBlob TBlob::Copy(const void* data, size_t length) { +TBlob TBlob::Copy(const void* data, size_t length) { return CopyConstruct<TAtomicCounter>(data, length); } @@ -292,11 +292,11 @@ TBlob TBlob::LockedFromMemoryMap(const TMemoryMap& map, ui64 offset, size_t leng return ConstructFromMap<TAtomicCounter>(map, offset, length, EMappingMode::Locked); } -TBlob TBlob::FromMemoryMapSingleThreaded(const TMemoryMap& map, ui64 offset, size_t length) { +TBlob TBlob::FromMemoryMapSingleThreaded(const TMemoryMap& map, ui64 offset, size_t length) { return ConstructFromMap<TSimpleCounter>(map, offset, length, EMappingMode::Standard); } -TBlob TBlob::FromMemoryMap(const TMemoryMap& map, ui64 offset, size_t length) { +TBlob TBlob::FromMemoryMap(const TMemoryMap& map, ui64 offset, size_t length) { return ConstructFromMap<TAtomicCounter>(map, offset, length, EMappingMode::Standard); } @@ -334,34 +334,34 @@ TBlob TBlob::FromFileContent(const TString& path) { return ConstructFromFileContent<TAtomicCounter>(file, 0, file.GetLength()); } -TBlob TBlob::FromFileContentSingleThreaded(const TFile& file) { +TBlob TBlob::FromFileContentSingleThreaded(const TFile& file) { return ConstructFromFileContent<TSimpleCounter>(file, 0, file.GetLength()); } -TBlob TBlob::FromFileContent(const TFile& file) { +TBlob TBlob::FromFileContent(const TFile& file) { return ConstructFromFileContent<TAtomicCounter>(file, 0, file.GetLength()); } -TBlob TBlob::FromFileContentSingleThreaded(const TFile& file, ui64 offset, size_t length) { +TBlob TBlob::FromFileContentSingleThreaded(const TFile& file, ui64 offset, size_t length) { return ConstructFromFileContent<TSimpleCounter>(file, offset, length); } -TBlob TBlob::FromFileContent(const TFile& file, ui64 offset, size_t length) { +TBlob TBlob::FromFileContent(const TFile& file, ui64 offset, size_t length) { return ConstructFromFileContent<TAtomicCounter>(file, offset, length); } template <class TCounter> -static inline TBlob ConstructFromBuffer(TBuffer& in) { +static inline TBlob ConstructFromBuffer(TBuffer& in) { using TBase = TBufferBlobBase<TCounter>; - THolder<TBase> base(new TBase(in)); - - TBlob ret(base->Buffer().Data(), base->Buffer().Size(), base.Get()); + THolder<TBase> base(new TBase(in)); + + TBlob ret(base->Buffer().Data(), base->Buffer().Size(), base.Get()); Y_UNUSED(base.Release()); - - return ret; -} - -template <class TCounter> + + return ret; +} + +template <class TCounter> static inline TBlob ConstructFromStream(IInputStream& in) { TBuffer buf; @@ -371,7 +371,7 @@ static inline TBlob ConstructFromStream(IInputStream& in) { TransferData(&in, &out); } - return ConstructFromBuffer<TCounter>(buf); + return ConstructFromBuffer<TCounter>(buf); } TBlob TBlob::FromStreamSingleThreaded(IInputStream& in) { @@ -381,14 +381,14 @@ TBlob TBlob::FromStreamSingleThreaded(IInputStream& in) { TBlob TBlob::FromStream(IInputStream& in) { return ConstructFromStream<TAtomicCounter>(in); } - -TBlob TBlob::FromBufferSingleThreaded(TBuffer& in) { - return ConstructFromBuffer<TSimpleCounter>(in); -} - -TBlob TBlob::FromBuffer(TBuffer& in) { - return ConstructFromBuffer<TAtomicCounter>(in); -} + +TBlob TBlob::FromBufferSingleThreaded(TBuffer& in) { + return ConstructFromBuffer<TSimpleCounter>(in); +} + +TBlob TBlob::FromBuffer(TBuffer& in) { + return ConstructFromBuffer<TAtomicCounter>(in); +} template <class TCounter, class S> TBlob ConstructFromString(S&& s) { diff --git a/util/memory/blob.h b/util/memory/blob.h index 20c02a68df..84d4ebaa4d 100644 --- a/util/memory/blob.h +++ b/util/memory/blob.h @@ -7,8 +7,8 @@ class TMemoryMap; class IInputStream; -class TFile; -class TBuffer; +class TFile; +class TBuffer; enum class EMappingMode { /// Just mmap a file allowing lazy page loading at access @@ -289,11 +289,11 @@ public: /// Creates a blob with a single-threaded (non atomic) refcounter. No memory allocation, no content copy. /// @details The input object becomes empty. static TBlob FromBufferSingleThreaded(TBuffer& in); - + /// Creates a blob with a multi-threaded (atomic) refcounter. No memory allocation, no content copy. /// @details The input object becomes empty. static TBlob FromBuffer(TBuffer& in); - + /// Creates a blob from TString with a single-threaded (non atomic) refcounter. static TBlob FromStringSingleThreaded(const TString& s); diff --git a/util/memory/blob_ut.cpp b/util/memory/blob_ut.cpp index 023f9a0487..405717c275 100644 --- a/util/memory/blob_ut.cpp +++ b/util/memory/blob_ut.cpp @@ -1,7 +1,7 @@ #include "blob.h" #include <library/cpp/testing/unittest/registar.h> - + #include <util/system/tempfile.h> #include <util/folder/path.h> #include <util/stream/output.h> @@ -13,7 +13,7 @@ Y_UNIT_TEST_SUITE(TBlobTest) { Y_UNIT_TEST(TestSubBlob) { TBlob child; const char* p = nullptr; - + { TBlob parent = TBlob::CopySingleThreaded("0123456789", 10); UNIT_ASSERT_EQUAL(parent.Length(), 10); @@ -21,12 +21,12 @@ Y_UNIT_TEST_SUITE(TBlobTest) { UNIT_ASSERT_EQUAL(memcmp(p, "0123456789", 10), 0); child = parent.SubBlob(2, 5); } // Don't worry about parent - + UNIT_ASSERT_EQUAL(child.Length(), 3); UNIT_ASSERT_EQUAL(memcmp(child.AsCharPtr(), "234", 3), 0); UNIT_ASSERT_EQUAL(p + 2, child.AsCharPtr()); } - + Y_UNIT_TEST(TestFromStream) { TString s("sjklfgsdyutfuyas54fa78s5f89a6df790asdf7"); TMemoryInput mi(s.data(), s.size()); diff --git a/util/memory/segmented_string_pool.h b/util/memory/segmented_string_pool.h index a40aa408f5..bb8818e7b6 100644 --- a/util/memory/segmented_string_pool.h +++ b/util/memory/segmented_string_pool.h @@ -11,17 +11,17 @@ #include <cstdio> #include <cstdlib> -/* - * Non-reallocated storage for the objects of POD type - */ +/* + * Non-reallocated storage for the objects of POD type + */ template <class T, class Alloc = std::allocator<T>> class segmented_pool: TNonCopyable { -protected: +protected: Alloc seg_allocator; struct seg_inf { T* data; // allocated chunk - size_t _size; // size of allocated chunk in sizeof(T)-units - size_t freepos; // offset to free chunk's memory in bytes + size_t _size; // size of allocated chunk in sizeof(T)-units + size_t freepos; // offset to free chunk's memory in bytes seg_inf() : data(nullptr) , _size(0) @@ -38,63 +38,63 @@ protected: using seg_container = TVector<seg_inf>; using seg_iterator = typename seg_container::iterator; using seg_const_iterator = typename seg_container::const_iterator; - const size_t segment_size; // default size of a memory chunk in sizeof(T)-units - size_t last_free; // size of free memory in chunk in sizeof(T)-units - size_t last_ins_size; // size of memory used in chunk by the last append() in bytes - seg_container segs; // array of memory chunks - seg_iterator curseg; // a segment for the current insertion - const char* Name; // for debug memory usage -protected: - void check_capacity(size_t len) { + const size_t segment_size; // default size of a memory chunk in sizeof(T)-units + size_t last_free; // size of free memory in chunk in sizeof(T)-units + size_t last_ins_size; // size of memory used in chunk by the last append() in bytes + seg_container segs; // array of memory chunks + seg_iterator curseg; // a segment for the current insertion + const char* Name; // for debug memory usage +protected: + void check_capacity(size_t len) { if (Y_UNLIKELY(!last_free || len > last_free)) { - if (curseg != segs.end() && curseg->freepos > 0) - ++curseg; - last_free = (len > segment_size ? len : segment_size); - if (curseg == segs.end() || curseg->_size < last_free) { - segs.push_back(seg_inf(seg_allocator.allocate(last_free), last_free)); + if (curseg != segs.end() && curseg->freepos > 0) + ++curseg; + last_free = (len > segment_size ? len : segment_size); + if (curseg == segs.end() || curseg->_size < last_free) { + segs.push_back(seg_inf(seg_allocator.allocate(last_free), last_free)); if (Y_UNLIKELY(Name)) printf("Pool \"%s\" was increased by %" PRISZT " bytes to %" PRISZT " Mb.\n", Name, last_free * sizeof(T), capacity() / 0x100000); - curseg = segs.end() - 1; - } + curseg = segs.end() - 1; + } Y_ASSERT(curseg->freepos == 0); Y_ASSERT(curseg->_size >= last_free); - } - } + } + } -public: +public: explicit segmented_pool(size_t segsz, const char* name = nullptr) : segment_size(segsz) , last_free(0) , last_ins_size(0) , Name(name) - { - curseg = segs.begin(); - } - ~segmented_pool() { - clear(); + { + curseg = segs.begin(); + } + ~segmented_pool() { + clear(); } - /* src - array of objects, len - count of elements in array */ + /* src - array of objects, len - count of elements in array */ T* append(const T* src, size_t len) { - check_capacity(len); + check_capacity(len); ui8* rv = (ui8*)curseg->data + curseg->freepos; - last_ins_size = sizeof(T) * len; - if (src) - memcpy(rv, src, last_ins_size); - curseg->freepos += last_ins_size, last_free -= len; - return (T*)rv; + last_ins_size = sizeof(T) * len; + if (src) + memcpy(rv, src, last_ins_size); + curseg->freepos += last_ins_size, last_free -= len; + return (T*)rv; } T* append() { - T* obj = get_raw(); + T* obj = get_raw(); new (obj) T(); - return obj; + return obj; } T* get_raw() { // append(0, 1) - check_capacity(1); + check_capacity(1); ui8* rv = (ui8*)curseg->data + curseg->freepos; - last_ins_size = sizeof(T); - curseg->freepos += last_ins_size, last_free -= 1; - return (T*)rv; - } + last_ins_size = sizeof(T); + curseg->freepos += last_ins_size, last_free -= 1; + return (T*)rv; + } size_t get_segment_size() const { return segment_size; } @@ -104,27 +104,27 @@ public: return true; return false; } - size_t size() const { - size_t r = 0; - for (seg_const_iterator i = segs.begin(); i != segs.end(); ++i) - r += i->freepos; - return r; - } - size_t capacity() const { - return segs.size() * segment_size * sizeof(T); - } - void restart() { + size_t size() const { + size_t r = 0; + for (seg_const_iterator i = segs.begin(); i != segs.end(); ++i) + r += i->freepos; + return r; + } + size_t capacity() const { + return segs.size() * segment_size * sizeof(T); + } + void restart() { if (curseg != segs.end()) ++curseg; for (seg_iterator i = segs.begin(); i != curseg; ++i) - i->freepos = 0; - curseg = segs.begin(); - last_free = 0; - last_ins_size = 0; - } + i->freepos = 0; + curseg = segs.begin(); + last_free = 0; + last_ins_size = 0; + } void clear() { - for (seg_iterator i = segs.begin(); i != segs.end(); ++i) - seg_allocator.deallocate(i->data, i->_size); + for (seg_iterator i = segs.begin(); i != segs.end(); ++i) + seg_allocator.deallocate(i->data, i->_size); segs.clear(); curseg = segs.begin(); last_free = 0; @@ -132,25 +132,25 @@ public: } void undo_last_append() { Y_ASSERT(curseg != segs.end()); // do not use before append() - if (last_ins_size) { + if (last_ins_size) { Y_ASSERT(last_ins_size <= curseg->freepos); - curseg->freepos -= last_ins_size; - last_free += last_ins_size / sizeof(T); + curseg->freepos -= last_ins_size; + last_free += last_ins_size / sizeof(T); last_ins_size = 0; } } - void alloc_first_seg() { + void alloc_first_seg() { Y_ASSERT(capacity() == 0); - check_capacity(segment_size); + check_capacity(segment_size); Y_ASSERT(capacity() == segment_size * sizeof(T)); - } + } }; class segmented_string_pool: public segmented_pool<char> { -private: +private: using _Base = segmented_pool<char>; -public: +public: segmented_string_pool() : segmented_string_pool(1024 * 1024) { @@ -162,8 +162,8 @@ public: } char* append(const char* src) { Y_ASSERT(src); - return _Base::append(src, strlen(src) + 1); - } + return _Base::append(src, strlen(src) + 1); + } char* append(const char* src, size_t len) { char* rv = _Base::append(nullptr, len + 1); if (src) @@ -174,16 +174,16 @@ public: char* Append(const TStringBuf s) { return append(s.data(), s.size()); } - void align_4() { - size_t t = (curseg->freepos + 3) & ~3; - last_free -= t - curseg->freepos; - curseg->freepos = t; - } + void align_4() { + size_t t = (curseg->freepos + 3) & ~3; + last_free -= t - curseg->freepos; + curseg->freepos = t; + } char* Allocate(size_t len) { return append(nullptr, len); } -}; - +}; + template <typename T, typename C> inline T* pool_push(segmented_pool<C>& pool, const T* v) { static_assert(sizeof(C) == 1, "only char type supported"); diff --git a/util/memory/segpool_alloc.h b/util/memory/segpool_alloc.h index 1a83b7a543..78a7bad222 100644 --- a/util/memory/segpool_alloc.h +++ b/util/memory/segpool_alloc.h @@ -1,6 +1,6 @@ #pragma once -#include "segmented_string_pool.h" +#include "segmented_string_pool.h" /** This is a partially implemented allocator class that uses segmented_pool diff --git a/util/network/address.h b/util/network/address.h index 448fcac0c9..f6ea9bf117 100644 --- a/util/network/address.h +++ b/util/network/address.h @@ -87,7 +87,7 @@ namespace NAddr { } socklen_t Len() const override { - return (socklen_t)AI_->ai_addrlen; + return (socklen_t)AI_->ai_addrlen; } private: diff --git a/util/network/init.h b/util/network/init.h index 08a79c0fca..16c27770f6 100644 --- a/util/network/init.h +++ b/util/network/init.h @@ -33,16 +33,16 @@ using SOCKET = int; using nfds_t = ULONG; #undef Yield - + struct sockaddr_un { short sun_family; char sun_path[108]; }; - + #define PF_LOCAL AF_UNIX #define NETDB_INTERNAL -1 #define NETDB_SUCCESS 0 - + #endif #if defined(_win_) || defined(_darwin_) diff --git a/util/network/ip.h b/util/network/ip.h index dc7c2d24a0..ad53edad85 100644 --- a/util/network/ip.h +++ b/util/network/ip.h @@ -29,7 +29,7 @@ static inline TIpHost IpFromString(const char* ipStr) { } static inline char* IpToString(TIpHost ip, char* buf, size_t len) { - if (!inet_ntop(AF_INET, (void*)&ip, buf, (socklen_t)len)) { + if (!inet_ntop(AF_INET, (void*)&ip, buf, (socklen_t)len)) { ythrow TSystemError() << "Failed to get ip address string"; } diff --git a/util/network/pollerimpl.h b/util/network/pollerimpl.h index e8c7e40fba..a9e8dffe9d 100644 --- a/util/network/pollerimpl.h +++ b/util/network/pollerimpl.h @@ -360,7 +360,7 @@ struct TSelectPollerNoTemplate { (*this)[fd].Set(data, filter); } - inline void Remove(SOCKET fd) { + inline void Remove(SOCKET fd) { erase(fd); } @@ -424,7 +424,7 @@ public: closesocket(Signal_[1]); } - inline void SetImpl(void* data, SOCKET fd, int what) { + inline void SetImpl(void* data, SOCKET fd, int what) { with_lock (CommandLock_) { Commands_.push_back(TCommand(fd, what, data)); } @@ -512,7 +512,7 @@ public: tout.tv_sec = timeout / 1000000; tout.tv_usec = timeout % 1000000; - int ret = ContSelect(int(maxFdNum + 1), in, out, errFds, &tout); + int ret = ContSelect(int(maxFdNum + 1), in, out, errFds, &tout); if (ret > 0 && FD_ISSET(WaitSock(), in)) { --ret; @@ -524,7 +524,7 @@ public: TEvent* eventsStart = events; for (typename TFds::iterator it = Fds_.begin(); it != Fds_.end(); ++it) { - const SOCKET fd = it->first; + const SOCKET fd = it->first; THandle& handle = it->second; if (FD_ISSET(fd, errFds)) { @@ -659,7 +659,7 @@ public: using TEvent = typename TBase::TEvent; - inline void Set(void* data, SOCKET fd, int what) { + inline void Set(void* data, SOCKET fd, int what) { if (what) { this->SetImpl(data, fd, what); } else { diff --git a/util/network/sock.h b/util/network/sock.h index b10be2f715..53ddd4b61b 100644 --- a/util/network/sock.h +++ b/util/network/sock.h @@ -4,7 +4,7 @@ #include <util/system/defaults.h> #include <util/string/cast.h> #include <util/stream/output.h> -#include <util/system/sysstat.h> +#include <util/system/sysstat.h> #if defined(_win_) || defined(_cygwin_) #include <util/system/file.h> @@ -193,7 +193,7 @@ struct TSockAddrLocal: public sockaddr_un, public ISockAddr { if (ret < 0) return -errno; - ret = Chmod(sun_path, mode); + ret = Chmod(sun_path, mode); if (ret < 0) return -errno; return 0; @@ -365,7 +365,7 @@ public: static ssize_t Check(ssize_t ret, const char* op = "") { if (ret < 0) - ythrow TSystemError(-(int)ret) << "socket operation " << op; + ythrow TSystemError(-(int)ret) << "socket operation " << op; return ret; } }; @@ -461,7 +461,7 @@ public: s = accept((SOCKET) * this, nullptr, nullptr); } - if (s == INVALID_SOCKET) + if (s == INVALID_SOCKET) return -errno; TSocketHolder sock(s); @@ -569,7 +569,7 @@ protected: return (size_t)ret; } - ythrow TSystemError(-(int)ret) << "can not read from socket input stream"; + ythrow TSystemError(-(int)ret) << "can not read from socket input stream"; } }; @@ -597,7 +597,7 @@ protected: const ssize_t ret = Socket->Send(ptr, len); if (ret < 0) { - ythrow TSystemError(-(int)ret) << "can not write to socket output stream"; + ythrow TSystemError(-(int)ret) << "can not write to socket output stream"; } Y_ASSERT((size_t)ret <= len); diff --git a/util/network/socket.cpp b/util/network/socket.cpp index 4f6e804346..597740e285 100644 --- a/util/network/socket.cpp +++ b/util/network/socket.cpp @@ -6,7 +6,7 @@ #include <util/system/defaults.h> #include <util/system/byteorder.h> - + #if defined(_unix_) #include <netdb.h> #include <sys/types.h> @@ -51,16 +51,16 @@ using namespace NAddr; #if defined(_win_) int inet_aton(const char* cp, struct in_addr* inp) { - sockaddr_in addr; - addr.sin_family = AF_INET; - int psz = sizeof(addr); + sockaddr_in addr; + addr.sin_family = AF_INET; + int psz = sizeof(addr); if (0 == WSAStringToAddress((char*)cp, AF_INET, nullptr, (LPSOCKADDR)&addr, &psz)) { - memcpy(inp, &addr.sin_addr, sizeof(in_addr)); - return 1; - } - return 0; -} - + memcpy(inp, &addr.sin_addr, sizeof(in_addr)); + return 1; + } + return 0; +} + #if (_WIN32_WINNT < 0x0600) const char* inet_ntop(int af, const void* src, char* dst, socklen_t size) { if (af != AF_INET) { @@ -238,8 +238,8 @@ int poll(struct pollfd fds[], nfds_t nfds, int timeout) noexcept { } #endif -#endif - +#endif + bool GetRemoteAddr(SOCKET Socket, char* str, socklen_t size) { if (!size) { return false; @@ -267,7 +267,7 @@ bool GetRemoteAddr(SOCKET Socket, char* str, socklen_t size) { void SetSocketTimeout(SOCKET s, long timeout) { SetSocketTimeout(s, timeout, 0); -} +} void SetSocketTimeout(SOCKET s, long sec, long msec) { #ifdef SO_SNDTIMEO @@ -282,15 +282,15 @@ void SetSocketTimeout(SOCKET s, long sec, long msec) { CheckedSetSockOpt(s, SOL_SOCKET, SO_SNDTIMEO, timeout, "send timeout"); #endif } - + void SetLinger(SOCKET s, bool on, unsigned len) { -#ifdef SO_LINGER - struct linger l = {on, (u_short)len}; +#ifdef SO_LINGER + struct linger l = {on, (u_short)len}; CheckedSetSockOpt(s, SOL_SOCKET, SO_LINGER, l, "linger"); -#endif -} - +#endif +} + void SetZeroLinger(SOCKET s) { SetLinger(s, 1, 0); } @@ -659,7 +659,7 @@ static inline SOCKET DoConnectImpl(const struct addrinfo* res, const TInstant& d SetNonBlock(s, true); - if (connect(s, res->ai_addr, (int)res->ai_addrlen)) { + if (connect(s, res->ai_addr, (int)res->ai_addrlen)) { int err = LastSystemError(); if (err == EINPROGRESS || err == EAGAIN || err == EWOULDBLOCK) { @@ -671,13 +671,13 @@ static inline SOCKET DoConnectImpl(const struct addrinfo* res, const TInstant& d POLLOUT, 0}; - const ssize_t n = PollD(&p, 1, deadLine); + const ssize_t n = PollD(&p, 1, deadLine); /* * timeout occured */ if (n < 0) { - ythrow TSystemError(-(int)n) << "can not connect"; + ythrow TSystemError(-(int)n) << "can not connect"; } CheckedGetSockOpt(s, SOL_SOCKET, SO_ERROR, err, "socket error"); @@ -902,7 +902,7 @@ size_t TSocketInput::DoRead(void* buf, size_t len) { return (size_t)ret; } - ythrow TSystemError(-(int)ret) << "can not read from socket input stream"; + ythrow TSystemError(-(int)ret) << "can not read from socket input stream"; } TSocketOutput::TSocketOutput(const TSocket& s) noexcept @@ -936,7 +936,7 @@ void TSocketOutput::DoWriteV(const TPart* parts, size_t count) { const ssize_t ret = S_.SendV(parts, count); if (ret < 0) { - ythrow TSystemError(-(int)ret) << "can not writev to socket output stream"; + ythrow TSystemError(-(int)ret) << "can not writev to socket output stream"; } /* @@ -1157,7 +1157,7 @@ static inline bool FlagsAreEnabled(int fd, int flags) { #endif #if defined(_win_) -static inline void SetNonBlockSocket(SOCKET fd, int value) { +static inline void SetNonBlockSocket(SOCKET fd, int value) { unsigned long inbuf = value; unsigned long outbuf = 0; DWORD written = 0; @@ -1171,7 +1171,7 @@ static inline void SetNonBlockSocket(SOCKET fd, int value) { } } -static inline bool IsNonBlockSocket(SOCKET fd) { +static inline bool IsNonBlockSocket(SOCKET fd) { unsigned long buf = 0; if (WSAIoctl(fd, FIONBIO, 0, 0, &buf, sizeof(buf), 0, 0, 0) == SOCKET_ERROR) { @@ -1182,7 +1182,7 @@ static inline bool IsNonBlockSocket(SOCKET fd) { } #endif -void SetNonBlock(SOCKET fd, bool value) { +void SetNonBlock(SOCKET fd, bool value) { #if defined(_unix_) #if defined(FIONBIO) Y_UNUSED(SetFlag); // shut up clang about unused function @@ -1201,7 +1201,7 @@ void SetNonBlock(SOCKET fd, bool value) { #endif } -bool IsNonBlock(SOCKET fd) { +bool IsNonBlock(SOCKET fd) { #if defined(_unix_) return FlagsAreEnabled(fd, O_NONBLOCK); #elif defined(_win_) diff --git a/util/network/socket.h b/util/network/socket.h index 40c8648b40..ea9c01f6bb 100644 --- a/util/network/socket.h +++ b/util/network/socket.h @@ -1,17 +1,17 @@ #pragma once - + #include "init.h" #include <util/system/yassert.h> #include <util/system/defaults.h> -#include <util/system/error.h> +#include <util/system/error.h> #include <util/stream/output.h> #include <util/stream/input.h> #include <util/generic/ptr.h> #include <util/generic/yexception.h> #include <util/generic/noncopyable.h> #include <util/datetime/base.h> - + #include <cerrno> #ifndef INET_ADDRSTRLEN @@ -50,14 +50,14 @@ int poll(struct pollfd fds[], nfds_t nfds, int timeout) noexcept; int inet_aton(const char* cp, struct in_addr* inp); #define get_host_error() WSAGetLastError() - + #define SHUT_RD SD_RECEIVE #define SHUT_WR SD_SEND #define SHUT_RDWR SD_BOTH #define INFTIM (-1) -#endif - +#endif + template <class T> static inline int SetSockOpt(SOCKET s, int level, int optname, T opt) noexcept { return setsockopt(s, level, optname, (const char*)&opt, sizeof(opt)); @@ -141,8 +141,8 @@ bool HasLocalAddress(SOCKET socket); **/ extern "C" bool IsReusePortAvailable(); -bool IsNonBlock(SOCKET fd); -void SetNonBlock(SOCKET fd, bool nonBlock = true); +bool IsNonBlock(SOCKET fd); +void SetNonBlock(SOCKET fd, bool nonBlock = true); struct addrinfo; diff --git a/util/network/socket_ut.cpp b/util/network/socket_ut.cpp index 6b20e11f70..ff7a9e6279 100644 --- a/util/network/socket_ut.cpp +++ b/util/network/socket_ut.cpp @@ -312,29 +312,29 @@ void TPollTest::TestPollInOut() { } } - int expectedCount = 0; + int expectedCount = 0; for (size_t i = 0; i < connectedSockets.size(); ++i) { pollfd fd = {(i % 5 == 4) ? INVALID_SOCKET : static_cast<SOCKET>(*connectedSockets[i]), POLLIN | POLLOUT, 0}; fds.push_back(fd); - if (i % 5 != 4) - ++expectedCount; + if (i % 5 != 4) + ++expectedCount; } int polledCount = poll(&fds[0], fds.size(), INFTIM); - UNIT_ASSERT_EQUAL(expectedCount, polledCount); + UNIT_ASSERT_EQUAL(expectedCount, polledCount); for (size_t i = 0; i < connectedSockets.size(); ++i) { - short revents = fds[i].revents; + short revents = fds[i].revents; if (i % 5 == 0) { - UNIT_ASSERT_EQUAL(static_cast<short>(POLLRDNORM | POLLWRNORM), revents); + UNIT_ASSERT_EQUAL(static_cast<short>(POLLRDNORM | POLLWRNORM), revents); } else if (i % 5 == 1) { - UNIT_ASSERT_EQUAL(static_cast<short>(POLLOUT | POLLWRNORM), revents); + UNIT_ASSERT_EQUAL(static_cast<short>(POLLOUT | POLLWRNORM), revents); } else if (i % 5 == 2) { - UNIT_ASSERT_EQUAL(static_cast<short>(POLLHUP | POLLRDNORM | POLLWRNORM), revents); + UNIT_ASSERT_EQUAL(static_cast<short>(POLLHUP | POLLRDNORM | POLLWRNORM), revents); } else if (i % 5 == 3) { - UNIT_ASSERT_EQUAL(static_cast<short>(POLLHUP | POLLWRNORM), revents); + UNIT_ASSERT_EQUAL(static_cast<short>(POLLHUP | POLLWRNORM), revents); } else if (i % 5 == 4) { - UNIT_ASSERT_EQUAL(static_cast<short>(POLLNVAL), revents); + UNIT_ASSERT_EQUAL(static_cast<short>(POLLNVAL), revents); } } #endif diff --git a/util/str_stl.h b/util/str_stl.h index f1e137181d..3e7f61e551 100644 --- a/util/str_stl.h +++ b/util/str_stl.h @@ -59,8 +59,8 @@ namespace NHashPrivate { template <class T> struct hash: public NHashPrivate::THashHelper<T, std::is_scalar<T>::value && !std::is_integral<T>::value> { -}; - +}; + template <typename T> struct hash<const T*> { inline size_t operator()(const T* t) const noexcept { @@ -76,11 +76,11 @@ template <> struct hash<const char*>: ::NHashPrivate::TStringHash<char> { }; -template <> +template <> struct THash<TStringBuf>: ::NHashPrivate::TStringHash<char> { }; -template <> +template <> struct hash<TString>: ::NHashPrivate::TStringHash<char> { }; @@ -236,7 +236,7 @@ struct TCIEqualTo<TString> { template <class T> struct TLess: public std::less<T> { }; - + template <> struct TLess<TString>: public TLess<TStringBuf> { using is_transparent = void; diff --git a/util/stream/file.cpp b/util/stream/file.cpp index dc5d2f6311..00b1827429 100644 --- a/util/stream/file.cpp +++ b/util/stream/file.cpp @@ -5,12 +5,12 @@ TUnbufferedFileInput::TUnbufferedFileInput(const TString& path) : File_(path, OpenExisting | RdOnly | Seq) -{ +{ if (!File_.IsOpen()) { ythrow TIoException() << "file " << path << " not open"; - } -} - + } +} + TUnbufferedFileInput::TUnbufferedFileInput(const TFile& file) : File_(file) { @@ -80,7 +80,7 @@ public: inline ~TImpl() = default; }; -TMappedFileInput::TMappedFileInput(const TFile& file) +TMappedFileInput::TMappedFileInput(const TFile& file) : TMemoryInput(nullptr, 0) , Impl_(new TImpl(file)) { @@ -90,8 +90,8 @@ TMappedFileInput::TMappedFileInput(const TFile& file) TMappedFileInput::TMappedFileInput(const TString& path) : TMemoryInput(nullptr, 0) , Impl_(new TImpl(TFile(path, OpenExisting | RdOnly))) -{ +{ Reset(Impl_->Data(), Impl_->Size()); -} - +} + TMappedFileInput::~TMappedFileInput() = default; diff --git a/util/stream/file.h b/util/stream/file.h index c1cf4f591d..a0010a909c 100644 --- a/util/stream/file.h +++ b/util/stream/file.h @@ -6,7 +6,7 @@ #include "buffered.h" #include "mem.h" -#include <util/system/file.h> +#include <util/system/file.h> #include <utility> /** diff --git a/util/stream/pipe.cpp b/util/stream/pipe.cpp index 51be1934a7..4f44b5ea60 100644 --- a/util/stream/pipe.cpp +++ b/util/stream/pipe.cpp @@ -39,7 +39,7 @@ TPipeBase::TPipeBase(const TString& command, const char* mode) TPipeBase::~TPipeBase() = default; TPipeInput::TPipeInput(const TString& command) - : TPipeBase(command, "r") + : TPipeBase(command, "r") { } @@ -62,7 +62,7 @@ size_t TPipeInput::DoRead(void* buf, size_t len) { } TPipeOutput::TPipeOutput(const TString& command) - : TPipeBase(command, "w") + : TPipeBase(command, "w") { } diff --git a/util/stream/zlib_ut.cpp b/util/stream/zlib_ut.cpp index 2290b4a9de..b6b89e4a82 100644 --- a/util/stream/zlib_ut.cpp +++ b/util/stream/zlib_ut.cpp @@ -73,7 +73,7 @@ Y_UNIT_TEST_SUITE(TZLibTest) { UNIT_ASSERT_EQUAL(d.ReadAll(), DATA); } - } + } Y_UNIT_TEST(Dictionary) { static constexpr TStringBuf data = "<html><body></body></html>"; diff --git a/util/string/cstriter.h b/util/string/cstriter.h index ca57728c39..2fa5576788 100644 --- a/util/string/cstriter.h +++ b/util/string/cstriter.h @@ -10,5 +10,5 @@ static inline bool operator==(It b, TCStringEndIterator) { template <class It> static inline bool operator!=(It b, TCStringEndIterator) { - return !!*b; + return !!*b; } diff --git a/util/string/strip_ut.cpp b/util/string/strip_ut.cpp index d1029d1498..7db5d4615a 100644 --- a/util/string/strip_ut.cpp +++ b/util/string/strip_ut.cpp @@ -134,5 +134,5 @@ Y_UNIT_TEST_SUITE(TStripStringTest) { UNIT_ASSERT_EQUAL(abs1 == "Very long description string written in unknown language.", true); UNIT_ASSERT_EQUAL(abs2 == "Very long description string written in unknown ...", true); UNIT_ASSERT_EQUAL(abs3 == "Very long description string written in ...", true); - } + } } diff --git a/util/string/util.cpp b/util/string/util.cpp index b14f20bf75..3579dde04d 100644 --- a/util/string/util.cpp +++ b/util/string/util.cpp @@ -2,10 +2,10 @@ #include <util/generic/utility.h> -#include <cstdio> -#include <cstdarg> +#include <cstdio> +#include <cstdarg> #include <cstdlib> - + int a2i(const TString& s) { return atoi(s.c_str()); } diff --git a/util/string/util.h b/util/string/util.h index 0d77a5042b..d423c328e6 100644 --- a/util/string/util.h +++ b/util/string/util.h @@ -2,10 +2,10 @@ //THIS FILE A COMPAT STUB HEADER -#include <cstring> +#include <cstring> #include <cstdarg> #include <algorithm> - + #include <util/system/defaults.h> #include <util/generic/string.h> #include <util/generic/strbuf.h> @@ -20,16 +20,16 @@ inline void RemoveIfLast(T& s, int c) { const size_t length = s.length(); if (length && s[length - 1] == c) s.remove(length - 1); -} - +} + /// Adds lastCh symbol to the the of the string if it is not already there. inline void addIfNotLast(TString& s, int lastCh) { size_t len = s.length(); if (!len || s[len - 1] != lastCh) { s.append(char(lastCh)); } -} - +} + /// @details Finishes the string with lastCh1 if lastCh2 is not present in the string and lastCh1 is not already at the end of the string. /// Else, if lastCh2 is not equal to the symbol before the last, it finishes the string with lastCh2. /// @todo ?? Define, when to apply the function. Is in use several times for URLs parsing. @@ -41,8 +41,8 @@ inline void addIfAbsent(TString& s, char lastCh1, char lastCh2) { } else if (pos < s.length() - 1) { addIfNotLast(s, lastCh2); } -} - +} + /// @} /* diff --git a/util/string/vector.h b/util/string/vector.h index e36c348bbe..6769446810 100644 --- a/util/string/vector.h +++ b/util/string/vector.h @@ -1,5 +1,5 @@ #pragma once - + #include "cast.h" #include "split.h" diff --git a/util/system/atomic_gcc.h b/util/system/atomic_gcc.h index ed8dc2bdc5..5adc1b7906 100644 --- a/util/system/atomic_gcc.h +++ b/util/system/atomic_gcc.h @@ -73,18 +73,18 @@ static inline intptr_t AtomicGetAndCas(TAtomic* a, intptr_t exchange, intptr_t c return compare; } -static inline intptr_t AtomicOr(TAtomic& a, intptr_t b) { +static inline intptr_t AtomicOr(TAtomic& a, intptr_t b) { return __atomic_or_fetch(&a, b, __ATOMIC_SEQ_CST); -} - +} + static inline intptr_t AtomicXor(TAtomic& a, intptr_t b) { return __atomic_xor_fetch(&a, b, __ATOMIC_SEQ_CST); } -static inline intptr_t AtomicAnd(TAtomic& a, intptr_t b) { +static inline intptr_t AtomicAnd(TAtomic& a, intptr_t b) { return __atomic_and_fetch(&a, b, __ATOMIC_SEQ_CST); -} - +} + static inline void AtomicBarrier() { __sync_synchronize(); } diff --git a/util/system/atomic_ut.cpp b/util/system/atomic_ut.cpp index 07211ffba7..8205bdcfd1 100644 --- a/util/system/atomic_ut.cpp +++ b/util/system/atomic_ut.cpp @@ -188,9 +188,9 @@ private: } }; -UNIT_TEST_SUITE_REGISTRATION(TAtomicTest<TAtomic>); - -#ifndef _MSC_VER +UNIT_TEST_SUITE_REGISTRATION(TAtomicTest<TAtomic>); + +#ifndef _MSC_VER // chooses type *other than* T1 template <typename T1, typename T2, typename T3> struct TChooser { @@ -216,7 +216,7 @@ public: TString Name() const noexcept override { return "TAtomicTest<TAltAtomic>"; } - + static TString StaticName() noexcept { return "TAtomicTest<TAltAtomic>"; } @@ -224,4 +224,4 @@ public: UNIT_TEST_SUITE_REGISTRATION(TTTest); -#endif +#endif diff --git a/util/system/atomic_win.h b/util/system/atomic_win.h index 65c290e6cc..509fa1dc69 100644 --- a/util/system/atomic_win.h +++ b/util/system/atomic_win.h @@ -4,7 +4,7 @@ #define USE_GENERIC_SETGET -#if defined(_i386_) +#if defined(_i386_) #pragma intrinsic(_InterlockedIncrement) #pragma intrinsic(_InterlockedDecrement) @@ -92,14 +92,14 @@ static inline intptr_t AtomicGetAndCas(TAtomic* a, intptr_t exchange, intptr_t c return _InterlockedCompareExchange64((volatile __int64*)a, exchange, compare); } -static inline intptr_t AtomicOr(TAtomic& a, intptr_t b) { - return _InterlockedOr64(&a, b) | b; -} - -static inline intptr_t AtomicAnd(TAtomic& a, intptr_t b) { - return _InterlockedAnd64(&a, b) & b; -} - +static inline intptr_t AtomicOr(TAtomic& a, intptr_t b) { + return _InterlockedOr64(&a, b) | b; +} + +static inline intptr_t AtomicAnd(TAtomic& a, intptr_t b) { + return _InterlockedAnd64(&a, b) & b; +} + static inline intptr_t AtomicXor(TAtomic& a, intptr_t b) { return _InterlockedXor64(&a, b) ^ b; } diff --git a/util/system/backtrace_ut.cpp b/util/system/backtrace_ut.cpp index 9b5ead71bc..dc83df033f 100644 --- a/util/system/backtrace_ut.cpp +++ b/util/system/backtrace_ut.cpp @@ -8,12 +8,12 @@ using PFunc = int (*)(void**, size_t); int Dbg1(void** buf, size_t len) { - volatile int ret = (int)BackTrace(buf, len); + volatile int ret = (int)BackTrace(buf, len); return ret; } int Dbg2(void** buf, size_t len) { - volatile int ret = (int)BackTrace(buf, len); + volatile int ret = (int)BackTrace(buf, len); return ret; } diff --git a/util/system/byteorder.h b/util/system/byteorder.h index 94b9fea515..1f32cf8473 100644 --- a/util/system/byteorder.h +++ b/util/system/byteorder.h @@ -1,6 +1,6 @@ #pragma once -#include "defaults.h" +#include "defaults.h" //#define USE_GENERIC_ENDIAN_CVT diff --git a/util/system/compat.cpp b/util/system/compat.cpp index 18fbfa296a..a00d03dbe9 100644 --- a/util/system/compat.cpp +++ b/util/system/compat.cpp @@ -2,12 +2,12 @@ #include "defaults.h" #include "progname.h" -#include <cctype> -#include <cerrno> -#include <cstdio> +#include <cctype> +#include <cerrno> +#include <cstdio> #include <cstring> -#include <cstdarg> -#include <cstdlib> +#include <cstdarg> +#include <cstdlib> #include <util/generic/string.h> @@ -34,12 +34,12 @@ void usleep(i64 len) { #include <fcntl.h> int ftruncate(int fd, i64 length) { - return _chsize_s(fd, length); + return _chsize_s(fd, length); } int truncate(const char* name, i64 length) { - int fd = ::_open(name, _O_WRONLY); - int ret = ftruncate(fd, length); - ::close(fd); - return ret; -} + int fd = ::_open(name, _O_WRONLY); + int ret = ftruncate(fd, length); + ::close(fd); + return ret; +} #endif diff --git a/util/system/compat.h b/util/system/compat.h index c53dbcca17..dc53e32ffe 100644 --- a/util/system/compat.h +++ b/util/system/compat.h @@ -49,18 +49,18 @@ extern "C" { #endif } -#ifdef _MSC_VER +#ifdef _MSC_VER #define popen _popen #define pclose _pclose -#endif - +#endif + #ifdef _win_ #define NAME_MAX FILENAME_MAX -#endif -#ifdef _sun_ +#endif +#ifdef _sun_ #define NAME_MAX PATH_MAX -#endif - +#endif + #ifdef _win_ #ifdef sleep // may be defined by perl @@ -71,10 +71,10 @@ void sleep(i64 len); void usleep(i64 len); #endif - + #ifdef _win_ -int ftruncate(int fd, i64 length); -int truncate(const char* name, i64 length); +int ftruncate(int fd, i64 length); +int truncate(const char* name, i64 length); #endif #if defined(GNUC) diff --git a/util/system/condvar.cpp b/util/system/condvar.cpp index 62f3d22356..d1ba5cf8b9 100644 --- a/util/system/condvar.cpp +++ b/util/system/condvar.cpp @@ -121,7 +121,7 @@ public: int ret = pthread_cond_broadcast(&Cond_); Y_VERIFY(ret == 0, "pthread_cond_broadcast failed: %s", LastSystemErrorText(ret)); } - + private: pthread_cond_t Cond_; }; diff --git a/util/system/context.h b/util/system/context.h index d2a349bfc5..361c5ae0a1 100644 --- a/util/system/context.h +++ b/util/system/context.h @@ -8,7 +8,7 @@ #include <util/generic/array_ref.h> #include <util/generic/utility.h> #include <util/generic/yexception.h> - + #define STACK_ALIGN (8 * PLATFORM_DATA_ALIGN) #if defined(_x86_64_) || defined(_i386_) || defined(_arm_) || defined(_ppc64_) diff --git a/util/system/datetime.h b/util/system/datetime.h index aa009974e0..6482847403 100644 --- a/util/system/datetime.h +++ b/util/system/datetime.h @@ -94,5 +94,5 @@ Y_FORCE_INLINE ui64 GetCycleCount() noexcept { return x; #else #error "unsupported arch" -#endif +#endif } diff --git a/util/system/defaults.h b/util/system/defaults.h index dcd7abea38..c99e483a7f 100644 --- a/util/system/defaults.h +++ b/util/system/defaults.h @@ -1,6 +1,6 @@ #pragma once -#include "platform.h" +#include "platform.h" #if defined _unix_ #define LOCSLASH_C '/' @@ -15,7 +15,7 @@ #endif // low and high parts of integers -#if !defined(_win_) +#if !defined(_win_) #include <sys/param.h> #endif @@ -79,7 +79,7 @@ #include <malloc.h> #elif defined(_sun_) #include <alloca.h> -#endif +#endif #ifdef NDEBUG #define Y_IF_DEBUG(X) diff --git a/util/system/dynlib.h b/util/system/dynlib.h index 66eaf4a5c1..ef4ab29823 100644 --- a/util/system/dynlib.h +++ b/util/system/dynlib.h @@ -1,5 +1,5 @@ #pragma once - + #include "defaults.h" #include <util/generic/ptr.h> @@ -7,7 +7,7 @@ #define Y_GET_FUNC(dll, name) FUNC_##name((dll).Sym(#name)) #define Y_GET_FUNC_OPTIONAL(dll, name) FUNC_##name((dll).SymOptional(#name)) - + #ifdef _win32_ #define DEFAULT_DLLOPEN_FLAGS 0 #else @@ -25,95 +25,95 @@ public: TDynamicLibrary() noexcept; TDynamicLibrary(const TString& path, int flags = DEFAULT_DLLOPEN_FLAGS); ~TDynamicLibrary(); - + void Open(const char* path, int flags = DEFAULT_DLLOPEN_FLAGS); void Close() noexcept; void* SymOptional(const char* name) noexcept; void* Sym(const char* name); bool IsLoaded() const noexcept; void SetUnloadable(bool unloadable); // Set to false to avoid unloading on destructor - + private: class TImpl; THolder<TImpl> Impl_; -}; - -// a wrapper for a symbol -template <class TLib> +}; + +// a wrapper for a symbol +template <class TLib> class TExternalSymbol { -private: +private: TLib* PLib; TDynamicLibrary* DLib; TString lname; TString vname; -public: +public: TExternalSymbol() noexcept { PLib = nullptr; DLib = nullptr; - } + } TExternalSymbol(const TExternalSymbol& es) { PLib = nullptr; DLib = nullptr; - if (es.IsDynamic()) + if (es.IsDynamic()) Open(es.LibName().data(), es.VtblName().data()); - else if (es.IsStatic()) - SetSym(es.Symbol()); - } + else if (es.IsStatic()) + SetSym(es.Symbol()); + } TExternalSymbol& operator=(const TExternalSymbol& es) { - if (this != &es) { - Close(); - if (es.IsDynamic()) + if (this != &es) { + Close(); + if (es.IsDynamic()) Open(es.LibName().data(), es.VtblName().data()); - else if (es.IsStatic()) - SetSym(es.Symbol()); - } - return *this; - } + else if (es.IsStatic()) + SetSym(es.Symbol()); + } + return *this; + } ~TExternalSymbol() { - delete DLib; - } - // set the symbol from dynamic source + delete DLib; + } + // set the symbol from dynamic source void Open(const char* lib_name, const char* vtbl_name) { if (DLib != nullptr || PLib != nullptr) - return; - try { - DLib = new TDynamicLibrary(); - DLib->Open(lib_name); + return; + try { + DLib = new TDynamicLibrary(); + DLib->Open(lib_name); PLib = (TLib*)DLib->Sym(vtbl_name); } catch (...) { - delete DLib; + delete DLib; DLib = nullptr; - throw; - } - lname = lib_name; - vname = vtbl_name; - } - // set the symbol from static source + throw; + } + lname = lib_name; + vname = vtbl_name; + } + // set the symbol from static source void SetSym(TLib* pl) noexcept { if (DLib == nullptr && PLib == nullptr) - PLib = pl; - } + PLib = pl; + } void Close() noexcept { - delete DLib; - DLib = 0; - PLib = 0; - lname.remove(); - vname.remove(); - } + delete DLib; + DLib = 0; + PLib = 0; + lname.remove(); + vname.remove(); + } TLib* Symbol() const noexcept { - return PLib; - } + return PLib; + } const TString& LibName() const noexcept { - return lname; - } + return lname; + } const TString& VtblName() const noexcept { - return vname; - } + return vname; + } bool IsStatic() const noexcept { return DLib == nullptr && PLib != nullptr; - } + } bool IsDynamic() const noexcept { return DLib && DLib->IsLoaded() && PLib != nullptr; - } -}; + } +}; diff --git a/util/system/err.cpp b/util/system/err.cpp index 5573ea1ee9..48bddb783d 100644 --- a/util/system/err.cpp +++ b/util/system/err.cpp @@ -1,4 +1,4 @@ -#include "defaults.h" +#include "defaults.h" #include "progname.h" #include "compat.h" #include "error.h" @@ -21,7 +21,7 @@ void vwarnx(const char* fmt, va_list args) { void vwarn(const char* fmt, va_list args) { int curErrNo = errno; auto curErrText = LastSystemErrorText(); - + Y_DEFER { errno = curErrNo; }; diff --git a/util/system/error.h b/util/system/error.h index 5f2d4cc547..e70c881fde 100644 --- a/util/system/error.h +++ b/util/system/error.h @@ -1,6 +1,6 @@ #pragma once -#include "defaults.h" +#include "defaults.h" #if defined(_win_) #include <winerror.h> @@ -38,7 +38,7 @@ #undef EWOULDBLOCK #undef ENAMETOOLONG #undef ENOTEMPTY - + #define EWOULDBLOCK WSAEWOULDBLOCK #define EINPROGRESS WSAEINPROGRESS #define EALREADY WSAEALREADY diff --git a/util/system/error_ut.cpp b/util/system/error_ut.cpp index 763b0dddb7..ad6857da82 100644 --- a/util/system/error_ut.cpp +++ b/util/system/error_ut.cpp @@ -4,12 +4,12 @@ #include <util/generic/ylimits.h> -#ifdef _win_ +#ifdef _win_ #include "winint.h" #else #include <fcntl.h> -#endif - +#endif + class TSysErrorTest: public TTestBase { UNIT_TEST_SUITE(TSysErrorTest); UNIT_TEST(TestErrorCode) @@ -30,11 +30,11 @@ private: } inline void GenFailure() { -#ifdef _win_ +#ifdef _win_ SetLastError(3); -#else +#else UNIT_ASSERT(open("/non-existent", O_RDONLY) < 0); -#endif +#endif } }; diff --git a/util/system/event.cpp b/util/system/event.cpp index 79b3cdb291..1eb0380eac 100644 --- a/util/system/event.cpp +++ b/util/system/event.cpp @@ -1,5 +1,5 @@ -#include "datetime.h" -#include "defaults.h" +#include "datetime.h" +#include "defaults.h" #include <cstdio> @@ -7,7 +7,7 @@ #include "event.h" #include "mutex.h" #include "condvar.h" - + #ifdef _win_ #include "winint.h" #endif @@ -66,7 +66,7 @@ public: Cond.Signal(); } } - + inline void Reset() noexcept { AtomicSet(Signaled, 0); } @@ -109,9 +109,9 @@ private: TSystemEvent::TSystemEvent(ResetMode rmode) : EvImpl_(new TEvImpl(rmode)) -{ -} - +{ +} + TSystemEvent::TSystemEvent(const TSystemEvent& other) noexcept : EvImpl_(other.EvImpl_) { @@ -123,11 +123,11 @@ TSystemEvent& TSystemEvent::operator=(const TSystemEvent& other) noexcept { } TSystemEvent::~TSystemEvent() = default; - + void TSystemEvent::Reset() noexcept { EvImpl_->Reset(); -} - +} + void TSystemEvent::Signal() noexcept { EvImpl_->Signal(); } diff --git a/util/system/event.h b/util/system/event.h index cab2fc478a..f2c999273a 100644 --- a/util/system/event.h +++ b/util/system/event.h @@ -1,15 +1,15 @@ #pragma once - + #include <util/generic/ptr.h> #include <util/datetime/base.h> - + struct TEventResetType { enum ResetMode { rAuto, // the state will be nonsignaled after Wait() returns rManual, // we need call Reset() to set the state to nonsignaled. }; }; - + /** * DEPRECATED! * @@ -23,7 +23,7 @@ public: TSystemEvent& operator=(const TSystemEvent& other) noexcept; ~TSystemEvent(); - + void Reset() noexcept; void Signal() noexcept; diff --git a/util/system/execpath.cpp b/util/system/execpath.cpp index 33198af58b..4433510773 100644 --- a/util/system/execpath.cpp +++ b/util/system/execpath.cpp @@ -29,7 +29,7 @@ #include "filemap.h" #include "execpath.h" -#include "fs.h" +#include "fs.h" #if defined(_freebsd_) static inline bool GoodPath(const TString& path) { diff --git a/util/system/execpath_ut.cpp b/util/system/execpath_ut.cpp index 16b01466f5..14fe4ad345 100644 --- a/util/system/execpath_ut.cpp +++ b/util/system/execpath_ut.cpp @@ -1,4 +1,4 @@ -#include "execpath.h" +#include "execpath.h" #include <library/cpp/testing/unittest/registar.h> diff --git a/util/system/fhandle.h b/util/system/fhandle.h index f8033e3c14..2e012cca43 100644 --- a/util/system/fhandle.h +++ b/util/system/fhandle.h @@ -1,22 +1,22 @@ -#pragma once - -#include "defaults.h" - +#pragma once + +#include "defaults.h" + using WIN_HANDLE = void*; #define INVALID_WIN_HANDLE ((WIN_HANDLE)(long)-1) using UNIX_HANDLE = int; #define INVALID_UNIX_HANDLE -1 -#if defined(_win_) +#if defined(_win_) using FHANDLE = WIN_HANDLE; #define INVALID_FHANDLE INVALID_WIN_HANDLE -#elif defined(_unix_) +#elif defined(_unix_) using FHANDLE = UNIX_HANDLE; #define INVALID_FHANDLE INVALID_UNIX_HANDLE -#else +#else #error -#endif +#endif #if defined(_cygwin_) using OS_HANDLE = WIN_HANDLE; diff --git a/util/system/file.cpp b/util/system/file.cpp index 4a261d020c..2698ee3d2f 100644 --- a/util/system/file.cpp +++ b/util/system/file.cpp @@ -25,7 +25,7 @@ #include <errno.h> -#if defined(_unix_) +#if defined(_unix_) #include <fcntl.h> #if defined(_linux_) && (!defined(_android_) || __ANDROID_API__ >= 21) && !defined(FALLOC_FL_KEEP_SIZE) @@ -35,12 +35,12 @@ #include <stdlib.h> #include <unistd.h> #include <sys/mman.h> -#elif defined(_win_) +#elif defined(_win_) #include "winint.h" #include "fs_win.h" #include <io.h> -#endif - +#endif + #if defined(_bionic_) #include <sys/sendfile.h> #define HAVE_POSIX_FADVISE 0 @@ -64,46 +64,46 @@ static bool IsStupidFlagCombination(EOpenMode oMode) { } TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept { - ui32 fcMode = 0; + ui32 fcMode = 0; EOpenMode createMode = oMode & MaskCreation; Y_VERIFY(!IsStupidFlagCombination(oMode), "oMode %d makes no sense", static_cast<int>(oMode)); if (!(oMode & MaskRW)) { - oMode |= RdWr; + oMode |= RdWr; } if (!(oMode & AMask)) { - oMode |= ARW; - } - -#ifdef _win_ - - switch (createMode) { - case OpenExisting: - fcMode = OPEN_EXISTING; - break; - case TruncExisting: - fcMode = TRUNCATE_EXISTING; - break; - case OpenAlways: - fcMode = OPEN_ALWAYS; - break; + oMode |= ARW; + } + +#ifdef _win_ + + switch (createMode) { + case OpenExisting: + fcMode = OPEN_EXISTING; + break; + case TruncExisting: + fcMode = TRUNCATE_EXISTING; + break; + case OpenAlways: + fcMode = OPEN_ALWAYS; + break; case CreateNew: - fcMode = CREATE_NEW; - break; + fcMode = CREATE_NEW; + break; case CreateAlways: - fcMode = CREATE_ALWAYS; - break; - default: - abort(); - break; - } - - ui32 faMode = 0; + fcMode = CREATE_ALWAYS; + break; + default: + abort(); + break; + } + + ui32 faMode = 0; if (oMode & RdOnly) { - faMode |= GENERIC_READ; + faMode |= GENERIC_READ; } if (oMode & WrOnly) { // WrOnly or RdWr - faMode |= GENERIC_WRITE; + faMode |= GENERIC_WRITE; } if (oMode & ::ForAppend) { faMode |= GENERIC_WRITE; @@ -113,14 +113,14 @@ TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept { bool inheritHandle = !(oMode & CloseOnExec); - ui32 shMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; - - ui32 attrMode = FILE_ATTRIBUTE_NORMAL; + ui32 shMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; + + ui32 attrMode = FILE_ATTRIBUTE_NORMAL; if ((createMode == OpenExisting || createMode == OpenAlways) && ((oMode & AMask) == (oMode & AR))) { - attrMode |= FILE_ATTRIBUTE_READONLY; + attrMode |= FILE_ATTRIBUTE_READONLY; } if (oMode & Seq) { - attrMode |= FILE_FLAG_SEQUENTIAL_SCAN; + attrMode |= FILE_FLAG_SEQUENTIAL_SCAN; } if (oMode & Temp) { // we use TTempFile instead of FILE_FLAG_DELETE_ON_CLOSE @@ -131,46 +131,46 @@ TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept { } if ((oMode & (Direct | DirectAligned)) && (oMode & WrOnly)) { // WrOnly or RdWr - attrMode |= /*FILE_FLAG_NO_BUFFERING |*/ FILE_FLAG_WRITE_THROUGH; + attrMode |= /*FILE_FLAG_NO_BUFFERING |*/ FILE_FLAG_WRITE_THROUGH; } - + Fd_ = NFsPrivate::CreateFileWithUtf8Name(fName, faMode, shMode, fcMode, attrMode, inheritHandle); - + if ((oMode & ::ForAppend) && (Fd_ != INVALID_FHANDLE)) { ::SetFilePointer(Fd_, 0, 0, FILE_END); } #elif defined(_unix_) - - switch (createMode) { - case OpenExisting: - fcMode = 0; - break; - case TruncExisting: - fcMode = O_TRUNC; - break; - case OpenAlways: - fcMode = O_CREAT; - break; + + switch (createMode) { + case OpenExisting: + fcMode = 0; + break; + case TruncExisting: + fcMode = O_TRUNC; + break; + case OpenAlways: + fcMode = O_CREAT; + break; case CreateNew: - fcMode = O_CREAT | O_EXCL; - break; + fcMode = O_CREAT | O_EXCL; + break; case CreateAlways: - fcMode = O_CREAT | O_TRUNC; - break; - default: - abort(); - break; - } - + fcMode = O_CREAT | O_TRUNC; + break; + default: + abort(); + break; + } + if ((oMode & RdOnly) && (oMode & WrOnly)) { - fcMode |= O_RDWR; + fcMode |= O_RDWR; } else if (oMode & RdOnly) { - fcMode |= O_RDONLY; + fcMode |= O_RDONLY; } else if (oMode & WrOnly) { - fcMode |= O_WRONLY; + fcMode |= O_WRONLY; } - + if (oMode & ::ForAppend) { fcMode |= O_APPEND; } @@ -180,9 +180,9 @@ TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept { } /* I don't now about this for unix... - if (oMode & Temp) { - } - */ + if (oMode & Temp) { + } + */ #if defined(_freebsd_) if (oMode & (Direct | DirectAligned)) { fcMode |= O_DIRECT; @@ -193,51 +193,51 @@ TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept { } #elif defined(_linux_) if (oMode & DirectAligned) { - /* + /* * O_DIRECT in Linux requires aligning request size and buffer address * to size of hardware sector (see hw_sector_size or ioctl BLKSSZGET). * Usually 512 bytes, but modern hardware works better with 4096 bytes. - */ - fcMode |= O_DIRECT; + */ + fcMode |= O_DIRECT; } if (oMode & Sync) { fcMode |= O_SYNC; } #endif - + #if defined(_linux_) fcMode |= O_LARGEFILE; #endif - ui32 permMode = 0; + ui32 permMode = 0; if (oMode & AXOther) { - permMode |= S_IXOTH; + permMode |= S_IXOTH; } if (oMode & AWOther) { - permMode |= S_IWOTH; + permMode |= S_IWOTH; } if (oMode & AROther) { - permMode |= S_IROTH; + permMode |= S_IROTH; } if (oMode & AXGroup) { - permMode |= S_IXGRP; + permMode |= S_IXGRP; } if (oMode & AWGroup) { - permMode |= S_IWGRP; + permMode |= S_IWGRP; } if (oMode & ARGroup) { - permMode |= S_IRGRP; + permMode |= S_IRGRP; } if (oMode & AXUser) { - permMode |= S_IXUSR; + permMode |= S_IXUSR; } if (oMode & AWUser) { - permMode |= S_IWUSR; + permMode |= S_IWUSR; } if (oMode & ARUser) { - permMode |= S_IRUSR; + permMode |= S_IRUSR; } - + do { Fd_ = ::open(fName.data(), fcMode, permMode); } while (Fd_ == -1 && errno == EINTR); @@ -264,12 +264,12 @@ TFileHandle::TFileHandle(const TString& fName, EOpenMode oMode) noexcept { } #else #error unsupported platform -#endif -} - +#endif +} + bool TFileHandle::Close() noexcept { - bool isOk = true; -#ifdef _win_ + bool isOk = true; +#ifdef _win_ if (Fd_ != INVALID_FHANDLE) { isOk = (::CloseHandle(Fd_) != 0); } @@ -287,28 +287,28 @@ bool TFileHandle::Close() noexcept { // that is disasterous Y_VERIFY(errno != EBADF, "must not quietly close bad descriptor: fd=%d", int(Fd_)); } -#else +#else #error unsupported platform -#endif +#endif Fd_ = INVALID_FHANDLE; - return isOk; -} - + return isOk; +} + static inline i64 DoSeek(FHANDLE h, i64 offset, SeekDir origin) noexcept { if (h == INVALID_FHANDLE) { - return -1L; + return -1L; } #if defined(_win_) - static ui32 dir[] = {FILE_BEGIN, FILE_CURRENT, FILE_END}; - LARGE_INTEGER pos; - pos.QuadPart = offset; - pos.LowPart = ::SetFilePointer(h, pos.LowPart, &pos.HighPart, dir[origin]); + static ui32 dir[] = {FILE_BEGIN, FILE_CURRENT, FILE_END}; + LARGE_INTEGER pos; + pos.QuadPart = offset; + pos.LowPart = ::SetFilePointer(h, pos.LowPart, &pos.HighPart, dir[origin]); if (pos.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) { - pos.QuadPart = -1; + pos.QuadPart = -1; } - return pos.QuadPart; + return pos.QuadPart; #elif defined(_unix_) - static int dir[] = {SEEK_SET, SEEK_CUR, SEEK_END}; + static int dir[] = {SEEK_SET, SEEK_CUR, SEEK_END}; #if defined(_sun_) return ::llseek(h, (offset_t)offset, dir[origin]); #else @@ -317,27 +317,27 @@ static inline i64 DoSeek(FHANDLE h, i64 offset, SeekDir origin) noexcept { #else #error unsupported platform #endif -} - +} + i64 TFileHandle::GetPosition() const noexcept { return DoSeek(Fd_, 0, sCur); -} - +} + i64 TFileHandle::Seek(i64 offset, SeekDir origin) noexcept { return DoSeek(Fd_, offset, origin); -} - +} + i64 TFileHandle::GetLength() const noexcept { // XXX: returns error code, but does not set errno if (!IsOpen()) { return -1L; } - return GetFileLength(Fd_); -} - + return GetFileLength(Fd_); +} + bool TFileHandle::Resize(i64 length) noexcept { if (!IsOpen()) { - return false; + return false; } i64 currentLength = GetLength(); if (length == currentLength) { @@ -346,23 +346,23 @@ bool TFileHandle::Resize(i64 length) noexcept { #if defined(_win_) i64 currentPosition = GetPosition(); if (currentPosition == -1L) { - return false; + return false; } Seek(length, sSet); if (!::SetEndOfFile(Fd_)) { - return false; + return false; } if (currentPosition < length) { Seek(currentPosition, sSet); } - return true; + return true; #elif defined(_unix_) return (0 == ftruncate(Fd_, (off_t)length)); -#else +#else #error unsupported platform -#endif -} - +#endif +} + bool TFileHandle::Reserve(i64 length) noexcept { // FIXME this should reserve disk space with fallocate if (!IsOpen()) { @@ -414,9 +414,9 @@ bool TFileHandle::ShrinkToFit() noexcept { bool TFileHandle::Flush() noexcept { if (!IsOpen()) { - return false; + return false; } -#if defined(_win_) +#if defined(_win_) bool ok = ::FlushFileBuffers(Fd_) != 0; /* * FlushFileBuffers fails if hFile is a handle to the console output. @@ -438,11 +438,11 @@ bool TFileHandle::Flush() noexcept { || errno == ENOTSUP #endif ; -#else +#else #error unsupported platform -#endif +#endif } - + bool TFileHandle::FlushData() noexcept { #if defined(_linux_) if (!IsOpen()) { @@ -461,14 +461,14 @@ bool TFileHandle::FlushData() noexcept { i32 TFileHandle::Read(void* buffer, ui32 byteCount) noexcept { // FIXME size and return must be 64-bit if (!IsOpen()) { - return -1; + return -1; } -#if defined(_win_) - DWORD bytesRead = 0; +#if defined(_win_) + DWORD bytesRead = 0; if (::ReadFile(Fd_, buffer, byteCount, &bytesRead, nullptr)) { - return bytesRead; + return bytesRead; } - return -1; + return -1; #elif defined(_unix_) i32 ret; do { @@ -477,19 +477,19 @@ i32 TFileHandle::Read(void* buffer, ui32 byteCount) noexcept { return ret; #else #error unsupported platform -#endif -} - +#endif +} + i32 TFileHandle::Write(const void* buffer, ui32 byteCount) noexcept { if (!IsOpen()) { - return -1; + return -1; } -#if defined(_win_) - DWORD bytesWritten = 0; +#if defined(_win_) + DWORD bytesWritten = 0; if (::WriteFile(Fd_, buffer, byteCount, &bytesWritten, nullptr)) { - return bytesWritten; + return bytesWritten; } - return -1; + return -1; #elif defined(_unix_) i32 ret; do { @@ -498,23 +498,23 @@ i32 TFileHandle::Write(const void* buffer, ui32 byteCount) noexcept { return ret; #else #error unsupported platform -#endif -} - +#endif +} + i32 TFileHandle::Pread(void* buffer, ui32 byteCount, i64 offset) const noexcept { -#if defined(_win_) +#if defined(_win_) OVERLAPPED io; Zero(io); - DWORD bytesRead = 0; - io.Offset = (ui32)offset; - io.OffsetHigh = (ui32)(offset >> 32); + DWORD bytesRead = 0; + io.Offset = (ui32)offset; + io.OffsetHigh = (ui32)(offset >> 32); if (::ReadFile(Fd_, buffer, byteCount, &bytesRead, &io)) { - return bytesRead; + return bytesRead; } if (::GetLastError() == ERROR_HANDLE_EOF) { return 0; } - return -1; + return -1; #elif defined(_unix_) i32 ret; do { @@ -523,20 +523,20 @@ i32 TFileHandle::Pread(void* buffer, ui32 byteCount, i64 offset) const noexcept return ret; #else #error unsupported platform -#endif -} - +#endif +} + i32 TFileHandle::Pwrite(const void* buffer, ui32 byteCount, i64 offset) const noexcept { -#if defined(_win_) +#if defined(_win_) OVERLAPPED io; Zero(io); - DWORD bytesWritten = 0; - io.Offset = (ui32)offset; - io.OffsetHigh = (ui32)(offset >> 32); + DWORD bytesWritten = 0; + io.Offset = (ui32)offset; + io.OffsetHigh = (ui32)(offset >> 32); if (::WriteFile(Fd_, buffer, byteCount, &bytesWritten, &io)) { - return bytesWritten; + return bytesWritten; } - return -1; + return -1; #elif defined(_unix_) i32 ret; do { @@ -545,26 +545,26 @@ i32 TFileHandle::Pwrite(const void* buffer, ui32 byteCount, i64 offset) const no return ret; #else #error unsupported platform -#endif -} - +#endif +} + FHANDLE TFileHandle::Duplicate() const noexcept { if (!IsOpen()) { - return INVALID_FHANDLE; + return INVALID_FHANDLE; } -#if defined(_win_) - FHANDLE dupHandle; +#if defined(_win_) + FHANDLE dupHandle; if (!::DuplicateHandle(GetCurrentProcess(), Fd_, GetCurrentProcess(), &dupHandle, 0, TRUE, DUPLICATE_SAME_ACCESS)) { - return INVALID_FHANDLE; + return INVALID_FHANDLE; } - return dupHandle; + return dupHandle; #elif defined(_unix_) return ::dup(Fd_); #else #error unsupported platform -#endif -} - +#endif +} + int TFileHandle::Duplicate2Posix(int dstHandle) const noexcept { if (!IsOpen()) { return -1; @@ -623,8 +623,8 @@ bool TFileHandle::LinkTo(const TFileHandle& fh) const noexcept { int TFileHandle::Flock(int op) noexcept { return ::Flock(Fd_, op); -} - +} + bool TFileHandle::SetDirect() { #ifdef _linux_ const long flags = fcntl(Fd_, F_GETFL); @@ -848,36 +848,36 @@ public: , FileName_(fname) { } - + inline TImpl(const TString& fName, EOpenMode oMode) : Handle_(fName, oMode) , FileName_(fName) { if (!Handle_.IsOpen()) { ythrow TFileError() << "can't open " << fName.Quote() << " with mode " << DecodeOpenMode(oMode) << " (" << Hex(oMode.ToBaseType()) << ")"; - } + } } - + inline ~TImpl() = default; - + inline void Close() { if (!Handle_.Close()) { ythrow TFileError() << "can't close " << FileName_.Quote(); } } - + const TString& GetName() const noexcept { return FileName_; } - + void SetName(const TString& newName) { FileName_ = newName; } - + const TFileHandle& GetHandle() const noexcept { return Handle_; } - + i64 Seek(i64 offset, SeekDir origin) { i64 pos = Handle_.Seek(offset, origin); if (pos == -1L) { @@ -885,13 +885,13 @@ public: } return pos; } - + void Resize(i64 length) { if (!Handle_.Resize(length)) { ythrow TFileError() << "can't resize " << FileName_.Quote() << " to size " << length; } } - + void Reserve(i64 length) { if (!Handle_.Reserve(length)) { ythrow TFileError() << "can't reserve " << length << " for file " << FileName_.Quote(); @@ -915,7 +915,7 @@ public: ythrow TFileError() << "can't flush " << FileName_.Quote(); } } - + void FlushData() { if (!Handle_.FlushData()) { ythrow TFileError() << "can't flush data " << FileName_.Quote(); @@ -931,7 +931,7 @@ public: dupH.Release(); return res; } - + // Maximum amount of bytes to be read via single system call. // Some libraries fail when it is greater than max int. // Syscalls can cause contention if they operate on very large data blocks. @@ -954,28 +954,28 @@ public: size_t Read(void* bufferIn, size_t numBytes) { ui8* buf = (ui8*)bufferIn; - + while (numBytes) { const size_t reallyRead = ReadOrFail(buf, numBytes); - + if (reallyRead == 0) { // file exhausted break; } - + buf += reallyRead; numBytes -= reallyRead; - } - + } + return buf - (ui8*)bufferIn; } - + void Load(void* buf, size_t len) { if (Read(buf, len) != len) { ythrow TFileError() << "can't read " << len << " bytes from " << FileName_.Quote(); } } - + // Maximum amount of bytes to be written via single system call. // Some libraries fail when it is greater than max int. // Syscalls can cause contention if they operate on very large data blocks. @@ -983,44 +983,44 @@ public: void Write(const void* buffer, size_t numBytes) { const ui8* buf = (const ui8*)buffer; - + while (numBytes) { const i32 toWrite = (i32)Min(MaxWritePortion, numBytes); const i32 reallyWritten = Handle_.Write(buf, toWrite); - + if (reallyWritten < 0) { ythrow TFileError() << "can't write " << toWrite << " bytes to " << FileName_.Quote(); } buf += reallyWritten; numBytes -= reallyWritten; - } + } } - + size_t Pread(void* bufferIn, size_t numBytes, i64 offset) const { ui8* buf = (ui8*)bufferIn; - + while (numBytes) { const i32 toRead = (i32)Min(MaxReadPortion, numBytes); const i32 reallyRead = RawPread(buf, toRead, offset); - + if (reallyRead < 0) { ythrow TFileError() << "can not read data from " << FileName_.Quote(); } - + if (reallyRead == 0) { // file exausted break; } - + buf += reallyRead; offset += reallyRead; numBytes -= reallyRead; - } - + } + return buf - (ui8*)bufferIn; } - + i32 RawPread(void* buf, ui32 len, i64 offset) const { return Handle_.Pread(buf, len, offset); } @@ -1030,24 +1030,24 @@ public: ythrow TFileError() << "can't read " << len << " bytes at offset " << offset << " from " << FileName_.Quote(); } } - + void Pwrite(const void* buffer, size_t numBytes, i64 offset) const { const ui8* buf = (const ui8*)buffer; - + while (numBytes) { const i32 toWrite = (i32)Min(MaxWritePortion, numBytes); const i32 reallyWritten = Handle_.Pwrite(buf, toWrite, offset); - + if (reallyWritten < 0) { ythrow TFileError() << "can't write " << toWrite << " bytes to " << FileName_.Quote(); } - + buf += reallyWritten; offset += reallyWritten; numBytes -= reallyWritten; - } + } } - + void Flock(int op) { if (0 != Handle_.Flock(op)) { ythrow TFileError() << "can't flock " << FileName_.Quote(); @@ -1085,18 +1085,18 @@ public: private: TFileHandle Handle_; TString FileName_; -}; - -TFile::TFile() +}; + +TFile::TFile() : Impl_(new TImpl(INVALID_FHANDLE)) -{ -} - -TFile::TFile(FHANDLE fd) +{ +} + +TFile::TFile(FHANDLE fd) : Impl_(new TImpl(fd)) -{ -} - +{ +} + TFile::TFile(FHANDLE fd, const TString& name) : Impl_(new TImpl(fd, name)) { @@ -1104,43 +1104,43 @@ TFile::TFile(FHANDLE fd, const TString& name) TFile::TFile(const TString& fName, EOpenMode oMode) : Impl_(new TImpl(fName, oMode)) -{ -} - +{ +} + TFile::~TFile() = default; - -void TFile::Close() { + +void TFile::Close() { Impl_->Close(); -} - +} + const TString& TFile::GetName() const noexcept { return Impl_->GetName(); -} - +} + i64 TFile::GetPosition() const noexcept { return Impl_->GetHandle().GetPosition(); -} - +} + i64 TFile::GetLength() const noexcept { return Impl_->GetHandle().GetLength(); -} - +} + bool TFile::IsOpen() const noexcept { return Impl_->GetHandle().IsOpen(); -} - +} + FHANDLE TFile::GetHandle() const noexcept { return Impl_->GetHandle(); -} - -i64 TFile::Seek(i64 offset, SeekDir origin) { +} + +i64 TFile::Seek(i64 offset, SeekDir origin) { return Impl_->Seek(offset, origin); -} - +} + void TFile::Resize(i64 length) { Impl_->Resize(length); -} - +} + void TFile::Reserve(i64 length) { Impl_->Reserve(length); } @@ -1153,10 +1153,10 @@ void TFile::ShrinkToFit() { Impl_->ShrinkToFit(); } -void TFile::Flush() { +void TFile::Flush() { Impl_->Flush(); -} - +} + void TFile::FlushData() { Impl_->FlushData(); } @@ -1164,13 +1164,13 @@ void TFile::FlushData() { TFile TFile::Duplicate() const { TFile res = Impl_->Duplicate(); res.Impl_->SetName(Impl_->GetName()); - return res; -} - -size_t TFile::Read(void* buf, size_t len) { + return res; +} + +size_t TFile::Read(void* buf, size_t len) { return Impl_->Read(buf, len); -} - +} + i32 TFile::RawRead(void* buf, size_t len) { return Impl_->RawRead(buf, len); } @@ -1179,34 +1179,34 @@ size_t TFile::ReadOrFail(void* buf, size_t len) { return Impl_->ReadOrFail(buf, len); } -void TFile::Load(void* buf, size_t len) { +void TFile::Load(void* buf, size_t len) { Impl_->Load(buf, len); -} - -void TFile::Write(const void* buf, size_t len) { +} + +void TFile::Write(const void* buf, size_t len) { Impl_->Write(buf, len); -} - -size_t TFile::Pread(void* buf, size_t len, i64 offset) const { +} + +size_t TFile::Pread(void* buf, size_t len, i64 offset) const { return Impl_->Pread(buf, len, offset); -} - +} + i32 TFile::RawPread(void* buf, ui32 len, i64 offset) const { return Impl_->RawPread(buf, len, offset); } -void TFile::Pload(void* buf, size_t len, i64 offset) const { +void TFile::Pload(void* buf, size_t len, i64 offset) const { Impl_->Pload(buf, len, offset); -} - -void TFile::Pwrite(const void* buf, size_t len, i64 offset) const { +} + +void TFile::Pwrite(const void* buf, size_t len, i64 offset) const { Impl_->Pwrite(buf, len, offset); -} - -void TFile::Flock(int op) { +} + +void TFile::Flock(int op) { Impl_->Flock(op); -} - +} + void TFile::SetDirect() { Impl_->SetDirect(); } @@ -1246,11 +1246,11 @@ TFile TFile::ForAppend(const TString& path) { return TFile(path, OpenAlways | WrOnly | Seq | ::ForAppend); } -TFile Duplicate(FILE* f) { - return Duplicate(fileno(f)); -} - -TFile Duplicate(int fd) { +TFile Duplicate(FILE* f) { + return Duplicate(fileno(f)); +} + +TFile Duplicate(int fd) { #if defined(_win_) /* There are two options of how to duplicate a file descriptor on Windows: * @@ -1278,10 +1278,10 @@ TFile Duplicate(int fd) { return TFile(dupHandle); #elif defined(_unix_) return TFile(::dup(fd)); -#else +#else #error unsupported platform -#endif -} +#endif +} bool PosixDisableReadAhead(FHANDLE fileHandle, void* addr) noexcept { int ret = -1; diff --git a/util/system/file.h b/util/system/file.h index 9502e159b6..7d2cb11d4e 100644 --- a/util/system/file.h +++ b/util/system/file.h @@ -1,27 +1,27 @@ #pragma once - -#include "fhandle.h" + +#include "fhandle.h" #include "flock.h" #include <util/generic/flags.h> #include <util/generic/ptr.h> #include <util/generic/noncopyable.h> - + #include <cstdio> enum EOpenModeFlag { OpenExisting = 0, // Opens a file. It fails if the file does not exist. - TruncExisting = 1, // Opens a file and truncates it to zero size. It fails if the file does not exist. + TruncExisting = 1, // Opens a file and truncates it to zero size. It fails if the file does not exist. OpenAlways = 2, // Opens a file, always. If a file does not exist, it creates a file. CreateNew = 3, // Creates a new file. It fails if a specified file exists. CreateAlways = 4, // Creates a new file, always. If a file exists, it overwrites the file. MaskCreation = 7, - - RdOnly = 8, // open for reading only - WrOnly = 16, // open for writing only + + RdOnly = 8, // open for reading only + WrOnly = 16, // open for writing only RdWr = 24, // open for reading and writing - MaskRW = 24, - + MaskRW = 24, + Seq = 0x20, // file access is primarily sequential (POSIX_FADV_SEQUENTIAL) Direct = 0x40, // file is being opened with no system caching (Does not work as intended! See implementation) Temp = 0x80, // avoid writing data back to disk if sufficient cache memory is available (no op for linux) @@ -32,44 +32,44 @@ enum EOpenModeFlag { DirectAligned = 0x1000, // file is actually being opened with no system caching (may require buffer alignment) (O_DIRECT) Sync = 0x2000, // no write call will return before the data is transferred to the disk (O_SYNC) NoReadAhead = 0x4000, // no sequential access expected, opposite for Seq (POSIX_FADV_RANDOM) - - AXOther = 0x00010000, - AWOther = 0x00020000, - AROther = 0x00040000, - AXGroup = 0x00100000, - AWGroup = 0x00200000, - ARGroup = 0x00400000, + + AXOther = 0x00010000, + AWOther = 0x00020000, + AROther = 0x00040000, + AXGroup = 0x00100000, + AWGroup = 0x00200000, + ARGroup = 0x00400000, AXUser = 0x01000000, AWUser = 0x02000000, ARUser = 0x04000000, AX = AXUser | AXGroup | AXOther, AW = AWUser | AWGroup, AR = ARUser | ARGroup | AROther, - ARW = AR | AW, - AMask = 0x0FFF0000, -}; - + ARW = AR | AW, + AMask = 0x0FFF0000, +}; + Y_DECLARE_FLAGS(EOpenMode, EOpenModeFlag) Y_DECLARE_OPERATORS_FOR_FLAGS(EOpenMode) TString DecodeOpenMode(ui32 openMode); -enum SeekDir { - sSet = 0, - sCur = 1, - sEnd = 2, -}; - +enum SeekDir { + sSet = 0, + sCur = 1, + sEnd = 2, +}; + class TFileHandle: public TNonCopyable { -public: +public: constexpr TFileHandle() = default; /// Warning: takes ownership of fd, so closes it in destructor. inline TFileHandle(FHANDLE fd) noexcept : Fd_(fd) - { - } - + { + } + inline TFileHandle(TFileHandle&& other) noexcept : Fd_(other.Fd_) { @@ -77,34 +77,34 @@ public: } TFileHandle(const TString& fName, EOpenMode oMode) noexcept; - + inline ~TFileHandle() { - Close(); - } - + Close(); + } + bool Close() noexcept; - + inline FHANDLE Release() noexcept { FHANDLE ret = Fd_; Fd_ = INVALID_FHANDLE; - return ret; - } - + return ret; + } + inline void Swap(TFileHandle& r) noexcept { DoSwap(Fd_, r.Fd_); - } - + } + inline operator FHANDLE() const noexcept { return Fd_; - } - + } + inline bool IsOpen() const noexcept { return Fd_ != INVALID_FHANDLE; - } - + } + i64 GetPosition() const noexcept; i64 GetLength() const noexcept; - + i64 Seek(i64 offset, SeekDir origin) noexcept; bool Resize(i64 length) noexcept; bool Reserve(i64 length) noexcept; @@ -118,17 +118,17 @@ public: i32 Pread(void* buffer, ui32 byteCount, i64 offset) const noexcept; i32 Pwrite(const void* buffer, ui32 byteCount, i64 offset) const noexcept; int Flock(int op) noexcept; - + FHANDLE Duplicate() const noexcept; int Duplicate2Posix(int dstHandle) const noexcept; - + //dup2 - like semantics, return true on success bool LinkTo(const TFileHandle& fh) const noexcept; //very low-level methods bool SetDirect(); void ResetDirect(); - + /* Manual file cache management, length = 0 means "as much as possible" */ //measure amount of cached data in bytes, returns -1 if failed @@ -140,66 +140,66 @@ public: //flush unwritten data in this range and optionally wait for completion bool FlushCache(i64 offset = 0, i64 length = 0, bool wait = true) noexcept; -private: +private: FHANDLE Fd_ = INVALID_FHANDLE; -}; - -class TFile { -public: - TFile(); +}; + +class TFile { +public: + TFile(); /// Takes ownership of handle, so closes it when the last holder of descriptor dies. - explicit TFile(FHANDLE fd); + explicit TFile(FHANDLE fd); TFile(FHANDLE fd, const TString& fname); TFile(const TString& fName, EOpenMode oMode); ~TFile(); - - void Close(); - + + void Close(); + const TString& GetName() const noexcept; i64 GetPosition() const noexcept; i64 GetLength() const noexcept; bool IsOpen() const noexcept; FHANDLE GetHandle() const noexcept; - - i64 Seek(i64 offset, SeekDir origin); + + i64 Seek(i64 offset, SeekDir origin); void Resize(i64 length); void Reserve(i64 length); void FallocateNoResize(i64 length); void ShrinkToFit(); - void Flush(); + void Flush(); void FlushData(); - + void LinkTo(const TFile& f) const; TFile Duplicate() const; - + // Reads up to 1 GB without retrying, returns -1 on error i32 RawRead(void* buf, size_t len); // Reads up to 1 GB without retrying, throws on error size_t ReadOrFail(void* buf, size_t len); // Retries incomplete reads until EOF, throws on error - size_t Read(void* buf, size_t len); + size_t Read(void* buf, size_t len); // Reads exactly len bytes, throws on premature EOF or error - void Load(void* buf, size_t len); + void Load(void* buf, size_t len); // Retries incomplete writes, will either write len bytes or throw - void Write(const void* buf, size_t len); + void Write(const void* buf, size_t len); // Retries incomplete reads until EOF, throws on error - size_t Pread(void* buf, size_t len, i64 offset) const; + size_t Pread(void* buf, size_t len, i64 offset) const; // Single pread call i32 RawPread(void* buf, ui32 len, i64 offset) const; // Reads exactly len bytes, throws on premature EOF or error - void Pload(void* buf, size_t len, i64 offset) const; + void Pload(void* buf, size_t len, i64 offset) const; // Retries incomplete writes, will either write len bytes or throw - void Pwrite(const void* buf, size_t len, i64 offset) const; + void Pwrite(const void* buf, size_t len, i64 offset) const; - void Flock(int op); + void Flock(int op); //do not use, their meaning very platform-dependant void SetDirect(); void ResetDirect(); - + /* Manual file cache management, length = 0 means "as much as possible" */ //measure amount of cached data in bytes, returns -1 if failed @@ -214,12 +214,12 @@ public: static TFile Temporary(const TString& prefix); static TFile ForAppend(const TString& path); -private: - class TImpl; +private: + class TImpl; TSimpleIntrusivePtr<TImpl> Impl_; -}; - -TFile Duplicate(FILE*); -TFile Duplicate(int); +}; + +TFile Duplicate(FILE*); +TFile Duplicate(int); bool PosixDisableReadAhead(FHANDLE fileHandle, void* addr) noexcept; diff --git a/util/system/file_ut.cpp b/util/system/file_ut.cpp index 941e6a50f3..322f36622c 100644 --- a/util/system/file_ut.cpp +++ b/util/system/file_ut.cpp @@ -1,13 +1,13 @@ #include "file.h" -#include "fs.h" +#include "fs.h" #include "tempfile.h" #include <library/cpp/testing/unittest/registar.h> #include <util/stream/file.h> #include <util/generic/yexception.h> - -class TFileTest: public TTestBase { + +class TFileTest: public TTestBase { UNIT_TEST_SUITE(TFileTest); UNIT_TEST(TestOpen); UNIT_TEST(TestOpenSync); @@ -115,45 +115,45 @@ public: UNIT_ASSERT_EQUAL(data.length(), 12); UNIT_ASSERT(data.StartsWith("12345")); } -}; - -UNIT_TEST_SUITE_REGISTRATION(TFileTest); - -void TFileTest::TestOpen() { +}; + +UNIT_TEST_SUITE_REGISTRATION(TFileTest); + +void TFileTest::TestOpen() { TString res; - TFile f1; - - try { - TFile f2("f1.txt", OpenExisting); - } catch (const yexception& e) { - res = e.what(); - } - UNIT_ASSERT(!res.empty()); - res.remove(); - - try { - TFile f2("f1.txt", OpenAlways); - f1 = f2; - } catch (const yexception& e) { - res = e.what(); - } - UNIT_ASSERT(res.empty()); - UNIT_ASSERT(f1.IsOpen()); - UNIT_ASSERT_VALUES_EQUAL(f1.GetName(), "f1.txt"); - UNIT_ASSERT_VALUES_EQUAL(f1.GetLength(), 0); - - try { + TFile f1; + + try { + TFile f2("f1.txt", OpenExisting); + } catch (const yexception& e) { + res = e.what(); + } + UNIT_ASSERT(!res.empty()); + res.remove(); + + try { + TFile f2("f1.txt", OpenAlways); + f1 = f2; + } catch (const yexception& e) { + res = e.what(); + } + UNIT_ASSERT(res.empty()); + UNIT_ASSERT(f1.IsOpen()); + UNIT_ASSERT_VALUES_EQUAL(f1.GetName(), "f1.txt"); + UNIT_ASSERT_VALUES_EQUAL(f1.GetLength(), 0); + + try { TFile f2("f1.txt", CreateNew); - } catch (const yexception& e) { - res = e.what(); - } - UNIT_ASSERT(!res.empty()); - res.remove(); - - f1.Close(); - UNIT_ASSERT(unlink("f1.txt") == 0); -} - + } catch (const yexception& e) { + res = e.what(); + } + UNIT_ASSERT(!res.empty()); + res.remove(); + + f1.Close(); + UNIT_ASSERT(unlink("f1.txt") == 0); +} + void TFileTest::TestOpenSync() { TFile f1("f1.txt", CreateNew | Sync); UNIT_ASSERT(f1.IsOpen()); @@ -162,60 +162,60 @@ void TFileTest::TestOpenSync() { UNIT_ASSERT(unlink("f1.txt") == 0); } -void TFileTest::TestRW() { +void TFileTest::TestRW() { TFile f1("f1.txt", CreateNew); - UNIT_ASSERT(f1.IsOpen()); - UNIT_ASSERT_VALUES_EQUAL(f1.GetName(), "f1.txt"); - UNIT_ASSERT_VALUES_EQUAL(f1.GetLength(), 0); - ui32 d[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + UNIT_ASSERT(f1.IsOpen()); + UNIT_ASSERT_VALUES_EQUAL(f1.GetName(), "f1.txt"); + UNIT_ASSERT_VALUES_EQUAL(f1.GetLength(), 0); + ui32 d[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; f1.Write(&d, sizeof(ui32) * 10); - UNIT_ASSERT_VALUES_EQUAL(f1.GetLength(), 40); - UNIT_ASSERT_VALUES_EQUAL(f1.GetPosition(), 40); - UNIT_ASSERT_VALUES_EQUAL(f1.Seek(12, sSet), 12); - f1.Flush(); - ui32 v; - f1.Load(&v, sizeof(v)); + UNIT_ASSERT_VALUES_EQUAL(f1.GetLength(), 40); + UNIT_ASSERT_VALUES_EQUAL(f1.GetPosition(), 40); + UNIT_ASSERT_VALUES_EQUAL(f1.Seek(12, sSet), 12); + f1.Flush(); + ui32 v; + f1.Load(&v, sizeof(v)); UNIT_ASSERT_VALUES_EQUAL(v, 3u); - UNIT_ASSERT_VALUES_EQUAL(f1.GetPosition(), 16); - - TFile f2 = f1; - UNIT_ASSERT(f2.IsOpen()); - UNIT_ASSERT_VALUES_EQUAL(f2.GetName(), "f1.txt"); - UNIT_ASSERT_VALUES_EQUAL(f2.GetPosition(), 16); - UNIT_ASSERT_VALUES_EQUAL(f2.GetLength(), 40); - f2.Write(&v, sizeof(v)); - - UNIT_ASSERT_VALUES_EQUAL(f1.GetPosition(), 20); - UNIT_ASSERT_VALUES_EQUAL(f1.Seek(-4, sCur), 16); - v = 0; - f1.Load(&v, sizeof(v)); + UNIT_ASSERT_VALUES_EQUAL(f1.GetPosition(), 16); + + TFile f2 = f1; + UNIT_ASSERT(f2.IsOpen()); + UNIT_ASSERT_VALUES_EQUAL(f2.GetName(), "f1.txt"); + UNIT_ASSERT_VALUES_EQUAL(f2.GetPosition(), 16); + UNIT_ASSERT_VALUES_EQUAL(f2.GetLength(), 40); + f2.Write(&v, sizeof(v)); + + UNIT_ASSERT_VALUES_EQUAL(f1.GetPosition(), 20); + UNIT_ASSERT_VALUES_EQUAL(f1.Seek(-4, sCur), 16); + v = 0; + f1.Load(&v, sizeof(v)); UNIT_ASSERT_VALUES_EQUAL(v, 3u); - f1.Close(); - UNIT_ASSERT(!f1.IsOpen()); - UNIT_ASSERT(!f2.IsOpen()); - UNIT_ASSERT(unlink("f1.txt") == 0); + f1.Close(); + UNIT_ASSERT(!f1.IsOpen()); + UNIT_ASSERT(!f2.IsOpen()); + UNIT_ASSERT(unlink("f1.txt") == 0); } -#ifdef _unix_ +#ifdef _unix_ #include <locale.h> -#endif - -void TFileTest::TestLocale() { -#ifdef _unix_ +#endif + +void TFileTest::TestLocale() { +#ifdef _unix_ const char* loc = setlocale(LC_CTYPE, nullptr); - setlocale(LC_CTYPE, "ru_RU.UTF-8"); -#endif - TFile f("Имя.txt", CreateNew); - UNIT_ASSERT(f.IsOpen()); - UNIT_ASSERT_VALUES_EQUAL(f.GetName(), "Имя.txt"); - UNIT_ASSERT_VALUES_EQUAL(f.GetLength(), 0); - f.Close(); + setlocale(LC_CTYPE, "ru_RU.UTF-8"); +#endif + TFile f("Имя.txt", CreateNew); + UNIT_ASSERT(f.IsOpen()); + UNIT_ASSERT_VALUES_EQUAL(f.GetName(), "Имя.txt"); + UNIT_ASSERT_VALUES_EQUAL(f.GetLength(), 0); + f.Close(); UNIT_ASSERT(NFs::Remove("Имя.txt")); -#ifdef _unix_ - setlocale(LC_CTYPE, loc); -#endif -} - +#ifdef _unix_ + setlocale(LC_CTYPE, loc); +#endif +} + void TFileTest::TestFlush() { TTempFile tmp("tmp"); diff --git a/util/system/filemap.cpp b/util/system/filemap.cpp index 7454a4cb94..aa05a4be33 100644 --- a/util/system/filemap.cpp +++ b/util/system/filemap.cpp @@ -1,10 +1,10 @@ #include "info.h" #include "madvise.h" -#include "defaults.h" +#include "defaults.h" #include "hi_lo.h" #include <util/generic/buffer.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #include <util/generic/singleton.h> #if defined(_win_) @@ -149,7 +149,7 @@ public: } else { PtrStart_ = nullptr; } -#endif +#endif } void CheckFile() const { @@ -210,7 +210,7 @@ public: return File_.IsOpen() #if defined(_win_) && Mapping_ != nullptr -#endif +#endif ; } @@ -437,8 +437,8 @@ TMemoryMap::EOpenMode TMemoryMap::GetMode() const noexcept { TFile TMemoryMap::GetFile() const noexcept { return Impl_->GetFile(); -} - +} + TFileMap::TFileMap(const TMemoryMap& map) noexcept : Map_(map) { @@ -523,8 +523,8 @@ TFileMap::~TFileMap() { } catch (...) { // ¯\_(ツ)_/¯ } -} - +} + void TFileMap::Precharge(size_t pos, size_t size) const { NPrivate::Precharge(Ptr(), MappedSize(), pos, size); } @@ -535,51 +535,51 @@ TMappedAllocation::TMappedAllocation(size_t size, bool shared, void* addr) , Shared_(shared) #if defined(_win_) , Mapping_(nullptr) -#endif -{ +#endif +{ if (size != 0) { Alloc(size, addr); } -} - +} + void* TMappedAllocation::Alloc(size_t size, void* addr) { assert(Ptr_ == nullptr); #if defined(_win_) (void)addr; Mapping_ = CreateFileMapping((HANDLE)-1, nullptr, PAGE_READWRITE, 0, size ? size : 1, nullptr); Ptr_ = MapViewOfFile(Mapping_, FILE_MAP_WRITE, 0, 0, size ? size : 1); -#else +#else Ptr_ = mmap(addr, size, PROT_READ | PROT_WRITE, (Shared_ ? MAP_SHARED : MAP_PRIVATE) | MAP_ANON, -1, 0); if (Ptr_ == (void*)MAP_FAILED) { Ptr_ = nullptr; - } -#endif + } +#endif if (Ptr_ != nullptr) { Size_ = size; } return Ptr_; -} - +} + void TMappedAllocation::Dealloc() { if (Ptr_ == nullptr) { - return; + return; } #if defined(_win_) UnmapViewOfFile(Ptr_); CloseHandle(Mapping_); Mapping_ = nullptr; -#else +#else munmap((caddr_t)Ptr_, Size_); -#endif +#endif Ptr_ = nullptr; Size_ = 0; -} - +} + void TMappedAllocation::swap(TMappedAllocation& with) { DoSwap(Ptr_, with.Ptr_); DoSwap(Size_, with.Size_); #if defined(_win_) DoSwap(Mapping_, with.Mapping_); -#endif -} +#endif +} diff --git a/util/system/filemap.h b/util/system/filemap.h index 11be64bff4..2edc313aaa 100644 --- a/util/system/filemap.h +++ b/util/system/filemap.h @@ -7,7 +7,7 @@ #include <util/generic/noncopyable.h> #include <util/generic/ptr.h> #include <util/generic/utility.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #include <util/generic/flags.h> #include <util/generic/string.h> @@ -117,7 +117,7 @@ public: TFileMap(const TFileMap& fm) noexcept; ~TFileMap(); - + TMapResult Map(i64 offset, size_t size); TMapResult ResizeAndRemap(i64 offset, size_t size); void Unmap(); @@ -125,7 +125,7 @@ public: void Flush(void* ptr, size_t size) { Flush(ptr, size, true); } - + void Flush() { Flush(Ptr(), MappedSize()); } @@ -288,11 +288,11 @@ private: }; class TMappedAllocation: TMoveOnly { -public: +public: TMappedAllocation(size_t size = 0, bool shared = false, void* addr = nullptr); ~TMappedAllocation() { - Dealloc(); - } + Dealloc(); + } TMappedAllocation(TMappedAllocation&& other) { this->swap(other); } @@ -301,13 +301,13 @@ public: return *this; } void* Alloc(size_t size, void* addr = nullptr); - void Dealloc(); - void* Ptr() const { + void Dealloc(); + void* Ptr() const { return Ptr_; - } - char* Data(ui32 pos = 0) const { + } + char* Data(ui32 pos = 0) const { return (char*)(Ptr_ ? ((char*)Ptr_ + pos) : nullptr); - } + } char* Begin() const noexcept { return (char*)Ptr(); } @@ -316,66 +316,66 @@ public: } size_t MappedSize() const { return Size_; - } + } void swap(TMappedAllocation& with); -private: +private: void* Ptr_ = nullptr; size_t Size_ = 0; bool Shared_ = false; #ifdef _win_ void* Mapping_ = nullptr; #endif -}; - +}; + template <class T> class TMappedArray: private TMappedAllocation { -public: +public: TMappedArray(size_t siz = 0) : TMappedAllocation(0) { - if (siz) - Create(siz); - } + if (siz) + Create(siz); + } ~TMappedArray() { - Destroy(); - } + Destroy(); + } T* Create(size_t siz) { Y_ASSERT(MappedSize() == 0 && Ptr() == nullptr); T* arr = (T*)Alloc((sizeof(T) * siz)); if (!arr) return nullptr; Y_ASSERT(MappedSize() == sizeof(T) * siz); - for (size_t n = 0; n < siz; n++) + for (size_t n = 0; n < siz; n++) new (&arr[n]) T(); return arr; - } - void Destroy() { - T* arr = (T*)Ptr(); - if (arr) { - for (size_t n = 0; n < size(); n++) - arr[n].~T(); - Dealloc(); - } - } + } + void Destroy() { + T* arr = (T*)Ptr(); + if (arr) { + for (size_t n = 0; n < size(); n++) + arr[n].~T(); + Dealloc(); + } + } T& operator[](size_t pos) { Y_ASSERT(pos < size()); - return ((T*)Ptr())[pos]; - } + return ((T*)Ptr())[pos]; + } const T& operator[](size_t pos) const { Y_ASSERT(pos < size()); - return ((T*)Ptr())[pos]; - } + return ((T*)Ptr())[pos]; + } T* begin() { return (T*)Ptr(); } T* end() { return (T*)((char*)Ptr() + MappedSize()); } - size_t size() const { + size_t size() const { return MappedSize() / sizeof(T); - } + } void swap(TMappedArray<T>& with) { TMappedAllocation::swap(with); - } -}; + } +}; diff --git a/util/system/filemap_ut.cpp b/util/system/filemap_ut.cpp index 73f109dc88..c53b874586 100644 --- a/util/system/filemap_ut.cpp +++ b/util/system/filemap_ut.cpp @@ -18,7 +18,7 @@ Y_UNIT_TEST_SUITE(TFileMapTest) { char data[] = "abcdefgh"; TFile file(FileName_, CreateAlways | WrOnly); - file.Write(static_cast<void*>(data), sizeof(data)); + file.Write(static_cast<void*>(data), sizeof(data)); file.Close(); { @@ -218,7 +218,7 @@ Y_UNIT_TEST_SUITE(TFileMapTest) { size_t sz = sizeof(data) / sizeof(data[0]); TFile file(FileName_, CreateAlways | WrOnly); - file.Write(static_cast<void*>(data), sizeof(data)); + file.Write(static_cast<void*>(data), sizeof(data)); file.Close(); mappedArray.Init(FileName_); @@ -253,8 +253,8 @@ Y_UNIT_TEST_SUITE(TFileMapTest) { // actual test end file = TFile(FileName_, WrOnly); - file.Seek(0, sEnd); - file.Write("x", 1); + file.Seek(0, sEnd); + file.Write("x", 1); file.Close(); bool caught = false; @@ -279,7 +279,7 @@ Y_UNIT_TEST_SUITE(TFileMapTest) { UNIT_ASSERT(mappedArray.begin() + sz == mappedArray.end()); for (size_t i = 0; i < sz; ++i) { - mappedArray[i] = (ui32)i; + mappedArray[i] = (ui32)i; } for (size_t i = 0; i < sz; ++i) { UNIT_ASSERT(mappedArray[i] == i); @@ -287,7 +287,7 @@ Y_UNIT_TEST_SUITE(TFileMapTest) { TMappedArray<ui32> mappedArray2(1000); mappedArray.swap(mappedArray2); - UNIT_ASSERT(mappedArray.size() == 1000 && mappedArray2.size() == sz); + UNIT_ASSERT(mappedArray.size() == 1000 && mappedArray2.size() == sz); } Y_UNIT_TEST(TestMemoryMap) { diff --git a/util/system/flock.cpp b/util/system/flock.cpp index fe88fecaff..f00b7fd5d1 100644 --- a/util/system/flock.cpp +++ b/util/system/flock.cpp @@ -1,36 +1,36 @@ #include "flock.h" - -#ifndef _unix_ - + +#ifndef _unix_ + #include <util/generic/utility.h> #include "winint.h" #include <io.h> #include <errno.h> - + #ifdef __cplusplus -extern "C" { +extern "C" { #endif - + int flock(int fd, int op) { return Flock((HANDLE)_get_osfhandle(fd), op); } - + int Flock(void* hdl, int op) { errno = 0; - + if (hdl == INVALID_HANDLE_VALUE) { errno = EBADF; return -1; } - + DWORD low = 1, high = 0; OVERLAPPED io; - + Zero(io); UnlockFileEx(hdl, 0, low, high, &io); - + switch (op & ~LOCK_NB) { case LOCK_EX: case LOCK_SH: { @@ -47,9 +47,9 @@ extern "C" { if (LockFileEx(hdl, mode, 0, low, high, &io)) { return 0; } - } + } break; - } + } case LOCK_UN: return 0; break; @@ -58,14 +58,14 @@ extern "C" { } errno = EINVAL; return -1; - } - + } + int fsync(int fd) { return _commit(fd); } - + #ifdef __cplusplus -} +} #endif - -#endif + +#endif diff --git a/util/system/flock.h b/util/system/flock.h index 797b1970a1..41eb6a1e5b 100644 --- a/util/system/flock.h +++ b/util/system/flock.h @@ -1,14 +1,14 @@ #pragma once - + #include "error.h" -#include "defaults.h" +#include "defaults.h" #include "file.h" - -#if defined(_unix_) - + +#if defined(_unix_) + #include <sys/file.h> #include <fcntl.h> - + static inline int Flock(int fd, int op) { return flock(fd, op); } @@ -16,20 +16,20 @@ static inline int Flock(int fd, int op) { #else // not _unix_ #ifdef __cplusplus -extern "C" { +extern "C" { #endif - + #define LOCK_SH 1 /* shared lock */ #define LOCK_EX 2 /* exclusive lock */ #define LOCK_NB 4 /* don't block when locking */ #define LOCK_UN 8 /* unlock */ - + int Flock(void* hndl, int operation); int flock(int fd, int operation); int fsync(int fd); - + #ifdef __cplusplus -} +} #endif - + #endif // not _unix_ diff --git a/util/system/fs.cpp b/util/system/fs.cpp index d2611a8ccc..5ca9a77c9f 100644 --- a/util/system/fs.cpp +++ b/util/system/fs.cpp @@ -1,9 +1,9 @@ -#include "fs.h" -#include "defaults.h" +#include "fs.h" +#include "defaults.h" #if defined(_win_) #include "fs_win.h" -#else +#else #include <unistd.h> #include <errno.h> #endif @@ -11,17 +11,17 @@ #include <util/generic/yexception.h> #include <util/memory/tempbuf.h> #include <util/stream/file.h> -#include <util/charset/wide.h> +#include <util/charset/wide.h> #include <util/folder/iterator.h> #include <util/system/fstat.h> #include <util/folder/path.h> bool NFs::Remove(const TString& path) { -#if defined(_win_) +#if defined(_win_) return NFsPrivate::WinRemove(path); -#else +#else return ::remove(path.data()) == 0; -#endif +#endif } void NFs::RemoveRecursive(const TString& path) { @@ -107,19 +107,19 @@ bool NFs::HardLink(const TString& existingPath, const TString& newPath) { } bool NFs::SymLink(const TString& targetPath, const TString& linkPath) { -#if defined(_win_) +#if defined(_win_) return NFsPrivate::WinSymLink(targetPath, linkPath); -#elif defined(_unix_) +#elif defined(_unix_) return 0 == symlink(targetPath.data(), linkPath.data()); -#endif -} - +#endif +} + TString NFs::ReadLink(const TString& path) { -#if defined(_win_) +#if defined(_win_) return NFsPrivate::WinReadLink(path); -#elif defined(_unix_) - TTempBuf buf; - while (true) { +#elif defined(_unix_) + TTempBuf buf; + while (true) { ssize_t r = readlink(path.data(), buf.Data(), buf.Size()); if (r < 0) { ythrow yexception() << "can't read link " << path << ", errno = " << errno; @@ -127,11 +127,11 @@ TString NFs::ReadLink(const TString& path) { if (r < (ssize_t)buf.Size()) { return TString(buf.Data(), r); } - buf = TTempBuf(buf.Size() * 2); - } -#endif -} - + buf = TTempBuf(buf.Size() * 2); + } +#endif +} + void NFs::Cat(const TString& dstPath, const TString& srcPath) { TUnbufferedFileInput src(srcPath); TUnbufferedFileOutput dst(TFile(dstPath, ForAppend | WrOnly | Seq)); diff --git a/util/system/fs.h b/util/system/fs.h index 237daf2d2d..1b51613cd5 100644 --- a/util/system/fs.h +++ b/util/system/fs.h @@ -29,7 +29,7 @@ namespace NFs { /// @returns true on success or false otherwise /// LastSystemError() is set in case of failure bool Remove(const TString& path); - + /// Remove a file or directory with contents /// /// @param[in] path Path to file or directory diff --git a/util/system/fs_ut.cpp b/util/system/fs_ut.cpp index de071ebf55..7c9636ce3c 100644 --- a/util/system/fs_ut.cpp +++ b/util/system/fs_ut.cpp @@ -1,24 +1,24 @@ -#include "fs.h" - +#include "fs.h" + #include <library/cpp/testing/unittest/registar.h> - + #include "file.h" #include "sysstat.h" #include "fstat.h" -#include <util/folder/dirut.h> +#include <util/folder/dirut.h> #include <util/folder/path.h> - + //WARNING: on windows the test must be run with administative rules -class TFsTest: public TTestBase { - UNIT_TEST_SUITE(TFsTest); +class TFsTest: public TTestBase { + UNIT_TEST_SUITE(TFsTest); UNIT_TEST(TestCreateRemove); UNIT_TEST(TestRename); UNIT_TEST(TestSymlink); UNIT_TEST(TestHardlink); UNIT_TEST(TestCwdOpts); UNIT_TEST(TestEnsureExists); - UNIT_TEST_SUITE_END(); + UNIT_TEST_SUITE_END(); public: void TestCreateRemove(); @@ -27,10 +27,10 @@ public: void TestHardlink(); void TestCwdOpts(); void TestEnsureExists(); -}; - -UNIT_TEST_SUITE_REGISTRATION(TFsTest); - +}; + +UNIT_TEST_SUITE_REGISTRATION(TFsTest); + static void Touch(const TFsPath& path) { TFile file(path, CreateAlways | WrOnly); file.Write("123", 3); @@ -192,10 +192,10 @@ void TFsTest::TestHardlink() { } static void RunSymLinkTest(TString fileLocalName, TString symLinkName) { - // if previous running was failed + // if previous running was failed TFsPath subDir = "tempsubdir"; TFsPath srcFile = subDir / fileLocalName; - + TFsPath subsubDir1 = subDir / "dir1"; TFsPath subsubDir2 = subDir / "dir2"; @@ -215,51 +215,51 @@ static void RunSymLinkTest(TString fileLocalName, TString symLinkName) { NFs::MakeDirectory(subDir); NFs::MakeDirectory(subsubDir1, NFs::FP_NONSECRET_FILE); NFs::MakeDirectory(subsubDir2, NFs::FP_SECRET_FILE); - { + { TFile file(srcFile, CreateNew | WrOnly); - file.Write("1234567", 7); - } + file.Write("1234567", 7); + } UNIT_ASSERT(NFs::SymLink(subDir, linkD1)); UNIT_ASSERT(NFs::SymLink("../dir2", linkD2)); UNIT_ASSERT(NFs::SymLink("../dir3", dangling)); UNIT_ASSERT_STRINGS_EQUAL(NFs::ReadLink(linkD2), TString("..") + LOCSLASH_S "dir2"); UNIT_ASSERT_STRINGS_EQUAL(NFs::ReadLink(dangling), TString("..") + LOCSLASH_S "dir3"); - { + { TFile file(linkD1 / fileLocalName, OpenExisting | RdOnly); - UNIT_ASSERT_VALUES_EQUAL(file.GetLength(), 7); - } + UNIT_ASSERT_VALUES_EQUAL(file.GetLength(), 7); + } UNIT_ASSERT(NFs::SymLink(srcFile, symLinkName)); - { + { TFile file(symLinkName, OpenExisting | RdOnly); - UNIT_ASSERT_VALUES_EQUAL(file.GetLength(), 7); - } - { + UNIT_ASSERT_VALUES_EQUAL(file.GetLength(), 7); + } + { TFileStat fs(linkD1); - UNIT_ASSERT(!fs.IsFile()); - UNIT_ASSERT(fs.IsDir()); - UNIT_ASSERT(!fs.IsSymlink()); - } - { + UNIT_ASSERT(!fs.IsFile()); + UNIT_ASSERT(fs.IsDir()); + UNIT_ASSERT(!fs.IsSymlink()); + } + { TFileStat fs(linkD1, true); - UNIT_ASSERT(!fs.IsFile()); - //UNIT_ASSERT(fs.IsDir()); // failed on unix - UNIT_ASSERT(fs.IsSymlink()); - } - { + UNIT_ASSERT(!fs.IsFile()); + //UNIT_ASSERT(fs.IsDir()); // failed on unix + UNIT_ASSERT(fs.IsSymlink()); + } + { TFileStat fs(symLinkName); - UNIT_ASSERT(fs.IsFile()); - UNIT_ASSERT(!fs.IsDir()); - UNIT_ASSERT(!fs.IsSymlink()); + UNIT_ASSERT(fs.IsFile()); + UNIT_ASSERT(!fs.IsDir()); + UNIT_ASSERT(!fs.IsSymlink()); UNIT_ASSERT_VALUES_EQUAL(fs.Size, 7u); - } + } - { + { TFileStat fs(symLinkName, true); //UNIT_ASSERT(fs.IsFile()); // no evidence that symlink has to be a file as well - UNIT_ASSERT(!fs.IsDir()); - UNIT_ASSERT(fs.IsSymlink()); - } - + UNIT_ASSERT(!fs.IsDir()); + UNIT_ASSERT(fs.IsSymlink()); + } + UNIT_ASSERT(NFs::Remove(symLinkName)); UNIT_ASSERT(NFs::Exists(srcFile)); @@ -274,7 +274,7 @@ static void RunSymLinkTest(TString fileLocalName, TString symLinkName) { UNIT_ASSERT(NFs::Remove(subsubDir1)); UNIT_ASSERT(NFs::Remove(subsubDir2)); UNIT_ASSERT(NFs::Remove(subDir)); -} +} void TFsTest::TestSymlink() { // if previous running was failed diff --git a/util/system/fs_win.cpp b/util/system/fs_win.cpp index a410ccac06..c03dcea108 100644 --- a/util/system/fs_win.cpp +++ b/util/system/fs_win.cpp @@ -1,13 +1,13 @@ -#include "fs_win.h" -#include "defaults.h" -#include "maxlen.h" - -#include <util/folder/dirut.h> -#include <util/charset/wide.h> +#include "fs_win.h" +#include "defaults.h" +#include "maxlen.h" + +#include <util/folder/dirut.h> +#include <util/charset/wide.h> #include "file.h" - + #include <winioctl.h> - + namespace NFsPrivate { static LPCWSTR UTF8ToWCHAR(const TStringBuf str, TUtf16String& wstr) { wstr.resize(str.size()); @@ -17,8 +17,8 @@ namespace NFsPrivate { wstr.erase(written); static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)"); return (const WCHAR*)wstr.data(); - } - + } + static TString WCHARToUTF8(const LPWSTR wstr, size_t len) { static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)"); @@ -49,7 +49,7 @@ namespace NFsPrivate { } return MoveFileExW(opPtr, npPtr, MOVEFILE_REPLACE_EXISTING) != 0; - } + } bool WinRemove(const TString& path) { TUtf16String wstr; @@ -64,9 +64,9 @@ namespace NFsPrivate { return ::RemoveDirectoryW(wname) != 0; return ::DeleteFileW(wname) != 0; } - + return false; - } + } bool WinSymLink(const TString& targetName, const TString& linkName) { TString tName(targetName); @@ -96,11 +96,11 @@ namespace NFsPrivate { LPCWSTR ptrFullTarget = UTF8ToWCHAR(fullTarget, fullTargetW); attr = ::GetFileAttributesW(ptrFullTarget); } - } - } + } + } return 0 != CreateSymbolicLinkW(lname, wname, attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY) ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0); - } - + } + bool WinHardLink(const TString& existingPath, const TString& newPath) { TUtf16String ep, np; LPCWSTR epPtr = UTF8ToWCHAR(existingPath, ep); @@ -144,29 +144,29 @@ namespace NFsPrivate { return CreateDirectoryW(ptr, (LPSECURITY_ATTRIBUTES) nullptr); } // edited part of <Ntifs.h> from Windows DDK - -#define SYMLINK_FLAG_RELATIVE 1 - + +#define SYMLINK_FLAG_RELATIVE 1 + struct TReparseBufferHeader { USHORT SubstituteNameOffset; USHORT SubstituteNameLength; USHORT PrintNameOffset; USHORT PrintNameLength; }; - + struct TSymbolicLinkReparseBuffer: public TReparseBufferHeader { ULONG Flags; // 0 or SYMLINK_FLAG_RELATIVE wchar16 PathBuffer[1]; }; - + struct TMountPointReparseBuffer: public TReparseBufferHeader { wchar16 PathBuffer[1]; }; - + struct TGenericReparseBuffer { wchar16 DataBuffer[1]; }; - + struct REPARSE_DATA_BUFFER { ULONG ReparseTag; USHORT ReparseDataLength; @@ -176,10 +176,10 @@ namespace NFsPrivate { TMountPointReparseBuffer MountPointReparseBuffer; TGenericReparseBuffer GenericReparseBuffer; }; - }; - + }; + // the end of edited part of <Ntifs.h> - + TString WinReadLink(const TString& name) { TFileHandle h = CreateFileWithUtf8Name(name, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, true); @@ -200,34 +200,34 @@ namespace NFsPrivate { } //this reparse point is unsupported in arcadia return TString(); - } else { + } else { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { buf = TTempBuf(buf.Size() * 2); } else { ythrow yexception() << "can't read link " << name; } - } - } - } - + } + } + } + // we can't use this function to get an analog of unix inode due to a lot of NTFS folders do not have this GUID //(it will be 'create' case really) /* -bool GetObjectId(const char* path, GUID* id) { - TFileHandle h = CreateFileWithUtf8Name(path, 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, +bool GetObjectId(const char* path, GUID* id) { + TFileHandle h = CreateFileWithUtf8Name(path, 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, true); - if (h.IsOpen()) { - FILE_OBJECTID_BUFFER fob; - DWORD resSize = 0; + if (h.IsOpen()) { + FILE_OBJECTID_BUFFER fob; + DWORD resSize = 0; if (DeviceIoControl(h, FSCTL_CREATE_OR_GET_OBJECT_ID, nullptr, 0, &fob, sizeof(fob), &resSize, nullptr)) { Y_ASSERT(resSize == sizeof(fob)); - memcpy(id, &fob.ObjectId, sizeof(GUID)); - return true; - } - } - memset(id, 0, sizeof(GUID)); - return false; -} -*/ + memcpy(id, &fob.ObjectId, sizeof(GUID)); + return true; + } + } + memset(id, 0, sizeof(GUID)); + return false; +} +*/ } diff --git a/util/system/fs_win.h b/util/system/fs_win.h index 8086129828..5c304d7d3c 100644 --- a/util/system/fs_win.h +++ b/util/system/fs_win.h @@ -1,11 +1,11 @@ -#pragma once - -#include "winint.h" -#include "defaults.h" - +#pragma once + +#include "winint.h" +#include "defaults.h" + #include <util/generic/strbuf.h> #include <util/generic/string.h> - + namespace NFsPrivate { bool WinRename(const TString& oldPath, const TString& newPath); diff --git a/util/system/fstat.cpp b/util/system/fstat.cpp index 81e98cbc6b..7e253b9f80 100644 --- a/util/system/fstat.cpp +++ b/util/system/fstat.cpp @@ -1,4 +1,4 @@ -#include "fstat.h" +#include "fstat.h" #include "file.h" #include <sys/stat.h> @@ -16,20 +16,20 @@ #define _S_IFLNK 0x80000000 ui32 GetFileMode(DWORD fileAttributes) { - ui32 mode = 0; - if (fileAttributes == 0xFFFFFFFF) - return mode; + ui32 mode = 0; + if (fileAttributes == 0xFFFFFFFF) + return mode; if (fileAttributes & FILE_ATTRIBUTE_DEVICE) - mode |= _S_IFCHR; + mode |= _S_IFCHR; if (fileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) - mode |= _S_IFLNK; // todo: was undefined by the moment of writing this code + mode |= _S_IFLNK; // todo: was undefined by the moment of writing this code if (fileAttributes & FILE_ATTRIBUTE_DIRECTORY) - mode |= _S_IFDIR; - if (fileAttributes & (FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE)) - mode |= _S_IFREG; + mode |= _S_IFDIR; + if (fileAttributes & (FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE)) + mode |= _S_IFREG; if ((fileAttributes & FILE_ATTRIBUTE_READONLY) == 0) mode |= _S_IWRITE; - return mode; + return mode; } #define S_ISDIR(st_mode) (st_mode & _S_IFDIR) @@ -57,21 +57,21 @@ static void MakeStat(TFileStat& st, const TSystemFStat& fs) { st.CTime = fs.st_ctime; st.INode = fs.st_ino; #else - timeval tv; + timeval tv; FileTimeToTimeval(&fs.ftCreationTime, &tv); - st.CTime = tv.tv_sec; + st.CTime = tv.tv_sec; FileTimeToTimeval(&fs.ftLastAccessTime, &tv); - st.ATime = tv.tv_sec; + st.ATime = tv.tv_sec; FileTimeToTimeval(&fs.ftLastWriteTime, &tv); - st.MTime = tv.tv_sec; + st.MTime = tv.tv_sec; st.NLinks = fs.nNumberOfLinks; st.Mode = GetFileMode(fs.dwFileAttributes); - st.Uid = 0; - st.Gid = 0; + st.Uid = 0; + st.Gid = 0; st.Size = ((ui64)fs.nFileSizeHigh << 32) | fs.nFileSizeLow; st.AllocationSize = st.Size; // FIXME st.INode = ((ui64)fs.nFileIndexHigh << 32) | fs.nFileIndexLow; -#endif +#endif } static bool GetStatByHandle(TSystemFStat& fs, FHANDLE f) { @@ -147,7 +147,7 @@ bool TFileStat::IsDir() const noexcept { bool TFileStat::IsSymlink() const noexcept { return S_ISLNK(Mode); } - + bool operator==(const TFileStat& l, const TFileStat& r) noexcept { return l.Mode == r.Mode && l.Uid == r.Uid && @@ -163,27 +163,27 @@ bool operator!=(const TFileStat& l, const TFileStat& r) noexcept { return !(l == r); } -i64 GetFileLength(FHANDLE fd) { +i64 GetFileLength(FHANDLE fd) { #if defined(_win_) - LARGE_INTEGER pos; - if (!::GetFileSizeEx(fd, &pos)) - return -1L; - return pos.QuadPart; + LARGE_INTEGER pos; + if (!::GetFileSizeEx(fd, &pos)) + return -1L; + return pos.QuadPart; #elif defined(_unix_) - struct stat statbuf; + struct stat statbuf; if (::fstat(fd, &statbuf) != 0) { - return -1L; + return -1L; } if (!(statbuf.st_mode & (S_IFREG | S_IFBLK | S_IFCHR))) { // st_size only makes sense for regular files or devices errno = EINVAL; return -1L; } - return statbuf.st_size; -#else + return statbuf.st_size; +#else #error unsupported platform -#endif -} +#endif +} i64 GetFileLength(const char* name) { #if defined(_win_) diff --git a/util/system/fstat.h b/util/system/fstat.h index 64e79e1b55..418303fd28 100644 --- a/util/system/fstat.h +++ b/util/system/fstat.h @@ -41,7 +41,7 @@ public: private: void MakeFromFileName(const char* fileName, bool nofollow); }; - -i64 GetFileLength(FHANDLE fd); + +i64 GetFileLength(FHANDLE fd); i64 GetFileLength(const char* name); i64 GetFileLength(const TString& name); diff --git a/util/system/fstat_ut.cpp b/util/system/fstat_ut.cpp index 160ecd936e..8bef783b32 100644 --- a/util/system/fstat_ut.cpp +++ b/util/system/fstat_ut.cpp @@ -1,11 +1,11 @@ -#include "fstat.h" -#include "file.h" -#include "sysstat.h" -#include "fs.h" - +#include "fstat.h" +#include "file.h" +#include "sysstat.h" +#include "fs.h" + #include <library/cpp/testing/unittest/registar.h> #include <library/cpp/testing/unittest/tests_data.h> - + #include <util/folder/path.h> Y_UNIT_TEST_SUITE(TestFileStat) { @@ -26,11 +26,11 @@ Y_UNIT_TEST_SUITE(TestFileStat) { UNIT_ASSERT(fs.NLinks == 1); oFs = fs; } - + UNIT_ASSERT(file.IsOpen()); UNIT_ASSERT_VALUES_EQUAL(file.GetLength(), 7); file.Close(); - } + } TFileStat cFs(fileName); UNIT_ASSERT(cFs.IsFile()); UNIT_ASSERT(!cFs.IsDir()); @@ -45,7 +45,7 @@ Y_UNIT_TEST_SUITE(TestFileStat) { UNIT_ASSERT_VALUES_EQUAL(cFs.INode, oFs.INode); UNIT_ASSERT(unlink(fileName.data()) == 0); } - + Y_UNIT_TEST(DirTest) { Mkdir("tmpd", MODE0777); TFileStat fs("tmpd"); @@ -60,8 +60,8 @@ Y_UNIT_TEST_SUITE(TestFileStat) { UNIT_ASSERT(!fs.IsSymlink()); UNIT_ASSERT(fs.Size == 0); UNIT_ASSERT(fs.CTime == 0); - } - + } + Y_UNIT_TEST(SymlinkToExistingFileTest) { const auto path = GetOutputPath() / "file_1"; const auto link = GetOutputPath() / "symlink_1"; diff --git a/util/system/hostname.cpp b/util/system/hostname.cpp index 386f646d6b..e64f4bb185 100644 --- a/util/system/hostname.cpp +++ b/util/system/hostname.cpp @@ -13,8 +13,8 @@ #include <WinSock2.h> #endif -#include "defaults.h" -#include "yassert.h" +#include "defaults.h" +#include "yassert.h" #include "hostname.h" namespace { diff --git a/util/system/info.cpp b/util/system/info.cpp index cf6681e89a..34699e5dd2 100644 --- a/util/system/info.cpp +++ b/util/system/info.cpp @@ -146,14 +146,14 @@ size_t NSystemInfo::NumberOfCpus() { size_t NSystemInfo::LoadAverage(double* la, size_t len) { #if defined(_win_) || defined(_musl_) || defined(_bionic_) - int ret = -1; -#else + int ret = -1; +#else for (size_t i = 0; i < len; ++i) { la[i] = 0; } int ret = getloadavg(la, len); -#endif +#endif if (ret < 0) { for (size_t i = 0; i < len; ++i) { diff --git a/util/system/info.h b/util/system/info.h index 73ebe48a9a..906439ac40 100644 --- a/util/system/info.h +++ b/util/system/info.h @@ -1,6 +1,6 @@ #pragma once -#include "defaults.h" +#include "defaults.h" namespace NSystemInfo { size_t NumberOfCpus(); diff --git a/util/system/maxlen.h b/util/system/maxlen.h index e1ff7f5008..1eac920e53 100644 --- a/util/system/maxlen.h +++ b/util/system/maxlen.h @@ -15,11 +15,11 @@ #define LINKTEXT_MAX 1024 -#ifdef WIN32 +#ifdef WIN32 #ifndef PATH_MAX #define PATH_MAX _MAX_PATH #endif -#else +#else #ifndef MAX_PATH #define MAX_PATH PATH_MAX diff --git a/util/system/mktemp.cpp b/util/system/mktemp.cpp index 505b7b4a4b..355d1be875 100644 --- a/util/system/mktemp.cpp +++ b/util/system/mktemp.cpp @@ -4,8 +4,8 @@ #include <util/generic/yexception.h> #include <util/stream/file.h> -#include <cerrno> -#include <cstring> +#include <cerrno> +#include <cstring> #ifdef _win32_ #include "winint.h" diff --git a/util/system/mktemp_system.cpp b/util/system/mktemp_system.cpp index 32bea2987c..d84e596c47 100644 --- a/util/system/mktemp_system.cpp +++ b/util/system/mktemp_system.cpp @@ -31,8 +31,8 @@ * SUCH DAMAGE. */ -#include "defaults.h" - +#include "defaults.h" + #include <sys/types.h> #include <fcntl.h> #include <errno.h> @@ -42,7 +42,7 @@ #include <ctype.h> #ifdef _win32_ - #include "winint.h" + #include "winint.h" #include <util/folder/dirut.h> #else #include <unistd.h> diff --git a/util/system/mutex.h b/util/system/mutex.h index 032630d134..60de2730f2 100644 --- a/util/system/mutex.h +++ b/util/system/mutex.h @@ -14,7 +14,7 @@ public: inline bool TryAcquire() noexcept { return true; } - + inline void Release() noexcept { } diff --git a/util/system/sem.cpp b/util/system/sem.cpp index 4a93b903b5..3e773e08a4 100644 --- a/util/system/sem.cpp +++ b/util/system/sem.cpp @@ -4,11 +4,11 @@ #include <malloc.h> #elif defined(_sun) #include <alloca.h> -#endif +#endif #include <cerrno> #include <cstring> - + #ifdef _win_ #include "winint.h" #else @@ -23,7 +23,7 @@ #endif #endif -#ifdef USE_SYSV_SEMAPHORES +#ifdef USE_SYSV_SEMAPHORES #include <errno.h> #include <sys/types.h> #include <sys/ipc.h> @@ -38,7 +38,7 @@ union semun { #else union semun arg; #endif -#endif +#endif #include <util/digest/city.h> #include <util/string/cast.h> @@ -50,7 +50,7 @@ namespace { private: #ifdef _win_ using SEMHANDLE = HANDLE; -#else +#else #ifdef USE_SYSV_SEMAPHORES using SEMHANDLE = int; #else @@ -102,12 +102,12 @@ namespace { } #endif #endif - } - + } + inline ~TSemaphoreImpl() { #ifdef _win_ ::CloseHandle(Handle); -#else +#else #ifdef USE_SYSV_SEMAPHORES // we DO NOT want 'semctl(Handle, 0, IPC_RMID)' for multiprocess tasks; //struct sembuf ops[] = {{0, 0, IPC_NOWAIT}}; @@ -116,13 +116,13 @@ namespace { #else sem_close(Handle); // we DO NOT want sem_unlink(...) #endif -#endif +#endif } - + inline void Release() noexcept { #ifdef _win_ ::ReleaseSemaphore(Handle, 1, 0); -#else +#else #ifdef USE_SYSV_SEMAPHORES struct sembuf ops[] = {{0, 1, SEM_UNDO}}; int ret = semop(Handle, ops, 1); @@ -130,15 +130,15 @@ namespace { int ret = sem_post(Handle); #endif Y_VERIFY(ret == 0, "can not release semaphore"); -#endif +#endif } - + //The UNIX semaphore object does not support a timed "wait", and //hence to maintain consistancy, for win32 case we use INFINITE or 0 timeout. inline void Acquire() noexcept { #ifdef _win_ Y_VERIFY(::WaitForSingleObject(Handle, INFINITE) == WAIT_OBJECT_0, "can not acquire semaphore"); -#else +#else #ifdef USE_SYSV_SEMAPHORES struct sembuf ops[] = {{0, -1, SEM_UNDO}}; int ret = semop(Handle, ops, 1); @@ -146,16 +146,16 @@ namespace { int ret = sem_wait(Handle); #endif Y_VERIFY(ret == 0, "can not acquire semaphore"); -#endif +#endif } - + inline bool TryAcquire() noexcept { #ifdef _win_ // zero-second time-out interval // WAIT_OBJECT_0: current free count > 0 // WAIT_TIMEOUT: current free count == 0 return ::WaitForSingleObject(Handle, 0) == WAIT_OBJECT_0; -#else +#else #ifdef USE_SYSV_SEMAPHORES struct sembuf ops[] = {{0, -1, SEM_UNDO | IPC_NOWAIT}}; int ret = semop(Handle, ops, 1); @@ -163,7 +163,7 @@ namespace { int ret = sem_trywait(Handle); #endif return ret == 0; -#endif +#endif } }; @@ -252,7 +252,7 @@ class TFastSemaphore::TImpl: public TString, public TSemaphoreImpl { public: inline TImpl(ui32 n) : TString(ToString(RandomNumber<ui64>())) - , TSemaphoreImpl(c_str(), n) + , TSemaphoreImpl(c_str(), n) { } }; diff --git a/util/system/sem.h b/util/system/sem.h index 0c964ad6b6..545dc263be 100644 --- a/util/system/sem.h +++ b/util/system/sem.h @@ -1,7 +1,7 @@ #pragma once - -#include "defaults.h" - + +#include "defaults.h" + #include <util/generic/ptr.h> //named sempahore @@ -9,21 +9,21 @@ class TSemaphore { public: TSemaphore(const char* name, ui32 maxFreeCount); ~TSemaphore(); - + //Increase the semaphore counter. void Release() noexcept; - + //Keep a thread held while the semaphore counter is equal 0. void Acquire() noexcept; - + //Try to enter the semaphore gate. A non-blocking variant of Acquire. //Returns 'true' if the semaphore counter decreased bool TryAcquire() noexcept; - + private: class TImpl; THolder<TImpl> Impl_; -}; +}; //unnamed semaphore, faster, than previous class TFastSemaphore { diff --git a/util/system/sigset.h b/util/system/sigset.h index 8dd02fd817..f4035425e3 100644 --- a/util/system/sigset.h +++ b/util/system/sigset.h @@ -2,7 +2,7 @@ // Functions for manipulating signal sets -#include "compat.h" +#include "compat.h" #if defined _unix_ #include <pthread.h> @@ -20,39 +20,39 @@ using sigset_t = ui32; inline int SigEmptySet(sigset_t* set) { #if defined _unix_ - return sigemptyset(set); + return sigemptyset(set); #else Y_UNUSED(set); - return 0; + return 0; #endif } inline int SigFillSet(sigset_t* set) { #if defined _unix_ - return sigfillset(set); + return sigfillset(set); #else Y_UNUSED(set); - return 0; + return 0; #endif } inline int SigAddSet(sigset_t* set, int signo) { #if defined _unix_ - return sigaddset(set, signo); + return sigaddset(set, signo); #else Y_UNUSED(set); Y_UNUSED(signo); - return 0; + return 0; #endif } inline int SigDelSet(sigset_t* set, int signo) { #if defined _unix_ - return sigdelset(set, signo); + return sigdelset(set, signo); #else Y_UNUSED(set); Y_UNUSED(signo); - return 0; + return 0; #endif } @@ -62,7 +62,7 @@ inline int SigIsMember(const sigset_t* set, int signo) { #else Y_UNUSED(set); Y_UNUSED(signo); - return 0; + return 0; #endif } diff --git a/util/system/sys_alloc.h b/util/system/sys_alloc.h index 4221a28f8c..1c23ea291d 100644 --- a/util/system/sys_alloc.h +++ b/util/system/sys_alloc.h @@ -12,11 +12,11 @@ inline void* y_allocate(size_t n) { throw std::bad_alloc(); } - return r; + return r; } inline void y_deallocate(void* p) { - free(p); + free(p); } /** @@ -39,5 +39,5 @@ inline void* y_reallocate(void* p, size_t new_sz) { throw std::bad_alloc(); } - return r; + return r; } diff --git a/util/system/sysstat.cpp b/util/system/sysstat.cpp index db3338b02e..2cc5aa7793 100644 --- a/util/system/sysstat.cpp +++ b/util/system/sysstat.cpp @@ -1,47 +1,47 @@ -#include "sysstat.h" - -#ifdef _win_ - +#include "sysstat.h" + +#ifdef _win_ + #include "winint.h" #include <errno.h> - -int Chmod(const char* fname, int mode) { - if (!fname) { - errno = EINVAL; - return -1; - } - ui32 fAttr = ::GetFileAttributesA(fname); + +int Chmod(const char* fname, int mode) { + if (!fname) { + errno = EINVAL; + return -1; + } + ui32 fAttr = ::GetFileAttributesA(fname); if (fAttr == 0xffffffff) - return -1; - if (mode & _S_IWRITE) { - fAttr &= ~FILE_ATTRIBUTE_READONLY; - } else { - fAttr |= FILE_ATTRIBUTE_READONLY; - } - if (!::SetFileAttributesA(fname, fAttr)) { - return -1; - } - return 0; -} - -int Mkdir(const char* path, int /*mode*/) { - errno = 0; - if (!path) { - errno = EINVAL; - return -1; - } + return -1; + if (mode & _S_IWRITE) { + fAttr &= ~FILE_ATTRIBUTE_READONLY; + } else { + fAttr |= FILE_ATTRIBUTE_READONLY; + } + if (!::SetFileAttributesA(fname, fAttr)) { + return -1; + } + return 0; +} + +int Mkdir(const char* path, int /*mode*/) { + errno = 0; + if (!path) { + errno = EINVAL; + return -1; + } if (!CreateDirectoryA(path, (LPSECURITY_ATTRIBUTES) nullptr)) { - ui32 errCode = GetLastError(); - if (errCode == ERROR_ALREADY_EXISTS) { - errno = EEXIST; - } else if (errCode == ERROR_PATH_NOT_FOUND) { - errno = ENOENT; - } else { - errno = EINVAL; - } - return -1; - } - return 0; -} - -#endif + ui32 errCode = GetLastError(); + if (errCode == ERROR_ALREADY_EXISTS) { + errno = EEXIST; + } else if (errCode == ERROR_PATH_NOT_FOUND) { + errno = ENOENT; + } else { + errno = EINVAL; + } + return -1; + } + return 0; +} + +#endif diff --git a/util/system/sysstat.h b/util/system/sysstat.h index b7c424c11b..1f4ac613be 100644 --- a/util/system/sysstat.h +++ b/util/system/sysstat.h @@ -1,52 +1,52 @@ -#pragma once - -#include "defaults.h" -#include <sys/stat.h> - -#ifdef _win_ +#pragma once + +#include "defaults.h" +#include <sys/stat.h> + +#ifdef _win_ #define S_IRUSR _S_IREAD #define S_IWUSR _S_IWRITE #define S_IXUSR _S_IEXEC #define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR) - + #define S_IRGRP _S_IREAD #define S_IWGRP _S_IWRITE #define S_IXGRP _S_IEXEC #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP) - + #define S_IROTH _S_IREAD #define S_IWOTH _S_IWRITE #define S_IXOTH _S_IEXEC #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH) -#endif - -int Chmod(const char* fname, int mode); -int Umask(int mode); - +#endif + +int Chmod(const char* fname, int mode); +int Umask(int mode); + static constexpr int MODE0777 = (S_IRWXU | S_IRWXG | S_IRWXO); static constexpr int MODE0775 = (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); static constexpr int MODE0755 = (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); - -int Mkdir(const char* path, int mode); - -/* uff... mkfifo(...) is not used now */ - -#ifdef _unix_ -inline int Chmod(const char* fname, int mode) { - return ::chmod(fname, mode); -} -inline int Umask(int mode) { - return ::umask(mode); -} -inline int Mkdir(const char* path, int mode) { - return ::mkdir(path, mode); -} -#endif - -#ifdef _win_ -inline int Umask(int /*mode*/) { - /* The only thing this method could make is to set FILE_ATTRIBUTE_READONLY on a handle from 'int open(...)', - but open() is deprecated. */ - return 0; -} -#endif + +int Mkdir(const char* path, int mode); + +/* uff... mkfifo(...) is not used now */ + +#ifdef _unix_ +inline int Chmod(const char* fname, int mode) { + return ::chmod(fname, mode); +} +inline int Umask(int mode) { + return ::umask(mode); +} +inline int Mkdir(const char* path, int mode) { + return ::mkdir(path, mode); +} +#endif + +#ifdef _win_ +inline int Umask(int /*mode*/) { + /* The only thing this method could make is to set FILE_ATTRIBUTE_READONLY on a handle from 'int open(...)', + but open() is deprecated. */ + return 0; +} +#endif diff --git a/util/system/thread.cpp b/util/system/thread.cpp index 6236746c2d..0a50954446 100644 --- a/util/system/thread.cpp +++ b/util/system/thread.cpp @@ -89,7 +89,7 @@ namespace { inline TId SystemThreadId() const noexcept { #if _WIN32_WINNT < 0x0502 - return (TId)ThreadId; + return (TId)ThreadId; #else return (TId)GetThreadId(Handle); #endif @@ -106,7 +106,7 @@ namespace { ::CloseHandle(Handle); } - static ui32 __stdcall Proxy(void* ptr) { + static ui32 __stdcall Proxy(void* ptr) { NTls::TCleaner cleaner; (void)cleaner; @@ -121,7 +121,7 @@ namespace { p->Result = p->Proc(p->Data); } - return 0; + return 0; } inline void Start() { @@ -148,7 +148,7 @@ namespace { }; using TThreadBase = TWinThread; -#else +#else //unix #define PCHECK(x, y) \ @@ -273,8 +273,8 @@ private: TThread::TThread(const TParams& p) : Impl_(new TImpl(p)) { -} - +} + TThread::TThread(TThreadProc threadProc, void* param) : Impl_(new TImpl(TParams(threadProc, param))) { diff --git a/util/system/thread.h b/util/system/thread.h index a6e8abdb5b..6d205ff7b4 100644 --- a/util/system/thread.h +++ b/util/system/thread.h @@ -7,7 +7,7 @@ #include <util/generic/ptr.h> #include <util/generic/string.h> -#include "defaults.h" +#include "defaults.h" #include "progname.h" bool SetHighestThreadPriority(); diff --git a/util/system/tls.h b/util/system/tls.h index 3c4f56dbeb..bcf8ebeb2a 100644 --- a/util/system/tls.h +++ b/util/system/tls.h @@ -4,7 +4,7 @@ #include <util/generic/ptr.h> #include <util/generic/noncopyable.h> - + #include <new> #if defined(_darwin_) @@ -124,7 +124,7 @@ #define Y_THREAD(T) ::NTls::TValue<T> #define Y_STATIC_THREAD(T) static Y_THREAD(T) - + // gcc and msvc support automatic tls for POD types #if defined(Y_DISABLE_THRKEY_OPTIMIZATION) // nothing to do @@ -169,8 +169,8 @@ namespace NTls { class TImpl; THolder<TImpl> Impl_; }; - - struct TCleaner { + + struct TCleaner { inline ~TCleaner() { TKey::Cleanup(); } @@ -220,7 +220,7 @@ namespace NTls { : Constructor_(new TDefaultConstructor()) , Key_(Dtor) { - } + } template <class T1> inline TValue(const T1& value) @@ -236,7 +236,7 @@ namespace NTls { inline operator const T&() const { return Get(); - } + } inline operator T&() { return Get(); diff --git a/util/system/user.cpp b/util/system/user.cpp index 83e89ea0a8..fe76d779c2 100644 --- a/util/system/user.cpp +++ b/util/system/user.cpp @@ -25,7 +25,7 @@ TString GetUsername() { for (;;) { #if defined(_win_) DWORD len = (DWORD)Min(nameBuf.Size(), size_t(32767)); - if (!GetUserNameA(nameBuf.Data(), &len)) { + if (!GetUserNameA(nameBuf.Data(), &len)) { DWORD err = GetLastError(); if ((err == ERROR_INSUFFICIENT_BUFFER) && (nameBuf.Size() <= 32767)) nameBuf = TTempBuf((size_t)len); diff --git a/util/system/utime.h b/util/system/utime.h index 1c52e6614d..0afc61719f 100644 --- a/util/system/utime.h +++ b/util/system/utime.h @@ -1,6 +1,6 @@ -#pragma once - -#include "defaults.h" - +#pragma once + +#include "defaults.h" + int TouchFile(const char* filePath); int SetModTime(const char* filePath, time_t modtime, time_t actime); diff --git a/util/system/winint.h b/util/system/winint.h index ebeaefb3d2..6eed1df32e 100644 --- a/util/system/winint.h +++ b/util/system/winint.h @@ -1,9 +1,9 @@ -#pragma once - +#pragma once + #include "platform.h" - -#if defined(_win_) - + +#if defined(_win_) + #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif @@ -13,7 +13,7 @@ #endif #include <windows.h> - + #undef GetFreeSpace #undef LoadImage #undef GetMessage @@ -33,11 +33,11 @@ #undef GetProp #undef SetProp #undef RemoveProp - + #undef IGNORE #undef ERROR #undef TRANSPARENT - + #undef CM_NONE -#endif +#endif diff --git a/util/system/yassert.h b/util/system/yassert.h index 529823440c..f256cdf5d7 100644 --- a/util/system/yassert.h +++ b/util/system/yassert.h @@ -56,7 +56,7 @@ inline void YaDebugBreak() { #undef Y_ASSERT -#if !defined(NDEBUG) && !defined(__GCCXML__) +#if !defined(NDEBUG) && !defined(__GCCXML__) #define Y_ASSERT(a) \ do { \ try { \ diff --git a/util/thread/pool.cpp b/util/thread/pool.cpp index 05fad02e9b..a83b996daa 100644 --- a/util/thread/pool.cpp +++ b/util/thread/pool.cpp @@ -1,7 +1,7 @@ #include <atomic> #include <util/system/defaults.h> - + #if defined(_unix_) #include <pthread.h> #endif @@ -21,7 +21,7 @@ #include <util/system/atomic.h> #include <util/system/condvar.h> #include <util/system/thread.h> - + #include <util/datetime/base.h> #include "factory.h" @@ -122,7 +122,7 @@ public: } Queue.Push(obj); - } + } QueuePushCond.Signal(); @@ -357,8 +357,8 @@ bool TThreadPool::Add(IObjectInQueue* obj) { } return Impl_->Add(obj); -} - +} + void TThreadPool::Start(size_t thrnum, size_t maxque) { Impl_.Reset(new TImpl(this, thrnum, maxque, Params)); } diff --git a/util/thread/pool.h b/util/thread/pool.h index d1ea3a67cb..e6c5a34625 100644 --- a/util/thread/pool.h +++ b/util/thread/pool.h @@ -1,5 +1,5 @@ #pragma once - + #include "fwd.h" #include "factory.h" @@ -311,8 +311,8 @@ public: private: class TImpl; THolder<TImpl> Impl_; -}; - +}; + /** Behave like TThreadPool or TAdaptiveThreadPool, choosen by thrnum parameter of Start() */ class TSimpleThreadPool: public TThreadPoolBase { public: diff --git a/util/ya.make b/util/ya.make index 6ebe7e40cf..4574f74b8a 100644 --- a/util/ya.make +++ b/util/ya.make @@ -9,11 +9,11 @@ NO_UTIL() # stream # string -PEERDIR( +PEERDIR( util/charset contrib/libs/zlib contrib/libs/double-conversion -) +) PEERDIR( contrib/libs/libc_compat |