From f7c9f837634738da105056a8b66457afa3ff8636 Mon Sep 17 00:00:00 2001 From: Jon Erling Hustadnes Date: Mon, 17 Dec 2018 18:30:52 +0100 Subject: Added check if bookmark file is a symlink. If it is, the os.rename is performed on the original file path instead of overwriting the symlink. --- ranger/container/bookmarks.py | 8 +++++++- tests/ranger/container/test_bookmarks.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ranger/container/bookmarks.py b/ranger/container/bookmarks.py index 59838c00..846a6585 100644 --- a/ranger/container/bookmarks.py +++ b/ranger/container/bookmarks.py @@ -185,7 +185,13 @@ class Bookmarks(FileManagerAware): old_perms = os.stat(self.path) os.chown(path_new, old_perms.st_uid, old_perms.st_gid) os.chmod(path_new, old_perms.st_mode) - os.rename(path_new, self.path) + + if os.path.islink(self.path): + link_path = os.path.realpath(self.path) + os.rename(path_new, link_path) + else: + os.rename(path_new, self.path) + except OSError as ex: self.fm.notify('Bookmarks error: {0}'.format(str(ex)), bad=True) return diff --git a/tests/ranger/container/test_bookmarks.py b/tests/ranger/container/test_bookmarks.py index 6fba2a3d..7802bd5d 100644 --- a/tests/ranger/container/test_bookmarks.py +++ b/tests/ranger/container/test_bookmarks.py @@ -56,3 +56,20 @@ def testbookmarks(tmpdir): secondstore.update_if_outdated() secondstore.update = origupdate secondstore.update_if_outdated() + + +def test_bookmark_symlink(tmpdir): + # Initialize plain file and symlink paths + bookmarkfile_link = tmpdir.join("bookmarkfile") + bookmarkfile_orig = tmpdir.join("bookmarkfile.org") + + # Create symlink pointing towards the original plain file. + os.symlink(bookmarkfile_orig, bookmarkfile_link) + + # Initialize the bookmark file and save the file. + bmstore = Bookmarks(str(bookmarkfile_link)) + bmstore.save() + + # Once saved, the bookmark file should still be a symlink pointing towards the plain file. + assert os.path.islink(bookmarkfile_link) + assert not os.path.islink(bookmarkfile_orig) -- cgit 1.4.1-2-gfad0