@@ -597,8 +597,7 @@ int __syscall_mknodat(int dirfd, intptr_t path, int mode, int dev) {
597
597
OpenReturnMode::Nothing);
598
598
}
599
599
600
- static int
601
- doMkdir (path::ParsedParent parsed, int mode, backend_t backend = NullBackend) {
600
+ static int doMkdir (path::ParsedParent parsed, int mode) {
602
601
if (auto err = parsed.getError ()) {
603
602
return err;
604
603
}
@@ -624,41 +623,57 @@ doMkdir(path::ParsedParent parsed, int mode, backend_t backend = NullBackend) {
624
623
return -EACCES;
625
624
}
626
625
627
- // By default, the backend that the directory is created in is the same as
628
- // the parent directory. However, if a backend is passed as a parameter,
629
- // then that backend is used.
630
- if (!backend) {
631
- backend = parent->getBackend ();
626
+ if (!lockedParent.insertDirectory (childName, mode)) {
627
+ // TODO Receive a specific error code, and report it here. For now, report
628
+ // a generic error.
629
+ return -EIO;
632
630
}
633
631
634
- if (backend == parent->getBackend ()) {
635
- if (!lockedParent.insertDirectory (childName, mode)) {
636
- // TODO Receive a specific error code, and report it here. For now, report
637
- // a generic error.
638
- return -EIO;
639
- }
640
- } else {
641
- auto created = backend->createDirectory (mode);
642
- if (!created) {
643
- // TODO Receive a specific error code, and report it here. For now, report
644
- // a generic error.
645
- return -EIO;
646
- }
647
- [[maybe_unused]] bool mounted = lockedParent.mountChild (childName, created);
648
- assert (mounted);
632
+ // TODO: Check that the insertion is successful.
633
+
634
+ return 0 ;
635
+ }
636
+
637
+ int wasmfs_mount (const char * path, backend_t backend) {
638
+ path::ParsedParent parsed = path::parseParent (path);
639
+ if (auto err = parsed.getError ()) {
640
+ return err;
649
641
}
642
+ auto & [parent, childNameView] = parsed.getParentChild ();
643
+ auto lockedParent = parent->locked ();
644
+ std::string childName (childNameView);
650
645
651
- // TODO: Check that the insertion is successful.
646
+ // Child must exist and must be directory
647
+ auto child = lockedParent.getChild (childName);
648
+ if (!child) {
649
+ return -EEXIST;
650
+ }
651
+ if (!child->dynCast <Directory>()) {
652
+ return -ENOTDIR;
653
+ }
654
+
655
+ auto created = backend->createDirectory (0777 );
656
+ if (!created) {
657
+ // TODO Receive a specific error code, and report it here. For now, report
658
+ // a generic error.
659
+ return -EIO;
660
+ }
661
+ [[maybe_unused]] bool mounted = lockedParent.mountChild (childName, created);
662
+ assert (mounted);
652
663
653
664
return 0 ;
654
665
}
655
666
656
- // This function is exposed to users and allows users to specify a particular
657
- // backend that a directory should be created within.
658
- int wasmfs_create_directory (char * path, int mode, backend_t backend) {
659
- static_assert (std::is_same_v<decltype (doMkdir (0 , 0 , 0 )), int >,
667
+ // Legacy function, use wasmfs_mount instead.
668
+ int wasmfs_create_directory (const char * path, int mode, backend_t backend) {
669
+ static_assert (std::is_same_v<decltype (doMkdir (0 , 0 )), int >,
660
670
" unexpected conversion from result of doMkdir to int" );
661
- return doMkdir (path::parseParent (path), mode, backend);
671
+ int rtn = doMkdir (path::parseParent (path), mode);
672
+ if (rtn != 0 ) {
673
+ return rtn;
674
+ }
675
+
676
+ return wasmfs_mount (path, backend);
662
677
}
663
678
664
679
// TODO: Test this.
0 commit comments