SimpleKernel 1.17.0
Loading...
Searching...
No Matches
write.cpp
Go to the documentation of this file.
1
5#include "filesystem.hpp"
6#include "kernel_log.hpp"
7#include "kstd_cstring"
8#include "spinlock.hpp"
9#include "vfs_internal.hpp"
10
11namespace vfs {
12
13auto Write(File* file, const void* buf, size_t count) -> Expected<size_t> {
14 if (file == nullptr || buf == nullptr) {
15 return std::unexpected(Error(ErrorCode::kInvalidArgument));
16 }
17
18 // 检查写入权限
19 LockGuard<SpinLock> guard(GetVfsState().vfs_lock_);
20 if ((file->flags & OpenFlags::kOWriteOnly) == 0U &&
21 (file->flags & OpenFlags::kOReadWrite) == 0U) {
22 return std::unexpected(Error(ErrorCode::kFsPermissionDenied));
23 }
24
25 if (file->ops == nullptr) {
26 return std::unexpected(Error(ErrorCode::kDeviceNotSupported));
27 }
28
29 return file->ops->Write(file, buf, count);
30}
31
32} // namespace vfs
RAII 风格的锁守卫模板类
Definition spinlock.hpp:131
@ kFsPermissionDenied
@ kInvalidArgument
@ kDeviceNotSupported
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
auto Write(File *file, const void *buf, size_t count) -> Expected< size_t >
向文件写入数据
Definition write.cpp:13
auto GetVfsState() -> VfsState &
Definition vfs.cpp:17
错误类型,用于 std::expected
Definition expected.hpp:343
File — 打开的文件实例(每次 open 产生一个)
Definition vfs.hpp:65