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/file.h | |
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/file.h')
-rw-r--r-- | util/system/file.h | 154 |
1 files changed, 77 insertions, 77 deletions
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; |