summaryrefslogtreecommitdiff
path: root/rdiff-backup/rdiff_backup/hash.py
diff options
context:
space:
mode:
authorbescoto <bescoto@2b77aa54-bcbc-44c9-a7ec-4f6cf2b41109>2005-10-27 06:16:39 +0000
committerbescoto <bescoto@2b77aa54-bcbc-44c9-a7ec-4f6cf2b41109>2005-10-27 06:16:39 +0000
commitd9b68d73175d004caed8c781c97308f7c2e3dccc (patch)
tree7be1e3b4c7a23324d6d4ef0ed5483b890ee58210 /rdiff-backup/rdiff_backup/hash.py
parent80470345fa1998a033078314f77930a60ea14107 (diff)
downloadrdiff-backup-d9b68d73175d004caed8c781c97308f7c2e3dccc.tar.gz
Write SHA1 digests for all regular files
git-svn-id: http://svn.savannah.nongnu.org/svn/rdiff-backup/trunk@662 2b77aa54-bcbc-44c9-a7ec-4f6cf2b41109
Diffstat (limited to 'rdiff-backup/rdiff_backup/hash.py')
-rw-r--r--rdiff-backup/rdiff_backup/hash.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/rdiff-backup/rdiff_backup/hash.py b/rdiff-backup/rdiff_backup/hash.py
new file mode 100644
index 0000000..4fcbdab
--- /dev/null
+++ b/rdiff-backup/rdiff_backup/hash.py
@@ -0,0 +1,53 @@
+# Copyright 2005 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
+
+"""Contains a file wrapper that returns a hash on close"""
+
+import sha
+
+class FileWrapper:
+ """Wrapper around a file-like object
+
+ Only use this with files that will be read through in a single
+ pass and then closed. (There is no seek().) When you close it,
+ return value will be a Report.
+
+ Currently this just calculates a sha1sum of the datastream.
+
+ """
+ def __init__(self, fileobj):
+ self.fileobj = fileobj
+ self.sha1 = sha.new()
+ self.closed = 0
+
+ def read(self, length = -1):
+ assert not self.closed
+ buf = self.fileobj.read(length)
+ self.sha1.update(buf)
+ return buf
+
+ def close(self):
+ return Report(self.fileobj.close(), self.sha1.hexdigest())
+
+
+class Report:
+ """Hold final information about a byte stream"""
+ def __init__(self, close_val, sha1_digest):
+ assert not close_val # For now just assume inner file closes correctly
+ self.sha1_digest = sha1_digest