summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2021-10-22 03:59:14 -1000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2021-10-23 17:47:38 +0100
commit9827758ebe3c0b6b4f06dedc814b810629b184e3 (patch)
treec12ab6e8d01d071a0027c5431ef72db16736a71c /bitbake/lib/bb
parenta07f53efa876d100190dc10351277f4a629ab9a1 (diff)
downloadpoky-9827758ebe3c0b6b4f06dedc814b810629b184e3.tar.gz
bitbake: fetch2/git: Avoid races over mirror tarball creation
There is a potential race over the mirror tarballs where a partial git repo could be extracted causing fetcher failures if the tarball is being rewritten whilst another build accesses it. Create the mirror tarball atomically to avoid this. [YOCTO #14441] (Bitbake rev: e3da0ecbd282da060b52a4bcf3ed36497295fde0) Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org> (cherry picked from commit 3250bc950c56bd7dd2114df26e5a8e13b04ceac8) Signed-off-by: Steve Sakoman <steve@sakoman.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb')
-rw-r--r--bitbake/lib/bb/fetch2/git.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/bitbake/lib/bb/fetch2/git.py b/bitbake/lib/bb/fetch2/git.py
index 112b833f87..81335c117e 100644
--- a/bitbake/lib/bb/fetch2/git.py
+++ b/bitbake/lib/bb/fetch2/git.py
@@ -67,6 +67,7 @@ import subprocess
67import tempfile 67import tempfile
68import bb 68import bb
69import bb.progress 69import bb.progress
70from contextlib import contextmanager
70from bb.fetch2 import FetchMethod 71from bb.fetch2 import FetchMethod
71from bb.fetch2 import runfetchcmd 72from bb.fetch2 import runfetchcmd
72from bb.fetch2 import logger 73from bb.fetch2 import logger
@@ -408,6 +409,20 @@ class Git(FetchMethod):
408 bb.utils.remove(tmpdir, recurse=True) 409 bb.utils.remove(tmpdir, recurse=True)
409 410
410 def build_mirror_data(self, ud, d): 411 def build_mirror_data(self, ud, d):
412
413 # Create as a temp file and move atomically into position to avoid races
414 @contextmanager
415 def create_atomic(filename, d):
416 fd, tfile = tempfile.mkstemp(dir=os.path.dirname(filename))
417 try:
418 yield tfile
419 umask = os.umask(0o666)
420 os.umask(umask)
421 os.chmod(tfile, (0o666 & ~umask))
422 runfetchcmd("mv %s %s" % (tfile, filename), d)
423 finally:
424 os.close(fd)
425
411 if ud.shallow and ud.write_shallow_tarballs: 426 if ud.shallow and ud.write_shallow_tarballs:
412 if not os.path.exists(ud.fullshallow): 427 if not os.path.exists(ud.fullshallow):
413 if os.path.islink(ud.fullshallow): 428 if os.path.islink(ud.fullshallow):
@@ -418,7 +433,8 @@ class Git(FetchMethod):
418 self.clone_shallow_local(ud, shallowclone, d) 433 self.clone_shallow_local(ud, shallowclone, d)
419 434
420 logger.info("Creating tarball of git repository") 435 logger.info("Creating tarball of git repository")
421 runfetchcmd("tar -czf %s ." % ud.fullshallow, d, workdir=shallowclone) 436 with create_atomic(ud.fullshallow, d) as tfile:
437 runfetchcmd("tar -czf %s ." % tfile, d, workdir=shallowclone)
422 runfetchcmd("touch %s.done" % ud.fullshallow, d) 438 runfetchcmd("touch %s.done" % ud.fullshallow, d)
423 finally: 439 finally:
424 bb.utils.remove(tempdir, recurse=True) 440 bb.utils.remove(tempdir, recurse=True)
@@ -427,7 +443,8 @@ class Git(FetchMethod):
427 os.unlink(ud.fullmirror) 443 os.unlink(ud.fullmirror)
428 444
429 logger.info("Creating tarball of git repository") 445 logger.info("Creating tarball of git repository")
430 runfetchcmd("tar -czf %s ." % ud.fullmirror, d, workdir=ud.clonedir) 446 with create_atomic(ud.fullmirror, d) as tfile:
447 runfetchcmd("tar -czf %s ." % tfile, d, workdir=ud.clonedir)
431 runfetchcmd("touch %s.done" % ud.fullmirror, d) 448 runfetchcmd("touch %s.done" % ud.fullmirror, d)
432 449
433 def clone_shallow_local(self, ud, dest, d): 450 def clone_shallow_local(self, ud, dest, d):