From 807c3ba5eca02f69b5e6ce547420312b68c7993f Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Mon, 13 Jan 2025 15:16:52 -0500 Subject: [PATCH] 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. --- .../src/filesystem/hl_operations/hl_mkdir.js | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/backend/src/filesystem/hl_operations/hl_mkdir.js b/src/backend/src/filesystem/hl_operations/hl_mkdir.js index f5db3e681..80291cb88 100644 --- a/src/backend/src/filesystem/hl_operations/hl_mkdir.js +++ b/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;