1 |
# Copyright 2002 Ben Escoto |
# Copyright 2002 2005 Ben Escoto |
2 |
# |
# |
3 |
# This file is part of rdiff-backup. |
# This file is part of rdiff-backup. |
4 |
# |
# |
20 |
"""Invoke rdiff utility to make signatures, deltas, or patch""" |
"""Invoke rdiff utility to make signatures, deltas, or patch""" |
21 |
|
|
22 |
import os, librsync |
import os, librsync |
23 |
import Globals, log, static, TempFile, rpath |
import Globals, log, static, TempFile, rpath, hash |
24 |
|
|
25 |
|
|
26 |
def get_signature(rp, blocksize = None): |
def get_signature(rp, blocksize = None): |
53 |
(rp_new.path, rp_signature.get_indexpath()), 7) |
(rp_new.path, rp_signature.get_indexpath()), 7) |
54 |
return librsync.DeltaFile(rp_signature.open("rb"), rp_new.open("rb")) |
return librsync.DeltaFile(rp_signature.open("rb"), rp_new.open("rb")) |
55 |
|
|
56 |
|
def get_delta_sigrp_hash(rp_signature, rp_new): |
57 |
|
"""Like above but also calculate hash of new as close() value""" |
58 |
|
log.Log("Getting delta with hash of %s with signature %s" % |
59 |
|
(rp_new.path, rp_signature.get_indexpath()), 7) |
60 |
|
return librsync.DeltaFile(rp_signature.open("rb"), |
61 |
|
hash.FileWrapper(rp_new.open("rb"))) |
62 |
|
|
63 |
|
|
64 |
def write_delta(basis, new, delta, compress = None): |
def write_delta(basis, new, delta, compress = None): |
65 |
"""Write rdiff delta which brings basis to new""" |
"""Write rdiff delta which brings basis to new""" |
66 |
log.Log("Writing delta %s from %s -> %s" % |
log.Log("Writing delta %s from %s -> %s" % |
76 |
def write_via_tempfile(fp, rp): |
def write_via_tempfile(fp, rp): |
77 |
"""Write fileobj fp to rp by writing to tempfile and renaming""" |
"""Write fileobj fp to rp by writing to tempfile and renaming""" |
78 |
tf = TempFile.new(rp) |
tf = TempFile.new(rp) |
79 |
tf.write_from_fileobj(fp) |
retval = tf.write_from_fileobj(fp) |
80 |
rpath.rename(tf, rp) |
rpath.rename(tf, rp) |
81 |
|
return retval |
82 |
|
|
83 |
def patch_local(rp_basis, rp_delta, outrp = None, delta_compressed = None): |
def patch_local(rp_basis, rp_delta, outrp = None, delta_compressed = None): |
84 |
"""Patch routine that must be run locally, writes to outrp |
"""Patch routine that must be run locally, writes to outrp |
92 |
if delta_compressed: deltafile = rp_delta.open("rb", 1) |
if delta_compressed: deltafile = rp_delta.open("rb", 1) |
93 |
else: deltafile = rp_delta.open("rb") |
else: deltafile = rp_delta.open("rb") |
94 |
patchfile = librsync.PatchedFile(rp_basis.open("rb"), deltafile) |
patchfile = librsync.PatchedFile(rp_basis.open("rb"), deltafile) |
95 |
if outrp: outrp.write_from_fileobj(patchfile) |
if outrp: return outrp.write_from_fileobj(patchfile) |
96 |
else: write_via_tempfile(patchfile, rp_basis) |
else: return write_via_tempfile(patchfile, rp_basis) |
97 |
|
|
98 |
def copy_local(rpin, rpout, rpnew = None): |
def copy_local(rpin, rpout, rpnew = None): |
99 |
"""Write rpnew == rpin using rpout as basis. rpout and rpnew local""" |
"""Write rpnew == rpin using rpout as basis. rpout and rpnew local""" |