Created
January 21, 2014 12:20
-
-
Save jvhaarst/8539018 to your computer and use it in GitHub Desktop.
Python script to convert symbolic links to hard links, if possible.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import uuid | |
random_unique_name = str(uuid.uuid4()) + str(uuid.uuid1()) | |
def find_mount_point(path): # http://stackoverflow.com/questions/4453602/how-to-find-the-mountpoint-a-file-resides-on | |
path = os.path.abspath(path) | |
while not os.path.ismount(path): | |
path = os.path.dirname(path) | |
return path | |
for root, dirs, files in os.walk(os.getcwd()): | |
for name in files: | |
fullname = os.path.join(root, name) | |
tempname = os.path.join(root, random_unique_name) | |
if os.path.islink(fullname): | |
linkpath = os.readlink(fullname) | |
if (find_mount_point(fullname) == (find_mount_point(os.path.split(linkpath)[0]))): | |
print "%s links to %s" % (fullname, linkpath) | |
# Rename current link to something temporary | |
os.rename(fullname, tempname) | |
try: | |
# Try to create hard link for the file | |
os.link(linkpath, fullname) | |
# If we succeeded, remove temporary file | |
os.unlink(tempname) | |
print "Created hardlink for %s" % fullname | |
except: | |
# If we failed, rename the temporary file back, and abort with some meaningful info | |
e = sys.exc_info()[0] | |
sys.stderr.write("Hardlinking failed on %s, aborting !" % fullname) | |
os.rename(tempname, fullname) | |
sys.exit(e) | |
else: | |
print "%s on other FS!" % name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment