Skip to content

Commit a4e273b

Browse files
committed
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 cd4985c commit a4e273b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/internal/fs/utils.js

+4
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ function preprocessSymlinkDestination(path, type, linkPath) {
302302
path = pathModule.resolve(linkPath, '..', path);
303303
return pathModule.toNamespacedPath(path);
304304
}
305+
if (pathModule.isAbsolute(path)) {
306+
// If the path is absolute, use the \\?\-prefix to enable long filenames
307+
return pathModule.toNamespacedPath(path);
308+
}
305309
// Windows symlinks don't tolerate forward slashes.
306310
return ('' + path).replace(/\//g, '\\');
307311
}
+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)