summaryrefslogtreecommitdiff
path: root/obnam/rsync.py
blob: cd083677c34530a647501e470ed9a9bfd28fd1ab (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright (C) 2006, 2007, 2008  Lars Wirzenius <liw@iki.fi>
#
# This program is free software; you can redistribute it and/or modify
# it 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.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


"""Rsync stuff for making backups"""


import logging
import os
import subprocess
import tempfile


import obnam


class UnknownCommand(obnam.ObnamException):

    def __init__(self, argv, errno):
        self._msg = "Unknown command (error %d): %s" % (errno, " ".join(argv))


class CommandFailure(obnam.ObnamException):

    def __init__(self, argv, returncode, stderr):
        self._msg = "Command failed: %s\nError code: %d\n%s" % \
                    (" ".join(argv), 
                     returncode, 
                     obnam.gpg.indent_string(stderr))


def run_command(argv, stdin=None, stdout=None, stderr=None):
    # We let stdin be None unless explicitly specified.
    if stdout is None:
        stdout = subprocess.PIPE
    if stderr is None:
        stderr = subprocess.PIPE

    try:
        p = subprocess.Popen(argv, stdin=stdin, stdout=stdout, stderr=stderr)
    except os.error, e:
        raise UnknownCommand(argv, e.errno)
    return p


def compute_signature(context, filename):
    """Compute an rsync signature for 'filename'"""

    argv = [context.config.get("backup", "odirect-pipe"),
            context.config.get("backup", "odirect-read"),
            filename,
            "rdiff", "--", "signature", "-", "-"]
    p = run_command(argv)
    stdout_data, stderr_data = p.communicate()
    
    if p.returncode == 0:
        return stdout_data
    else:
        raise CommandFailure(argv, p.returncode, stderr_data)


def compute_delta(context, signature, filename):
    """Compute an rsync delta for a file, given signature of old version
    
    Return list of ids of DELTAPART objects.
    
    """

    (fd, tempname) = tempfile.mkstemp()
    os.write(fd, signature)
    os.close(fd)

    argv = [context.config.get("backup", "odirect-pipe"),
            context.config.get("backup", "odirect-read"),
            filename,
            "rdiff", "--", "delta", tempname, "-", "-"]
    p = run_command(argv)

    list = []
    block_size = context.config.getint("backup", "block-size")
    while True:
        data = p.stdout.read(block_size)
        if not data:
            break
        id = obnam.obj.object_id_new()
        o = obnam.obj.DeltaPartObject(id=id)
        o.add(obnam.cmp.Component(obnam.cmp.DELTADATA, data))
        o = o.encode()
        obnam.io.enqueue_object(context, context.content_oq, 
                                context.contmap, id, o, False)
        list.append(id)
    exit = p.wait()
    os.remove(tempname)
    if exit == 0:
        return list
    else:
        raise CommandFailure(argv, exit, "")


def apply_delta(context, basis, deltaparts, new, open=os.open, cmd="rdiff"):
    """Apply an rsync delta for a file, to get a new version of it"""
    
    devnull = open("/dev/null", os.O_WRONLY)

    argv = [cmd, "--", "patch", basis, "-", new]

    p = run_command(argv, stdin=subprocess.PIPE, stdout=devnull)

    ret = True
    for id in deltaparts:
        deltapart = obnam.io.get_object(context, id)
        deltadata = deltapart.first_string_by_kind(obnam.cmp.DELTADATA)
        p.stdin.write(deltadata)

    stdout_data, stderr_data = p.communicate(input="")
    os.close(devnull)
    if p.returncode != 0:
        raise CommandFailure(argv, p.returncode, stderr_data)
    else:
        return ret