summaryrefslogtreecommitdiff
path: root/rdiff-backup/rdiff_backup/Rdiff.py
blob: 9ad6afd4757f2d68f58c838ccc5abd466e8a3e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Copyright 2002 Ben Escoto
#
# This file is part of rdiff-backup.
#
# rdiff-backup is free software; you can redistribute it and/or modify
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# rdiff-backup is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rdiff-backup; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

"""Invoke rdiff utility to make signatures, deltas, or patch"""

import os, librsync
import Globals, log, static, TempFile, rpath


def get_signature(rp, blocksize = None):
	"""Take signature of rpin file and return in file object"""
	if not blocksize: blocksize = find_blocksize(rp.getsize())
	log.Log("Getting signature of %s with blocksize %s" %
			(rp.get_indexpath(), blocksize), 7)
	return librsync.SigFile(rp.open("rb"), blocksize)

def find_blocksize(file_len):
	"""Return a reasonable block size to use on files of length file_len

	If the block size is too big, deltas will be bigger than is
	necessary.  If the block size is too small, making deltas and
	patching can take a really long time.

	"""
	if file_len < 1024000: return 512 # set minimum of 512 bytes
	else: # Split file into about 2000 pieces, rounding to 512
		return long((file_len/(2000*512))*512)

def get_delta_sigfileobj(sig_fileobj, rp_new):
	"""Like get_delta but signature is in a file object"""
	log.Log("Getting delta of %s with signature stream" % (rp_new.path,), 7)
	return librsync.DeltaFile(sig_fileobj, rp_new.open("rb"))

def get_delta_sigrp(rp_signature, rp_new):
	"""Take signature rp and new rp, return delta file object"""
	log.Log("Getting delta of %s with signature %s" %
			(rp_new.path, rp_signature.get_indexpath()), 7)
	return librsync.DeltaFile(rp_signature.open("rb"), rp_new.open("rb"))

def write_delta(basis, new, delta, compress = None):
	"""Write rdiff delta which brings basis to new"""
	log.Log("Writing delta %s from %s -> %s" %
			(basis.path, new.path, delta.path), 7)
	deltafile = librsync.DeltaFile(get_signature(basis), new.open("rb"))
	delta.write_from_fileobj(deltafile, compress)

def write_patched_fp(basis_fp, delta_fp, out_fp):
	"""Write patched file to out_fp given input fps.  Closes input files"""
	rpath.copyfileobj(librsync.PatchedFile(basis_fp, delta_fp), out_fp)
	assert not basis_fp.close() and not delta_fp.close()

def write_via_tempfile(fp, rp):
	"""Write fileobj fp to rp by writing to tempfile and renaming"""
	tf = TempFile.new(rp)
	tf.write_from_fileobj(fp)
	rpath.rename(tf, rp)

def patch_local(rp_basis, rp_delta, outrp = None, delta_compressed = None):
	"""Patch routine that must be run locally, writes to outrp

	This should be run local to rp_basis because it needs to be a real
	file (librsync may need to seek around in it).  If outrp is None,
	patch rp_basis instead.

	"""
	assert rp_basis.conn is Globals.local_connection
	if delta_compressed: deltafile = rp_delta.open("rb", 1)
	else: deltafile = rp_delta.open("rb")
	patchfile = librsync.PatchedFile(rp_basis.open("rb"), deltafile)
	if outrp: outrp.write_from_fileobj(patchfile)
	else: write_via_tempfile(patchfile, rp_basis)

def copy_local(rpin, rpout, rpnew = None):
	"""Write rpnew == rpin using rpout as basis.  rpout and rpnew local"""
	assert rpout.conn is Globals.local_connection
	deltafile = rpin.conn.librsync.DeltaFile(get_signature(rpout),
											 rpin.open("rb"))
	patched_file = librsync.PatchedFile(rpout.open("rb"), deltafile)

	if rpnew: rpnew.write_from_fileobj(patched_file)
	else: write_via_tempfile(patched_file, rpout)