Accessing a Cygwin Symlink from Windows

Accessing a cygwin symlink from windows

Not that I know of. Cygwin doesn't update the OS to have symlinks, rather, it allows you to 'fake' symlinks from within the Cygwin shell. You can set up the shell to use Windows LNK files, which may do what you want, but ...

From the Cygwin Documentation:

Creating shortcuts with cygutils

Another problem area is between
Unix-style links, which link one file
to another, and Microsoft .lnk files,
which provide a shortcut to a file.
They seem similar at first glance but,
in reality, are fairly different. By
default, Cygwin does not create
symlinks as .lnk files, but there's an
option to do that, see the section
called “The CYGWIN environment
variable”. These symlink .lnk files
are compatible with Windows-created
.lnk files, but they are still
different. They do not include much of
the information that is available in a
standard Microsoft shortcut, such as
the working directory, an icon, etc.
The cygutils package includes a
mkshortcut utility for creating
standard native Microsoft .lnk files.

But here's the problem. If Cygwin
handled these native shortcuts like
any other symlink, you could not
archive Microsoft .lnk files into tar
archives and keep all the information
in them. After unpacking, these
shortcuts would have lost all the
extra information and would be no
different than standard Cygwin
symlinks. Therefore these two types of
links are treated differently.
Unfortunately, this means that the
usual Unix way of creating and using
symlinks does not work with native
Windows shortcuts.

why are some cygwin symlinks not visible from a cmd.exe session

The reason why the links are not visible is due to their file Attribute

S = System are not visible in CMD by DOS/Windows design,

from CMD, sorry in German, we have:

$ cmd
Microsoft Windows [Version 10.0.19041.450]
(c) 2020 Microsoft Corporation. Alle Rechte vorbehalten.

D:\cygwin64\bin>attrib zipinfo
S D:\cygwin64\bin\zipinfo

D:\cygwin64\bin>dir zipinfo
Datenträger in Laufwerk D: ist DATA
Volumeseriennummer: D603-FB6E

Verzeichnis von D:\cygwin64\bin

Datei nicht gefunden

D:\cygwin64\bin>dir /A:S zipinfo
Datenträger in Laufwerk D: ist DATA
Volumeseriennummer: D603-FB6E

Verzeichnis von D:\cygwin64\bin

19.06.2018 22:17 16 zipinfo
1 Datei(en), 16 Bytes
0 Verzeichnis(se), 542.542.495.744 Bytes frei

Enable native NTFS symbolic links for Cygwin

⸻⸻⸻  Short answer  ⸻⸻⸻

Define environment variable:

CYGWIN=winsymlinks:nativestrict

As pointed out by mwm you may also have to run bash as Administrator.

⸻⸻⸻  Long answer  ⸻⸻⸻

Default Cygwin symlinks are just regular files

By default Cygwin creates text files as workaround for Windows symlink flaw.
These files are not really symlinks.
Almost all Windows programs do not considers these files as symlinks.

Native symlinks are available on recent Windows versions

Recent NTFS and Windows implement symlinks:

  • NTFS junction point can be used as directory symlink
    since NTFS 3.0 (Windows 2000) using linkd or junction tools.
  • NTFS symbolic link can also be used as symlink
    (for both file and directory) since Windows Vista using mklink tool.

Cygwin can create native NTFS symlinks

Simplified extract of the Cygwin documentation:

Symbolic links


[...]

Cygwin creates symbolic links potentially in multiple different ways:

  • The default symlinks are plain files containing a magic cookie
    followed by the path to which the link points. [...]

  • The shortcut style symlinks are Windows .lnk [...] created
    if the environment variable CYGWIN [...] is set to contain
    the string winsymlinks or winsymlinks:lnk. [...]

  • Native Windows symlinks are only created on Windows Vista/2008 and later,
    and only on filesystems supporting reparse points.
    Due to to their weird restrictions and behaviour, they are only created
    if the user explicitely requests creating them.
    This is done by setting the environment variable CYGWIN
    to contain the string winsymlinks:native or winsymlinks:nativestrict.
    [...]

  • On the NFS filesystem, Cygwin always creates real NFS symlinks.

Configuring Cygwin

Cygwin User's Guide presents variable CYGWIN and option winsymlinks:

