From d80f2fa847bfaef98dc8d482898f5c15f268e4bd Mon Sep 17 00:00:00 2001 From: Nariman Jelveh Date: Wed, 17 Jul 2024 13:36:22 -0700 Subject: [PATCH] fix: fix path issues under win32 platform --- src/backend/src/api/filesystem/FSNodeParam.js | 4 ++- src/backend/src/helpers.js | 13 ++++++--- .../src/routers/filesystem_api/readdir.js | 10 ------- src/backend/src/util/pathutil.js | 27 ++++++++++++++----- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/backend/src/api/filesystem/FSNodeParam.js b/src/backend/src/api/filesystem/FSNodeParam.js index 5097c8a6f..a7ee048e5 100644 --- a/src/backend/src/api/filesystem/FSNodeParam.js +++ b/src/backend/src/api/filesystem/FSNodeParam.js @@ -19,6 +19,7 @@ const { is_valid_path } = require("../../filesystem/validation"); const { is_valid_uuid4 } = require("../../helpers"); const { Context } = require("../../util/context"); +const { PathBuilder } = require("../../util/pathutil"); const APIError = require("../APIError"); const _path = require('path'); @@ -73,6 +74,7 @@ module.exports = class FSNodeParam { }); } - return await fs.node({ path: _path.resolve('/', uidOrPath) }); + const resolved_path = PathBuilder.resolve(uidOrPath, { puterfs: true }); + return await fs.node({ path: resolved_path }); } } \ No newline at end of file diff --git a/src/backend/src/helpers.js b/src/backend/src/helpers.js index 8205bb14a..cdcf4c963 100644 --- a/src/backend/src/helpers.js +++ b/src/backend/src/helpers.js @@ -31,6 +31,7 @@ const { BaseDatabaseAccessService } = require('./services/database/BaseDatabaseA const { LLRmNode } = require('./filesystem/ll_operations/ll_rmnode'); const { Context } = require('./util/context'); const { NodeUIDSelector } = require('./filesystem/node/selectors'); +const { PathBuilder } = require('./util/pathutil'); let systemfs = null; let services = null; @@ -1155,8 +1156,12 @@ async function jwt_auth(req){ async function mkdir(options){ const fs = systemfs; - const dirpath = _path.dirname(_path.resolve('/', options.path)); - let target_name = _path.basename(_path.resolve('/', options.path)); + debugger; + + const resolved_path = PathBuilder.resolve(options.path, { puterfs: true }); + + const dirpath = _path.dirname(resolved_path); + let target_name = _path.basename(resolved_path); const overwrite = options.overwrite ?? false; const dedupe_name = options.dedupe_name ?? false; const immutable = options.immutable ?? false; @@ -1182,7 +1187,7 @@ async function mkdir(options){ // dirpath not found if(parent === false && !create_missing_parents) - throw "Target path not found"; + throw new Error("Target path not found"); // create missing parent directories else if(parent === false && create_missing_parents){ const dirs = _path.resolve('/', dirpath).split('/'); @@ -1206,7 +1211,7 @@ async function mkdir(options){ // try setting parent again parent = await convert_path_to_fsentry(dirpath); if(parent === false) - throw "Target path not found"; + throw new Error("Target path not found"); } // check permission diff --git a/src/backend/src/routers/filesystem_api/readdir.js b/src/backend/src/routers/filesystem_api/readdir.js index cc1280fd3..180b09ceb 100644 --- a/src/backend/src/routers/filesystem_api/readdir.js +++ b/src/backend/src/routers/filesystem_api/readdir.js @@ -81,16 +81,6 @@ module.exports = eggspress('/readdir', { const no_thumbs = req.values.no_thumbs; const no_assocs = req.values.no_assocs; - { - const fs = require('fs'); - fs.appendFileSync('/tmp/readdir.log', - JSON.stringify({ - recursive, - no_thumbs, - no_assocs, - }, null, 2) + '\n'); - } - const hl_readdir = new HLReadDir(); const result = await hl_readdir.run({ subject, diff --git a/src/backend/src/util/pathutil.js b/src/backend/src/util/pathutil.js index 489827f8f..c75d070a3 100644 --- a/src/backend/src/util/pathutil.js +++ b/src/backend/src/util/pathutil.js @@ -28,25 +28,40 @@ class PathBuilder extends AdvancedBase { path: require('path'), } - constructor() { + constructor(parameters = {}) { super(); + if ( parameters.puterfs ) { + this.modules.path = + this.modules.path.posix; + } this.path_ = ''; } - static create () { - return new PathBuilder(); + static create (parameters) { + return new PathBuilder(parameters); } static add (fragment, options) { return PathBuilder.create().add(fragment, options); } - static resolve (fragment) { - const p = PathBuilder.create(); + static resolve (fragment, parameters = {}) { + const { puterfs } = parameters; + + const p = PathBuilder.create(parameters); const require = p.require; const node_path = require('path'); fragment = node_path.resolve(fragment); - return p.add(fragment).build(); + if ( process.platform === 'win32' && !parameters.puterfs ) { + fragment = '/' + fragment.slice('c:\\'.length); // >:-( + } + let result = p.add(fragment).build(); + if ( puterfs && process.platform === 'win32' && + result.startsWith('\\') + ) { + result = '/' + result.slice(1); + } + return result; } add (fragment, options) {