summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Watt <JPEWhacker@gmail.com>2021-06-01 21:36:48 -0500
committerRichard Purdie <richard.purdie@linuxfoundation.org>2021-06-03 23:09:42 +0100
commitc78b8a87db50323846834729f72310d6ec95a39d (patch)
treef2ecae20a8048fbdc0940921877e044f001f68d3
parent775b07b57322fee339ae6091bccac9b482c91306 (diff)
downloadpoky-c78b8a87db50323846834729f72310d6ec95a39d.tar.gz
classes/reproducible_build: Use atomic rename for SDE file
If an existing source date epoch file was found during do_unpack, it was deleted and a new one would be written in its place. This causes a race with check-before-use code in get_source_date_epoch_value. Resolve the problem by making do_unpack write the new source date epoch to a temporary file, then do an atomic rename to ensure it's always present, and change the check-before-use code to use a EAFP exception instead of checking for file existence. [YOCTO #14384] (From OE-Core rev: 0b5e3b33187bf78a2d62cc886463e4b27d6bd228) Signed-off-by: Joshua Watt <JPEWhacker@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--meta/classes/reproducible_build.bbclass13
1 files changed, 6 insertions, 7 deletions
diff --git a/meta/classes/reproducible_build.bbclass b/meta/classes/reproducible_build.bbclass
index ea643f2860..278eeedc74 100644
--- a/meta/classes/reproducible_build.bbclass
+++ b/meta/classes/reproducible_build.bbclass
@@ -80,17 +80,16 @@ python create_source_date_epoch_stamp() {
80 import oe.reproducible 80 import oe.reproducible
81 81
82 epochfile = d.getVar('SDE_FILE') 82 epochfile = d.getVar('SDE_FILE')
83 # If it exists we need to regenerate as the sources may have changed 83 tmp_file = "%s.new" % epochfile
84 if os.path.isfile(epochfile):
85 bb.debug(1, "Deleting existing SOURCE_DATE_EPOCH from: %s" % epochfile)
86 os.remove(epochfile)
87 84
88 source_date_epoch = oe.reproducible.get_source_date_epoch(d, d.getVar('S')) 85 source_date_epoch = oe.reproducible.get_source_date_epoch(d, d.getVar('S'))
89 86
90 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) 87 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
91 bb.utils.mkdirhier(d.getVar('SDE_DIR')) 88 bb.utils.mkdirhier(d.getVar('SDE_DIR'))
92 with open(epochfile, 'w') as f: 89 with open(tmp_file, 'w') as f:
93 f.write(str(source_date_epoch)) 90 f.write(str(source_date_epoch))
91
92 os.rename(tmp_file, epochfile)
94} 93}
95 94
96def get_source_date_epoch_value(d): 95def get_source_date_epoch_value(d):
@@ -100,7 +99,7 @@ def get_source_date_epoch_value(d):
100 99
101 epochfile = d.getVar('SDE_FILE') 100 epochfile = d.getVar('SDE_FILE')
102 source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK')) 101 source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK'))
103 if os.path.isfile(epochfile): 102 try:
104 with open(epochfile, 'r') as f: 103 with open(epochfile, 'r') as f:
105 s = f.read() 104 s = f.read()
106 try: 105 try:
@@ -113,7 +112,7 @@ def get_source_date_epoch_value(d):
113 bb.warn("SOURCE_DATE_EPOCH value '%s' is invalid. Reverting to SOURCE_DATE_EPOCH_FALLBACK" % s) 112 bb.warn("SOURCE_DATE_EPOCH value '%s' is invalid. Reverting to SOURCE_DATE_EPOCH_FALLBACK" % s)
114 source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK')) 113 source_date_epoch = int(d.getVar('SOURCE_DATE_EPOCH_FALLBACK'))
115 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch) 114 bb.debug(1, "SOURCE_DATE_EPOCH: %d" % source_date_epoch)
116 else: 115 except FileNotFoundError:
117 bb.debug(1, "Cannot find %s. SOURCE_DATE_EPOCH will default to %d" % (epochfile, source_date_epoch)) 116 bb.debug(1, "Cannot find %s. SOURCE_DATE_EPOCH will default to %d" % (epochfile, source_date_epoch))
118 117
119 d.setVar('__CACHED_SOURCE_DATE_EPOCH', str(source_date_epoch)) 118 d.setVar('__CACHED_SOURCE_DATE_EPOCH', str(source_date_epoch))