The CYGWIN environment variable is used to configure many global settings [...].
It contains the options listed below, separated by blank characters. [...]

  • [...]
  • [...]
  • [...]
  • [...]
  • winsymlinks:{lnk,native,nativestrict} -
    if set to just winsymlinks or winsymlinks:lnk, Cygwin creates symlinks
    as Windows shortcuts with a special headerand the R/O attribute set.

    If set to winsymlinks:native or winsymlinks:nativestrict,
    Cygwin creates symlinks as native Windows symlinks on filesystems
    and OS versions supporting them. If the OS is known not to support
    native symlinks (Windows XP, Windows Server 2003), a warning message
    is produced once per session.

    The difference between winsymlinks:native and
    winsymlinks: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 with
    winsymlinks:native, while with winsymlinks:nativestrict
    the symlink(2) system call will immediately fail.

CYGWIN=winsymlinks:native
always creates a link but uses a Cygwin fall-back when target does not exists

on Cygwin:

$ export CYGWIN="winsymlinks:native"
$ ln -s -v target mylink
`mylink' -> `target'
$ echo content > target

on MinGW:

$ cat mylink
content

People using both Windows and Cygwin programs may have issues when a symlink is created as a dummy file (Cygwin fallback when target is missing)...

CYGWIN=winsymlinks:nativestrict
always uses native-Windows symlink but fails when target does not exist

on Cygwin:

$ export CYGWIN="winsymlinks:nativestrict"
$ rm -f a b
$ ln -sv a b
ln: failed to create symbolic link `b': No such file or directory
$ touch b
$ ln -sv a b
ln: failed to create symbolic link `b': File exists
$ rm b
$ touch a
$ ln -sv a b
`b' -> `a'

Because nativestrict requires the target exists before the symlink creation, some commands/scripts may fail when creating a link.

Note: Only administrators have the ability to create native NT symlinks
so under Windows UAC, the Cygwin terminal emulator (mintty)
should be run with elevated privileges
(right-click the shortcut and choose Run as Administrator
or set the mintty shortcut property, Advanced → Run as Administrator).

Special thanks to Guria and Spooky for their contributions.

How to use symlinks created by Windows git with Cygwin

I wrote a simple bash script to find all the "links", easy since they are marked with flags 120000 according to this answer. Then it was a simple matter to just pick up the target filename (the content of the text file) and then replace the file with a symbolic link.

Since it is a bash script it can easily be used from Cygwin and MSys.

#!/bin/bash
#
# https://stackoverflow.com/a/38140374/204658
#
# Script to convert symbolic links created by a windows git
# clone/checkout to cygwin links. When a typical Windows git checks
# out a symbolic link from the repo it (MsysGit, at least) tend to
# produce text files with flag 120000 and the link target file name as
# the content. Strategy would simply be to find all those and replace
# by cygwin links.
#
# This will work if you are not planning on commiting anything, e.g.
# in a Jenkins, or other CI, build environment

for f in `git ls-files -s | awk '$1 == "120000" {print $4}'`
do
# echo $f is a symlink pointing to $dir/$target
dir=$(dirname "${f}")
pushd "$dir" 2>&1 > /dev/null
file=$(basename "$f")
target=`cat "$file"`
rm "$file"
ln -s "$target" "$file"
popd 2>&1 > /dev/null
done

check the difference of symlink type in cygwin

I ended up scripting this. The test in cygwin would be to call a dos command and grep for the info needed.

if fsutil reparsepoint query "$1" | grep -iq "Tag value: Symbolic Link" ; then
echo "Windows Native Symlink"
fi

cygwin winsymlinks:native doesn't work

I also battled with this one for a while on Windows 7 with Cygwin.

Everything I read seemed to say that it needed export CYGWIN="winsymlinks:native", but no luck for me.

Then I read this blog http://zzamboni.org/blog/making-cygwin-windows-and-emacs-understand-th/ which said that just "winsymlinks" was all you need. Tried that and it worked beautifully :)

Just use this environment variable.
export CYGWIN="winsymlinks"

Soft linking an executable in Cygwin

The problem is that ln needs to link to an absolute path, not a relative one. The problem was not apparent in the OP, because the path you wrote looked absolute because it started with /.

Something like ln -s /home/<username>/Coding/Julia/usr/bin/julia.exe /usr/local/bin/julia.exe should work.

Note:
By including .exe on the end of your LINK_NAME you get the ability to call both julia and julia.exe. Whether you intend to use the long form or not, it's best to include .exe on both the TARGET and LINK_NAME.



Related Topics



Leave a reply



Submit