Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | import bb.siggen |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 7 | import bb.runqueue |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 8 | import oe |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 9 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 10 | def sstate_rundepfilter(siggen, fn, recipename, task, dep, depname, dataCaches): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | # Return True if we should keep the dependency, False to drop it |
| 12 | def isNative(x): |
| 13 | return x.endswith("-native") |
| 14 | def isCross(x): |
| 15 | return "-cross-" in x |
| 16 | def isNativeSDK(x): |
| 17 | return x.startswith("nativesdk-") |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 18 | def isKernel(mc, fn): |
| 19 | inherits = " ".join(dataCaches[mc].inherits[fn]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | return inherits.find("/module-base.bbclass") != -1 or inherits.find("/linux-kernel-base.bbclass") != -1 |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 21 | def isPackageGroup(mc, fn): |
| 22 | inherits = " ".join(dataCaches[mc].inherits[fn]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 23 | return "/packagegroup.bbclass" in inherits |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 24 | def isAllArch(mc, fn): |
| 25 | inherits = " ".join(dataCaches[mc].inherits[fn]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | return "/allarch.bbclass" in inherits |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 27 | def isImage(mc, fn): |
| 28 | return "/image.bbclass" in " ".join(dataCaches[mc].inherits[fn]) |
Patrick Williams | db4c27e | 2022-08-05 08:10:29 -0500 | [diff] [blame] | 29 | def isSPDXTask(task): |
| 30 | return task in ("do_create_spdx", "do_create_runtime_spdx") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 32 | depmc, _, deptaskname, depmcfn = bb.runqueue.split_tid_mcfn(dep) |
| 33 | mc, _ = bb.runqueue.split_mc(fn) |
| 34 | |
Patrick Williams | 7784c42 | 2022-11-17 07:29:11 -0600 | [diff] [blame^] | 35 | # We can skip the rm_work task signature to avoid running the task |
| 36 | # when we remove some tasks from the dependencie chain |
| 37 | # i.e INHERIT:remove = "create-spdx" will trigger the do_rm_work |
| 38 | if task == "do_rm_work": |
| 39 | return False |
| 40 | |
Patrick Williams | db4c27e | 2022-08-05 08:10:29 -0500 | [diff] [blame] | 41 | # Keep all dependencies between SPDX tasks in the signature. SPDX documents |
| 42 | # are linked together by hashes, which means if a dependent document changes, |
| 43 | # all downstream documents must be re-written (even if they are "safe" |
| 44 | # dependencies). |
| 45 | if isSPDXTask(task) and isSPDXTask(deptaskname): |
| 46 | return True |
| 47 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 48 | # (Almost) always include our own inter-task dependencies (unless it comes |
| 49 | # from a mcdepends). The exception is the special |
| 50 | # do_kernel_configme->do_unpack_and_patch dependency from archiver.bbclass. |
| 51 | if recipename == depname and depmc == mc: |
| 52 | if task == "do_kernel_configme" and deptaskname == "do_unpack_and_patch": |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 53 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | return True |
| 55 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 56 | # Exclude well defined recipe->dependency |
| 57 | if "%s->%s" % (recipename, depname) in siggen.saferecipedeps: |
| 58 | return False |
| 59 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 60 | # Check for special wildcard |
| 61 | if "*->%s" % depname in siggen.saferecipedeps and recipename != depname: |
| 62 | return False |
| 63 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | # Don't change native/cross/nativesdk recipe dependencies any further |
| 65 | if isNative(recipename) or isCross(recipename) or isNativeSDK(recipename): |
| 66 | return True |
| 67 | |
| 68 | # Only target packages beyond here |
| 69 | |
| 70 | # allarch packagegroups are assumed to have well behaved names which don't change between architecures/tunes |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 71 | if isPackageGroup(mc, fn) and isAllArch(mc, fn) and not isNative(depname): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 72 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | |
| 74 | # Exclude well defined machine specific configurations which don't change ABI |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 75 | if depname in siggen.abisaferecipes and not isImage(mc, fn): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | return False |
| 77 | |
| 78 | # Kernel modules are well namespaced. We don't want to depend on the kernel's checksum |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 79 | # if we're just doing an RRECOMMENDS:xxx = "kernel-module-*", not least because the checksum |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | # is machine specific. |
| 81 | # Therefore if we're not a kernel or a module recipe (inheriting the kernel classes) |
| 82 | # and we reccomend a kernel-module, we exclude the dependency. |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 83 | if dataCaches and isKernel(depmc, depmcfn) and not isKernel(mc, fn): |
| 84 | for pkg in dataCaches[mc].runrecs[fn]: |
| 85 | if " ".join(dataCaches[mc].runrecs[fn][pkg]).find("kernel-module-") != -1: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 86 | return False |
| 87 | |
| 88 | # Default to keep dependencies |
| 89 | return True |
| 90 | |
| 91 | def sstate_lockedsigs(d): |
| 92 | sigs = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 93 | types = (d.getVar("SIGGEN_LOCKEDSIGS_TYPES") or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | for t in types: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 95 | siggen_lockedsigs_var = "SIGGEN_LOCKEDSIGS_%s" % t |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 96 | lockedsigs = (d.getVar(siggen_lockedsigs_var) or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | for ls in lockedsigs: |
| 98 | pn, task, h = ls.split(":", 2) |
| 99 | if pn not in sigs: |
| 100 | sigs[pn] = {} |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 101 | sigs[pn][task] = [h, siggen_lockedsigs_var] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 102 | return sigs |
| 103 | |
| 104 | class SignatureGeneratorOEBasic(bb.siggen.SignatureGeneratorBasic): |
| 105 | name = "OEBasic" |
| 106 | def init_rundepcheck(self, data): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 107 | self.abisaferecipes = (data.getVar("SIGGEN_EXCLUDERECIPES_ABISAFE") or "").split() |
| 108 | self.saferecipedeps = (data.getVar("SIGGEN_EXCLUDE_SAFE_RECIPE_DEPS") or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | pass |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 110 | def rundep_check(self, fn, recipename, task, dep, depname, dataCaches = None): |
| 111 | return sstate_rundepfilter(self, fn, recipename, task, dep, depname, dataCaches) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 113 | class SignatureGeneratorOEBasicHashMixIn(object): |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 114 | supports_multiconfig_datacaches = True |
| 115 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 116 | def init_rundepcheck(self, data): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 117 | self.abisaferecipes = (data.getVar("SIGGEN_EXCLUDERECIPES_ABISAFE") or "").split() |
| 118 | self.saferecipedeps = (data.getVar("SIGGEN_EXCLUDE_SAFE_RECIPE_DEPS") or "").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 119 | self.lockedsigs = sstate_lockedsigs(data) |
| 120 | self.lockedhashes = {} |
| 121 | self.lockedpnmap = {} |
| 122 | self.lockedhashfn = {} |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 123 | self.machine = data.getVar("MACHINE") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | self.mismatch_msgs = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 125 | self.unlockedrecipes = (data.getVar("SIGGEN_UNLOCKED_RECIPES") or |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 126 | "").split() |
| 127 | self.unlockedrecipes = { k: "" for k in self.unlockedrecipes } |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 128 | self._internal = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 129 | pass |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 130 | |
| 131 | def tasks_resolved(self, virtmap, virtpnmap, dataCache): |
| 132 | # Translate virtual/xxx entries to PN values |
| 133 | newabisafe = [] |
| 134 | for a in self.abisaferecipes: |
| 135 | if a in virtpnmap: |
| 136 | newabisafe.append(virtpnmap[a]) |
| 137 | else: |
| 138 | newabisafe.append(a) |
| 139 | self.abisaferecipes = newabisafe |
| 140 | newsafedeps = [] |
| 141 | for a in self.saferecipedeps: |
| 142 | a1, a2 = a.split("->") |
| 143 | if a1 in virtpnmap: |
| 144 | a1 = virtpnmap[a1] |
| 145 | if a2 in virtpnmap: |
| 146 | a2 = virtpnmap[a2] |
| 147 | newsafedeps.append(a1 + "->" + a2) |
| 148 | self.saferecipedeps = newsafedeps |
| 149 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 150 | def rundep_check(self, fn, recipename, task, dep, depname, dataCaches = None): |
| 151 | return sstate_rundepfilter(self, fn, recipename, task, dep, depname, dataCaches) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 152 | |
| 153 | def get_taskdata(self): |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 154 | return (self.lockedpnmap, self.lockedhashfn, self.lockedhashes) + super().get_taskdata() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | |
| 156 | def set_taskdata(self, data): |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 157 | self.lockedpnmap, self.lockedhashfn, self.lockedhashes = data[:3] |
| 158 | super().set_taskdata(data[3:]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 159 | |
| 160 | def dump_sigs(self, dataCache, options): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 161 | sigfile = os.getcwd() + "/locked-sigs.inc" |
| 162 | bb.plain("Writing locked sigs to %s" % sigfile) |
| 163 | self.dump_lockedsigs(sigfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 164 | return super(bb.siggen.SignatureGeneratorBasicHash, self).dump_sigs(dataCache, options) |
| 165 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 166 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 167 | def get_taskhash(self, tid, deps, dataCaches): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 168 | if tid in self.lockedhashes: |
| 169 | if self.lockedhashes[tid]: |
| 170 | return self.lockedhashes[tid] |
| 171 | else: |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 172 | return super().get_taskhash(tid, deps, dataCaches) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 173 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 174 | h = super().get_taskhash(tid, deps, dataCaches) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 175 | |
| 176 | (mc, _, task, fn) = bb.runqueue.split_tid_mcfn(tid) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 177 | |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 178 | recipename = dataCaches[mc].pkg_fn[fn] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 179 | self.lockedpnmap[fn] = recipename |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 180 | self.lockedhashfn[fn] = dataCaches[mc].hashfn[fn] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 181 | |
| 182 | unlocked = False |
| 183 | if recipename in self.unlockedrecipes: |
| 184 | unlocked = True |
| 185 | else: |
| 186 | def recipename_from_dep(dep): |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 187 | (depmc, _, _, depfn) = bb.runqueue.split_tid_mcfn(dep) |
| 188 | return dataCaches[depmc].pkg_fn[depfn] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 189 | |
| 190 | # If any unlocked recipe is in the direct dependencies then the |
| 191 | # current recipe should be unlocked as well. |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 192 | depnames = [ recipename_from_dep(x) for x in deps if mc == bb.runqueue.mc_from_tid(x)] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 193 | if any(x in y for y in depnames for x in self.unlockedrecipes): |
| 194 | self.unlockedrecipes[recipename] = '' |
| 195 | unlocked = True |
| 196 | |
| 197 | if not unlocked and recipename in self.lockedsigs: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 198 | if task in self.lockedsigs[recipename]: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 199 | h_locked = self.lockedsigs[recipename][task][0] |
| 200 | var = self.lockedsigs[recipename][task][1] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 201 | self.lockedhashes[tid] = h_locked |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 202 | self._internal = True |
| 203 | unihash = self.get_unihash(tid) |
| 204 | self._internal = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 205 | #bb.warn("Using %s %s %s" % (recipename, task, h)) |
| 206 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 207 | if h != h_locked and h_locked != unihash: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 208 | self.mismatch_msgs.append('The %s:%s sig is computed to be %s, but the sig is locked to %s in %s' |
| 209 | % (recipename, task, h, h_locked, var)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 210 | |
| 211 | return h_locked |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 212 | |
| 213 | self.lockedhashes[tid] = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 214 | #bb.warn("%s %s %s" % (recipename, task, h)) |
| 215 | return h |
| 216 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 217 | def get_stampfile_hash(self, tid): |
| 218 | if tid in self.lockedhashes and self.lockedhashes[tid]: |
| 219 | return self.lockedhashes[tid] |
| 220 | return super().get_stampfile_hash(tid) |
| 221 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 222 | def get_unihash(self, tid): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 223 | if tid in self.lockedhashes and self.lockedhashes[tid] and not self._internal: |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 224 | return self.lockedhashes[tid] |
| 225 | return super().get_unihash(tid) |
| 226 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 227 | def dump_sigtask(self, fn, task, stampbase, runtime): |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 228 | tid = fn + ":" + task |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 229 | if tid in self.lockedhashes and self.lockedhashes[tid]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 230 | return |
| 231 | super(bb.siggen.SignatureGeneratorBasicHash, self).dump_sigtask(fn, task, stampbase, runtime) |
| 232 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 233 | def dump_lockedsigs(self, sigfile, taskfilter=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 234 | types = {} |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 235 | for tid in self.runtaskdeps: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 236 | if taskfilter: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 237 | if not tid in taskfilter: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 238 | continue |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 239 | fn = bb.runqueue.fn_from_tid(tid) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 240 | t = self.lockedhashfn[fn].split(" ")[1].split(":")[5] |
| 241 | t = 't-' + t.replace('_', '-') |
| 242 | if t not in types: |
| 243 | types[t] = [] |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 244 | types[t].append(tid) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 245 | |
| 246 | with open(sigfile, "w") as f: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 247 | l = sorted(types) |
| 248 | for t in l: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 249 | f.write('SIGGEN_LOCKEDSIGS_%s = "\\\n' % t) |
| 250 | types[t].sort() |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 251 | sortedtid = sorted(types[t], key=lambda tid: self.lockedpnmap[bb.runqueue.fn_from_tid(tid)]) |
| 252 | for tid in sortedtid: |
| 253 | (_, _, task, fn) = bb.runqueue.split_tid_mcfn(tid) |
| 254 | if tid not in self.taskhash: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 255 | continue |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 256 | f.write(" " + self.lockedpnmap[fn] + ":" + task + ":" + self.get_unihash(tid) + " \\\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 257 | f.write(' "\n') |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 258 | f.write('SIGGEN_LOCKEDSIGS_TYPES:%s = "%s"' % (self.machine, " ".join(l))) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 259 | |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 260 | def dump_siglist(self, sigfile, path_prefix_strip=None): |
| 261 | def strip_fn(fn): |
| 262 | nonlocal path_prefix_strip |
| 263 | if not path_prefix_strip: |
| 264 | return fn |
| 265 | |
| 266 | fn_exp = fn.split(":") |
| 267 | if fn_exp[-1].startswith(path_prefix_strip): |
| 268 | fn_exp[-1] = fn_exp[-1][len(path_prefix_strip):] |
| 269 | |
| 270 | return ":".join(fn_exp) |
| 271 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 272 | with open(sigfile, "w") as f: |
| 273 | tasks = [] |
| 274 | for taskitem in self.taskhash: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 275 | (fn, task) = taskitem.rsplit(":", 1) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 276 | pn = self.lockedpnmap[fn] |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 277 | tasks.append((pn, task, strip_fn(fn), self.taskhash[taskitem])) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 278 | for (pn, task, fn, taskhash) in sorted(tasks): |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 279 | f.write('%s:%s %s %s\n' % (pn, task, fn, taskhash)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 280 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 281 | def checkhashes(self, sq_data, missed, found, d): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 282 | warn_msgs = [] |
| 283 | error_msgs = [] |
| 284 | sstate_missing_msgs = [] |
| 285 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 286 | for tid in sq_data['hash']: |
| 287 | if tid not in found: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 288 | for pn in self.lockedsigs: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 289 | taskname = bb.runqueue.taskname_from_tid(tid) |
| 290 | if sq_data['hash'][tid] in iter(self.lockedsigs[pn].values()): |
| 291 | if taskname == 'do_shared_workdir': |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 292 | continue |
| 293 | sstate_missing_msgs.append("Locked sig is set for %s:%s (%s) yet not in sstate cache?" |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 294 | % (pn, taskname, sq_data['hash'][tid])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 295 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 296 | checklevel = d.getVar("SIGGEN_LOCKEDSIGS_TASKSIG_CHECK") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 297 | if checklevel == 'warn': |
| 298 | warn_msgs += self.mismatch_msgs |
| 299 | elif checklevel == 'error': |
| 300 | error_msgs += self.mismatch_msgs |
| 301 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 302 | checklevel = d.getVar("SIGGEN_LOCKEDSIGS_SSTATE_EXISTS_CHECK") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 303 | if checklevel == 'warn': |
| 304 | warn_msgs += sstate_missing_msgs |
| 305 | elif checklevel == 'error': |
| 306 | error_msgs += sstate_missing_msgs |
| 307 | |
| 308 | if warn_msgs: |
| 309 | bb.warn("\n".join(warn_msgs)) |
| 310 | if error_msgs: |
| 311 | bb.fatal("\n".join(error_msgs)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 312 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 313 | class SignatureGeneratorOEBasicHash(SignatureGeneratorOEBasicHashMixIn, bb.siggen.SignatureGeneratorBasicHash): |
| 314 | name = "OEBasicHash" |
| 315 | |
| 316 | class SignatureGeneratorOEEquivHash(SignatureGeneratorOEBasicHashMixIn, bb.siggen.SignatureGeneratorUniHashMixIn, bb.siggen.SignatureGeneratorBasicHash): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 317 | name = "OEEquivHash" |
| 318 | |
| 319 | def init_rundepcheck(self, data): |
| 320 | super().init_rundepcheck(data) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 321 | self.server = data.getVar('BB_HASHSERVE') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 322 | if not self.server: |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 323 | bb.fatal("OEEquivHash requires BB_HASHSERVE to be set") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 324 | self.method = data.getVar('SSTATE_HASHEQUIV_METHOD') |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 325 | if not self.method: |
| 326 | bb.fatal("OEEquivHash requires SSTATE_HASHEQUIV_METHOD to be set") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 327 | |
| 328 | # Insert these classes into siggen's namespace so it can see and select them |
| 329 | bb.siggen.SignatureGeneratorOEBasic = SignatureGeneratorOEBasic |
| 330 | bb.siggen.SignatureGeneratorOEBasicHash = SignatureGeneratorOEBasicHash |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 331 | bb.siggen.SignatureGeneratorOEEquivHash = SignatureGeneratorOEEquivHash |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 332 | |
| 333 | |
| 334 | def find_siginfo(pn, taskname, taskhashlist, d): |
| 335 | """ Find signature data files for comparison purposes """ |
| 336 | |
| 337 | import fnmatch |
| 338 | import glob |
| 339 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 340 | if not taskname: |
| 341 | # We have to derive pn and taskname |
| 342 | key = pn |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 343 | splitit = key.split('.bb:') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 344 | taskname = splitit[1] |
| 345 | pn = os.path.basename(splitit[0]).split('_')[0] |
| 346 | if key.startswith('virtual:native:'): |
| 347 | pn = pn + '-native' |
| 348 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 349 | hashfiles = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 350 | filedates = {} |
| 351 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 352 | def get_hashval(siginfo): |
| 353 | if siginfo.endswith('.siginfo'): |
| 354 | return siginfo.rpartition(':')[2].partition('_')[0] |
| 355 | else: |
| 356 | return siginfo.rpartition('.')[2] |
| 357 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 358 | # First search in stamps dir |
| 359 | localdata = d.createCopy() |
| 360 | localdata.setVar('MULTIMACH_TARGET_SYS', '*') |
| 361 | localdata.setVar('PN', pn) |
| 362 | localdata.setVar('PV', '*') |
| 363 | localdata.setVar('PR', '*') |
| 364 | localdata.setVar('EXTENDPE', '') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 365 | stamp = localdata.getVar('STAMP') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 366 | if pn.startswith("gcc-source"): |
| 367 | # gcc-source shared workdir is a special case :( |
| 368 | stamp = localdata.expand("${STAMPS_DIR}/work-shared/gcc-${PV}-${PR}") |
| 369 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 370 | filespec = '%s.%s.sigdata.*' % (stamp, taskname) |
| 371 | foundall = False |
| 372 | import glob |
| 373 | for fullpath in glob.glob(filespec): |
| 374 | match = False |
| 375 | if taskhashlist: |
| 376 | for taskhash in taskhashlist: |
| 377 | if fullpath.endswith('.%s' % taskhash): |
| 378 | hashfiles[taskhash] = fullpath |
| 379 | if len(hashfiles) == len(taskhashlist): |
| 380 | foundall = True |
| 381 | break |
| 382 | else: |
| 383 | try: |
| 384 | filedates[fullpath] = os.stat(fullpath).st_mtime |
| 385 | except OSError: |
| 386 | continue |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 387 | hashval = get_hashval(fullpath) |
| 388 | hashfiles[hashval] = fullpath |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 389 | |
| 390 | if not taskhashlist or (len(filedates) < 2 and not foundall): |
| 391 | # That didn't work, look in sstate-cache |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 392 | hashes = taskhashlist or ['?' * 64] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 393 | localdata = bb.data.createCopy(d) |
| 394 | for hashval in hashes: |
| 395 | localdata.setVar('PACKAGE_ARCH', '*') |
| 396 | localdata.setVar('TARGET_VENDOR', '*') |
| 397 | localdata.setVar('TARGET_OS', '*') |
| 398 | localdata.setVar('PN', pn) |
| 399 | localdata.setVar('PV', '*') |
| 400 | localdata.setVar('PR', '*') |
| 401 | localdata.setVar('BB_TASKHASH', hashval) |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 402 | localdata.setVar('SSTATE_CURRTASK', taskname[3:]) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 403 | swspec = localdata.getVar('SSTATE_SWSPEC') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 404 | if taskname in ['do_fetch', 'do_unpack', 'do_patch', 'do_populate_lic', 'do_preconfigure'] and swspec: |
| 405 | localdata.setVar('SSTATE_PKGSPEC', '${SSTATE_SWSPEC}') |
| 406 | elif pn.endswith('-native') or "-cross-" in pn or "-crosssdk-" in pn: |
| 407 | localdata.setVar('SSTATE_EXTRAPATH', "${NATIVELSBSTRING}/") |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 408 | filespec = '%s.siginfo' % localdata.getVar('SSTATE_PKG') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 409 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 410 | matchedfiles = glob.glob(filespec) |
| 411 | for fullpath in matchedfiles: |
| 412 | actual_hashval = get_hashval(fullpath) |
| 413 | if actual_hashval in hashfiles: |
| 414 | continue |
| 415 | hashfiles[hashval] = fullpath |
| 416 | if not taskhashlist: |
| 417 | try: |
| 418 | filedates[fullpath] = os.stat(fullpath).st_mtime |
| 419 | except: |
| 420 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 421 | |
| 422 | if taskhashlist: |
| 423 | return hashfiles |
| 424 | else: |
| 425 | return filedates |
| 426 | |
| 427 | bb.siggen.find_siginfo = find_siginfo |
| 428 | |
| 429 | |
| 430 | def sstate_get_manifest_filename(task, d): |
| 431 | """ |
| 432 | Return the sstate manifest file path for a particular task. |
| 433 | Also returns the datastore that can be used to query related variables. |
| 434 | """ |
| 435 | d2 = d.createCopy() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 436 | extrainf = d.getVarFlag("do_" + task, 'stamp-extra-info') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 437 | if extrainf: |
| 438 | d2.setVar("SSTATE_MANMACH", extrainf) |
| 439 | return (d2.expand("${SSTATE_MANFILEPREFIX}.%s" % task), d2) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 440 | |
| 441 | def find_sstate_manifest(taskdata, taskdata2, taskname, d, multilibcache): |
| 442 | d2 = d |
| 443 | variant = '' |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 444 | curr_variant = '' |
| 445 | if d.getVar("BBEXTENDCURR") == "multilib": |
| 446 | curr_variant = d.getVar("BBEXTENDVARIANT") |
| 447 | if "virtclass-multilib" not in d.getVar("OVERRIDES"): |
| 448 | curr_variant = "invalid" |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 449 | if taskdata2.startswith("virtual:multilib"): |
| 450 | variant = taskdata2.split(":")[2] |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 451 | if curr_variant != variant: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 452 | if variant not in multilibcache: |
| 453 | multilibcache[variant] = oe.utils.get_multilib_datastore(variant, d) |
| 454 | d2 = multilibcache[variant] |
| 455 | |
| 456 | if taskdata.endswith("-native"): |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 457 | pkgarchs = ["${BUILD_ARCH}", "${BUILD_ARCH}_${ORIGNATIVELSBSTRING}"] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 458 | elif taskdata.startswith("nativesdk-"): |
| 459 | pkgarchs = ["${SDK_ARCH}_${SDK_OS}", "allarch"] |
| 460 | elif "-cross-canadian" in taskdata: |
| 461 | pkgarchs = ["${SDK_ARCH}_${SDK_ARCH}-${SDKPKGSUFFIX}"] |
| 462 | elif "-cross-" in taskdata: |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 463 | pkgarchs = ["${BUILD_ARCH}"] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 464 | elif "-crosssdk" in taskdata: |
| 465 | pkgarchs = ["${BUILD_ARCH}_${SDK_ARCH}_${SDK_OS}"] |
| 466 | else: |
| 467 | pkgarchs = ['${MACHINE_ARCH}'] |
| 468 | pkgarchs = pkgarchs + list(reversed(d2.getVar("PACKAGE_EXTRA_ARCHS").split())) |
| 469 | pkgarchs.append('allarch') |
| 470 | pkgarchs.append('${SDK_ARCH}_${SDK_ARCH}-${SDKPKGSUFFIX}') |
| 471 | |
| 472 | for pkgarch in pkgarchs: |
| 473 | manifest = d2.expand("${SSTATE_MANIFESTS}/manifest-%s-%s.%s" % (pkgarch, taskdata, taskname)) |
| 474 | if os.path.exists(manifest): |
| 475 | return manifest, d2 |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 476 | bb.fatal("Manifest %s not found in %s (variant '%s')?" % (manifest, d2.expand(" ".join(pkgarchs)), variant)) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 477 | return None, d2 |
| 478 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 479 | def OEOuthashBasic(path, sigfile, task, d): |
| 480 | """ |
| 481 | Basic output hash function |
| 482 | |
| 483 | Calculates the output hash of a task by hashing all output file metadata, |
| 484 | and file contents. |
| 485 | """ |
| 486 | import hashlib |
| 487 | import stat |
| 488 | import pwd |
| 489 | import grp |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 490 | import re |
| 491 | import fnmatch |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 492 | |
| 493 | def update_hash(s): |
| 494 | s = s.encode('utf-8') |
| 495 | h.update(s) |
| 496 | if sigfile: |
| 497 | sigfile.write(s) |
| 498 | |
| 499 | h = hashlib.sha256() |
| 500 | prev_dir = os.getcwd() |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 501 | corebase = d.getVar("COREBASE") |
| 502 | tmpdir = d.getVar("TMPDIR") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 503 | include_owners = os.environ.get('PSEUDO_DISABLED') == '0' |
Andrew Geissler | f034379 | 2020-11-18 10:42:21 -0600 | [diff] [blame] | 504 | if "package_write_" in task or task == "package_qa": |
| 505 | include_owners = False |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 506 | include_timestamps = False |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 507 | include_root = True |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 508 | if task == "package": |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 509 | include_timestamps = True |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 510 | include_root = False |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 511 | hash_version = d.getVar('HASHEQUIV_HASH_VERSION') |
| 512 | extra_sigdata = d.getVar("HASHEQUIV_EXTRA_SIGDATA") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 513 | |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 514 | filemaps = {} |
| 515 | for m in (d.getVar('SSTATE_HASHEQUIV_FILEMAP') or '').split(): |
| 516 | entry = m.split(":") |
| 517 | if len(entry) != 3 or entry[0] != task: |
| 518 | continue |
| 519 | filemaps.setdefault(entry[1], []) |
| 520 | filemaps[entry[1]].append(entry[2]) |
| 521 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 522 | try: |
| 523 | os.chdir(path) |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 524 | basepath = os.path.normpath(path) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 525 | |
| 526 | update_hash("OEOuthashBasic\n") |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 527 | if hash_version: |
| 528 | update_hash(hash_version + "\n") |
| 529 | |
| 530 | if extra_sigdata: |
| 531 | update_hash(extra_sigdata + "\n") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 532 | |
| 533 | # It is only currently useful to get equivalent hashes for things that |
| 534 | # can be restored from sstate. Since the sstate object is named using |
| 535 | # SSTATE_PKGSPEC and the task name, those should be included in the |
| 536 | # output hash calculation. |
| 537 | update_hash("SSTATE_PKGSPEC=%s\n" % d.getVar('SSTATE_PKGSPEC')) |
| 538 | update_hash("task=%s\n" % task) |
| 539 | |
| 540 | for root, dirs, files in os.walk('.', topdown=True): |
| 541 | # Sort directories to ensure consistent ordering when recursing |
| 542 | dirs.sort() |
| 543 | files.sort() |
| 544 | |
| 545 | def process(path): |
| 546 | s = os.lstat(path) |
| 547 | |
| 548 | if stat.S_ISDIR(s.st_mode): |
| 549 | update_hash('d') |
| 550 | elif stat.S_ISCHR(s.st_mode): |
| 551 | update_hash('c') |
| 552 | elif stat.S_ISBLK(s.st_mode): |
| 553 | update_hash('b') |
| 554 | elif stat.S_ISSOCK(s.st_mode): |
| 555 | update_hash('s') |
| 556 | elif stat.S_ISLNK(s.st_mode): |
| 557 | update_hash('l') |
| 558 | elif stat.S_ISFIFO(s.st_mode): |
| 559 | update_hash('p') |
| 560 | else: |
| 561 | update_hash('-') |
| 562 | |
| 563 | def add_perm(mask, on, off='-'): |
| 564 | if mask & s.st_mode: |
| 565 | update_hash(on) |
| 566 | else: |
| 567 | update_hash(off) |
| 568 | |
| 569 | add_perm(stat.S_IRUSR, 'r') |
| 570 | add_perm(stat.S_IWUSR, 'w') |
| 571 | if stat.S_ISUID & s.st_mode: |
| 572 | add_perm(stat.S_IXUSR, 's', 'S') |
| 573 | else: |
| 574 | add_perm(stat.S_IXUSR, 'x') |
| 575 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 576 | if include_owners: |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 577 | # Group/other permissions are only relevant in pseudo context |
| 578 | add_perm(stat.S_IRGRP, 'r') |
| 579 | add_perm(stat.S_IWGRP, 'w') |
| 580 | if stat.S_ISGID & s.st_mode: |
| 581 | add_perm(stat.S_IXGRP, 's', 'S') |
| 582 | else: |
| 583 | add_perm(stat.S_IXGRP, 'x') |
| 584 | |
| 585 | add_perm(stat.S_IROTH, 'r') |
| 586 | add_perm(stat.S_IWOTH, 'w') |
| 587 | if stat.S_ISVTX & s.st_mode: |
| 588 | update_hash('t') |
| 589 | else: |
| 590 | add_perm(stat.S_IXOTH, 'x') |
| 591 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 592 | try: |
| 593 | update_hash(" %10s" % pwd.getpwuid(s.st_uid).pw_name) |
| 594 | update_hash(" %10s" % grp.getgrgid(s.st_gid).gr_name) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 595 | except KeyError as e: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 596 | bb.warn("KeyError in %s" % path) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 597 | msg = ("KeyError: %s\nPath %s is owned by uid %d, gid %d, which doesn't match " |
| 598 | "any user/group on target. This may be due to host contamination." % (e, path, s.st_uid, s.st_gid)) |
| 599 | raise Exception(msg).with_traceback(e.__traceback__) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 600 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 601 | if include_timestamps: |
| 602 | update_hash(" %10d" % s.st_mtime) |
| 603 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 604 | update_hash(" ") |
| 605 | if stat.S_ISBLK(s.st_mode) or stat.S_ISCHR(s.st_mode): |
| 606 | update_hash("%9s" % ("%d.%d" % (os.major(s.st_rdev), os.minor(s.st_rdev)))) |
| 607 | else: |
| 608 | update_hash(" " * 9) |
| 609 | |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 610 | filterfile = False |
| 611 | for entry in filemaps: |
| 612 | if fnmatch.fnmatch(path, entry): |
| 613 | filterfile = True |
| 614 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 615 | update_hash(" ") |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 616 | if stat.S_ISREG(s.st_mode) and not filterfile: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 617 | update_hash("%10d" % s.st_size) |
| 618 | else: |
| 619 | update_hash(" " * 10) |
| 620 | |
| 621 | update_hash(" ") |
| 622 | fh = hashlib.sha256() |
| 623 | if stat.S_ISREG(s.st_mode): |
| 624 | # Hash file contents |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 625 | if filterfile: |
| 626 | # Need to ignore paths in crossscripts and postinst-useradd files. |
| 627 | with open(path, 'rb') as d: |
| 628 | chunk = d.read() |
| 629 | chunk = chunk.replace(bytes(basepath, encoding='utf8'), b'') |
| 630 | for entry in filemaps: |
| 631 | if not fnmatch.fnmatch(path, entry): |
| 632 | continue |
| 633 | for r in filemaps[entry]: |
| 634 | if r.startswith("regex-"): |
| 635 | chunk = re.sub(bytes(r[6:], encoding='utf8'), b'', chunk) |
| 636 | else: |
| 637 | chunk = chunk.replace(bytes(r, encoding='utf8'), b'') |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 638 | fh.update(chunk) |
Patrick Williams | 93c203f | 2021-10-06 16:15:23 -0500 | [diff] [blame] | 639 | else: |
| 640 | with open(path, 'rb') as d: |
| 641 | for chunk in iter(lambda: d.read(4096), b""): |
| 642 | fh.update(chunk) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 643 | update_hash(fh.hexdigest()) |
| 644 | else: |
| 645 | update_hash(" " * len(fh.hexdigest())) |
| 646 | |
| 647 | update_hash(" %s" % path) |
| 648 | |
| 649 | if stat.S_ISLNK(s.st_mode): |
| 650 | update_hash(" -> %s" % os.readlink(path)) |
| 651 | |
| 652 | update_hash("\n") |
| 653 | |
| 654 | # Process this directory and all its child files |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 655 | if include_root or root != ".": |
| 656 | process(root) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 657 | for f in files: |
| 658 | if f == 'fixmepath': |
| 659 | continue |
| 660 | process(os.path.join(root, f)) |
| 661 | finally: |
| 662 | os.chdir(prev_dir) |
| 663 | |
| 664 | return h.hexdigest() |
| 665 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 666 | |