Skip to content
This repository has been archived by the owner on May 29, 2022. It is now read-only.

Commit

Permalink
Merge c1c27fb into e8ffdcf
Browse files Browse the repository at this point in the history
  • Loading branch information
znedw committed Apr 19, 2020
2 parents e8ffdcf + c1c27fb commit e334e74
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -98,7 +98,7 @@ general
- ``add_limit_percent`` - Max percent the total torrent size is allowed
to vary
- ``link_type`` - What kind of link should AutoTorrent make? the options are
hard and soft.
hard, soft and reflink (if supported).
- ``scan_mode`` - options are unsplitable, normal and exact. These can be used
in combination. See the `scan_modes <#scan-modes>`_ section for more information.

Expand Down
79 changes: 79 additions & 0 deletions autotorrent/at.py
Expand Up @@ -3,6 +3,7 @@
import os
import hashlib
import logging
import platform

from collections import defaultdict

Expand Down Expand Up @@ -349,9 +350,87 @@ def link_files(self, destination_path, files):
os.symlink(f['actual_path'], destination)
elif self.link_type == 'hard':
os.link(f['actual_path'], destination)
elif self.link_type == 'ref':
self.reflink(f['actual_path'], destination)
else:
raise UnknownLinkTypeException('%r is not a known link type' % self.link_type)

def reflink(self, path, destination):
"""
Perform a reflink (if supported, currently only xfs, apfs, btrfs is)
"""
system = platform.system()
logger.debug('platform is %r', system)
try:
if system == "Windows":
ret = self.reflink_windows(path, destination)
elif system == "Darwin":
ret = self.reflink_darwin(path, destination)
elif system == "Linux":
ret = self.reflink_linux(path, destination)
else:
ret = -1
except IOError:
ret = -1

if ret != 0:
raise Exception("reflink is not supported")


def reflink_linux(self, path, destination):
"""
Linux only reflink via syscall FICLONE on supported filesystems
"""
import os
import fcntl

FICLONE = 0x40049409

try:
ret = 255
with open(path, "r") as s, open(destination, "w+") as d:
ret = fcntl.ioctl(d.fileno(), FICLONE, s.fileno())
finally:
if ret != 0:
os.unlink(destination)

return ret

def reflink_windows(self, path, destination):
return -1

def reflink_darwin(self, path, destination):
import ctypes

LIBC = "libc.dylib"
LIBC_FALLBACK = "/usr/lib/libSystem.dylib"
try:
clib = ctypes.CDLL(LIBC)
except OSError as exc:
logger.debug(
"unable to access '{}' (errno '{}'). "
"Falling back to '{}'.".format(LIBC, exc.errno, LIBC_FALLBACK)
)
if exc.errno != errno.ENOENT:
raise
# NOTE: trying to bypass System Integrity Protection (SIP)
clib = ctypes.CDLL(LIBC_FALLBACK)

if not hasattr(clib, "clonefile"):
return -1

clonefile = clib.clonefile
clonefile.argtypes = [ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int]
clonefile.restype = ctypes.c_int

return clonefile(
ctypes.c_char_p(path.encode("utf-8")),
ctypes.c_char_p(destination.encode("utf-8")),
ctypes.c_int(0),
)



def rewrite_hashed_files(self, destination_path, files):
"""
Rewrites files from the actual_path to the correct file inside destination_path.
Expand Down

0 comments on commit e334e74

Please sign in to comment.