Cygwin: Implicitly support the /dev/fd symlink and friends

Bash has a very convenient feature that is called process substitution
(e.g. `diff -u <(seq 0 10) <(seq 1 11)`). To make this work, Bash
requires the `/dev/fd` symlink to exist, and Cygwin therefore creates
this symlink (together with the `stdin`, `stdout` and `stderr` ones)
upon start-up.

This strategy is incompatible with the idea of providing a subset of
Cygwin in a `.zip` file (because there is no standard way to represent
symlinks in `.zip` files, and besides, older Windows versions would
potentially lack support for them anyway).

That type of `.zip` file is what Git for Windows wants to use, though,
bundling a minimal subset for third-party applications in MinGit (see
https://github.com/git-for-windows/git/wiki/MinGit for details).

Let's side-step this problem completely by creating those symlinks
implicitly, similar to the way `/dev/` is populated with special
devices.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin 2022-02-21 14:36:37 +01:00 committed by Corinna Vinschen
parent 8bb6821c7a
commit 4ec08891a0
7 changed files with 879 additions and 707 deletions

View File

@ -232,6 +232,7 @@ DLL_FILES= \
fhandler_console.cc \
fhandler_cygdrive.cc \
fhandler_dev.cc \
fhandler_dev_fd.cc \
fhandler_disk_file.cc \
fhandler_dsp.cc \
fhandler_fifo.cc \

File diff suppressed because it is too large Load Diff

View File

@ -70,6 +70,7 @@ enum fh_devices
FH_NETDRIVE= FHDEV (DEV_VIRTFS_MAJOR, 194),
FH_DEV = FHDEV (DEV_VIRTFS_MAJOR, 193),
FH_CYGDRIVE= FHDEV (DEV_VIRTFS_MAJOR, 192),
FH_DEV_FD = FHDEV (DEV_VIRTFS_MAJOR, 191),
FH_SIGNALFD= FHDEV (DEV_VIRTFS_MAJOR, 13),
FH_TIMERFD = FHDEV (DEV_VIRTFS_MAJOR, 14),
@ -436,7 +437,7 @@ extern const _device dev_fs_storage;
#define isprocsys_dev(devn) (devn == FH_PROCSYS)
#define isvirtual_dev(devn) \
(isproc_dev (devn) || devn == FH_CYGDRIVE || devn == FH_NETDRIVE)
(isproc_dev (devn) || devn == FH_CYGDRIVE || devn == FH_NETDRIVE || devn == FH_DEV_FD)
#define iscons_dev(n) \
((device::major ((dev_t) (n)) == DEV_CONS_MAJOR) \

View File

@ -175,6 +175,10 @@ const _device dev_error_storage =
"/dev/fd%(0-15)d", BRACK(FHDEV(DEV_FLOPPY_MAJOR, {$1})), "\\Device\\Floppy{$1}", exists_ntdev, S_IFBLK
"/dev/scd%(0-15)d", BRACK(FHDEV(DEV_CDROM_MAJOR, {$1})), "\\Device\\CdRom{$1}", exists_ntdev, S_IFBLK
"/dev/sr%(0-15)d", BRACK(FHDEV(DEV_CDROM_MAJOR, {$1})), "\\Device\\CdRom{$1}", exists_ntdev, S_IFBLK
"/dev/fd", BRACK(FH_DEV_FD), "/proc/self/fd", exists, S_IFLNK
"/dev/stdin", BRACK(FH_DEV_FD), "/proc/self/fd/0", exists, S_IFLNK
"/dev/stdout", BRACK(FH_DEV_FD), "/proc/self/fd/1", exists, S_IFLNK
"/dev/stderr", BRACK(FH_DEV_FD), "/proc/self/fd/2", exists, S_IFLNK
%other {return NULL;}
%%
#undef BRACK

View File

@ -582,6 +582,9 @@ fh_alloc (path_conv& pc)
case FH_DEV:
fh = cnew (fhandler_dev);
break;
case FH_DEV_FD:
fh = cnew (fhandler_dev_fd);
break;
case FH_CYGDRIVE:
fh = cnew (fhandler_cygdrive);
break;

View File

@ -3027,6 +3027,33 @@ class fhandler_procnet: public fhandler_proc
}
};
class fhandler_dev_fd: public fhandler_virtual
{
public:
fhandler_dev_fd ();
virtual_ftype_t exists();
int __reg2 fstat (struct stat *buf);
bool fill_filebuf ();
fhandler_dev_fd (void *) {}
virtual void copy_from (fhandler_base *x)
{
pc.free_strings ();
*this = *reinterpret_cast<fhandler_dev_fd *> (x);
_copy_from_reset_helper ();
}
virtual fhandler_dev_fd *clone (cygheap_types malloc_type = HEAP_FHANDLER)
{
void *ptr = (void *) ccalloc (malloc_type, 1, sizeof (fhandler_dev_fd));
fhandler_dev_fd *fh = new (ptr) fhandler_dev_fd (ptr);
fh->copy_from (this);
return fh;
}
};
class fhandler_signalfd : public fhandler_base
{
sigset_t sigset;
@ -3226,6 +3253,7 @@ typedef union
char __dev_raw[sizeof (fhandler_dev_raw)];
char __dev_tape[sizeof (fhandler_dev_tape)];
char __dev_zero[sizeof (fhandler_dev_zero)];
char __dev_fd[sizeof (fhandler_dev_fd)];
char __disk_file[sizeof (fhandler_disk_file)];
char __fifo[sizeof (fhandler_fifo)];
char __netdrive[sizeof (fhandler_netdrive)];

View File

@ -0,0 +1,53 @@
/* fhandler_process_fd.cc: fhandler for the /dev/{fd,std{in,out,err}} symlinks
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include "path.h"
#include "fhandler.h"
fhandler_dev_fd::fhandler_dev_fd ():
fhandler_virtual ()
{
}
virtual_ftype_t
fhandler_dev_fd::exists ()
{
return virt_symlink;
}
int __reg2
fhandler_dev_fd::fstat (struct stat *buf)
{
const char *path = get_name ();
debug_printf ("fstat (%s)", path);
fhandler_base::fstat (buf);
buf->st_mode = S_IFLNK | STD_RBITS | S_IWUSR | S_IWGRP | S_IWOTH | STD_XBITS;
buf->st_ino = get_ino ();
return 0;
}
bool
fhandler_dev_fd::fill_filebuf ()
{
const char *path = get_name ();
debug_printf ("fill_filebuf (%s)", path);
const char *native = get_native_name ();
if (!native)
{
return false;
}
free(filebuf);
filebuf = cstrdup (native);
return true;
}