Further down this rabbit hole.
Sorry for the noise, but the more I go deeper, the worse it looks like, and I need to vent to people who might understand.
The
#SFTP specification version 3, section 7 actually provides the number of links of each file the "longname" of the SSH_FXP_NAME response:
The SSH_FXP_NAME response has the following format:
uint32 id
uint32 count
repeats count times:
string filename
string longname
ATTRS attrs
[...]
`longname' is an expanded format for the file name, similar to what
is returned by "ls -l" on Unix systems
[...]
The format of the `longname' field is unspecified by this protocol.
It MUST be suitable for use in the output of a directory listing
command (in fact, the recommended operation for a directory listing
command is to simply display this data).
So far so good: we should have the info we need (at least from unix servers) despite in an unfortunate textual format.
The specification also states that such long name MUST not be parsed and clients should rely on the binary attributes that follow, except that the same specification forgot to include st_nlink (maybe because not all operating system supports such info).
However let assume that we ignore that argument and patch sshfs to read and parse such text just to provide the proper st_nlink.
Will
#OpenSSH properly implement the specification?
NO! It doesn't! π
Let's give it a try:
sftp> cd test
sftp> ls -l
-rw-r--r-- 2 tesio users 0 Mar 26 20:34 copy
-rw-r--r-- 2 tesio users 0 Mar 26 20:34 file
sftp> ls -l *
-rw-r--r-- ? 115209 100 0 Mar 26 21:34 copy
-rw-r--r-- ? 115209 100 0 Mar 26 21:34 file
It turns out that the sftp-server from OpenSSH handles
ls -l
differently when it is received
without any argument or
with an argument.
In the first case (no argument) the request comply to the specification.
In the second, it does not, apparently for no reason (the
ls_file
function
receives remote = 1).
So to properly fill
st_nlink
in
#sshfs, for any file we should sent an additional SSH_FXP_READDIR request to the server
for the parent directory, find our file of interest within the list of files and extract the number of links from its longname.
And we are talking about OpenSSH!
Guys... I wanna cry.
CC:
@rozenglass@fedi.dreamscape.link @grunfink@comam.es @khm@hj.9fs.net @Velveteen@labyrinth.zone