summaryrefslogtreecommitdiff
path: root/obnamlib/whole_file_checksummer.py
blob: 6c4aa5dfd247d08ee6e99942fb883ebaf6104c09 (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
# Copyright 2016  Lars Wirzenius
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import obnamlib


class WholeFileCheckSummer(object):

    '''Compute a whole-file checksum.

    Ask the repository its preferred checksum algorithm. Use that.

    If the algorithm is MD5, compute the checksum from all the bytes
    in the file. For everything else, compute the checksum from (size,
    checksum) pairs for all the chunks in the file. This convoluted
    thing is because the latter is necessary for speed, and the former
    is necessary for backwards compatibility.

    '''

    def __init__(self, file_key):
        self._all_bytes = file_key == obnamlib.REPO_FILE_MD5
        self._use_hex = file_key != obnamlib.REPO_FILE_MD5
        self._summer = self._create_checksum_algorithm(file_key)

    def _create_checksum_algorithm(self, file_key):
        if file_key is None:
            return _NullChecksum()
        name = obnamlib.get_checksum_algorithm_name(file_key)
        return obnamlib.get_checksum_algorithm(name)

    def append_chunk(self, chunk_data, chunk_id):
        if self._all_bytes:
            self._summer.update(chunk_data)
        else:
            thing = '{},{};'.format(len(chunk_data), chunk_id)
            self._summer.update(thing)

    def get_checksum(self):
        '''Get the current whole-file checksum.'''
        if self._use_hex:
            return self._summer.hexdigest()
        else:
            return self._summer.digest()


class _NullChecksum(object):

    def update(self, data):
        pass

    def hexdigest(self):
        return None