@@ -2014,7 +2014,7 @@ symlink_native (const char *oldpath, path_conv &win32_newpath)
20142014 path_conv win32_oldpath;
20152015 PUNICODE_STRING final_oldpath, final_newpath;
20162016 UNICODE_STRING final_oldpath_buf;
2017- DWORD flags;
2017+ DWORD flags = 0 ;
20182018
20192019 if (resolve_symlink_target (oldpath, win32_newpath, win32_oldpath))
20202020 final_oldpath = win32_oldpath.get_nt_native_path ();
@@ -2062,14 +2062,39 @@ symlink_native (const char *oldpath, path_conv &win32_newpath)
20622062 wcpcpy (e_old, c_old);
20632063 }
20642064 }
2065- /* If the symlink target doesn't exist, don't create native symlink.
2066- Otherwise the directory flag in the symlink is potentially wrong
2067- when the target comes into existence, and native tools will fail.
2068- This is so screwball. This is no problem on AFS, fortunately . */
2069- if (! win32_oldpath.exists () && ! win32_oldpath.fs_is_afs ())
2065+
2066+ /* The directory flag in the symlink must match the target type,
2067+ otherwise native tools will fail (fortunately this is no problem
2068+ on AFS). Do our best to guess the symlink type correctly . */
2069+ if (win32_oldpath.exists () || win32_oldpath.fs_is_afs ())
20702070 {
2071- SetLastError (ERROR_FILE_NOT_FOUND);
2072- return -1 ;
2071+ /* If the target exists (or on AFS), check the target type. Note
2072+ that this may still be wrong if the target is changed after
2073+ creating the symlink (e.g. in bulk operations such as rsync,
2074+ unpacking archives or VCS checkouts). */
2075+ if (win32_oldpath.isdir ())
2076+ flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
2077+ }
2078+ else
2079+ {
2080+ if (allow_winsymlinks == WSYM_nativestrict)
2081+ {
2082+ /* In nativestrict mode, if the target does not exist, use
2083+ trailing '/' in the target path as hint to create a
2084+ directory symlink. */
2085+ ssize_t len = strlen (oldpath);
2086+ if (len && isdirsep (oldpath[len - 1 ]))
2087+ flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
2088+ }
2089+ else
2090+ {
2091+ /* In native mode, if the target does not exist, fall back
2092+ to creating a Cygwin symlink file (or in case of MSys:
2093+ try to copy the (non-existing) target, which will of
2094+ course fail). */
2095+ SetLastError (ERROR_FILE_NOT_FOUND);
2096+ return -1 ;
2097+ }
20732098 }
20742099 /* Don't allow native symlinks to Cygwin special files. However, the
20752100 caller shoud know because this case shouldn't be covered by the
@@ -2098,7 +2123,6 @@ symlink_native (const char *oldpath, path_conv &win32_newpath)
20982123 final_oldpath->Buffer [1 ] = L' \\ ' ;
20992124 }
21002125 /* Try to create native symlink. */
2101- flags = win32_oldpath.isdir () ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0 ;
21022126 if (wincap.has_unprivileged_createsymlink ())
21032127 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
21042128 if (!CreateSymbolicLinkW (final_newpath->Buffer , final_oldpath->Buffer ,
0 commit comments