From a11c53cb4dfe1d3d6252dd05b18d5f45cac24492 Mon Sep 17 00:00:00 2001 From: Mathieu Bridon Date: Mon, 13 Jul 2015 22:55:21 +0200 Subject: [PATCH] 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. --- roles/distgit/files/make-new-lookaside-links | 23 +++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/roles/distgit/files/make-new-lookaside-links b/roles/distgit/files/make-new-lookaside-links index c5c9b5d1f7..09fdf9533d 100755 --- a/roles/distgit/files/make-new-lookaside-links +++ b/roles/distgit/files/make-new-lookaside-links @@ -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)