Skip to content

Commit d80f2fa

Browse files
committed
fix: fix path issues under win32 platform
1 parent 5c636d4 commit d80f2fa

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

src/backend/src/api/filesystem/FSNodeParam.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const { is_valid_path } = require("../../filesystem/validation");
2020
const { is_valid_uuid4 } = require("../../helpers");
2121
const { Context } = require("../../util/context");
22+
const { PathBuilder } = require("../../util/pathutil");
2223
const APIError = require("../APIError");
2324
const _path = require('path');
2425

@@ -73,6 +74,7 @@ module.exports = class FSNodeParam {
7374
});
7475
}
7576

76-
return await fs.node({ path: _path.resolve('/', uidOrPath) });
77+
const resolved_path = PathBuilder.resolve(uidOrPath, { puterfs: true });
78+
return await fs.node({ path: resolved_path });
7779
}
7880
}

src/backend/src/helpers.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const { BaseDatabaseAccessService } = require('./services/database/BaseDatabaseA
3131
const { LLRmNode } = require('./filesystem/ll_operations/ll_rmnode');
3232
const { Context } = require('./util/context');
3333
const { NodeUIDSelector } = require('./filesystem/node/selectors');
34+
const { PathBuilder } = require('./util/pathutil');
3435

3536
let systemfs = null;
3637
let services = null;
@@ -1155,8 +1156,12 @@ async function jwt_auth(req){
11551156
async function mkdir(options){
11561157
const fs = systemfs;
11571158

1158-
const dirpath = _path.dirname(_path.resolve('/', options.path));
1159-
let target_name = _path.basename(_path.resolve('/', options.path));
1159+
debugger;
1160+
1161+
const resolved_path = PathBuilder.resolve(options.path, { puterfs: true });
1162+
1163+
const dirpath = _path.dirname(resolved_path);
1164+
let target_name = _path.basename(resolved_path);
11601165
const overwrite = options.overwrite ?? false;
11611166
const dedupe_name = options.dedupe_name ?? false;
11621167
const immutable = options.immutable ?? false;
@@ -1182,7 +1187,7 @@ async function mkdir(options){
11821187

11831188
// dirpath not found
11841189
if(parent === false && !create_missing_parents)
1185-
throw "Target path not found";
1190+
throw new Error("Target path not found");
11861191
// create missing parent directories
11871192
else if(parent === false && create_missing_parents){
11881193
const dirs = _path.resolve('/', dirpath).split('/');
@@ -1206,7 +1211,7 @@ async function mkdir(options){
12061211
// try setting parent again
12071212
parent = await convert_path_to_fsentry(dirpath);
12081213
if(parent === false)
1209-
throw "Target path not found";
1214+
throw new Error("Target path not found");
12101215
}
12111216

12121217
// check permission

src/backend/src/routers/filesystem_api/readdir.js

-10
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,6 @@ module.exports = eggspress('/readdir', {
8181
const no_thumbs = req.values.no_thumbs;
8282
const no_assocs = req.values.no_assocs;
8383

84-
{
85-
const fs = require('fs');
86-
fs.appendFileSync('/tmp/readdir.log',
87-
JSON.stringify({
88-
recursive,
89-
no_thumbs,
90-
no_assocs,
91-
}, null, 2) + '\n');
92-
}
93-
9484
const hl_readdir = new HLReadDir();
9585
const result = await hl_readdir.run({
9686
subject,

src/backend/src/util/pathutil.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,40 @@ class PathBuilder extends AdvancedBase {
2828
path: require('path'),
2929
}
3030

31-
constructor() {
31+
constructor(parameters = {}) {
3232
super();
33+
if ( parameters.puterfs ) {
34+
this.modules.path =
35+
this.modules.path.posix;
36+
}
3337
this.path_ = '';
3438
}
3539

36-
static create () {
37-
return new PathBuilder();
40+
static create (parameters) {
41+
return new PathBuilder(parameters);
3842
}
3943

4044
static add (fragment, options) {
4145
return PathBuilder.create().add(fragment, options);
4246
}
4347

44-
static resolve (fragment) {
45-
const p = PathBuilder.create();
48+
static resolve (fragment, parameters = {}) {
49+
const { puterfs } = parameters;
50+
51+
const p = PathBuilder.create(parameters);
4652
const require = p.require;
4753
const node_path = require('path');
4854
fragment = node_path.resolve(fragment);
49-
return p.add(fragment).build();
55+
if ( process.platform === 'win32' && !parameters.puterfs ) {
56+
fragment = '/' + fragment.slice('c:\\'.length); // >:-(
57+
}
58+
let result = p.add(fragment).build();
59+
if ( puterfs && process.platform === 'win32' &&
60+
result.startsWith('\\')
61+
) {
62+
result = '/' + result.slice(1);
63+
}
64+
return result;
5065
}
5166

5267
add (fragment, options) {

0 commit comments

Comments
 (0)