scripts/file_utils/unln.py

183 lines
6.6 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
#
# unln.py, Copyright © 2013 Matteo Cypriani <mcy@lm7.fr>
#
########################################################################
# This program is licensed under the terms of the Expat license.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
########################################################################
#
# This script separates file names given as arguments from their respective
# inodes by doing the equivalent of (cp -p file tmp && mv tmp file).
import argparse
import sys
import os
import tempfile
import shutil
def verbose(*message, **args):
"""Prints the message on the standard output if verbose mode is on.
This function is a simple wrapper around print(), and you can use any
keyword argument you would use with print().
"""
if options.verbose:
print(*message, **args)
def warn(*message, prefix="Warning!", **args):
"""Prints the message on the error output, prepended by 'prefix'.
The standard output is flushed prior to printing the error message, to
enable the messages to be displayed in the right order.
This function is a simple wrapper around print(), and you can use any
keyword argument you would use with print().
"""
sys.stdout.flush()
print(prefix, *message, file=sys.stderr, **args)
def quote(string):
""" Quotes a string. """
return "``{}´´".format(string)
def dereference(path):
"""Follows a symbolic link recursively.
Returns the final target's path, that may or may not exist in the file
system. If 'path' is not a symbolic link, it is returned as is.
"""
if not os.path.islink(path):
return path
dereferenced = os.readlink(path)
# If it's a relative link we must convert it to an absolute path
if not os.path.isabs(dereferenced):
dereferenced = os.path.join(os.path.dirname(path), dereferenced)
return dereference(dereferenced)
# Parse command-line arguments
arg_parser = argparse.ArgumentParser(
description="Separates a file name from its other hard links",
epilog="For more information about this program, see the README file \
provided with the distribution.")
arg_parser.add_argument("-L", "--dereference", action="store_true",
help="follow symbolic links")
arg_parser.add_argument("-s", "--sync", action="store_true",
help="sync files after a copy to be more fail safe \
(requires Python 3.3 or above, will be ignored with lower versions)")
arg_parser.add_argument("-v", "--verbose", action="store_true",
help="increase output verbosity")
arg_parser.add_argument("filenames", metavar="file", nargs="+",
help="file names to work on")
options = arg_parser.parse_args()
# List of filenames that were supposed to be worked on but could not because of
# an error
error_filenames = []
# Main loop
for filename in options.filenames:
# Is a symbolic link?
if os.path.islink(filename):
if not options.dereference:
warn(quote(filename), "is a symbolic link, ignoring.")
continue
# Dereference the link
dereferenced = dereference(filename)
verbose("Dereferenced {} -> {}.".format(
quote(filename), quote(dereferenced)))
filename = dereferenced
# Exists?
if not os.path.exists(filename):
warn(quote(filename), "does not exist, ignoring.")
continue
# Is a regular file?
if not os.path.isfile(filename):
warn(quote(filename), "is not a regular file, ignoring.")
continue
# Number of links?
nlinks = os.stat(filename).st_nlink
if nlinks == 1:
verbose(quote(filename), "has only one link, ignoring.")
continue
verbose(quote(filename), "has", nlinks, "hard links, proceeding", end="... ")
# Reserve a temporary file name
dirname = os.path.dirname(filename) # filename's directory
progname = os.path.basename(__file__) # name of this program
try:
handle, tmpfilename = tempfile.mkstemp(
prefix="{}-".format(filename),
suffix="-{}.tmp".format(progname),
dir=dirname)
except:
warn("Cannot create temporary file to copy {}:".format(quote(filename)),
sys.exc_info()[1])
error_filenames.append(filename)
continue
os.close(handle) # we just need a temporary file name
# Copy the original file to the temporary file
verbose("Copying to temporary file", quote(tmpfilename), end="... ")
try:
shutil.copy2(filename, tmpfilename) # cp -p
except:
warn("Cannot copy {} to {}:".format(quote(filename), quote(tmpfilename)),
sys.exc_info()[1])
error_filenames.append(filename)
# Delete temporary file
os.remove(tmpfilename)
continue
# Sync (if we are running Python >= 3.3)
if options.sync and sys.version_info.minor >= 3:
verbose("Syncing", end="... ")
os.sync()
# Rename the temporary file with the original file name
verbose("Moving back {} to {}...".format(
quote(tmpfilename), quote(filename)))
try:
os.rename(tmpfilename, filename)
except:
warn("Cannot move {} to {}:".format(
quote(tmpfilename), quote(filename)),
sys.exc_info()[1], "Deleting temporary file", quote(tmpfilename))
error_filenames.append(filename)
# Delete temporary file
os.remove(tmpfilename)
# Display the list of files in error
if error_filenames:
warn("The following regular files still have more than one hard link:")
print(*error_filenames, sep="\n", file=sys.stderr)