@@ -1716,6 +1716,7 @@ symlink_native (const char *oldpath, path_conv &win32_newpath)
1716
1716
path_conv win32_oldpath;
1717
1717
PUNICODE_STRING final_oldpath, final_newpath;
1718
1718
UNICODE_STRING final_oldpath_buf;
1719
+ bool isdir;
1719
1720
1720
1721
if (isabspath (oldpath))
1721
1722
{
@@ -1776,14 +1777,37 @@ symlink_native (const char *oldpath, path_conv &win32_newpath)
1776
1777
wcpcpy (e_old, c_old);
1777
1778
}
1778
1779
}
1779
- /* If the symlink target doesn't exist, don't create native symlink.
1780
- Otherwise the directory flag in the symlink is potentially wrong
1781
- when the target comes into existence, and native tools will fail.
1782
- This is so screwball. This is no problem on AFS, fortunately . */
1783
- if (! win32_oldpath.exists () && ! win32_oldpath.fs_is_afs ())
1780
+
1781
+ /* The directory flag in the symlink must match the target type,
1782
+ otherwise native tools will fail (fortunately this is no problem
1783
+ on AFS). Do our best to guess the symlink type correctly . */
1784
+ if (win32_oldpath.exists () || win32_oldpath.fs_is_afs ())
1784
1785
{
1785
- SetLastError (ERROR_FILE_NOT_FOUND);
1786
- return -1 ;
1786
+ /* If the target exists (or on AFS), check the target type. Note
1787
+ that this may still be wrong if the target is changed after
1788
+ creating the symlink (e.g. in bulk operations such as rsync,
1789
+ unpacking archives or VCS checkouts). */
1790
+ isdir = win32_oldpath.isdir ();
1791
+ }
1792
+ else
1793
+ {
1794
+ if (allow_winsymlinks == WSYM_nativestrict)
1795
+ {
1796
+ /* In nativestrict mode, if the target does not exist, use
1797
+ trailing '/' in the target path as hint to create a
1798
+ directory symlink. */
1799
+ ssize_t len = strlen (oldpath);
1800
+ isdir = len && isdirsep (oldpath[len - 1 ]);
1801
+ }
1802
+ else
1803
+ {
1804
+ /* In native mode, if the target does not exist, fall back
1805
+ to creating a Cygwin symlink file (or in case of MSys:
1806
+ try to copy the (non-existing) target, which will of
1807
+ course fail). */
1808
+ SetLastError (ERROR_FILE_NOT_FOUND);
1809
+ return -1 ;
1810
+ }
1787
1811
}
1788
1812
/* Don't allow native symlinks to Cygwin special files. However, the
1789
1813
caller shoud know because this case shouldn't be covered by the
@@ -1813,8 +1837,7 @@ symlink_native (const char *oldpath, path_conv &win32_newpath)
1813
1837
}
1814
1838
/* Try to create native symlink. */
1815
1839
if (!CreateSymbolicLinkW (final_newpath->Buffer , final_oldpath->Buffer ,
1816
- win32_oldpath.isdir ()
1817
- ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0 ))
1840
+ isdir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0 ))
1818
1841
{
1819
1842
/* Repair native newpath, we still need it. */
1820
1843
final_newpath->Buffer [1 ] = L' ?' ;
0 commit comments