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/system | |
parent | b036a557f285146e5e35d4213e29a094ab907bcf (diff) | |
download | ydb-980edcd3304699edf9d4e4d6a656e585028e2a72.tar.gz |
Restoring authorship annotation for <leo@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system')
56 files changed, 991 insertions, 991 deletions
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 { \ |