win, fs: detect if symlink target is a directory · nodejs/node@cda6b20 (original) (raw)

`@@ -905,16 +905,47 @@ function symlink(target, path, type_, callback_) {

`

905

905

`validatePath(target, 'target');

`

906

906

`validatePath(path);

`

907

907

``

908

``

`-

const flags = stringToSymlinkType(type);

`

909

908

`const req = new FSReqCallback();

`

910

909

`req.oncomplete = callback;

`

911

910

``

``

911

`+

if (isWindows && type === null) {

`

``

912

`+

let absoluteTarget;

`

``

913

`+

try {

`

``

914

`+

// Symlinks targets can be relative to the newly created path.

`

``

915

`+

// Calculate absolute file name of the symlink target, and check

`

``

916

`+

// if it is a directory. Ignore resolve error to keep symlink

`

``

917

`+

// errors consistent between platforms if invalid path is

`

``

918

`+

// provided.

`

``

919

`+

absoluteTarget = pathModule.resolve(path, '..', target);

`

``

920

`+

} catch { }

`

``

921

`+

if (absoluteTarget !== undefined) {

`

``

922

`+

stat(absoluteTarget, (err, stat) => {

`

``

923

`+

const resolvedType = !err && stat.isDirectory() ? 'dir' : 'file';

`

``

924

`+

const resolvedFlags = stringToSymlinkType(resolvedType);

`

``

925

`+

binding.symlink(preprocessSymlinkDestination(target,

`

``

926

`+

resolvedType,

`

``

927

`+

path),

`

``

928

`+

pathModule.toNamespacedPath(path), resolvedFlags, req);

`

``

929

`+

});

`

``

930

`+

return;

`

``

931

`+

}

`

``

932

`+

}

`

``

933

+

``

934

`+

const flags = stringToSymlinkType(type);

`

912

935

`binding.symlink(preprocessSymlinkDestination(target, type, path),

`

913

936

`pathModule.toNamespacedPath(path), flags, req);

`

914

937

`}

`

915

938

``

916

939

`function symlinkSync(target, path, type) {

`

917

940

`type = (typeof type === 'string' ? type : null);

`

``

941

`+

if (isWindows && type === null) {

`

``

942

`+

try {

`

``

943

`+

const absoluteTarget = pathModule.resolve(path, '..', target);

`

``

944

`+

if (statSync(absoluteTarget).isDirectory()) {

`

``

945

`+

type = 'dir';

`

``

946

`+

}

`

``

947

`+

} catch { }

`

``

948

`+

}

`

918

949

`target = toPathIfFileURL(target);

`

919

950

`path = toPathIfFileURL(path);

`

920

951

`validatePath(target, 'target');

`