I stumbled upon this cool Cygwin option that allows you to use native Windows (NTFS) symlinks (like CreateSymbolicLinkW
in the
Win32 API, or mklink
via cmd
shell). Apparently there's a CYGWIN
environment variable that "is used to configure many global settings for
the Cygwin runtime system".
One of those options is winsymlinks:{lnk,native,nativestrict}
.
if set to just
winsymlinks
orwinsymlinks:lnk
, Cygwin creates symlinks as Windows shortcuts with a special header and the R/O attribute set.If set to
winsymlinks:native
orwinsymlinks:nativestrict
, Cygwin creates symlinks as native Windows symlinks on filesystems and OS versions supporting them.The difference between
winsymlinks:native
andwinsymlinks:nativestrict
is this: If the filesystem supports native symlinks and Cygwin fails to create a native symlink for some reason, it will fall back to creating Cygwin default symlinks withwinsymlinks:native
, while withwinsymlinks:nativestrict
thesymlink(2)
system call will immediately fail.For more information on symbolic links, see the section called “Symbolic links”.
So basically, setting the winsymlinks
option to nativestrict
forces Cygwin to use native symlinks!
Normally, when you make symlinks in Cygwin, it creates plain files containing a magic cookie, followed by the path to which the link points to. You can read up on this in the Cygwin documentation.
But with this option, we can create symlinks using ln
and it will create NTFS-style symlinks automatically!
CYGWIN=winsymlinks:nativestrict
ln -sf /source/file /new/path
Cool! No need to open cmd
and manually symlink my files anymore.
Note: You still need Administrator privileges, or must have the SeCreateSymbolicLinkPrivilege
privilege
for this to work.
Beware though that directory symlinks to symlinks do NOT work correctly:
mkdir adir
ln -s adir 1
ln -s 1 2
Now 2 is a .symlink file and not a native symlink! Even nativestrict does NOT fail here. You get silent corruption.