MCPcopy Create free account
hub / github.com/Simple-XX/SimpleKernel / Seek

Function Seek

src/filesystem/vfs/seek.cpp:13–56  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11namespace vfs {
12
13auto Seek(File* file, int64_t offset, SeekWhence whence) -> Expected<uint64_t> {
14 if (file == nullptr) {
15 return std::unexpected(Error(ErrorCode::kInvalidArgument));
16 }
17
18 LockGuard<SpinLock> guard(GetVfsState().vfs_lock_);
19 if (file->ops != nullptr) {
20 return file->ops->Seek(file, offset, whence);
21 }
22
23 // 默认实现
24 uint64_t new_offset = file->offset;
25
26 switch (whence) {
27 case SeekWhence::kSet:
28 if (offset < 0) {
29 return std::unexpected(Error(ErrorCode::kInvalidArgument));
30 }
31 new_offset = static_cast<uint64_t>(offset);
32 break;
33 case SeekWhence::kCur:
34 if (offset < 0 && static_cast<uint64_t>(-offset) > file->offset) {
35 return std::unexpected(Error(ErrorCode::kInvalidArgument));
36 }
37 new_offset =
38 static_cast<uint64_t>(static_cast<int64_t>(file->offset) + offset);
39 break;
40 case SeekWhence::kEnd:
41 if (file->inode == nullptr) {
42 return std::unexpected(Error(ErrorCode::kFsCorrupted));
43 }
44 if (offset < 0 && static_cast<uint64_t>(-offset) > file->inode->size) {
45 return std::unexpected(Error(ErrorCode::kInvalidArgument));
46 }
47 new_offset = static_cast<uint64_t>(
48 static_cast<int64_t>(file->inode->size) + offset);
49 break;
50 default:
51 return std::unexpected(Error(ErrorCode::kInvalidArgument));
52 }
53
54 file->offset = new_offset;
55 return new_offset;
56}
57
58} // namespace vfs

Callers 1

ramfs_testFunction · 0.85

Calls 3

ErrorClass · 0.85
GetVfsStateFunction · 0.85
SeekMethod · 0.45

Tested by 1

ramfs_testFunction · 0.68