Skip to content

FIX: Use realpath to determine hard link source #1388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Next release
* API: Default model level for the bedpostx workflow has been set to "2" following FSL 5.0.9 lead
* ENH: New interfaces for interacting with AWS S3: S3DataSink and S3DataGrabber (https://github.com/nipy/nipype/pull/1201)
* ENH: Interfaces for MINC tools (https://github.com/nipy/nipype/pull/1304)
* FIX: Use realpath to determine hard link source (https://github.com/nipy/nipype/pull/1388)

Release 0.11.0 (September 15, 2015)
============
Expand Down
13 changes: 9 additions & 4 deletions nipype/utils/filemanip.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def nipype_hardlink_wrapper(raw_src, raw_dst):
If the hardlink fails, then fall back to using
a standard copy.
"""
src = os.path.normpath(raw_src)
# Use realpath to avoid hardlinking symlinks
src = os.path.realpath(raw_src)
# Use normpath, in case destination is a symlink
dst = os.path.normpath(raw_dst)
del raw_src
del raw_dst
Expand Down Expand Up @@ -283,12 +285,15 @@ def copyfile(originalfile, newfile, copy=False, create_new=False,
matofile = originalfile[:-4] + ".mat"
if os.path.exists(matofile):
matnfile = newfile[:-4] + ".mat"
copyfile(matofile, matnfile, copy)
copyfile(hdrofile, hdrnfile, copy)
copyfile(matofile, matnfile, copy, create_new, hashmethod,
use_hardlink)
copyfile(hdrofile, hdrnfile, copy, create_new, hashmethod,
use_hardlink)
elif originalfile.endswith(".BRIK"):
hdrofile = originalfile[:-5] + ".HEAD"
hdrnfile = newfile[:-5] + ".HEAD"
copyfile(hdrofile, hdrnfile, copy)
copyfile(hdrofile, hdrnfile, copy, create_new, hashmethod,
use_hardlink)

return newfile

Expand Down
35 changes: 35 additions & 0 deletions nipype/utils/tests/test_filemanip.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,41 @@ def test_copyfiles():
os.unlink(new_hdr2)


def test_linkchain():
if os.name is not 'posix':
return
orig_img, orig_hdr = _temp_analyze_files()
pth, fname = os.path.split(orig_img)
new_img1 = os.path.join(pth, 'newfile1.img')
new_hdr1 = os.path.join(pth, 'newfile1.hdr')
new_img2 = os.path.join(pth, 'newfile2.img')
new_hdr2 = os.path.join(pth, 'newfile2.hdr')
new_img3 = os.path.join(pth, 'newfile3.img')
new_hdr3 = os.path.join(pth, 'newfile3.hdr')
copyfile(orig_img, new_img1)
yield assert_true, os.path.islink(new_img1)
yield assert_true, os.path.islink(new_hdr1)
copyfile(new_img1, new_img2, copy=True)
yield assert_false, os.path.islink(new_img2)
yield assert_false, os.path.islink(new_hdr2)
yield assert_false, os.path.samefile(orig_img, new_img2)
yield assert_false, os.path.samefile(orig_hdr, new_hdr2)
copyfile(new_img1, new_img3, copy=True, use_hardlink=True)
yield assert_false, os.path.islink(new_img3)
yield assert_false, os.path.islink(new_hdr3)
yield assert_true, os.path.samefile(orig_img, new_img3)
yield assert_true, os.path.samefile(orig_hdr, new_hdr3)
os.unlink(new_img1)
os.unlink(new_hdr1)
os.unlink(new_img2)
os.unlink(new_hdr2)
os.unlink(new_img3)
os.unlink(new_hdr3)
# final cleanup
os.unlink(orig_img)
os.unlink(orig_hdr)


def test_filename_to_list():
x = filename_to_list('foo.nii')
yield assert_equal, x, ['foo.nii']
Expand Down