user124114
user124114

Reputation: 8692

Python copy-on-write using hardlinks

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

Upvotes: 5

Views: 797

Answers (1)

brice
brice

Reputation: 25039

Were you looking for something like this?

import sys
old_open = __builtins__.open    

# Override builtin open()
def my_open(fname, *args, **kwargs):
    # If the path is a hardlink, (ie, check that st_nlink >1)
    if os.path.isfile(fname) and os.stat(fname).st_nlink > 1: 
        os.unlink(fname)
    return old_open(fname, *args, **kwargs)
__buitlins__.open = my_open

Upvotes: 2

Related Questions