Skip to content

Commit 3b46e7f

Browse files
bzozcodebytere
authored andcommitted
win,fs: use namespaced path in absolute symlinks
Use the namespaced (with the \\?\ prefix) paths for symlink targets when the path is absolute. This allows creation of symlinks to files with long filenames. Fixes: #27795 PR-URL: #33351 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent d09f6d5 commit 3b46e7f

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/internal/fs/utils.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,14 @@ function preprocessSymlinkDestination(path, type, linkPath) {
272272
// A relative target is relative to the link's parent directory.
273273
path = pathModule.resolve(linkPath, '..', path);
274274
return pathModule.toNamespacedPath(path);
275-
} else {
276-
// Windows symlinks don't tolerate forward slashes.
277-
return ('' + path).replace(/\//g, '\\');
278275
}
276+
277+
if (pathModule.isAbsolute(path)) {
278+
// If the path is absolute, use the \\?\-prefix to enable long filenames
279+
return pathModule.toNamespacedPath(path);
280+
}
281+
// Windows symlinks don't tolerate forward slashes.
282+
return ('' + path).replace(/\//g, '\\');
279283
}
280284

281285
// Constructor for file stats.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const path = require('path');
6+
const fs = require('fs');
7+
8+
const tmpdir = require('../common/tmpdir');
9+
tmpdir.refresh();
10+
const tmpDir = tmpdir.path;
11+
const longPath = path.join(...[tmpDir].concat(Array(30).fill('1234567890')));
12+
fs.mkdirSync(longPath, { recursive: true });
13+
14+
// Test if we can have symlinks to files and folders with long filenames
15+
const targetDirtectory = path.join(longPath, 'target-directory');
16+
fs.mkdirSync(targetDirtectory);
17+
const pathDirectory = path.join(tmpDir, 'new-directory');
18+
fs.symlink(targetDirtectory, pathDirectory, 'dir', common.mustCall((err) => {
19+
assert.ifError(err);
20+
assert(fs.existsSync(pathDirectory));
21+
}));
22+
23+
const targetFile = path.join(longPath, 'target-file');
24+
fs.writeFileSync(targetFile, 'data');
25+
const pathFile = path.join(tmpDir, 'new-file');
26+
fs.symlink(targetFile, pathFile, common.mustCall((err) => {
27+
assert.ifError(err);
28+
assert(fs.existsSync(pathFile));
29+
}));

0 commit comments

Comments
 (0)