Skip to content

Commit 26777a8

Browse files
authored
Revert "Convert VirtualFileSystem tryGetCurrent() to current()" (#4450)
1 parent caf5335 commit 26777a8

File tree

3 files changed

+45
-23
lines changed

3 files changed

+45
-23
lines changed

src/workerd/api/filesystem.c++

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ 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 = workerd::VirtualFileSystem::current(js);
139+
auto& vfs = JSG_REQUIRE_NONNULL(
140+
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
140141
KJ_SWITCH_ONEOF(pathOrFd) {
141142
KJ_CASE_ONEOF(path, FilePath) {
142143
NormalizedFilePath normalizedPath(kj::mv(path));
@@ -187,7 +188,8 @@ kj::Maybe<Stat> FileSystemModule::stat(
187188

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

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

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

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

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

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

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

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

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

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

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

575585
jsg::BufferSource FileSystemModule::readAll(jsg::Lock& js, kj::OneOf<int, FilePath> pathOrFd) {
576-
auto& vfs = workerd::VirtualFileSystem::current(js);
586+
auto& vfs = JSG_REQUIRE_NONNULL(
587+
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
577588
KJ_SWITCH_ONEOF(pathOrFd) {
578589
KJ_CASE_ONEOF(path, FilePath) {
579590
NormalizedFilePath normalized(kj::mv(path));
@@ -642,7 +653,8 @@ uint32_t FileSystemModule::writeAll(jsg::Lock& js,
642653
kj::OneOf<int, FilePath> pathOrFd,
643654
jsg::BufferSource data,
644655
WriteAllOptions options) {
645-
auto& vfs = workerd::VirtualFileSystem::current(js);
656+
auto& vfs = JSG_REQUIRE_NONNULL(
657+
workerd::VirtualFileSystem::tryGetCurrent(js), Error, "No current virtual file system"_kj);
646658

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

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

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

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

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

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

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

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

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

13701387
jsg::Promise<jsg::Ref<FileSystemDirectoryHandle>> StorageManager::getDirectory(
13711388
jsg::Lock& js, const jsg::TypeHandler<jsg::Ref<jsg::DOMException>>& exception) {
1372-
auto& vfs = workerd::VirtualFileSystem::current(js);
1373-
return js.resolvedPromise(js.alloc<FileSystemDirectoryHandle>(jsg::USVString(), vfs.getRoot(js)));
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)));
13741396
}
13751397

13761398
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-
const VirtualFileSystem& VirtualFileSystem::current(jsg::Lock&) {
1146+
kj::Maybe<const VirtualFileSystem&> VirtualFileSystem::tryGetCurrent(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::current():
57+
// We can access the filesystem using VirtualFileSystem::tryGetCurrent():
5858
//
5959
// ```cpp
6060
// jsg::Lock& js = ...
61-
// auto& vfs = VirtualFileSystem::current(js);
61+
// KJ_IF_SOME(vfs, VirtualFileSystem::tryGetCurrent(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 const VirtualFileSystem& current(jsg::Lock&) KJ_WARN_UNUSED_RESULT;
521+
static kj::Maybe<const VirtualFileSystem&> tryGetCurrent(jsg::Lock&) KJ_WARN_UNUSED_RESULT;
522522

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

0 commit comments

Comments
 (0)