浏览代码

fix: mkdir with create_missing when some parents exist

There was an issue affecting mkdir requests with create_missing_parents
set to true which prevented the correct behavior if some of the listed
parents were already present. This commit ensures mkdir selects the
deepest existing node as the parent before proceeding.
KernelDeimos 4 月之前
父节点
当前提交
807c3ba5ec
共有 1 个文件被更改,包括 25 次插入3 次删除
  1. 25 3
      src/backend/src/filesystem/hl_operations/hl_mkdir.js

+ 25 - 3
src/backend/src/filesystem/hl_operations/hl_mkdir.js

@@ -386,13 +386,35 @@ class HLMkdir extends HLFilesystemOperation {
     }
 
     async _create_parents ({ parent_node }) {
-        const { values } = this;
+        const { context, values } = this;
         const { _path } = this.modules;
 
+        const fs = context.get('services').get('filesystem');
+
+        // Determine the deepest existing node
+        let deepest_existing = parent_node;
+        let remaining_path  = _path.dirname(values.path).split('/').filter(Boolean);
+        {
+            const parts = remaining_path.slice();
+            for (;;) {
+                if ( remaining_path.length === 0 ) {
+                    return deepest_existing;
+                }
+                const component = remaining_path[0];
+                const next_selector = new NodeChildSelector(deepest_existing.selector, component);
+                const next_node = await fs.node(next_selector);
+                if ( ! await next_node.exists() ) {
+                    break;
+                }
+                deepest_existing = next_node;
+                remaining_path.shift();
+            }
+        }
+
         const tree_op = new MkTree();
         await tree_op.run({
-            parent: parent_node,
-            tree: [_path.dirname(values.path)],
+            parent: deepest_existing,
+            tree: [remaining_path.join('/')],
         });
 
         this.parent_directories_created = tree_op.directories_created;