diff --git a/.travis.yml b/.travis.yml index 1a7405298..944e9e25c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,9 +16,13 @@ python: # version, that causes fpm to crash. matrix: include: + # 3.7 needs Xenial image because of https://github.com/travis-ci/travis-ci/issues/9069 + - python: "3.7" + dist: xenial + sudo: true - if: type = cron OR tag is present os: osx - osx_image: xcode9.3 + osx_image: xcode7.3 language: ruby rvm: 2.4.0 env: @@ -27,7 +31,7 @@ matrix: - PATH="$PYENV_ROOT/bin:$PATH" - if: type = cron OR tag is present os: osx - osx_image: xcode9.3 + osx_image: xcode7.3 language: ruby rvm: 2.4.0 env: @@ -36,17 +40,13 @@ matrix: - PATH="$PYENV_ROOT/bin:$PATH" - if: type = cron OR tag is present os: osx - osx_image: xcode9.3 + osx_image: xcode7.3 language: ruby rvm: 2.4.0 env: - PYTHON_VER=3.5.0 - PYENV_ROOT="$HOME/.pyenv" - PATH="$PYENV_ROOT/bin:$PATH" - # 3.7 needs Xenial image because of https://github.com/travis-ci/travis-ci/issues/9069 - - python: "3.7" - dist: xenial - sudo: true before_install: - bash ./scripts/ci/before_install.sh - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then diff --git a/dvc/reflink_ffi.py b/dvc/reflink_ffi.py new file mode 100644 index 000000000..6056f47ad --- /dev/null +++ b/dvc/reflink_ffi.py @@ -0,0 +1,88 @@ +import platform +from cffi import FFI + + +WINDOWS_SRC = """ +int reflink(char *src, char *dst) +{ + return -1; +} +""" + +DARWIN_SRC = """ +#include +/* NOTE: sys/clonefile.h is available since OS X 10.12 */ +#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 +#include /* for clonefile(2) */ +#include +int reflink(char *src, char *dst) +{ + return clonefile(src, dst, 0); +} +#else +int reflink(char *src, char *dst) +{ + return -1; +} +#endif +""" + +LINUX_SRC = """ +#include +#include +#include +#include +#include +#include +#include +int reflink(char *src, char *dst) +{ + int ret = -1; +#ifdef FICLONE + int src_fd = -1; + int dst_fd = -1; + + src_fd = open(src, O_RDONLY); + if (src_fd < 0) + return ret; + + dst_fd = open(dst, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); + if (src_fd < 0) { + close(src_fd); + return ret; + } + + ret = ioctl(dst_fd, FICLONE, src_fd); + if (ret != 0) + unlink(dst); + +out: + if (src_fd >= 0) + close(src_fd); + if (dst_fd >= 0) + close(dst_fd); +#endif + return ret; +} +""" + +ffibuilder = FFI() + +ffibuilder.cdef(""" +int reflink(char *src, char *dst); +""") + +system = platform.system() +if system == 'Windows': + src = WINDOWS_SRC +elif system == 'Linux': + src = LINUX_SRC +elif system == 'Darwin': + src = DARWIN_SRC +else: + raise NotImplementedError + +ffibuilder.set_source("reflink", src) + +if __file__ == '__main__': + ffibuilder.compile(verbose=True) diff --git a/dvc/system.py b/dvc/system.py index 0bc454e94..09bf6836b 100644 --- a/dvc/system.py +++ b/dvc/system.py @@ -1,6 +1,5 @@ import os import ctypes -import reflink if os.name == 'nt': import ntfsutils.hardlink as winlink @@ -36,7 +35,15 @@ class System(object): @staticmethod def reflink(source, link_name): - return reflink.reflink(source, link_name) + from dvc.reflink import reflink + from dvc.exceptions import DvcException + + ret = reflink(source, link_name) + if ret != 0: + raise DvcException("Failed to reflink '{}' -> '{}' returned " + "non-zero code '{}'".format(source, + link_name, + ret)) # FIXME # Temporary fix while waiting for the PR to be merged and released: diff --git a/requirements.txt b/requirements.txt index 9dc90468d..8e64030ef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,9 +18,9 @@ pyasn1>=0.4.1 schema>=0.6.7 paramiko>=2.4.1 jsonpath-rw==1.4.0 -reflink==0.2.0 requests>=2.18.4 wheel>=0.31.1 futures>=3.2.0; python_version == "2.7" grandalf==0.6 asciicanvas==0.0.3 +cffi diff --git a/setup.py b/setup.py index f2cdfd56d..074be0903 100644 --- a/setup.py +++ b/setup.py @@ -21,10 +21,10 @@ install_requires = [ "pyasn1>=0.4.1", "schema>=0.6.7", "jsonpath-rw==1.4.0", - "reflink==0.2.0", "requests>=2.18.4", "grandalf==0.6", "asciicanvas==0.0.3", + "cffi", ] # Extra dependencies for remote integrations