summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Thursfield <sam.thursfield@codethink.co.uk>2015-04-22 15:58:45 +0000
committerSam Thursfield <sam.thursfield@codethink.co.uk>2015-05-07 13:58:06 +0000
commit6db8c4f9f6547f18c6e4dccca9f296cb9eb3e9d5 (patch)
treefe7640bf5a692beda314922cbd99f18f1c3cf92f
parent07a0b8322d2925e2c813a7f5541b0593c46f6693 (diff)
downloadmorph-6db8c4f9f6547f18c6e4dccca9f296cb9eb3e9d5.tar.gz
Make listing contents of local tarball cache more robust
Previously it would crash with a backtrace if there were unexpected filenames in the directory. It's still not amazingly robust, but I don't have time to rewrite the whole thing now. This code seems to have ignored that cachefs.walkfiles() returns filenames with a preceeding '/', which I have fixed now. Change-Id: I98b3094bd6c82b26984513ee81a1eab9bf253a34
-rw-r--r--morphlib/localartifactcache.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/morphlib/localartifactcache.py b/morphlib/localartifactcache.py
index e6695c4e..19125deb 100644
--- a/morphlib/localartifactcache.py
+++ b/morphlib/localartifactcache.py
@@ -129,9 +129,20 @@ class LocalArtifactCache(object):
returns a [(cache_key, set(artifacts), last_used)]
'''
+ def is_artifact(filename):
+ # This is just enough to avoid crashes from random unpacked
+ # directory trees and temporary files in the cachedir. A
+ # better mechanism is needed. It's not simple to tell
+ # OSFS.walkfiles() to do a non-recursive walk, sadly.
+ return '.' in filename and '/' not in filename
+
CacheInfo = collections.namedtuple('CacheInfo', ('artifacts', 'mtime'))
contents = collections.defaultdict(lambda: CacheInfo(set(), 0))
for filename in self.cachefs.walkfiles():
+ # filenames are returned with a preceeding /.
+ filename = filename[1:]
+ if not is_artifact(filename): # pragma: no cover
+ continue
cachekey = filename[:63]
artifact = filename[65:]
artifacts, max_mtime = contents[cachekey]
@@ -146,5 +157,5 @@ class LocalArtifactCache(object):
def remove(self, cachekey):
'''Remove all artifacts associated with the given cachekey.'''
for filename in (x for x in self.cachefs.walkfiles()
- if x.startswith(cachekey)):
+ if x[1:].startswith(cachekey)):
self.cachefs.remove(filename)