file_utils.hpp
1 /*
2  * Copyright (c) 2025, NVIDIA CORPORATION.
3  *
4  * 根据 Apache 许可证 2.0 版(以下简称“许可证”)获得许可;
5  * 除非遵守许可证的规定,否则您不得使用此文件。
6  * 您可以在以下位置获取许可证的副本:
7  *
8  * https://apache.ac.cn/licenses/LICENSE-2.0
9  *
10  * 除非适用法律要求或书面同意,否则根据许可证分发的软件是
11  * 按“原样”分发的,没有任何明示或暗示的保证或条件。
12  * 有关特定语言的权限和
13  * 许可证下的限制,请参阅许可证。
14  */
15 /*
16  */#pragma once
17 
18 #include <optional>
19 #include <string>
20 
21 #include <kvikio/shim/cufile_h_wrapper.hpp>
22 
23 namespace kvikio {
27 class FileWrapper {
28  private
29  int _fd{-1};
30 
31  public
40  FileWrapper(std::string const& file_path, std::string const& flags, bool o_direct, mode_t mode);
41 
45  FileWrapper() noexcept = default;
46 
47  ~FileWrapper() noexcept;
48  FileWrapper(FileWrapper const&) = delete;
49  FileWrapper& operator=(FileWrapper const&) = delete;
50  FileWrapper(FileWrapper&& o) noexcept;
51  FileWrapper& operator=(FileWrapper&& o) noexcept;
52 
61  void open(std::string const& file_path, std::string const& flags, bool o_direct, mode_t mode);
62 
68  bool opened() const noexcept;
69 
73  void close() noexcept;
74 
80  int fd() const noexcept;
81 };
82 
87  private
88  CUfileHandle_t _handle{};
89  bool _registered{false};
90 
91  public
92  CUFileHandleWrapper() noexcept = default;
93  ~CUFileHandleWrapper() noexcept;
95  CUFileHandleWrapper& operator=(CUFileHandleWrapper const&) = delete;
97  CUFileHandleWrapper& operator=(CUFileHandleWrapper&& o) noexcept;
98 
106  std::optional<CUfileError_t> register_handle(int fd) noexcept;
107 
113  bool registered() const noexcept;
114 
120  CUfileHandle_t handle() const noexcept;
121 
125  void unregister_handle() noexcept;
126 };
127 
138 int open_fd_parse_flags(std::string const& flags, bool o_direct);
139 
148 int open_fd(std::string const& file_path, std::string const& flags, bool o_direct, mode_t mode);
149 
155 [[nodiscard]] int open_flags(int fd);
156 
163 [[nodiscard]] std::size_t get_file_size(int file_descriptor);
164 
165 } // namespace kvikio
为 cuFile 句柄提供 RAII 的类。
bool registered() const noexcept
检查句柄是否已注册。
std::optional< CUfileError_t > register_handle(int fd) noexcept
给定文件描述符,注册文件句柄。
为文件处理提供 RAII 的类。
void open(std::string const &file_path, std::string const &flags, bool o_direct, mode_t mode)
使用 open(2) 打开文件
bool opened() const noexcept
检查文件是否已打开。
void close() noexcept
如果文件已打开则关闭;否则不执行任何操作。
int fd() const noexcept
返回文件描述符。
FileWrapper() noexcept=default
构造一个空的文件包装对象,不打开文件。
FileWrapper(std::string const &file_path, std::string const &flags, bool o_direct, mode_t mode)
打开文件。
KvikIO 命名空间。
定义: batch.hpp:27
std::size_t get_file_size(int file_descriptor)
从文件描述符获取文件大小 fstat(3)
int open_fd(std::string const &file_path, std::string const &flags, bool o_direct, mode_t mode)
使用 open(2) 打开文件
int open_fd_parse_flags(std::string const &flags, bool o_direct)
解析以字符串形式给定的文件打开标志并返回 oflags。
int open_flags(int fd)
获取文件描述符的标志 (参见 open(2))