Using Python 2.5+, UNIX:
I have a program which simulates directory "copy-on-write" functionality, by hardlinking all entries. Currently all the underlying code, some of which I don't have access to, uses standard open(fname, 'w')
to write the regular files.
But with hardlinks, this means the same inode is used (just truncated), so the original content is destroyed as well. For copy-on-write, I would of course want the original to remain untouched (old inode) and the open('w')
call to create a new inode.
Any ideas on the best way to achieve this? Monkey-patch open
somehow?
What I came up with so far is overriding open
to try to delete the file first (if it exists) and only then do open('w')
:
import __builtin__
_open = __builtin__.open
def my_open(name, mode='r', *args, **kwargs):
"""Simulate copy-on-write, by deleting the file first if it exists"""
if 'w' in mode and os.path.exists(name): # TODO: use isfile()?
os.remove(name)
return _open(name, mode, *args, **kwargs)
__builtin__.open = my_open