From: Johannes Berg Date: Thu, 31 Dec 2020 22:29:58 +0000 (+0100) Subject: metadata: fix symlink stat() vs. readlink() race X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=bup.git;a=commitdiff_plain;h=03b9ae219db65e7bdfa7e7669761889dfdf1f591 metadata: fix symlink stat() vs. readlink() race We might stat() the file and get some size, but by the time we readlink() the link has changed and the size is something else. Arguably, we can't really avoid races here if we don't have a consistent snapshot of the filesystem to save, however, in this case we later get an assertion failure when the data is read back from the index (or repo), and it's easy to avoid. Set the size from the actually recorded symlink target. Signed-off-by: Johannes Berg Reviewed-by: Rob Browning Tested-by: Rob Browning --- diff --git a/lib/bup/metadata.py b/lib/bup/metadata.py index 0d57780..67f3a07 100644 --- a/lib/bup/metadata.py +++ b/lib/bup/metadata.py @@ -479,6 +479,9 @@ class Metadata: try: if stat.S_ISLNK(st.st_mode): self.symlink_target = os.readlink(path) + # might have read a different link than the + # one that was in place when we did stat() + self.size = len(self.symlink_target) except OSError as e: add_error('readlink: %s' % e)