distgit: Harden the hardlink script a bit

Turns out the lookaside cache is not as clean as I expected.

Specifically, it contains some files where we'd expect directories, for
example:

/srv/cache/lookaside/pkgs/GFS-kernel/@13013.1e77f453ba1c86cd7616087d0643bbd8e
/srv/cache/lookaside/pkgs/openswan/tmpLRV5Gn5556cb2fcea6ba862ce14e1debf98b6d

This commit makes the script print an error instead of crashing on an
OSError in such a case.
This commit is contained in:
Mathieu Bridon 2015-07-13 22:55:21 +02:00
parent e7074e8ad9
commit a11c53cb4d

View file

@ -68,7 +68,7 @@ def get_file_hash(full_path, hashtype):
def verify_source(dir, expected_name, expected_hash, hashtype):
sources = os.listdir(dir)
sources = listdir(dir)
if len(sources) == 0:
raise Exception("No source file in %s" % dir)
@ -89,6 +89,19 @@ def verify_source(dir, expected_name, expected_hash, hashtype):
return source_path
def listdir(dir):
try:
for f in os.listdir(dir):
yield f
except OSError as e:
if e.errno == errno.ENOTDIR:
error("%s is not a directory" % dir)
else:
raise
def makedirs(dir):
try:
os.makedirs(dir)
@ -122,18 +135,18 @@ def main(root, link_hashtype, perform=False):
except OSError as e:
die(e)
for pkg_name in os.listdir(root):
for source_name in os.listdir(pkg_name):
for pkg_name in listdir(root):
for source_name in listdir(pkg_name):
source_dir = os.path.join(pkg_name, source_name)
for hash in os.listdir(source_dir):
for hash in listdir(source_dir):
if hash in ('md5', 'sha512'):
# This is not a hash, but a new-style path containing the
# hashtype. Let's just verify what it contains
hashtype = hash
hashtype_dir = os.path.join(source_dir, hash)
for hash in os.listdir(hashtype_dir):
for hash in listdir(hashtype_dir):
try:
verify_source(os.path.join(hashtype_dir, hash),
source_name, hash, hashtype)