From f2c37cae260c4050fb64d224aa827c7ed4e7fb3d Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Sun, 18 Aug 2013 20:09:14 -0700 Subject: [PATCH 1/6] Fix division-by-zero bug --- src/bitrot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bitrot.py b/src/bitrot.py index 97fe219..4275b99 100644 --- a/src/bitrot.py +++ b/src/bitrot.py @@ -112,7 +112,7 @@ def run(verbosity=1, test=False): new_mtime = int(st.st_mtime) current_size += st.st_size if verbosity: - size_fmt = '\r{:>6.1%}'.format(current_size/total_size) + size_fmt = '\r{:>6.1%}'.format(current_size/total_size if total_size > 0 else 0) if size_fmt != last_reported_size: sys.stdout.write(size_fmt) sys.stdout.flush() From 3b3770d46a495909ffab32eecc58f036daa6a122 Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Sun, 18 Aug 2013 20:16:36 -0700 Subject: [PATCH 2/6] Ignore broken symlinks/files that disappear --- src/bitrot.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/bitrot.py b/src/bitrot.py index 97fe219..52ce145 100644 --- a/src/bitrot.py +++ b/src/bitrot.py @@ -101,11 +101,17 @@ def run(verbosity=1, test=False): for path, _, files in os.walk(current_dir): for f in files: p = os.path.join(path, f) - st = os.stat(p) - if not stat.S_ISREG(st.st_mode) or p == bitrot_db: - continue - paths.append(p) - total_size += st.st_size + try: + st = os.stat(p) + except OSError as ex: + #import pdb; pdb.set_trace() + if ex.errno != 2: + raise + else: + if not stat.S_ISREG(st.st_mode) or p == bitrot_db: + continue + paths.append(p) + total_size += st.st_size paths.sort() for p in paths: st = os.stat(p) From 37104d7b78352c2b3e6c954056b81853fbf20aa2 Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Fri, 23 Aug 2013 14:04:09 -0700 Subject: [PATCH 3/6] Clean up division by zero fix --- src/bitrot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bitrot.py b/src/bitrot.py index 4275b99..cd449db 100644 --- a/src/bitrot.py +++ b/src/bitrot.py @@ -112,7 +112,7 @@ def run(verbosity=1, test=False): new_mtime = int(st.st_mtime) current_size += st.st_size if verbosity: - size_fmt = '\r{:>6.1%}'.format(current_size/total_size if total_size > 0 else 0) + size_fmt = '\r{:>6.1%}'.format(current_size/(total_size or 1)) if size_fmt != last_reported_size: sys.stdout.write(size_fmt) sys.stdout.flush() From a9b57b5814b45b2d8f5e0f810ae5719e4f5ddd9f Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Fri, 23 Aug 2013 14:05:21 -0700 Subject: [PATCH 4/6] Remove dangling pdb usage --- src/bitrot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/bitrot.py b/src/bitrot.py index 52ce145..0932c21 100644 --- a/src/bitrot.py +++ b/src/bitrot.py @@ -104,7 +104,6 @@ def run(verbosity=1, test=False): try: st = os.stat(p) except OSError as ex: - #import pdb; pdb.set_trace() if ex.errno != 2: raise else: From 0afdaddd0a20200ab61dd318194ee125dc04732c Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Mon, 26 Aug 2013 18:59:00 -0700 Subject: [PATCH 5/6] Create index on hash --- src/bitrot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bitrot.py b/src/bitrot.py index 97fe219..00728c3 100644 --- a/src/bitrot.py +++ b/src/bitrot.py @@ -70,12 +70,12 @@ def get_sqlite3_cursor(path, copy=False): conn = sqlite3.connect(path) atexit.register(conn.close) cur = conn.cursor() - for name, in cur.execute('SELECT name FROM sqlite_master'): - if name == 'bitrot': - break - else: + names = set(name for name, in cur.execute('SELECT name FROM sqlite_master')) + if 'bitrot' not in names: cur.execute('CREATE TABLE bitrot (path TEXT PRIMARY KEY, ' 'mtime INTEGER, hash TEXT, timestamp TEXT)') + if 'bitrot_hash_idx' not in names: + cur.execute('CREATE INDEX bitrot_hash_idx ON bitrot (hash)') return conn From af81b67d5823fcb281e5b9ee8925a560b560d470 Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Thu, 17 Oct 2013 11:42:23 -0700 Subject: [PATCH 6/6] Use proper defined constant ENOENT --- src/bitrot.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bitrot.py b/src/bitrot.py index 0932c21..63a1184 100644 --- a/src/bitrot.py +++ b/src/bitrot.py @@ -29,6 +29,7 @@ from __future__ import unicode_literals import argparse import atexit import datetime +import errno import hashlib import os import shutil @@ -104,7 +105,7 @@ def run(verbosity=1, test=False): try: st = os.stat(p) except OSError as ex: - if ex.errno != 2: + if ex.errno != errno.ENOENT: raise else: if not stat.S_ISREG(st.st_mode) or p == bitrot_db: