Skip to content

Commit 8acabe8

Browse files
authored
Convert VirtualFileSystem tryGetCurrent() to current() (#4451)
1 parent f17e8f1 commit 8acabe8

File tree

3 files changed

+23
-45
lines changed

3 files changed

+23
-45
lines changed

src/workerd/api/filesystem.c++

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ Stat::Stat(const workerd::Stat& stat)
136136

137137
kj::Maybe<Stat> FileSystemModule::stat(
138138
jsg::Lock& js, kj::OneOf<int, FilePath> pathOrFd, StatOptions options) {
139-
auto& vfs = JSG_REQUIRE_NONNULL(
140-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
139+
auto& vfs = workerd::VirtualFileSystem::current(js);
141140
KJ_SWITCH_ONEOF(pathOrFd) {
142141
KJ_CASE_ONEOF(path, FilePath) {
143142
NormalizedFilePath normalizedPath(kj::mv(path));
@@ -188,8 +187,7 @@ kj::Maybe<Stat> FileSystemModule::stat(
188187

189188
void FileSystemModule::setLastModified(
190189
jsg::Lock& js, kj::OneOf<int, FilePath> pathOrFd, kj::Date lastModified, StatOptions options) {
191-
auto& vfs = JSG_REQUIRE_NONNULL(
192-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
190+
auto& vfs = workerd::VirtualFileSystem::current(js);
193191
KJ_SWITCH_ONEOF(pathOrFd) {
194192
KJ_CASE_ONEOF(path, FilePath) {
195193
NormalizedFilePath normalizedPath(kj::mv(path));
@@ -249,8 +247,7 @@ void FileSystemModule::setLastModified(
249247
}
250248

251249
void FileSystemModule::truncate(jsg::Lock& js, kj::OneOf<int, FilePath> pathOrFd, uint32_t size) {
252-
auto& vfs = JSG_REQUIRE_NONNULL(
253-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
250+
auto& vfs = workerd::VirtualFileSystem::current(js);
254251
KJ_SWITCH_ONEOF(pathOrFd) {
255252
KJ_CASE_ONEOF(path, FilePath) {
256253
NormalizedFilePath normalizedPath(kj::mv(path));
@@ -305,8 +302,7 @@ void FileSystemModule::truncate(jsg::Lock& js, kj::OneOf<int, FilePath> pathOrFd
305302
}
306303

307304
kj::String FileSystemModule::readLink(jsg::Lock& js, FilePath path, ReadLinkOptions options) {
308-
auto& vfs = JSG_REQUIRE_NONNULL(
309-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
305+
auto& vfs = workerd::VirtualFileSystem::current(js);
310306
NormalizedFilePath normalizedPath(kj::mv(path));
311307
KJ_IF_SOME(node, vfs.resolve(js, normalizedPath, {.followLinks = false})) {
312308
KJ_SWITCH_ONEOF(node) {
@@ -340,8 +336,7 @@ kj::String FileSystemModule::readLink(jsg::Lock& js, FilePath path, ReadLinkOpti
340336

341337
void FileSystemModule::link(jsg::Lock& js, FilePath from, FilePath to, LinkOptions options) {
342338
// The from argument is where we are creating the link, while the to is the target.
343-
auto& vfs = JSG_REQUIRE_NONNULL(
344-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
339+
auto& vfs = workerd::VirtualFileSystem::current(js);
345340
NormalizedFilePath normalizedFrom(kj::mv(from));
346341
NormalizedFilePath normalizedTo(kj::mv(to));
347342

@@ -412,8 +407,7 @@ void FileSystemModule::link(jsg::Lock& js, FilePath from, FilePath to, LinkOptio
412407
}
413408

414409
void FileSystemModule::unlink(jsg::Lock& js, FilePath path) {
415-
auto& vfs = JSG_REQUIRE_NONNULL(
416-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
410+
auto& vfs = workerd::VirtualFileSystem::current(js);
417411
NormalizedFilePath normalizedPath(kj::mv(path));
418412
const jsg::Url& url = normalizedPath;
419413
auto relative = url.getRelative();
@@ -453,8 +447,7 @@ void FileSystemModule::unlink(jsg::Lock& js, FilePath path) {
453447
}
454448

455449
int FileSystemModule::open(jsg::Lock& js, FilePath path, OpenOptions options) {
456-
auto& vfs = JSG_REQUIRE_NONNULL(
457-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
450+
auto& vfs = workerd::VirtualFileSystem::current(js);
458451
NormalizedFilePath normalizedPath(kj::mv(path));
459452
KJ_SWITCH_ONEOF(vfs.openFd(js, normalizedPath,
460453
workerd::VirtualFileSystem::OpenOptions{
@@ -475,15 +468,13 @@ int FileSystemModule::open(jsg::Lock& js, FilePath path, OpenOptions options) {
475468
}
476469

477470
void FileSystemModule::close(jsg::Lock& js, int fd) {
478-
auto& vfs = JSG_REQUIRE_NONNULL(
479-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
471+
auto& vfs = workerd::VirtualFileSystem::current(js);
480472
vfs.closeFd(js, fd);
481473
}
482474

483475
uint32_t FileSystemModule::write(
484476
jsg::Lock& js, int fd, kj::Array<jsg::BufferSource> data, WriteOptions options) {
485-
auto& vfs = JSG_REQUIRE_NONNULL(
486-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
477+
auto& vfs = workerd::VirtualFileSystem::current(js);
487478

488479
KJ_IF_SOME(opened, vfs.tryGetFd(js, fd)) {
489480
static const auto getPosition = [](jsg::Lock& js, auto& opened, auto& file,
@@ -540,8 +531,7 @@ uint32_t FileSystemModule::write(
540531

541532
uint32_t FileSystemModule::read(
542533
jsg::Lock& js, int fd, kj::Array<jsg::BufferSource> data, WriteOptions options) {
543-
auto& vfs = JSG_REQUIRE_NONNULL(
544-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
534+
auto& vfs = workerd::VirtualFileSystem::current(js);
545535
KJ_IF_SOME(opened, vfs.tryGetFd(js, fd)) {
546536
if (!opened->read) {
547537
throwUVException(js, UV_EBADF, "read"_kj);
@@ -583,8 +573,7 @@ uint32_t FileSystemModule::read(
583573
}
584574

585575
jsg::BufferSource FileSystemModule::readAll(jsg::Lock& js, kj::OneOf<int, FilePath> pathOrFd) {
586-
auto& vfs = JSG_REQUIRE_NONNULL(
587-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
576+
auto& vfs = workerd::VirtualFileSystem::current(js);
588577
KJ_SWITCH_ONEOF(pathOrFd) {
589578
KJ_CASE_ONEOF(path, FilePath) {
590579
NormalizedFilePath normalized(kj::mv(path));
@@ -653,8 +642,7 @@ uint32_t FileSystemModule::writeAll(jsg::Lock& js,
653642
kj::OneOf<int, FilePath> pathOrFd,
654643
jsg::BufferSource data,
655644
WriteAllOptions options) {
656-
auto& vfs = JSG_REQUIRE_NONNULL(
657-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
645+
auto& vfs = workerd::VirtualFileSystem::current(js);
658646

659647
if (data.size() > kMax) {
660648
throwUVException(js, UV_EFBIG, "writeAll"_kj);
@@ -814,8 +802,7 @@ uint32_t FileSystemModule::writeAll(jsg::Lock& js,
814802
void FileSystemModule::renameOrCopy(
815803
jsg::Lock& js, FilePath src, FilePath dest, RenameOrCopyOptions options) {
816804
// The source must exist, the destination must not.
817-
auto& vfs = JSG_REQUIRE_NONNULL(
818-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
805+
auto& vfs = workerd::VirtualFileSystem::current(js);
819806
NormalizedFilePath normalizedSrc(kj::mv(src));
820807
NormalizedFilePath normalizedDest(kj::mv(dest));
821808

@@ -931,8 +918,7 @@ void FileSystemModule::renameOrCopy(
931918

932919
jsg::Optional<kj::String> FileSystemModule::mkdir(
933920
jsg::Lock& js, FilePath path, MkdirOptions options) {
934-
auto& vfs = JSG_REQUIRE_NONNULL(
935-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
921+
auto& vfs = workerd::VirtualFileSystem::current(js);
936922
NormalizedFilePath normalizedPath(kj::mv(path));
937923
const jsg::Url& url = normalizedPath;
938924

@@ -1077,8 +1063,7 @@ jsg::Optional<kj::String> FileSystemModule::mkdir(
10771063

10781064
void FileSystemModule::rm(jsg::Lock& js, FilePath path, RmOptions options) {
10791065
// TODO(node-fs): Implement the force option.
1080-
auto& vfs = JSG_REQUIRE_NONNULL(
1081-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
1066+
auto& vfs = workerd::VirtualFileSystem::current(js);
10821067
NormalizedFilePath normalizedPath(kj::mv(path));
10831068
const jsg::Url& url = normalizedPath;
10841069
auto relative = url.getRelative();
@@ -1193,8 +1178,7 @@ void readdirImpl(jsg::Lock& js,
11931178

11941179
kj::Array<FileSystemModule::DirEntHandle> FileSystemModule::readdir(
11951180
jsg::Lock& js, FilePath path, ReadDirOptions options) {
1196-
auto& vfs = JSG_REQUIRE_NONNULL(
1197-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
1181+
auto& vfs = workerd::VirtualFileSystem::current(js);
11981182
NormalizedFilePath normalizedPath(kj::mv(path));
11991183

12001184
KJ_IF_SOME(node, vfs.resolve(js, normalizedPath, {.followLinks = false})) {
@@ -1221,8 +1205,7 @@ kj::Array<FileSystemModule::DirEntHandle> FileSystemModule::readdir(
12211205
}
12221206

12231207
jsg::Ref<FileFdHandle> FileFdHandle::constructor(jsg::Lock& js, int fd) {
1224-
auto& vfs = JSG_REQUIRE_NONNULL(
1225-
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
1208+
auto& vfs = workerd::VirtualFileSystem::current(js);
12261209
return js.alloc<FileFdHandle>(js, vfs, fd);
12271210
}
12281211

@@ -1386,13 +1369,8 @@ jsg::Ref<jsg::DOMException> fsErrorToDomException(jsg::Lock& js, workerd::FsErro
13861369

13871370
jsg::Promise<jsg::Ref<FileSystemDirectoryHandle>> StorageManager::getDirectory(
13881371
jsg::Lock& js, const jsg::TypeHandler<jsg::Ref<jsg::DOMException>>& exception) {
1389-
KJ_IF_SOME(vfs, workerd::VirtualFileSystem::tryGetCurrent(js)) {
1390-
return js.resolvedPromise(
1391-
js.alloc<FileSystemDirectoryHandle>(jsg::USVString(), vfs.getRoot(js)));
1392-
}
1393-
1394-
auto ex = js.domException(kj::str("SecurityError"), kj::str("No current virtual file system"));
1395-
return js.rejectedPromise<jsg::Ref<FileSystemDirectoryHandle>>(exception.wrap(js, kj::mv(ex)));
1372+
auto& vfs = workerd::VirtualFileSystem::current(js);
1373+
return js.resolvedPromise(js.alloc<FileSystemDirectoryHandle>(jsg::USVString(), vfs.getRoot(js)));
13961374
}
13971375

13981376
FileSystemDirectoryHandle::FileSystemDirectoryHandle(

src/workerd/io/worker-fs.c++

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ kj::Rc<Directory> getLazyDirectoryImpl(kj::Function<kj::Rc<Directory>()> func) {
11431143
return kj::rc<LazyDirectory>(kj::mv(func));
11441144
}
11451145

1146-
kj::Maybe<const VirtualFileSystem&> VirtualFileSystem::tryGetCurrent(jsg::Lock&) {
1146+
const VirtualFileSystem& VirtualFileSystem::current(jsg::Lock&) {
11471147
// Note that the jsg::Lock& argument here is not actually used. We require
11481148
// that a jsg::Lock reference is passed in as proof that current() is called
11491149
// from within a valid isolate lock so that the Worker::Api::current()

src/workerd/io/worker-fs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454
// │ └── random
5555
// └── tmp
5656
//
57-
// We can access the filesystem using VirtualFileSystem::tryGetCurrent():
57+
// We can access the filesystem using VirtualFileSystem::current():
5858
//
5959
// ```cpp
6060
// jsg::Lock& js = ...
61-
// KJ_IF_SOME(vfs, VirtualFileSystem::tryGetCurrent(js)) {
61+
// auto& vfs = VirtualFileSystem::current(js);
6262
// // Resolve the root of the file system
6363
// KJ_IF_SOME(node, vfs.resolve(js, "file:///path/to/thing"_url)) {
6464
// KJ_SWITCH_ONEOF(node) {
@@ -518,7 +518,7 @@ class VirtualFileSystem {
518518
virtual const jsg::Url& getDevRoot() const KJ_WARN_UNUSED_RESULT = 0;
519519

520520
// Get the current virtual file system for the current isolate lock.
521-
static kj::Maybe<const VirtualFileSystem&> tryGetCurrent(jsg::Lock&) KJ_WARN_UNUSED_RESULT;
521+
static const VirtualFileSystem& current(jsg::Lock&) KJ_WARN_UNUSED_RESULT;
522522

523523
// ==========================================================================
524524
// File Descriptor support

0 commit comments

Comments
 (0)