file.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #pragma once
  2. #include "fhandle.h"
  3. #include "flock.h"
  4. #include <util/generic/flags.h>
  5. #include <util/generic/ptr.h>
  6. #include <util/generic/noncopyable.h>
  7. #include <cstdio>
  8. enum EOpenModeFlag {
  9. OpenExisting = 0, // Opens a file. It fails if the file does not exist.
  10. TruncExisting = 1, // Opens a file and truncates it to zero size. It fails if the file does not exist.
  11. OpenAlways = 2, // Opens a file, always. If a file does not exist, it creates a file.
  12. CreateNew = 3, // Creates a new file. It fails if a specified file exists.
  13. CreateAlways = 4, // Creates a new file, always. If a file exists, it overwrites the file.
  14. MaskCreation = 7,
  15. RdOnly = 8, // open for reading only
  16. WrOnly = 16, // open for writing only
  17. RdWr = 24, // open for reading and writing
  18. MaskRW = 24,
  19. Seq = 0x20, // file access is primarily sequential (POSIX_FADV_SEQUENTIAL)
  20. Direct = 0x40, // file is being opened with no system caching (Does not work as intended! See implementation)
  21. Temp = 0x80, // avoid writing data back to disk if sufficient cache memory is available (no op for linux)
  22. ForAppend = 0x100, // write appends data to the end of file (O_APPEND)
  23. Transient = 0x200, // actually, temporary file - 'delete on close' for windows, unlink after creation for unix
  24. NoReuse = 0x400, // no second access expected (POSIX_FADV_NOREUSE)
  25. CloseOnExec = 0x800, // set close-on-exec right at open (O_CLOEXEC)
  26. DirectAligned = 0x1000, // file is actually being opened with no system caching (may require buffer alignment) (O_DIRECT)
  27. Sync = 0x2000, // no write call will return before the data is transferred to the disk (O_SYNC)
  28. NoReadAhead = 0x4000, // no sequential access expected, opposite for Seq (POSIX_FADV_RANDOM)
  29. AXOther = 0x00010000,
  30. AWOther = 0x00020000,
  31. AROther = 0x00040000,
  32. AXGroup = 0x00100000,
  33. AWGroup = 0x00200000,
  34. ARGroup = 0x00400000,
  35. AXUser = 0x01000000,
  36. AWUser = 0x02000000,
  37. ARUser = 0x04000000,
  38. AX = AXUser | AXGroup | AXOther,
  39. AW = AWUser | AWGroup,
  40. AR = ARUser | ARGroup | AROther,
  41. ARW = AR | AW,
  42. AMask = 0x0FFF0000,
  43. };
  44. Y_DECLARE_FLAGS(EOpenMode, EOpenModeFlag)
  45. Y_DECLARE_OPERATORS_FOR_FLAGS(EOpenMode)
  46. TString DecodeOpenMode(ui32 openMode);
  47. enum SeekDir {
  48. sSet = 0,
  49. sCur = 1,
  50. sEnd = 2,
  51. };
  52. class TFileHandle: public TNonCopyable {
  53. public:
  54. constexpr TFileHandle() = default;
  55. /// Warning: takes ownership of fd, so closes it in destructor.
  56. inline TFileHandle(FHANDLE fd) noexcept
  57. : Fd_(fd)
  58. {
  59. }
  60. inline TFileHandle(TFileHandle&& other) noexcept
  61. : Fd_(other.Fd_)
  62. {
  63. other.Fd_ = INVALID_FHANDLE;
  64. }
  65. TFileHandle(const TString& fName, EOpenMode oMode) noexcept;
  66. inline ~TFileHandle() {
  67. Close();
  68. }
  69. bool Close() noexcept;
  70. inline FHANDLE Release() noexcept {
  71. FHANDLE ret = Fd_;
  72. Fd_ = INVALID_FHANDLE;
  73. return ret;
  74. }
  75. inline void Swap(TFileHandle& r) noexcept {
  76. DoSwap(Fd_, r.Fd_);
  77. }
  78. inline operator FHANDLE() const noexcept {
  79. return Fd_;
  80. }
  81. inline bool IsOpen() const noexcept {
  82. return Fd_ != INVALID_FHANDLE;
  83. }
  84. i64 GetPosition() const noexcept;
  85. i64 GetLength() const noexcept;
  86. i64 Seek(i64 offset, SeekDir origin) noexcept;
  87. bool Resize(i64 length) noexcept;
  88. bool Reserve(i64 length) noexcept;
  89. bool FallocateNoResize(i64 length) noexcept;
  90. bool ShrinkToFit() noexcept;
  91. bool Flush() noexcept;
  92. //flush data only, without file metadata
  93. bool FlushData() noexcept;
  94. i32 Read(void* buffer, ui32 byteCount) noexcept;
  95. i32 Write(const void* buffer, ui32 byteCount) noexcept;
  96. i32 Pread(void* buffer, ui32 byteCount, i64 offset) const noexcept;
  97. i32 Pwrite(const void* buffer, ui32 byteCount, i64 offset) const noexcept;
  98. int Flock(int op) noexcept;
  99. FHANDLE Duplicate() const noexcept;
  100. int Duplicate2Posix(int dstHandle) const noexcept;
  101. //dup2 - like semantics, return true on success
  102. bool LinkTo(const TFileHandle& fh) const noexcept;
  103. //very low-level methods
  104. bool SetDirect();
  105. void ResetDirect();
  106. /* Manual file cache management, length = 0 means "as much as possible" */
  107. //measure amount of cached data in bytes, returns -1 if failed
  108. i64 CountCache(i64 offset = 0, i64 length = 0) const noexcept;
  109. //read data into cache and optionally wait for completion
  110. void PrefetchCache(i64 offset = 0, i64 length = 0, bool wait = true) const noexcept;
  111. //remove clean and unused data from cache
  112. void EvictCache(i64 offset = 0, i64 length = 0) const noexcept;
  113. //flush unwritten data in this range and optionally wait for completion
  114. bool FlushCache(i64 offset = 0, i64 length = 0, bool wait = true) noexcept;
  115. private:
  116. FHANDLE Fd_ = INVALID_FHANDLE;
  117. };
  118. class TFile {
  119. public:
  120. TFile();
  121. /// Takes ownership of handle, so closes it when the last holder of descriptor dies.
  122. explicit TFile(FHANDLE fd);
  123. TFile(FHANDLE fd, const TString& fname);
  124. TFile(const TString& fName, EOpenMode oMode);
  125. ~TFile();
  126. void Close();
  127. const TString& GetName() const noexcept;
  128. i64 GetPosition() const noexcept;
  129. i64 GetLength() const noexcept;
  130. bool IsOpen() const noexcept;
  131. FHANDLE GetHandle() const noexcept;
  132. i64 Seek(i64 offset, SeekDir origin);
  133. void Resize(i64 length);
  134. void Reserve(i64 length);
  135. void FallocateNoResize(i64 length);
  136. void ShrinkToFit();
  137. void Flush();
  138. void FlushData();
  139. void LinkTo(const TFile& f) const;
  140. TFile Duplicate() const;
  141. // Reads up to 1 GB without retrying, returns -1 on error
  142. i32 RawRead(void* buf, size_t len);
  143. // Reads up to 1 GB without retrying, throws on error
  144. size_t ReadOrFail(void* buf, size_t len);
  145. // Retries incomplete reads until EOF, throws on error
  146. size_t Read(void* buf, size_t len);
  147. // Reads exactly len bytes, throws on premature EOF or error
  148. void Load(void* buf, size_t len);
  149. // Retries incomplete writes, will either write len bytes or throw
  150. void Write(const void* buf, size_t len);
  151. // Retries incomplete reads until EOF, throws on error
  152. size_t Pread(void* buf, size_t len, i64 offset) const;
  153. // Single pread call
  154. i32 RawPread(void* buf, ui32 len, i64 offset) const;
  155. // Reads exactly len bytes, throws on premature EOF or error
  156. void Pload(void* buf, size_t len, i64 offset) const;
  157. // Retries incomplete writes, will either write len bytes or throw
  158. void Pwrite(const void* buf, size_t len, i64 offset) const;
  159. void Flock(int op);
  160. //do not use, their meaning very platform-dependant
  161. void SetDirect();
  162. void ResetDirect();
  163. /* Manual file cache management, length = 0 means "as much as possible" */
  164. //measure amount of cached data in bytes, returns -1 if failed
  165. i64 CountCache(i64 offset = 0, i64 length = 0) const noexcept;
  166. //read data into cache and optionally wait for completion
  167. void PrefetchCache(i64 offset = 0, i64 length = 0, bool wait = true) const noexcept;
  168. //remove clean and unused data from cache, incomplete pages could stay
  169. void EvictCache(i64 offset = 0, i64 length = 0) const noexcept;
  170. //flush unwritten data in this range and optionally wait for completion
  171. void FlushCache(i64 offset = 0, i64 length = 0, bool wait = true);
  172. static TFile Temporary(const TString& prefix);
  173. static TFile ForAppend(const TString& path);
  174. private:
  175. class TImpl;
  176. TSimpleIntrusivePtr<TImpl> Impl_;
  177. };
  178. TFile Duplicate(FILE*);
  179. TFile Duplicate(int);
  180. bool PosixDisableReadAhead(FHANDLE fileHandle, void* addr) noexcept;