From 7e20039ef4ba458f8d6765e4a599b78ff2e79c93 Mon Sep 17 00:00:00 2001 From: XiaochenCui Date: Mon, 21 Jul 2025 14:08:15 -0700 Subject: [PATCH] fs/mkdir: update check logic, update test cases --- .../src/filesystem/hl_operations/hl_mkdir.js | 23 ++++-------- tools/api-tester/lib/TestSDK.js | 2 +- tools/api-tester/tests/mkdir.js | 37 ++++++++++++++----- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/backend/src/filesystem/hl_operations/hl_mkdir.js b/src/backend/src/filesystem/hl_operations/hl_mkdir.js index ce4ef64a9..17cc12a2c 100644 --- a/src/backend/src/filesystem/hl_operations/hl_mkdir.js +++ b/src/backend/src/filesystem/hl_operations/hl_mkdir.js @@ -280,23 +280,8 @@ class HLMkdir extends HLFilesystemOperation { } let parent_node = values.parent || await fs.node(new RootNodeSelector()); - if ( parent_node.isRoot ) { - // root directory is read-only - throw APIError.create('forbidden', null, { - message: 'Cannot create directories in the root directory.' - }); - } console.log('USING PARENT', parent_node.selector.describe()); - // TODO: this can be removed upon completion of: https://github.com/HeyPuter/puter/issues/1352 - if ( parent_node.isRoot ) { - // root directory is read-only - throw APIError.create('forbidden', null, { - message: 'Cannot create directories in the root directory.' - }); - } - - let target_basename = _path.basename(values.path); // "top_parent" is the immediate parent of the target directory @@ -306,6 +291,14 @@ class HLMkdir extends HLFilesystemOperation { : await this._get_existing_top_parent({ top_parent: parent_node }) ; + // TODO: this can be removed upon completion of: https://github.com/HeyPuter/puter/issues/1352 + if ( top_parent.isRoot ) { + // root directory is read-only + throw APIError.create('forbidden', null, { + message: 'Cannot create directories in the root directory.' + }); + } + // `parent_node` becomes the parent of the last directory name // specified under `path`. parent_node = await this._create_parents({ diff --git a/tools/api-tester/lib/TestSDK.js b/tools/api-tester/lib/TestSDK.js index 9feb8dba1..5f8638d65 100644 --- a/tools/api-tester/lib/TestSDK.js +++ b/tools/api-tester/lib/TestSDK.js @@ -193,7 +193,7 @@ module.exports = class TestSDK { this.mkdir_v2 = async (parent, path, opts) => { const res = await this.post('mkdir', { parent: p(parent), - path: p(path), + path: path, // "path" arg should remain relative in this api ...(opts ?? {}) }); return res.data; diff --git a/tools/api-tester/tests/mkdir.js b/tools/api-tester/tests/mkdir.js index 882a4ba57..1383db6b1 100644 --- a/tools/api-tester/tests/mkdir.js +++ b/tools/api-tester/tests/mkdir.js @@ -152,19 +152,38 @@ module.exports = { create_missing_parents: true, }); expect(result.name).equal('c'); + + await t.case('can stat directories along the path', async () => { + let stat = await t.stat('a'); + expect(stat.name).equal('a'); + + stat = await t.stat('a/b'); + expect(stat.name).equal('b'); + + stat = await t.stat('a/b/c'); + expect(stat.name).equal('c'); + }); }); - await t.case('can stat the directory', async () => { - const stat = await t.stat(path); - expect(stat.name).equal('c'); - }); + await t.case('composite path', async () => { + const result = await t.mkdir_v2('1/2', '3/4', { + create_missing_parents: true, + }); + expect(result.name).equal('4'); - await t.case('can stat the parent directory', async () => { - let stat = await t.stat('a'); - expect(stat.name).equal('a'); + await t.case('can stat directories along the path', async () => { + let stat = await t.stat('1'); + expect(stat.name).equal('1'); - stat = await t.stat('a/b'); - expect(stat.name).equal('b'); + stat = await t.stat('1/2'); + expect(stat.name).equal('2'); + + stat = await t.stat('1/2/3'); + expect(stat.name).equal('3'); + + stat = await t.stat('1/2/3/4'); + expect(stat.name).equal('4'); + }); }); }); }