SimpleKernel 1.17.0
Loading...
Searching...
No Matches
close.cpp
Go to the documentation of this file.
1
5#include <etl/memory.h>
6
7#include "filesystem.hpp"
8#include "kernel_log.hpp"
9#include "kstd_cstring"
10#include "spinlock.hpp"
11#include "vfs_internal.hpp"
12
13namespace vfs {
14
15auto Close(File* file) -> Expected<void> {
16 if (file == nullptr) {
17 return std::unexpected(Error(ErrorCode::kInvalidArgument));
18 }
19
20 LockGuard<SpinLock> guard(GetVfsState().vfs_lock_);
21 if (file->ops != nullptr) {
22 auto result = file->ops->Close(file);
23 if (!result.has_value()) {
24 return result;
25 }
26 }
27
28 if (file->dentry != nullptr && file->dentry->ref_count > 0) {
29 file->dentry->ref_count--;
30 }
31
32 etl::unique_ptr<File> file_guard(file);
33 return {};
34}
35
36} // namespace vfs
RAII 风格的锁守卫模板类
Definition spinlock.hpp:131
@ kInvalidArgument
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
auto Close(File *file) -> Expected< void >
关闭文件
Definition close.cpp:15
auto GetVfsState() -> VfsState &
Definition vfs.cpp:17
错误类型,用于 std::expected
Definition expected.hpp:343
File — 打开的文件实例(每次 open 产生一个)
Definition vfs.hpp:65