pythonlinuxsshfs

Python's os.link fails on sshfs network drive


On my Linux (CentOS 6) system I have a sshfs network share mounted on e.g. /data/remote. I can freely create symlinks via bash

ln -s /data/remote/source /data/remote/destination

But the following python code fails

# test_os_link.py
import os
if not os.path.exists('/data/remote/destination'):
    os.link('/data/remote/source', '/data/remote/destination')

Fails with

Traceback (most recent call last):
  File "test_os_link.py", line 2, in <module>
    os.link('/data/remote/source', '/data/remote/destination')
OSError: [Errno 38] Function not implemented

The sshfs is mounted as

sshfs -o follow_symlinks user@host:/data/remote /data/remote

As You see the directory structure is identical on remote and local system, thus symlinks within /data/remote will be valid on both systems.

strace reports the following:

stat("test_os_link.py", {st_mode=S_IFREG|0644, st_size=167, ...}) = 0
open("test_os_link.py", O_RDONLY)       = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=167, ...}) = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7fff7240bf10) = -1 ENOTTY (Inappropriate ioctl for device)
fstat(3, {st_mode=S_IFREG|0644, st_size=167, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f48c6080000
lseek(3, 0, SEEK_CUR)                   = 0
read(3, "import os\nos.link(\"/data/remote/dest"..., 4096) = 167
lseek(3, 167, SEEK_SET)                 = 167
brk(0x1e64000)                          = 0x1e64000
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f48c6080000, 4096)            = 0
mmap(NULL, 200704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f48c6050000
mmap(NULL, 266240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f48c600f000
brk(0x1e85000)                          = 0x1e85000
mmap(NULL, 266240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f48c5fce000
link("/data/remote/source", "/data/remote/destination") = -1 ENOSYS (Function not implemented)
write(2, "Traceback (most recent call last"..., 35Traceback (most recent call last):
) = 35
write(2, "  File \"test_os_link.py\", line 2"..., 46  File "test_os_link.py", line 2, in <module>
) = 46
open("test_os_link.py", O_RDONLY)       = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=167, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f48c5fcd000
read(3, "import os\nos.link(\"/data/remote/dest"..., 4096) = 167
write(2, "    ", 4    )                     = 4
write(2, "os.link(\"/data/remote/source"..., 79os.link("/data/remote/source","/data/remote/destination")
) = 79
close(3)                                = 0
munmap(0x7f48c5fcd000, 4096)            = 0
write(2, "OSError", 7OSError)                  = 7
write(2, ": ", 2: )                       = 2

Solution

  • os.link creates hard links, which are not supported by all filesystems.

    Use os.symlink to create symbolic links. This is the equivalent to ln -s.