From 4ff9ee8902529714487ef3b35b4f6562b4ce1dd6 Mon Sep 17 00:00:00 2001 From: Robbe Derks Date: Mon, 10 Jan 2022 16:57:05 +0100 Subject: [PATCH] monkeypatch os.link (#23480) --- common/file_helpers.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/common/file_helpers.py b/common/file_helpers.py index 3373d82a03daaa..592b2a199a75e5 100644 --- a/common/file_helpers.py +++ b/common/file_helpers.py @@ -81,6 +81,17 @@ def _get_fileobject(): return writer.get_fileobject(dir=temp_dir) return _get_fileobject +def monkeypatch_os_link(): + # This is neccesary on EON/C2, where os.link is patched out of python + if not hasattr(os, 'link'): + from cffi import FFI + ffi = FFI() + ffi.cdef("int link(const char *oldpath, const char *newpath);") + libc = ffi.dlopen(None) + + def link(src, dest): + return libc.link(src.encode(), dest.encode()) + os.link = link def atomic_write_on_fs_tmp(path, **kwargs): """Creates an atomic writer using a temporary file in a temporary directory @@ -88,6 +99,7 @@ def atomic_write_on_fs_tmp(path, **kwargs): """ # TODO(mgraczyk): This use of AtomicWriter relies on implementation details to set the temp # directory. + monkeypatch_os_link() writer = AtomicWriter(path, **kwargs) return writer._open(_get_fileobject_func(writer, get_tmpdir_on_same_filesystem(path))) @@ -96,5 +108,6 @@ def atomic_write_in_dir(path, **kwargs): """Creates an atomic writer using a temporary file in the same directory as the destination file. """ + monkeypatch_os_link() writer = AtomicWriter(path, **kwargs) return writer._open(_get_fileobject_func(writer, os.path.dirname(path)))