fetch2/local: verify checksums for file:// urls
PATCH 1/1 fetch2/local: verify checksums for file:// urls
Local.urldata_init unconditionally cleared ud.needdonestamp, which short-circuited the donestamp + verify_checksum cycle for every file:// url. As a result, a checksum supplied as a url parameter (for instance UNINATIVE_CHECKSUM expanded into the uninative tarball's file:// url) was never compared against the file's actual contents - the fetcher silently used whatever bytes were at the path.
Keep needdonestamp at the FetchData default (True) when the url carries any of the known checksum parameters, so the standard verify_checksum flow runs and a mismatch raises ChecksumError just like it does for http:// and friends. The check uses CHECKSUM_LIST and handles both the bare ;<algo>sum= form and the named ;name=foo;foo.<algo>sum= form that recipes (and UNINATIVE_CHECKSUM) actually use. When no checksum is supplied (the common case for file:// recipe patches) needdonestamp stays False and behavior is unchanged - no .done / .lock files appear in DL_DIR for those urls.
For checksummed file:// urls, the donestamp + lockfile do land in DL_DIR (keyed by basename, alongside the existing http(s) state for the same basename); this is intentional and matches how every other fetcher tracks verified content.
An earlier fix (b8b14d975a25 "fetch2: Ensure we don't have file downloads overwriting each other", 2017) added a short-circuit at the top of verify_donestamp() so that when origud is a file:// url with needdonestamp=False, the function returns True early. That short-circuit was protecting the SSTATE_MIRRORS case where a file:// url is mapped to an http:// mirror and the http path would otherwise race against the file:// origud. It still does the right thing under this change: the only file:// origud whose needdonestamp now flips to True is one that carries an explicit checksum parameter, and in that case we actively want the mirror to run verify_donestamp(), not bypass it. The unchecksummed file:// origud case (the original SSTATE_MIRRORS scenario) keeps needdonestamp=False and the 2017 short-circuit fires unchanged.
Two related changes in fetch2 are worth pointing at:
* commit 6424f4b7e9c1 ("fetch2: Ensure mirror tarballs don't enforce checksum", 2022) marks SSTATE_MIRRORS targets with newud.ignore_checksums = True. Its commit message states "local file fetches now validate checksums" - this fix is what finally makes that statement true. The ignore_checksums flag also acts as the safety hatch that prevents flipping needdonestamp on for checksummed file:// urls from propagating verification onto mirror-of-git-repo tarballs (whose checksums by design do not match).
* commit 4b8de2e7d126 ("wget: Avoid bad checksum race issues", 2022) established the don't-mutate-on-mismatch doctrine for downloaded artifacts via the localpath= and fatal_nochecksum= parameters. The rename_bad_checksum() early-return below applies the same doctrine to a fetcher whose 'localpath' is the user's source tree.
A checksum mismatch on a file:// url must not mutate the user's source tree the way it does for downloaded artifacts in DL_DIR. The standard error path renames <localpath> to <localpath>_bad-checksum_<sha> via rename_bad_checksum(), which for file:// would rename a recipe-shipped patch or an absolute path the user had supplied. Skip that rename inside rename_bad_checksum() when ud.type == 'file' so all four call sites stay consistent and a ChecksumError surfaces cleanly without on-disk side effects.
Add unit tests in FetcherLocalTest covering: the no-checksum hot path keeps needdonestamp=False (no regression for recipe patches); matching and mismatching sha256 and md5 checksums; the named ;name=foo;foo.sha256sum= form; and that a directory url with a checksum is silently ignored (supports_checksum() is False for directories). The mismatch tests assert that the source file is not renamed.
--- lib/bb/fetch2/__init__.py | 8 ++++ lib/bb/fetch2/local.py | 19 ++++++++- lib/bb/tests/fetch.py | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 2 deletions(-)
lib/bb/fetch2/__init__.py +8 -0
@@ -1075,6 +1075,14 @@ def rename_bad_checksum(ud, suffix): if ud.localpath is None: return + # For file:// URLs the localpath points at the user's source tree + # (e.g. recipe-shipped files under FILESPATH or an absolute path the + # user passed in). Renaming those out from under the user is + # destructive and surprising; surface the ChecksumError without + # mutating their files. + if ud.type == 'file': + return + new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) if not bb.utils.movefile(ud.localpath, new_localpath):
lib/bb/fetch2/local.py +17 -2
@@ -17,7 +17,7 @@ import os import urllib.request, urllib.parse, urllib.error import bb import bb.utils -from bb.fetch2 import FetchMethod, FetchError, ParameterError +from bb.fetch2 import CHECKSUM_LIST, FetchMethod, FetchError, ParameterError from bb.fetch2 import logger class Local(FetchMethod):
@@ -31,7 +31,22 @@ class Local(FetchMethod): # We don't set localfile as for this fetcher the file is already local! ud.basename = os.path.basename(ud.path) ud.basepath = ud.path - ud.needdonestamp = False + # Without a donestamp the verify_checksum path is bypassed entirely + # for file:// urls, so an explicit checksum on the url (for instance + # UNINATIVE_CHECKSUM expanded into the uninative tarball file:// url) + # is never compared against the file's actual contents. Keep + # needdonestamp at the FetchData default (True) when any of the + # known checksum parameters is present, so the standard + # donestamp + verify_checksum cycle runs and a mismatch raises + # ChecksumError. When no checksum is supplied (the common case for + # recipe patches under file://) needdonestamp stays False and + # behavior is unchanged. + name = ud.parm.get("name") + ud.needdonestamp = any( + (name and "%s.%ssum" % (name, a) in ud.parm) + or "%ssum" % a in ud.parm + for a in CHECKSUM_LIST + ) if "*" in ud.path: raise bb.fetch2.ParameterError("file:// urls using globbing are no longer supported. Please place the files in a directory and reference that instead.", ud.url) return
lib/bb/tests/fetch.py +104 -0
110 lines not on this page · raw message
---
base-commit: 5d722b5d65e4eef7befe6376983385421e993f86
change-id: 20260429-b4-yocto-9993-cadd9cbed7ce
Best regards,
Signature · 1 line
Jhonata Poma-Hansen <[email protected]>
Quoted 73 lines · Jhonata Poma-Hansen via lists.openembedded.org
Local.urldata_init unconditionally cleared ud.needdonestamp, which short-circuited the donestamp +
verify_checksumcycle for every file:// url. As a result, a checksum supplied as a url parameter (for instanceUNINATIVE_CHECKSUMexpanded into the uninative tarball's file:// url) was never compared against the file's actual contents - the fetcher silently used whatever bytes were at the path.Keep needdonestamp at the FetchData default (True) when the url carries any of the known checksum parameters, so the standard
verify_checksumflow runs and a mismatch raises ChecksumError just like it does for http:// and friends. The check usesCHECKSUM_LISTand handles both the bare ;<algo>sum= form and the named ;name=foo;foo.<algo>sum= form that recipes (andUNINATIVE_CHECKSUM) actually use. When no checksum is supplied (the common case for file:// recipe patches) needdonestamp stays False and behavior is unchanged - no .done / .lock files appear inDL_DIRfor those urls.For checksummed file:// urls, the donestamp + lockfile do land in
DL_DIR(keyed by basename, alongside the existing http(s) state for the same basename); this is intentional and matches how every other fetcher tracks verified content.An earlier fix (
b8b14d975a25"fetch2: Ensure we don't have file downloads overwriting each other", 2017) added a short-circuit at the top ofverify_donestamp()so that when origud is a file:// url with needdonestamp=False, the function returns True early. That short-circuit was protecting theSSTATE_MIRRORScase where a file:// url is mapped to an http:// mirror and the http path would otherwise race against the file:// origud. It still does the right thing under this change: the only file:// origud whose needdonestamp now flips to True is one that carries an explicit checksum parameter, and in that case we actively want the mirror to runverify_donestamp(), not bypass it. The unchecksummed file:// origud case (the originalSSTATE_MIRRORSscenario) keeps needdonestamp=False and the 2017 short-circuit fires unchanged.Two related changes in fetch2 are worth pointing at:
* commit
6424f4b7e9c1("fetch2: Ensure mirror tarballs don't enforce checksum", 2022) marksSSTATE_MIRRORStargets with newud.ignore_checksums = True. Its commit message states "local file fetches now validate checksums" - this fix is what finally makes that statement true. Theignore_checksumsflag also acts as the safety hatch that prevents flipping needdonestamp on for checksummed file:// urls from propagating verification onto mirror-of-git-repo tarballs (whose checksums by design do not match).
* commit4b8de2e7d126("wget: Avoid bad checksum race issues", 2022) established the don't-mutate-on-mismatch doctrine for downloaded artifacts via the localpath= and fatal_nochecksum= parameters. Therename_bad_checksum()early-return below applies the same doctrine to a fetcher whose 'localpath' is the user's source tree.A checksum mismatch on a file:// url must not mutate the user's source tree the way it does for downloaded artifacts in
DL_DIR. The standard error path renames <localpath> to <localpath>_bad-checksum_<sha> viarename_bad_checksum(), which for file:// would rename a recipe-shipped patch or an absolute path the user had supplied. Skip that rename insiderename_bad_checksum()when ud.type == 'file' so all four call sites stay consistent and a ChecksumError surfaces cleanly without on-disk side effects.Add unit tests in FetcherLocalTest covering: the no-checksum hot path keeps needdonestamp=False (no regression for recipe patches); matching and mismatching sha256 and md5 checksums; the named ;name=foo;foo.sha256sum= form; and that a directory url with a checksum is silently ignored (
supports_checksum()is False for directories). The mismatch tests assert that the source file is not renamed.
Quoted 4 lines
[YOCTO #9993]Signed-off-by Jhonata Poma-Hansen <[email protected]>
Hi,
The commit message here is incredibly verbose, to the point that it is difficult to read. While being verbose, it fails to mention that UNINATIVE_URL uses 'https://' in our metadata and so the issue only arises when you replace this with a local 'file://' URL (this is mentioned in the description of issue #9993).
Please simplify the commit message and focus on the relevant information.
Quoted 11 lines
--- lib/bb/fetch2/__init__.py | 8 ++++ lib/bb/fetch2/local.py | 19 ++++++++- lib/bb/tests/fetch.py | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 2 deletions(-)lib/bb/fetch2/__init__.py +0
-0
lib/bb/fetch2/__init__.py def rename_bad_checksum(ud, suffix): · line 1084
Quoted 10 lines
lib/bb/fetch2/__init__.py +7
-0@@ -1075,6 +1075,14 @@ def rename_bad_checksum(ud, suffix): if ud.localpath is None: return + # For file:// URLs the localpath points at the user's source tree + # (e.g. recipe-shipped files under FILESPATH or an absolute path the + # user passed in). Renaming those out from under the user is + # destructive and surprising; surface the ChecksumError without + # mutating their files. + if ud.type == 'file': + return
We should not be adding special cases for specific fetchers in __init__.py. Perhaps we could instead check if ud.localpath is under DL_DIR, or move this function to the FetchMethod class so that the Local fetcher can override it with an empty function.
Quoted 28 lines
lib/bb/fetch2/__init__.py +1
-0+ new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) if not bb.utils.movefile(ud.localpath, new_localpath):lib/bb/fetch2/local.py +7
-2@@ -17,7 +17,7 @@ import os import urllib.request, urllib.parse, urllib.error import bb import bb.utils -from bb.fetch2 import FetchMethod, FetchError, ParameterError +from bb.fetch2 import CHECKSUM_LIST, FetchMethod, FetchError, ParameterError from bb.fetch2 import logger class Local(FetchMethod):@@ -31,7 +31,22 @@ class Local(FetchMethod): # We don't set localfile as for this fetcher the file is already local! ud.basename = os.path.basename(ud.path) ud.basepath = ud.path - ud.needdonestamp = False + # Without a donestamp the verify_checksum path is bypassed entirely + # for file:// urls, so an explicit checksum on the url (for instance + # UNINATIVE_CHECKSUM expanded into the uninative tarball file:// url) + # is never compared against the file's actual contents. Keep + # needdonestamp at the FetchData default (True) when any of the + # known checksum parameters is present, so the standard
lib/bb/fetch2/local.py class Local(FetchMethod): · line 49
Quoted 10 lines
lib/bb/fetch2/local.py +10
-0+ # donestamp + verify_checksum cycle runs and a mismatch raises + # ChecksumError. When no checksum is supplied (the common case for + # recipe patches under file://) needdonestamp stays False and + # behavior is unchanged. + name = ud.parm.get("name") + ud.needdonestamp = any( + (name and "%s.%ssum" % (name, a) in ud.parm) + or "%ssum" % a in ud.parm + for a in CHECKSUM_LIST + )
The comment here is of about the right level of detail for the commit message. A one or two line comment here to explain the conditional may be useful, but more than that is probably too verbose.
Quoted 19 lines
lib/bb/fetch2/local.py +0
-0if "*" in ud.path: raise bb.fetch2.ParameterError("file:// urls using globbing are no longer supported. Please place the files in a directory and reference that instead.", ud.url) returnlib/bb/tests/fetch.py +8
-0@@ -813,6 +813,110 @@ class FetcherLocalTest(FetcherTest): tree = self.fetchUnpack(['file://archive.tar.bz2;subdir=bar;striplevel=1']) self.assertEqual(tree, ['bar/c', 'bar/d', 'bar/subdir/e']) + def test_local_no_checksum_no_donestamp(self): + # The common case: file:// recipe patches without a checksum + # parameter must keep the historical "no donestamp tracking" + # behavior so this fix does not regress the hot path. After + # download(), no .done / .lock entries must appear in DL_DIR + # for the bare file:// url. + with open(os.path.join(self.localsrcdir, 'plain'), 'wb') as f: + f.write(b"plain\n")
lib/bb/tests/fetch.py class FetcherLocalTest(FetcherTest): · line 833
Quoted 10 lines
lib/bb/tests/fetch.py +10
-0+ fetcher = bb.fetch.Fetch(['file://plain'], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertFalse(ud.needdonestamp) + # When needdonestamp is False, FetchData leaves ud.donestamp + # unset (None); we assert that no .done / .lock entries are + # written to DL_DIR for the bare file:// url after download. + fetcher.download() + for entry in os.listdir(self.dldir): + self.assertFalse(entry.startswith('plain.done')) + self.assertFalse(entry.startswith('plain.lock'))
I don't think this test case adds much value.
Quoted 8 lines
lib/bb/tests/fetch.py +8
-0+ + def test_local_checksum_match(self): + # A correct sha256 must drive the verify_checksum cycle to + # completion: download() returns without raising AND the + # donestamp lands in DL_DIR. The donestamp is the on-disk + # evidence that update_stamp() ran after verify_checksum() + # passed - without the fix, needdonestamp=False short-circuits + # update_stamp() and no donestamp is written even on "match".
lib/bb/tests/fetch.py class FetcherLocalTest(FetcherTest): · line 851
Quoted 10 lines
lib/bb/tests/fetch.py +10
-0+ import hashlib + content = b"file:// checksum match test\n" + with open(os.path.join(self.localsrcdir, 'sumfile'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + good], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp))
This test case looks good, but the comment is again too verbose, please simplify it.
Quoted 8 lines
lib/bb/tests/fetch.py +8
-0+ + def test_local_checksum_mismatch(self): + content = b"file:// checksum mismatch test\n" + srcpath = os.path.join(self.localsrcdir, 'sumfile') + with open(srcpath, 'wb') as f: + f.write(content) + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + bad], self.d)
lib/bb/tests/fetch.py class FetcherLocalTest(FetcherTest): · line 869
Quoted 10 lines
lib/bb/tests/fetch.py +10
-0+ with self.assertRaises(bb.fetch2.FetchError): + fetcher.download() + # The user's source tree must not be mutated. rename_bad_checksum + # would otherwise leave a <path>_bad-checksum_<sha> sibling and + # remove the original. + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content) + for entry in os.listdir(self.localsrcdir): + self.assertNotIn('_bad-checksum_', entry)
This test case is mostly good. I don't think we need to loop through the whole of the directory listing, can we just confirm that the bad-checksum marker for sumfile doesn't exist?
Quoted 9 lines
lib/bb/tests/fetch.py +9
-0+ + def test_local_checksum_mismatch_md5(self): + # Coverage for an algorithm other than sha256 so a regression + # that special-cased sha256 detection does not slip through. + # Mirror the sha256 mismatch assertions: source file present, + # contents unchanged, no _bad-checksum_ sibling left behind. + content = b"file:// md5 mismatch test\n" + srcpath = os.path.join(self.localsrcdir, 'sumfile_md5') + with open(srcpath, 'wb') as f:
lib/bb/tests/fetch.py class FetcherLocalTest(FetcherTest): · line 888
Quoted 10 lines
lib/bb/tests/fetch.py +10
-0+ f.write(content) + bad = "0" * 32 + fetcher = bb.fetch.Fetch(['file://sumfile_md5;md5sum=' + bad], self.d) + with self.assertRaises(bb.fetch2.FetchError): + fetcher.download() + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content) + for entry in os.listdir(self.localsrcdir): + self.assertNotIn('_bad-checksum_', entry)
I don't think this test case adds value, the implementation is not sha256sum specific.
Quoted 8 lines
lib/bb/tests/fetch.py +8
-0+ + def test_local_checksum_named(self): + # UNINATIVE_CHECKSUM (the original reproducer for this bug) and + # several other recipe-driven file:// urls use the name= prefix + # form: foo.sha256sum= rather than bare sha256sum=. Confirm the + # named form flips needdonestamp AND that the verify cycle runs + # (donestamp on disk after download). + import hashlib
lib/bb/tests/fetch.py class FetcherLocalTest(FetcherTest): · line 906
Quoted 10 lines
lib/bb/tests/fetch.py +10
-0+ content = b"file:// named checksum test\n" + with open(os.path.join(self.localsrcdir, 'sumfile_named'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch( + ['file://sumfile_named;name=blob;blob.sha256sum=' + good], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp))
This is a case of testing at the wrong level of detail. Do we already have test coverage that setting foo.some_property for a SRC_URI entry with name=foo results in the property being set for the appropriate entry? If so, we don't need further test coverage here. If not, we should add a generic test case for that.
lib/bb/tests/fetch.py class FetcherLocalTest(FetcherTest): · line 918
Quoted 12 lines
lib/bb/tests/fetch.py +12
-0+ + def test_local_checksum_directory_ignored(self): + # Local supports directory urls; supports_checksum returns False + # for directories. A checksum parm on a directory url should + # therefore be a no-op rather than a hard error: verify_checksum + # short-circuits on supports_checksum() and no ChecksumError is + # raised even when the checksum value is bogus. + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://dir;sha256sum=' + bad], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertFalse(ud.method.supports_checksum(ud)) + fetcher.download()
This tests that we suppress an error that we should actually be raising. If the checksum is invalid, then we should raise an error.
Best regards,
Signature · 1 line
Paul Barker
PATCH v2 1/1 fetch2/local: verify checksums for file:// urls
UNINATIVE_URL ships as an https:// url in metadata and verify_checksum catches a corrupt download. Replace it with a local file:// (the supported form for offline or pinned uninative tarballs) and the checksum supplied via UNINATIVE_CHECKSUM was silently ignored:
Local.urldata_init unconditionally cleared ud.needdonestamp, short-circuiting the donestamp + verify_checksum cycle for every file:// url. Recipes that supply ;sha256sum= on a file:// url hit the same hole.
Keep needdonestamp at the FetchData default (True) when the url carries any of the known checksum parameters (bare ;<algo>sum= or named ;name=foo;foo.<algo>sum= form), so the standard verify_checksum flow runs and a mismatch raises ChecksumError just like it does for http(s)://. When no checksum is supplied (the common file:// recipe patch case) needdonestamp stays False and behavior is unchanged.
A checksum mismatch on a file:// url must not mutate the user's source tree the way it does for downloaded artifacts under DL_DIR. Move rename_bad_checksum onto FetchMethod and override it as a no-op in the Local fetcher so a ChecksumError surfaces without an on-disk <localpath>_bad-checksum_<sha> sibling.
---
Changes in v2:
- EDITME: describe what is new in this series revision.
- EDITME: use bulletpoints and terse descriptions.
- Link to v1: https://lore.kernel.org/r/[email protected]
--- lib/bb/fetch2/__init__.py | 36 ++++++++++++++++++------------------ lib/bb/fetch2/local.py | 18 ++++++++++++++++-- lib/bb/tests/fetch.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 20 deletions(-)
lib/bb/fetch2/__init__.py +18 -18
@@ -738,7 +738,7 @@ def verify_donestamp(ud, d, origud=None): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) return False
@@ -771,7 +771,7 @@ def update_stamp(ud, d): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) raise
@@ -1067,20 +1067,6 @@ def build_mirroruris(origud, mirrors, ld): return uris, uds -def rename_bad_checksum(ud, suffix): - """ - Renames files to have suffix from parameter - """ - - if ud.localpath is None: - return - - new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) - bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) - if not bb.utils.movefile(ud.localpath, new_localpath): - bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) - - def try_mirror_url(fetch, origud, ud, ld, check = False): # Return of None or a value means we're finished # False means try another url
@@ -1156,7 +1142,7 @@ def try_mirror_url(fetch, origud, ud, ld, check = False): logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url)) logger.warning(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
@@ -1472,6 +1458,20 @@ class FetchMethod(object): """ return True + def rename_bad_checksum(self, ud, suffix): + """ + Rename ud.localpath with the given suffix on checksum mismatch. + Local overrides this to a no-op since its localpath points at the + user's source tree. + """ + if ud.localpath is None: + return + + new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) + bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) + if not bb.utils.movefile(ud.localpath, new_localpath): + bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) + def verify_donestamp(self, ud, d): """ Verify the donestamp file
@@ -1937,7 +1937,7 @@ class Fetch(object): logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u) logger.debug(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
lib/bb/fetch2/local.py +16 -2
@@ -17,7 +17,7 @@ import os import urllib.request, urllib.parse, urllib.error import bb import bb.utils -from bb.fetch2 import FetchMethod, FetchError, ParameterError +from bb.fetch2 import CHECKSUM_LIST, FetchMethod, FetchError, ParameterError from bb.fetch2 import logger class Local(FetchMethod):
@@ -31,11 +31,25 @@ class Local(FetchMethod): # We don't set localfile as for this fetcher the file is already local! ud.basename = os.path.basename(ud.path) ud.basepath = ud.path - ud.needdonestamp = False + # Honor explicit checksum params (UNINATIVE_CHECKSUM, recipe + # ;sha256sum=) by leaving needdonestamp at FetchData's default so + # verify_checksum runs. + name = ud.parm.get("name") + ud.needdonestamp = any( + (name and "%s.%ssum" % (name, a) in ud.parm) + or "%ssum" % a in ud.parm + for a in CHECKSUM_LIST + ) if "*" in ud.path: raise bb.fetch2.ParameterError("file:// urls using globbing are no longer supported. Please place the files in a directory and reference that instead.", ud.url) return + def rename_bad_checksum(self, ud, suffix): + # file:// urls point at the user's source tree (recipe-shipped files + # under FILESPATH or an absolute path the user passed in); skip the + # rename so a ChecksumError surfaces without mutating their files. + return + def localpath(self, urldata, d): """ Return the local filename of a given url assuming a successful fetch.
lib/bb/tests/fetch.py +30 -0
@@ -813,6 +813,36 @@ class FetcherLocalTest(FetcherTest): tree = self.fetchUnpack(['file://archive.tar.bz2;subdir=bar;striplevel=1']) self.assertEqual(tree, ['bar/c', 'bar/d', 'bar/subdir/e']) + def test_local_checksum_match(self): + # Correct sha256 must run verify_checksum to completion; donestamp + # lands in DL_DIR. + import hashlib + content = b"file:// checksum match test\n" + with open(os.path.join(self.localsrcdir, 'sumfile'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + good], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp)) + + def test_local_checksum_mismatch(self): + # Bad sha256 raises ChecksumError without renaming the user's + # source file to the <localpath>_bad-checksum_<sha> sibling. + content = b"file:// checksum mismatch test\n" + srcpath = os.path.join(self.localsrcdir, 'sumfile') + with open(srcpath, 'wb') as f: + f.write(content) + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + bad], self.d) + with self.assertRaises(bb.fetch2.FetchError): + fetcher.download() + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content) + self.assertFalse(os.path.exists(srcpath + '_bad-checksum_' + bad)) + def dummyGitTest(self, suffix): # Create dummy local Git repo src_dir = tempfile.mkdtemp(dir=self.tempdir,
---
base-commit: 5d722b5d65e4eef7befe6376983385421e993f86
change-id: 20260429-b4-yocto-9993-cadd9cbed7ce
Best regards,
Signature · 1 line
Jhonata Poma-Hansen <[email protected]>
Apologies, the "Changes in v2:" section in v2 ships with two literal b4 placeholder lines starting with "EDITME: ..." that I forgot to fill in before sending. Please disregard those two lines. The actual changes from v1, addressing Paul Barker's review (lore.kernel.org/r/[email protected]):
- Commit body trimmed; UNINATIVE_URL trigger now leads paragraph 1 (Paul flagged this was missing from v1).
- Dropped the long historical commit references (b8b14d975a25, 6424f4b7e9c1, 4b8de2e7d126) from the commit body.
- rename_bad_checksum moved from a module-level helper in bb/fetch2/__init__.py onto the FetchMethod class; Local overrides it as a no-op so a ChecksumError surfaces without renaming files under the user's source tree. Four callsites updated to ud.method.rename_bad_checksum(...). No "ud.type == 'file'" special case in __init__.py.
- Inline comment in local.py trimmed from ~10 lines to 3.
- Tests reduced to two keepers in FetcherLocalTest:
- test_local_checksum_match: simpler comment; same assertion.
- test_local_checksum_mismatch: dropped the os.listdir loop, replaced with a direct assertFalse on the specific bad-checksum sibling path.
Dropped: test_local_no_checksum_no_donestamp, test_local_checksum_mismatch_md5, test_local_checksum_named, test_local_checksum_directory_ignored (the directory-with-bad- checksum case Paul noted should raise; left out of this patch rather than widening scope).
Thanks,
Jhonata
PATCH v3 1/1 fetch2/local: verify checksums for file:// urls
In reply to Jhonata Poma-HansenLocal.urldata_init unconditionally cleared ud.needdonestamp, which skipped the donestamp + verify_checksum cycle for every file:// url. A checksum supplied via a url parameter, such as UNINATIVE_CHECKSUM expanded into the uninative tarball's file:// url, was therefore never compared against the file's actual contents.
Keep needdonestamp at its default when the url carries a checksum parameter, and move rename_bad_checksum onto FetchMethod so Local can override it as a no-op, avoiding a rename of the user's source file on mismatch.
Based on a patch by Jhonata Poma-Hansen, adapted to this tree's Local.urldata_init(), which now uses ud.decodedurl in place of the ud.path referenced by the original patch.
[YOCTO #9993]
--- bitbake/lib/bb/fetch2/__init__.py | 36 +++++++++++++++---------------- bitbake/lib/bb/fetch2/local.py | 18 ++++++++++++++-- bitbake/lib/bb/tests/fetch.py | 30 ++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 20 deletions(-)
bitbake/lib/bb/fetch2/__init__.py +18 -18
@@ -701,7 +701,7 @@ def verify_donestamp(ud, d, origud=None): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) return False
@@ -734,7 +734,7 @@ def update_stamp(ud, d): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) raise
@@ -1029,20 +1029,6 @@ def build_mirroruris(origud, mirrors, ld): return uris, uds -def rename_bad_checksum(ud, suffix): - """ - Renames files to have suffix from parameter - """ - - if ud.localpath is None: - return - - new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) - bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) - if not bb.utils.movefile(ud.localpath, new_localpath): - bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) - - def try_mirror_url(fetch, origud, ud, ld, check = False): # Return of None or a value means we're finished # False means try another url
@@ -1111,7 +1097,7 @@ def try_mirror_url(fetch, origud, ud, ld, check = False): logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url)) logger.warning(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
@@ -1450,6 +1436,20 @@ class FetchMethod(object): """ return True + def rename_bad_checksum(self, ud, suffix): + """ + Rename ud.localpath with the given suffix on checksum mismatch. + Local overrides this to a no-op since its localpath points at the + user's source tree. + """ + if ud.localpath is None: + return + + new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) + bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) + if not bb.utils.movefile(ud.localpath, new_localpath): + bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) + def verify_donestamp(self, ud, d): """ Verify the donestamp file
@@ -1887,7 +1887,7 @@ class Fetch(object): logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u) logger.debug(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
bitbake/lib/bb/fetch2/local.py +16 -2
@@ -17,7 +17,7 @@ import os import urllib.request, urllib.parse, urllib.error import bb import bb.utils -from bb.fetch2 import FetchMethod, FetchError, ParameterError +from bb.fetch2 import CHECKSUM_LIST, FetchMethod, FetchError, ParameterError from bb.fetch2 import logger class Local(FetchMethod):
@@ -32,11 +32,25 @@ class Local(FetchMethod): ud.decodedurl = urllib.parse.unquote(ud.url.split("://")[1].split(";")[0]) ud.basename = os.path.basename(ud.decodedurl) ud.basepath = ud.decodedurl - ud.needdonestamp = False + # Honor explicit checksum params (UNINATIVE_CHECKSUM, recipe + # ;sha256sum=) by leaving needdonestamp at FetchData's default so + # verify_checksum runs. + name = ud.parm.get("name") + ud.needdonestamp = any( + (name and "%s.%ssum" % (name, a) in ud.parm) + or "%ssum" % a in ud.parm + for a in CHECKSUM_LIST + ) if "*" in ud.decodedurl: raise bb.fetch2.ParameterError("file:// urls using globbing are no longer supported. Please place the files in a directory and reference that instead.", ud.url) return + def rename_bad_checksum(self, ud, suffix): + # file:// urls point at the user's source tree (recipe-shipped files + # under FILESPATH or an absolute path the user passed in); skip the + # rename so a ChecksumError surfaces without mutating their files. + return + def localpath(self, urldata, d): """ Return the local filename of a given url assuming a successful fetch.
bitbake/lib/bb/tests/fetch.py +30 -0
@@ -808,6 +808,36 @@ class FetcherLocalTest(FetcherTest): tree = self.fetchUnpack(['file://archive.tar.bz2;subdir=bar;striplevel=1']) self.assertEqual(tree, ['bar/c', 'bar/d', 'bar/subdir/e']) + def test_local_checksum_match(self): + # Correct sha256 must run verify_checksum to completion; donestamp + # lands in DL_DIR. + import hashlib + content = b"file:// checksum match test\n" + with open(os.path.join(self.localsrcdir, 'sumfile'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + good], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp)) + + def test_local_checksum_mismatch(self): + # Bad sha256 raises ChecksumError without renaming the user's + # source file to the <localpath>_bad-checksum_<sha> sibling. + content = b"file:// checksum mismatch test\n" + srcpath = os.path.join(self.localsrcdir, 'sumfile') + with open(srcpath, 'wb') as f: + f.write(content) + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + bad], self.d) + with self.assertRaises(bb.fetch2.FetchError): + fetcher.download() + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content) + self.assertFalse(os.path.exists(srcpath + '_bad-checksum_' + bad)) + def dummyGitTest(self, suffix): # Create dummy local Git repo src_dir = tempfile.mkdtemp(dir=self.tempdir,
Signature · 1 line
2.34.1
Hi,
Quoted 6 lines · Savvas Etairidis via lists.openembedded.org
Local.urldata_init unconditionally cleared ud.needdonestamp, which skipped the donestamp +
verify_checksumcycle for every file:// url. A checksum supplied via a url parameter, such asUNINATIVE_CHECKSUMexpanded into the uninative tarball's file:// url, was therefore never compared against the file's actual contents.
Quoted 8 lines
Keep needdonestamp at its default when the url carries a checksum parameter, and move
rename_bad_checksumonto FetchMethod so Local can override it as a no-op, avoiding a rename of the user's source file on mismatch.Based on a patch by Jhonata Poma-Hansen, adapted to this tree's
Local.urldata_init(), which now uses ud.decodedurl in place of the ud.path referenced by the original patch.
This cannot be applied to the master branch cleanly. Can you rebase and re-send you patch?
Antonin
PATCH v4 1/1 fetch/local: verify checksums for file:// urls
[YOCTO #9993]
Local.urldata_init() always cleared needdonestamp, so a checksum given in a file:// url (e.g. UNINATIVE_CHECKSUM when UNINATIVE_URL points at a local file://) was never verified. Keep the donestamp when the url carries a checksum so verify_checksum runs, and make rename_bad_checksum() a FetchMethod method that Local overrides as a no-op, so a mismatch doesn't rename the user's source file.
Based on a patch by Jhonata Poma-Hansen.
--- lib/bb/fetch/__init__.py | 36 ++++++++++++++++++------------------ lib/bb/fetch/local.py | 18 ++++++++++++++++-- lib/bb/tests/fetch.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 20 deletions(-)
lib/bb/fetch/__init__.py +18 -18
@@ -742,7 +742,7 @@ def verify_donestamp(ud, d, origud=None): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) return False
@@ -775,7 +775,7 @@ def update_stamp(ud, d): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) raise
@@ -1079,20 +1079,6 @@ def build_mirroruris(origud, mirrors, ld): return uris, uds -def rename_bad_checksum(ud, suffix): - """ - Renames files to have suffix from parameter - """ - - if ud.localpath is None: - return - - new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) - bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) - if not bb.utils.movefile(ud.localpath, new_localpath): - bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) - - def try_mirror_url(fetch, origud, ud, ld, check = False): # Return of None or a value means we're finished # False means try another url
@@ -1163,7 +1149,7 @@ def try_mirror_url(fetch, origud, ud, ld, check = False): logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url)) logger.warning(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
@@ -1479,6 +1465,20 @@ class FetchMethod(object): """ return True + def rename_bad_checksum(self, ud, suffix): + """ + Rename ud.localpath with the given suffix on checksum mismatch. + Local overrides this to a no-op since its localpath points at the + user's source tree. + """ + if ud.localpath is None: + return + + new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) + bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) + if not bb.utils.movefile(ud.localpath, new_localpath): + bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) + def verify_donestamp(self, ud, d): """ Verify the donestamp file
@@ -1943,7 +1943,7 @@ class Fetch(object): logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u) logger.debug(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
lib/bb/fetch/local.py +16 -2
@@ -17,7 +17,7 @@ import os import urllib.request, urllib.parse, urllib.error import bb import bb.utils -from bb.fetch import FetchMethod, FetchError, ParameterError +from bb.fetch import CHECKSUM_LIST, FetchMethod, FetchError, ParameterError from bb.fetch import logger class Local(FetchMethod):
@@ -31,11 +31,25 @@ class Local(FetchMethod): # We don't set localfile as for this fetcher the file is already local! ud.basename = os.path.basename(ud.path) ud.basepath = ud.path - ud.needdonestamp = False + # Honor explicit checksum params (UNINATIVE_CHECKSUM, recipe + # ;sha256sum=) by leaving needdonestamp at FetchData's default so + # verify_checksum runs. + name = ud.parm.get("name") + ud.needdonestamp = any( + (name and "%s.%ssum" % (name, a) in ud.parm) + or "%ssum" % a in ud.parm + for a in CHECKSUM_LIST + ) if "*" in ud.path: raise bb.fetch.ParameterError("file:// urls using globbing are no longer supported. Please place the files in a directory and reference that instead.", ud.url) return + def rename_bad_checksum(self, ud, suffix): + # file:// urls point at the user's source tree (recipe-shipped files + # under FILESPATH or an absolute path the user passed in); skip the + # rename so a ChecksumError surfaces without mutating their files. + return + def localpath(self, urldata, d): """ Return the local filename of a given url assuming a successful fetch.
lib/bb/tests/fetch.py +30 -0
@@ -876,6 +876,36 @@ class FetcherLocalTest(FetcherTest): with self.subTest(striplevel=repr(value)): self.assertInvalidStriplevel(value) + def test_local_checksum_match(self): + # Correct sha256 must run verify_checksum to completion; donestamp + # lands in DL_DIR. + import hashlib + content = b"file:// checksum match test\n" + with open(os.path.join(self.localsrcdir, 'sumfile'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + good], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp)) + + def test_local_checksum_mismatch(self): + # Bad sha256 raises ChecksumError without renaming the user's + # source file to the <localpath>_bad-checksum_<sha> sibling. + content = b"file:// checksum mismatch test\n" + srcpath = os.path.join(self.localsrcdir, 'sumfile') + with open(srcpath, 'wb') as f: + f.write(content) + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + bad], self.d) + with self.assertRaises(bb.fetch.FetchError): + fetcher.download() + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content) + self.assertFalse(os.path.exists(srcpath + '_bad-checksum_' + bad)) + def dummyGitTest(self, suffix): # Create dummy local Git repo src_dir = tempfile.mkdtemp(dir=self.tempdir,
Signature · 1 line
2.34.1
Re: [PATCH v4] fetch/local: verify checksums for file:// urls
Quoted 9 lines · Savvas Etairidis
[YOCTO #9993]
Local.urldata_init()always cleared needdonestamp, so a checksum given in a file:// url (e.g.UNINATIVE_CHECKSUMwhenUNINATIVE_URLpoints at a local file://) was never verified. Keep the donestamp when the url carries a checksum soverify_checksumruns, and makerename_bad_checksum()a FetchMethod method that Local overrides as a no-op, so a mismatch doesn't rename the user's source file.
Quoted 3 lines
Based on a patch by Jhonata Poma-Hansen.
Signed-off-by Savvas Etairidis <[email protected]>
Hi,
Please include a list of changes since the previous version of the patch after the '---' separator. For an example see 'Changes from v2' in [1].
[1]: https://lore.kernel.org/all/[email protected]/
Have you spoken to Jhonata about these patches? It's good practice to ask some questions and engage in discussion before taking over a patch series someone else was working on.
Also, please ensure that you're following our policy on AI-assisted contributions [2] if these patches were generated with AI.
[2]: https://docs.yoctoproject.org/contributor-guide/submit-changes.html#acceptance-of-ai-generated-code
Best regards,
Signature · 1 line
Paul Barker
PATCH v5 1/1 fetch/local: verify checksums for file:// urls
[YOCTO #9993]
Local.urldata_init() always cleared needdonestamp, so a checksum given in a file:// url (e.g. UNINATIVE_CHECKSUM when UNINATIVE_URL points at a local file://) was never verified. Keep the donestamp when the url carries a checksum so verify_checksum runs, and make rename_bad_checksum() a FetchMethod method that Local overrides as a no-op, so a mismatch doesn't rename the user's source file.
Based on a patch by Jhonata Poma-Hansen.
---
changes in v5:
- Rebased on top of master, added changelog entry
changes in v4:
- Rebased on top of master
changes in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen
---
--- lib/bb/fetch/__init__.py | 36 ++++++++++++++++++------------------ lib/bb/fetch/local.py | 18 ++++++++++++++++-- lib/bb/tests/fetch.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 20 deletions(-)
lib/bb/fetch/__init__.py +18 -18
@@ -742,7 +742,7 @@ def verify_donestamp(ud, d, origud=None): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) return False
@@ -775,7 +775,7 @@ def update_stamp(ud, d): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) raise
@@ -1079,20 +1079,6 @@ def build_mirroruris(origud, mirrors, ld): return uris, uds -def rename_bad_checksum(ud, suffix): - """ - Renames files to have suffix from parameter - """ - - if ud.localpath is None: - return - - new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) - bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) - if not bb.utils.movefile(ud.localpath, new_localpath): - bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) - - def try_mirror_url(fetch, origud, ud, ld, check = False): # Return of None or a value means we're finished # False means try another url
@@ -1163,7 +1149,7 @@ def try_mirror_url(fetch, origud, ud, ld, check = False): logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url)) logger.warning(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
@@ -1479,6 +1465,20 @@ class FetchMethod(object): """ return True + def rename_bad_checksum(self, ud, suffix): + """ + Rename ud.localpath with the given suffix on checksum mismatch. + Local overrides this to a no-op since its localpath points at the + user's source tree. + """ + if ud.localpath is None: + return + + new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) + bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) + if not bb.utils.movefile(ud.localpath, new_localpath): + bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) + def verify_donestamp(self, ud, d): """ Verify the donestamp file
@@ -1943,7 +1943,7 @@ class Fetch(object): logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u) logger.debug(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
lib/bb/fetch/local.py +16 -2
@@ -17,7 +17,7 @@ import os import urllib.request, urllib.parse, urllib.error import bb import bb.utils -from bb.fetch import FetchMethod, FetchError, ParameterError +from bb.fetch import CHECKSUM_LIST, FetchMethod, FetchError, ParameterError from bb.fetch import logger class Local(FetchMethod):
@@ -31,11 +31,25 @@ class Local(FetchMethod): # We don't set localfile as for this fetcher the file is already local! ud.basename = os.path.basename(ud.path) ud.basepath = ud.path - ud.needdonestamp = False + # Honor explicit checksum params (UNINATIVE_CHECKSUM, recipe + # ;sha256sum=) by leaving needdonestamp at FetchData's default so + # verify_checksum runs. + name = ud.parm.get("name") + ud.needdonestamp = any( + (name and "%s.%ssum" % (name, a) in ud.parm) + or "%ssum" % a in ud.parm + for a in CHECKSUM_LIST + ) if "*" in ud.path: raise bb.fetch.ParameterError("file:// urls using globbing are no longer supported. Please place the files in a directory and reference that instead.", ud.url) return + def rename_bad_checksum(self, ud, suffix): + # file:// urls point at the user's source tree (recipe-shipped files + # under FILESPATH or an absolute path the user passed in); skip the + # rename so a ChecksumError surfaces without mutating their files. + return + def localpath(self, urldata, d): """ Return the local filename of a given url assuming a successful fetch.
lib/bb/tests/fetch.py +30 -0
@@ -876,6 +876,36 @@ class FetcherLocalTest(FetcherTest): with self.subTest(striplevel=repr(value)): self.assertInvalidStriplevel(value) + def test_local_checksum_match(self): + # Correct sha256 must run verify_checksum to completion; donestamp + # lands in DL_DIR. + import hashlib + content = b"file:// checksum match test\n" + with open(os.path.join(self.localsrcdir, 'sumfile'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + good], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp)) + + def test_local_checksum_mismatch(self): + # Bad sha256 raises ChecksumError without renaming the user's + # source file to the <localpath>_bad-checksum_<sha> sibling. + content = b"file:// checksum mismatch test\n" + srcpath = os.path.join(self.localsrcdir, 'sumfile') + with open(srcpath, 'wb') as f: + f.write(content) + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + bad], self.d) + with self.assertRaises(bb.fetch.FetchError): + fetcher.download() + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content) + self.assertFalse(os.path.exists(srcpath + '_bad-checksum_' + bad)) + def dummyGitTest(self, suffix): # Create dummy local Git repo src_dir = tempfile.mkdtemp(dir=self.tempdir,
Signature · 1 line
2.34.1
Re: [PATCH v5] fetch/local: verify checksums for file:// urls
Hi,
My bad I assumed that Jhonata stopped working on this based on the ticket comments.
I send him a personal email about this.
Regards,
Savvas
Στις Δευ 28 Σεπ 2026 στις 10:59 μ.μ., ο/η Savvas Etairidis < [email protected]> έγραψε:
Quoted 212 lines
[YOCTO #9993]
Local.urldata_init()always cleared needdonestamp, so a checksum given in a file:// url (e.g.UNINATIVE_CHECKSUMwhenUNINATIVE_URLpoints at a local file://) was never verified. Keep the donestamp when the url carries a checksum soverify_checksumruns, and makerename_bad_checksum()a FetchMethod method that Local overrides as a no-op, so a mismatch doesn't rename the user's source file.Based on a patch by Jhonata Poma-Hansen.
Signed-off-by Savvas Etairidis <[email protected]>---
changes in v5:
- Rebased on top of master, added changelog entrychanges in v4:
- Rebased on top of masterchanges in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen---
--- lib/bb/fetch/__init__.py | 36 ++++++++++++++++++------------------ lib/bb/fetch/local.py | 18 ++++++++++++++++-- lib/bb/tests/fetch.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 20 deletions(-)lib/bb/fetch/__init__.py +2
-16@@ -742,7 +742,7 @@ def verify_donestamp(ud, d, origud=None): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) return False@@ -775,7 +775,7 @@ def update_stamp(ud, d): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) raise@@ -1079,20 +1079,6 @@ def build_mirroruris(origud, mirrors, ld): return uris, uds -def rename_bad_checksum(ud, suffix): - """ - Renames files to have suffix from parameter - """ - - if ud.localpath is None: - return - - new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) - bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) - if not bb.utils.movefile(ud.localpath, new_localpath): - bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) - - def try_mirror_url(fetch, origud, ud, ld, check = False): # Return of None or a value means we're finished# False means try another url
lib/bb/fetch/__init__.py +0
-0@@ -1163,7 +1149,7 @@ def try_mirror_url(fetch, origud, ud, ld, check =False):
logger.warning("Mirror checksum failure for url %s (original
url: %s)\nCleaning and trying again." % (ud.url, origud.url))
logger.warning(str(e))
ifos.path.exists(ud.localpath):lib/bb/fetch/__init__.py +15
-1- rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:@@ -1479,6 +1465,20 @@ class FetchMethod(object): """ return True + def rename_bad_checksum(self, ud, suffix): + """ + Rename ud.localpath with the given suffix on checksum mismatch. + Local overrides this to a no-op since its localpath points at the + user's source tree. + """ + if ud.localpath is None: + return + + new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) + bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) + if not bb.utils.movefile(ud.localpath, new_localpath): + bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) + def verify_donestamp(self, ud, d): """Verify the donestamp file
lib/bb/fetch/__init__.py +1
-1@@ -1943,7 +1943,7 @@ class Fetch(object): logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u) logger.debug(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError):raise
else:lib/bb/fetch/local.py +1
-1@@ -17,7 +17,7 @@ import os import urllib.request, urllib.parse, urllib.error import bb import bb.utils -from bb.fetch import FetchMethod, FetchError, ParameterError +from bb.fetch import CHECKSUM_LIST, FetchMethod, FetchError,ParameterError
from bb.fetch import loggerclass Local(FetchMethod):
lib/bb/fetch/local.py +0
-0@@ -31,11 +31,25 @@ class Local(FetchMethod): # We don't set localfile as for this fetcher the file is alreadylocal!
ud.basename =os.path.basename(ud.path)
ud.basepath = ud.pathlib/bb/fetch/local.py +9
-1- ud.needdonestamp = False + # Honor explicit checksum params (UNINATIVE_CHECKSUM, recipe + # ;sha256sum=) by leaving needdonestamp at FetchData's default so + # verify_checksum runs. + name = ud.parm.get("name") + ud.needdonestamp = any( + (name and "%s.%ssum" % (name, a) in ud.parm) + or "%ssum" % a in ud.parm + for a in CHECKSUM_LIST + ) if "*" in ud.path: raise bb.fetch.ParameterError("file:// urls using globbingare no longer supported. Please place the files in a directory and reference that instead.", ud.url)
returnlib/bb/fetch/local.py +2
-0+ def rename_bad_checksum(self, ud, suffix): + # file:// urls point at the user's source tree (recipe-shippedfiles
+ # under FILESPATH or an absolute path the user passed in); skip
thelib/bb/fetch/local.py +3
-0+ # rename so a ChecksumError surfaces without mutating their files. + return + def localpath(self, urldata, d): """ Return the local filename of a given url assuming a successfulfetch.
lib/bb/tests/fetch.py +30
-0@@ -876,6 +876,36 @@ class FetcherLocalTest(FetcherTest): with self.subTest(striplevel=repr(value)): self.assertInvalidStriplevel(value) + def test_local_checksum_match(self): + # Correct sha256 must run verify_checksum to completion; donestamp + # lands in DL_DIR. + import hashlib + content = b"file:// checksum match test\n" + with open(os.path.join(self.localsrcdir, 'sumfile'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + good], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp)) + + def test_local_checksum_mismatch(self): + # Bad sha256 raises ChecksumError without renaming the user's + # source file to the <localpath>_bad-checksum_<sha> sibling. + content = b"file:// checksum mismatch test\n" + srcpath = os.path.join(self.localsrcdir, 'sumfile') + with open(srcpath, 'wb') as f: + f.write(content) + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + bad], self.d) + with self.assertRaises(bb.fetch.FetchError): + fetcher.download() + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content) + self.assertFalse(os.path.exists(srcpath + '_bad-checksum_' + bad)) + def dummyGitTest(self, suffix):# Create dummy local Git repo
src_dir= tempfile.mkdtemp(dir=self.tempdir,
--
2.34.1
Re: [PATCH v5] fetch/local: verify checksums for file:// urls
Hi,
Thank you for taking over the patch series. It has been sitting on my to-do list for quite a while,
it's great that somebody else can push it forward.
Best regards,
Jhonata
Il giorno lun 28 set 2026 alle ore 23:02 Sawas Etairidis < [email protected]> ha scritto:
Quoted 228 lines
Hi,
My bad I assumed that Jhonata stopped working on this based on the ticket comments.
I send him a personal email about this.Regards,
SavvasΣτις Δευ 28 Σεπ 2026 στις 10:59 μ.μ., ο/η Savvas Etairidis < [email protected]> έγραψε:
Quoted 215 lines
[YOCTO #9993]
Local.urldata_init()always cleared needdonestamp, so a checksum given in a file:// url (e.g.UNINATIVE_CHECKSUMwhenUNINATIVE_URLpoints at a local file://) was never verified. Keep the donestamp when the url carries a checksum soverify_checksumruns, and makerename_bad_checksum()a FetchMethod method that Local overrides as a no-op, so a mismatch doesn't rename the user's source file.Based on a patch by Jhonata Poma-Hansen.
Signed-off-by Savvas Etairidis <[email protected]>---
changes in v5:
- Rebased on top of master, added changelog entrychanges in v4:
- Rebased on top of masterchanges in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen---
--- lib/bb/fetch/__init__.py | 36 ++++++++++++++++++------------------ lib/bb/fetch/local.py | 18 ++++++++++++++++-- lib/bb/tests/fetch.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 20 deletions(-)lib/bb/fetch/__init__.py +2
-16@@ -742,7 +742,7 @@ def verify_donestamp(ud, d, origud=None): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) return False@@ -775,7 +775,7 @@ def update_stamp(ud, d): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) raise@@ -1079,20 +1079,6 @@ def build_mirroruris(origud, mirrors, ld): return uris, uds -def rename_bad_checksum(ud, suffix): - """ - Renames files to have suffix from parameter - """ - - if ud.localpath is None: - return - - new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) - bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) - if not bb.utils.movefile(ud.localpath, new_localpath): - bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) - - def try_mirror_url(fetch, origud, ud, ld, check = False): # Return of None or a value means we're finished# False means try another url
lib/bb/fetch/__init__.py +0
-0@@ -1163,7 +1149,7 @@ def try_mirror_url(fetch, origud, ud, ld, check =False):
logger.warning("Mirror checksum failure for url %s (original
url: %s)\nCleaning and trying again." % (ud.url, origud.url))
logger.warning(str(e))
ifos.path.exists(ud.localpath):lib/bb/fetch/__init__.py +15
-1- rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:@@ -1479,6 +1465,20 @@ class FetchMethod(object): """ return True + def rename_bad_checksum(self, ud, suffix): + """ + Rename ud.localpath with the given suffix on checksum mismatch. + Local overrides this to a no-op since its localpath points at the + user's source tree. + """ + if ud.localpath is None: + return + + new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) + bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) + if not bb.utils.movefile(ud.localpath, new_localpath): + bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) + def verify_donestamp(self, ud, d): """Verify the donestamp file
lib/bb/fetch/__init__.py +1
-1@@ -1943,7 +1943,7 @@ class Fetch(object): logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u) logger.debug(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError):raise
else:lib/bb/fetch/local.py +1
-1@@ -17,7 +17,7 @@ import os import urllib.request, urllib.parse, urllib.error import bb import bb.utils -from bb.fetch import FetchMethod, FetchError, ParameterError +from bb.fetch import CHECKSUM_LIST, FetchMethod, FetchError,ParameterError
from bb.fetch import loggerclass Local(FetchMethod):
lib/bb/fetch/local.py +0
-0@@ -31,11 +31,25 @@ class Local(FetchMethod): # We don't set localfile as for this fetcher the file is alreadylocal!
ud.basename =os.path.basename(ud.path)
ud.basepath = ud.pathlib/bb/fetch/local.py +9
-1- ud.needdonestamp = False + # Honor explicit checksum params (UNINATIVE_CHECKSUM, recipe + # ;sha256sum=) by leaving needdonestamp at FetchData's default so + # verify_checksum runs. + name = ud.parm.get("name") + ud.needdonestamp = any( + (name and "%s.%ssum" % (name, a) in ud.parm) + or "%ssum" % a in ud.parm + for a in CHECKSUM_LIST + ) if "*" in ud.path: raise bb.fetch.ParameterError("file:// urls using globbingare no longer supported. Please place the files in a directory and reference that instead.", ud.url)
returnlib/bb/fetch/local.py +2
-0+ def rename_bad_checksum(self, ud, suffix): + # file:// urls point at the user's source tree (recipe-shippedfiles
+ # under FILESPATH or an absolute path the user passed in); skip
the
+ # rename so a ChecksumError surfaces without mutating their
files.lib/bb/fetch/local.py +2
-0+ return + def localpath(self, urldata, d): """ Return the local filename of a given url assuming a successfulfetch.
lib/bb/tests/fetch.py +2
-0@@ -876,6 +876,36 @@ class FetcherLocalTest(FetcherTest): with self.subTest(striplevel=repr(value)): self.assertInvalidStriplevel(value) + def test_local_checksum_match(self): + # Correct sha256 must run verify_checksum to completion;donestamp
lib/bb/tests/fetch.py +7
-0+ # lands in DL_DIR. + import hashlib + content = b"file:// checksum match test\n" + with open(os.path.join(self.localsrcdir, 'sumfile'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + good],self.d)
lib/bb/tests/fetch.py +14
-0+ ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp)) + + def test_local_checksum_mismatch(self): + # Bad sha256 raises ChecksumError without renaming the user's + # source file to the <localpath>_bad-checksum_<sha> sibling. + content = b"file:// checksum mismatch test\n" + srcpath = os.path.join(self.localsrcdir, 'sumfile') + with open(srcpath, 'wb') as f: + f.write(content) + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + bad],self.d)
lib/bb/tests/fetch.py +6
-0+ with self.assertRaises(bb.fetch.FetchError): + fetcher.download() + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content) + self.assertFalse(os.path.exists(srcpath + '_bad-checksum_' +bad))
+
defdummyGitTest(self, suffix):
# Create dummy local Git reposrc_dir= tempfile.mkdtemp(dir=self.tempdir,
--
2.34.1
Re: [PATCH v5] fetch/local: verify checksums for file:// urls
In reply to Savvas EtairidisHi Savvas,
Some review comments inline.
Quoted 8 lines · Savvas Etairidis
[YOCTO #9993]
Local.urldata_init()always cleared needdonestamp, so a checksum given in a file:// url (e.g.UNINATIVE_CHECKSUMwhenUNINATIVE_URLpoints at a local file://) was never verified. Keep the donestamp when the url carries a checksum soverify_checksumruns, and makerename_bad_checksum()a FetchMethod method that Local overrides as a no-op, so a mismatch doesn't rename the user's source file.
This commit message is incredibly terse, and it has changed from v3 without any explanation as to why.
Quoted 75 lines
Based on a patch by Jhonata Poma-Hansen.
Signed-off-by Savvas Etairidis <[email protected]>---
changes in v5:
- Rebased on top of master, added changelog entrychanges in v4:
- Rebased on top of masterchanges in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen---
--- lib/bb/fetch/__init__.py | 36 ++++++++++++++++++------------------ lib/bb/fetch/local.py | 18 ++++++++++++++++-- lib/bb/tests/fetch.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 20 deletions(-)lib/bb/fetch/__init__.py +3
-17@@ -742,7 +742,7 @@ def verify_donestamp(ud, d, origud=None): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) return False@@ -775,7 +775,7 @@ def update_stamp(ud, d): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) raise@@ -1079,20 +1079,6 @@ def build_mirroruris(origud, mirrors, ld): return uris, uds -def rename_bad_checksum(ud, suffix): - """ - Renames files to have suffix from parameter - """ - - if ud.localpath is None: - return - - new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) - bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) - if not bb.utils.movefile(ud.localpath, new_localpath): - bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) - - def try_mirror_url(fetch, origud, ud, ld, check = False): # Return of None or a value means we're finished # False means try another url@@ -1163,7 +1149,7 @@ def try_mirror_url(fetch, origud, ud, ld, check = False): logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url)) logger.warning(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
lib/bb/fetch/__init__.py class FetchMethod(object): · line 1473
Quoted 9 lines
lib/bb/fetch/__init__.py +6
-0@@ -1479,6 +1465,20 @@ class FetchMethod(object): """ return True + def rename_bad_checksum(self, ud, suffix): + """ + Rename ud.localpath with the given suffix on checksum mismatch. + Local overrides this to a no-op since its localpath points at the + user's source tree. + """
The docstring for a method explaining how another class overrides this function is an AI-generated code smell. It makes no sense to document this here.
Quoted 37 lines
lib/bb/fetch/__init__.py +9
-1+ if ud.localpath is None: + return + + new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) + bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) + if not bb.utils.movefile(ud.localpath, new_localpath): + bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) + def verify_donestamp(self, ud, d): """ Verify the donestamp file@@ -1943,7 +1943,7 @@ class Fetch(object): logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u) logger.debug(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:lib/bb/fetch/local.py +1
-1@@ -17,7 +17,7 @@ import os import urllib.request, urllib.parse, urllib.error import bb import bb.utils -from bb.fetch import FetchMethod, FetchError, ParameterError +from bb.fetch import CHECKSUM_LIST, FetchMethod, FetchError, ParameterError from bb.fetch import logger class Local(FetchMethod):@@ -31,11 +31,25 @@ class Local(FetchMethod): # We don't set localfile as for this fetcher the file is already local! ud.basename = os.path.basename(ud.path) ud.basepath = ud.path
lib/bb/fetch/local.py class Local(FetchMethod): · line 42
Quoted 10 lines
lib/bb/fetch/local.py +9
-1- ud.needdonestamp = False + # Honor explicit checksum params (UNINATIVE_CHECKSUM, recipe + # ;sha256sum=) by leaving needdonestamp at FetchData's default so + # verify_checksum runs. + name = ud.parm.get("name") + ud.needdonestamp = any( + (name and "%s.%ssum" % (name, a) in ud.parm) + or "%ssum" % a in ud.parm + for a in CHECKSUM_LIST + )
This expression is a bit confusing, can we instead check for the presence of %s_name attributes set by configure_checksum() calls in FetchData.__init__()?
The comment can also be simpler, perhaps something like "We need a done stamp file only if a checksum is provided". We should also separate things with a blank line if we need a comment before the ud.needdonestamp assignment.
lib/bb/fetch/local.py class Local(FetchMethod): · line 51
Quoted 9 lines
lib/bb/fetch/local.py +5
-0if "*" in ud.path: raise bb.fetch.ParameterError("file:// urls using globbing are no longer supported. Please place the files in a directory and reference that instead.", ud.url) return + def rename_bad_checksum(self, ud, suffix): + # file:// urls point at the user's source tree (recipe-shipped files + # under FILESPATH or an absolute path the user passed in); skip the + # rename so a ChecksumError surfaces without mutating their files. + return
Again the comment can be simpler, perhaps "Don't rename the user's files".
Quoted 9 lines
lib/bb/fetch/local.py +1
-0+ def localpath(self, urldata, d): """ Return the local filename of a given url assuming a successful fetch.lib/bb/tests/fetch.py +0
-0
lib/bb/tests/fetch.py class FetcherLocalTest(FetcherTest): · line 881
Quoted 6 lines
lib/bb/tests/fetch.py +3
-0@@ -876,6 +876,36 @@ class FetcherLocalTest(FetcherTest): with self.subTest(striplevel=repr(value)): self.assertInvalidStriplevel(value) + def test_local_checksum_match(self): + # Correct sha256 must run verify_checksum to completion; donestamp + # lands in DL_DIR.
This comment is also an AI-generated code smell. Please ensure that you're following our AI-assisted contribution policy [1]. If the original patch from Jhonata was AI-generated then you need to document this.
[1]: https://docs.yoctoproject.org/contributor-guide/submit-changes.html#acceptance-of-ai-generated-code
lib/bb/tests/fetch.py class FetcherLocalTest(FetcherTest): · line 891
Quoted 10 lines
lib/bb/tests/fetch.py +10
-0+ import hashlib + content = b"file:// checksum match test\n" + with open(os.path.join(self.localsrcdir, 'sumfile'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + good], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp))
We shouldn't need either of the assertTrue() calls here. The checksum mismatch case will catch failure to check the checksum, and fetcher.download() will raise an error if it fails.
Quoted 5 lines
lib/bb/tests/fetch.py +5
-0+ + def test_local_checksum_mismatch(self): + # Bad sha256 raises ChecksumError without renaming the user's + # source file to the <localpath>_bad-checksum_<sha> sibling. + content = b"file:// checksum mismatch test\n"
lib/bb/tests/fetch.py class FetcherLocalTest(FetcherTest): · line 906
Quoted 10 lines
lib/bb/tests/fetch.py +10
-0+ srcpath = os.path.join(self.localsrcdir, 'sumfile') + with open(srcpath, 'wb') as f: + f.write(content) + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + bad], self.d) + with self.assertRaises(bb.fetch.FetchError): + fetcher.download() + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content)
Why are we checking the content of the file? We're just looking for a rename.
lib/bb/tests/fetch.py class FetcherLocalTest(FetcherTest): · line 911
Quoted 5 lines
lib/bb/tests/fetch.py +2
-0+ self.assertFalse(os.path.exists(srcpath + '_bad-checksum_' + bad)) + def dummyGitTest(self, suffix): # Create dummy local Git repo src_dir = tempfile.mkdtemp(dir=self.tempdir,
Best regards,
Signature · 1 line
Paul Barker
PATCH v6 1/1 fetch/local: verify checksums for file:// urls
[YOCTO #9993]
A checksum given in a file:// url (e.g. UNINATIVE_CHECKSUM when UNINATIVE_URL points at a local file://) was never verified, because Local disables the donestamp and so verify_checksum() was never called. Override verify_donestamp() in Local to check the file against those checksums on every fetch, via a new FetchData.verify_checksum_if_expected() helper. A mismatch raises ChecksumError before the rename/mirror handling, so the user's file is never renamed.
Hashing directly rather than enabling the donestamp avoids trusting a cached checksum for a file edited with an old mtime, and avoids stamp and lock paths in DL_DIR derived from the url path colliding with other recipes or real downloads.
The file is re-hashed on every fetch; this is a known cost, but only for file:// urls that carry a checksum, which are typically small.
Based on a patch by Jhonata Poma-Hansen.
AI-Generated: Uses Claude Code (Claude Opus 5.5)
---
changes in v6:
- Rebased on top of master
- Fixed issues reported by reviewer
- Check the file directly in Local.verify_donestamp(), via a new FetchData.verify_checksum_if_expected() helper, instead of enabling the donestamp. Fixes stale checksums for files edited with an old mtime and DL_DIR stamp/lock path collisions
- Drop the rename_bad_checksum() refactor, no longer needed
- Document the cost of re-hashing on every fetch
- Tests: use a correct md5sum and a wrong sha256sum in the mismatch test, add a test for a file edited with an old mtime, and check that nothing is written to DL_DIR
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetch
changes in v5:
- Rebased on top of master, added changelog entry
changes in v4:
- Rebased on top of master
changes in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen
---
--- lib/bb/fetch/__init__.py | 13 ++++++++++++ lib/bb/fetch/local.py | 16 ++++++++++++++ lib/bb/tests/fetch.py | 46 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+)
lib/bb/fetch/__init__.py +13 -0
@@ -1413,6 +1413,19 @@ class FetchData(object): self.donestamp = basepath + '.done' self.lockfile = basepath + '.lock' + def verify_checksum_if_expected(self, d): + """ + Verify localpath against the checksums given in the url, if any. + """ + expected = False + for checksum_id in CHECKSUM_LIST: + if getattr(self, "%s_expected" % checksum_id): + expected = True + break + + if expected: + verify_checksum(self, d) + def setup_revisions(self, d): self.revision = srcrev_internal_helper(self, d, self.name)
lib/bb/fetch/local.py +16 -0
@@ -36,6 +36,22 @@ class Local(FetchMethod): raise bb.fetch.ParameterError("file:// urls using globbing are no longer supported. Please place the files in a directory and reference that instead.", ud.url) return + def verify_donestamp(self, ud, d): + # file:// urls have no donestamp; verify any checksum given in the url + # against the file itself on every fetch, since it may be edited in + # place without its mtime changing. A missing file is reported by + # download() with the searched paths, so there's nothing to check here. + if not os.path.exists(ud.localpath): + return True + # Re-hashing on every run is a known cost: verify_checksum() computes + # every algorithm in CHECKSUM_LIST, and Fetch.download() calls + # verify_donestamp() twice, so a 100MB file adds ~1s per fetch. + # This only affects file:// urls that carry a checksum, which are + # typically small (e.g. uninative tarballs), so correctness is + # preferred over caching. + ud.verify_checksum_if_expected(d) + return True + def localpath(self, urldata, d): """ Return the local filename of a given url assuming a successful fetch.
lib/bb/tests/fetch.py +46 -0
52 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v6] fetch/local: verify checksums for file:// urls
Quoted 11 lines · Savvas Etairidis
[YOCTO #9993]
A checksum given in a file:// url (e.g.
UNINATIVE_CHECKSUMwhenUNINATIVE_URLpoints at a local file://) was never verified, because Local disables the donestamp and soverify_checksum()was never called. Overrideverify_donestamp()in Local to check the file against those checksums on every fetch, via a newFetchData.verify_checksum_if_expected()helper. A mismatch raises ChecksumError before the rename/mirror handling, so the user's file is never renamed.
Quoted 6 lines
Hashing directly rather than enabling the donestamp avoids trusting a cached checksum for a file edited with an old mtime, and avoids stamp and lock paths in
DL_DIRderived from the url path colliding with other recipes or real downloads.
The file is re-hashed on every fetch; this is a known cost, but only for file:// urls that carry a checksum, which are typically small.
We shouldn't assume that anything coming from a file:// URL is "typically small", or that it is on fast local storage (it could be on a HDD array accessed over NFS).
Quoted 9 lines
Based on a patch by Jhonata Poma-Hansen.
AI-Generated: Uses Claude Code (Claude Opus 5.5)
Signed-off-by Savvas Etairidis <[email protected]>---
changes in v6:
- Rebased on top of master
You don't need to mention this if it didn't result in actual change.
Quoted 1 line
- Fixed issues reported by reviewer
Such as? This is a completely different approach so it's useful to know which bits of feedback were actually relevant and which were made obsolete by the change in approach.
Quoted 4 lines
- Check the file directly in
Local.verify_donestamp(), via a newFetchData.verify_checksum_if_expected()helper, instead of enabling the donestamp. Fixes stale checksums for files edited with an old mtime andDL_DIRstamp/lock path collisions
What does "edited with an old mtime" mean?
Quoted 5 lines
- Drop the
rename_bad_checksum()refactor, no longer needed
- Document the cost of re-hashing on every fetch
- Tests: use a correct md5sum and a wrong sha256sum in the mismatch test, add a test for a file edited with an old mtime, and check that nothing is written toDL_DIR
Why complicate this by using different hash algorithms for the different tests?
Quoted 10 lines
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetch
changes in v5:
- Rebased on top of master, added changelog entrychanges in v4:
- Rebased on top of masterchanges in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen
Best regards,
Signature · 1 line
Paul Barker
PATCH v7 1/1 fetch/local: verify checksums for file:// urls
[YOCTO #9993]
A checksum given in a file:// url (e.g. UNINATIVE_CHECKSUM when UNINATIVE_URL points at a local file://) was never verified, because Local disables the donestamp and so verify_checksum() was never called. Override verify_donestamp() in Local to check the file against those checksums. A mismatch raises ChecksumError before the rename/mirror handling, so the user's file is never renamed.
Based on a patch by Jhonata Poma-Hansen.
AI-Generated: Uses Claude Code (Claude Opus 5.5)
---
changes in v7:
- We shouldn't assume that anything coming from a file:// URL is "typically small", or that it is on fast local storage (it could be on a HDD array accessed over NFS).
* Rework the code to avoid re-hashing the file on every fetch, and instead only re-hash it when the checksum is actually expected to be verified.
- You don't need to mention this if it didn't result in actual change.
* Wont do in the future.
- Such as? This is a completely different approach so it's useful to know which bits of feedback were actually relevant and which were made obsolete by the change in approach.
* Hoped I did better this time.
- What does "edited with an old mtime" mean?
* In that test the file attributes are modified to have an old mtime, which is a corner case that was not handled correctly before. This test was removed in v6 because the new approach doesn't have that issue anymore.
- Why complicate this by using different hash algorithms for the different tests?
The test wants to cover the case where we have a valid md5sum but an invalid sha256sum or a invalid md5sum but a valid sha256sum.
- Added a test to covern new functionality.
changes in v6:
- Rebased on top of master
- Fixed issues reported by reviewer
- Check the file directly in Local.verify_donestamp(), via a new FetchData.verify_checksum_if_expected() helper, instead of enabling the donestamp. Fixes stale checksums for files edited with an old mtime and DL_DIR stamp/lock path collisions
- Drop the rename_bad_checksum() refactor, no longer needed
- Document the cost of re-hashing on every fetch
- Tests: use a correct md5sum and a wrong sha256sum in the mismatch test, add a test for a file edited with an old mtime, and check that nothing is written to DL_DIR
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetch
changes in v5:
- Rebased on top of master, added changelog entry
changes in v4:
- Rebased on top of master
changes in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen
---
--- lib/bb/fetch/__init__.py | 9 ++++ lib/bb/fetch/local.py | 82 +++++++++++++++++++++++++++++ lib/bb/tests/fetch.py | 109 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+)
lib/bb/fetch/__init__.py +9 -0
@@ -1413,6 +1413,15 @@ class FetchData(object): self.donestamp = basepath + '.done' self.lockfile = basepath + '.lock' + def checksum_expected(self): + """ + Is any checksum given for this url? + """ + for checksum_id in CHECKSUM_LIST: + if getattr(self, "%s_expected" % checksum_id): + return True + return False + def setup_revisions(self, d): self.revision = srcrev_internal_helper(self, d, self.name)
lib/bb/fetch/local.py +82 -0
99 lines not on this page · raw message
lib/bb/tests/fetch.py +109 -0
121 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v7] fetch/local: verify checksums for file:// urls
Quoted 29 lines · Savvas Etairidis
[YOCTO #9993]
A checksum given in a file:// url (e.g.
UNINATIVE_CHECKSUMwhenUNINATIVE_URLpoints at a local file://) was never verified, because Local disables the donestamp and soverify_checksum()was never called. Overrideverify_donestamp()in Local to check the file against those checksums. A mismatch raises ChecksumError before the rename/mirror handling, so the user's file is never renamed.Based on a patch by Jhonata Poma-Hansen.
AI-Generated: Uses Claude Code (Claude Opus 5.5)
Signed-off-by Savvas Etairidis <[email protected]>---
changes in v7:
- We shouldn't assume that anything coming from a file:// URL is "typically small", or that it is on fast local storage (it could be on a HDD array accessed over NFS).
* Rework the code to avoid re-hashing the file on every fetch, and instead only re-hash it when the checksum is actually expected to be verified.
- You don't need to mention this if it didn't result in actual change.
* Wont do in the future.
- Such as? This is a completely different approach so it's useful to know which bits of feedback were actually relevant and which were made obsolete by the change in approach.
* Hoped I did better this time.
- What does "edited with an old mtime" mean?
* In that test the file attributes are modified to have an old mtime,
Quoted 8 lines
which is a corner case that was not handled correctly before. This test was removed in v6 because the new approach doesn't have that issue anymore.
- Why complicate this by using different hash algorithms for the different tests?
The test wants to cover the case where we have a valid md5sum but an invalid sha256sum or a invalid md5sum but a valid sha256sum.
- Added a test to covern new functionality.
This is now inventing a new stamp file mechanism, and the tests are significantly more complicated than they were in the previous version of the patch. I'm sorry to say that this is moving further away from a solution that we can merge, instead of closer to one.
This should look like the other fetchers and use the existing done stamps mechanism. That may not be able to detect changes in files if the mtime is moved backwards, but the local filesystem is not a security boundary so this is probably ok. It's also better to get something in place and document the limitations in the bugzilla entry rather than continuing to iterate and add complexity.
There is also no need to test with different hash algorithms here. Two simple test cases, one for a matching checksum and one for a mismatch, should be enough, and they should use the same hash algorithm for consistency. The different hash algorithms should be tested in separate unit tests that focus on those features. AI tools really get carried away when adding test cases and some judgement is definitely needed to keep them under control.
Best regards,
Signature · 1 line
Paul Barker
Re: [PATCH v7] fetch/local: verify checksums for file:// urls
Hi,
The new stamp file mechanism was there to cover the following case:
Developer A builds with file:///mnt/nas/foo.tar.xz;sha256sum=<X>, which caches the file's checksums in a donestamp. Developer B then replaces the file on the NAS with an older version that keeps its older timestamp (e.g. mv, cp -p, rsync -a). On A's next build the file is older than the donestamp, so the cached checksums are trusted and B's file is used in place without being hashed, even though it no longer matches <X>.
As that was a larger code change, it needed more unit tests. To me it is better to have one test too many than one too few, with priority given to edge cases, as long as the tests take seconds or less to run.
I will send a new version where the existing donestamp is enabled for file:// urls that have a checksum, with the two original unit tests: one for a matching checksum and one for a mismatch. Note that this version does not cover the case above.
Regards,
Savvas
Στις Παρ 2 Οκτ 2026 στις 10:50 π.μ., ο/η Paul Barker <[email protected]> έγραψε:
Quoted 68 lines · Savvas Etairidis
Quoted 11 lines
[YOCTO #9993]
A checksum given in a file:// url (e.g.
UNINATIVE_CHECKSUMwhenUNINATIVE_URLpoints at a local file://) was never verified, because Local disables the donestamp and soverify_checksum()was never called. Overrideverify_donestamp()in Local to check the file against those checksums. A mismatch raises ChecksumError before the rename/mirror handling, so the user's file is never renamed.Based on a patch by Jhonata Poma-Hansen.
Quoted 7 lines
AI-Generated: Uses Claude Code (Claude Opus 5.5)
Signed-off-by Savvas Etairidis <[email protected]>---
changes in v7:
- We shouldn't assume that anything coming from a file:// URL is "typically small", or that it is on fast local storage (it could be ona
Quoted 3 lines
HDD array accessed over NFS).
* Rework the code to avoid re-hashing the file on every fetch, and instead only re-hash it when the checksum is actually expected to beverified.
Quoted 4 lines
- You don't need to mention this if it didn't result in actual change.
* Wont do in the future.
- Such as? This is a completely different approach so it's useful to know which bits of feedback were actually relevant and which were madeQuoted 8 lines
obsolete by the change in approach.
* Hoped I did better this time.
- What does "edited with an old mtime" mean?
* In that test the file attributes are modified to have an old mtime, which is a corner case that was not handled correctly before. This test was removed in v6 because the new approach doesn't have that issue anymore.
- Why complicate this by using different hash algorithms for thedifferent
Quoted 4 lines
tests?
The test wants to cover the case where we have a valid md5sum but an invalid sha256sum or a invalid md5sum but a valid sha256sum.
- Added a test to covern new functionality.This is now inventing a new stamp file mechanism, and the tests are significantly more complicated than they were in the previous version of the patch. I'm sorry to say that this is moving further away from a solution that we can merge, instead of closer to one.
This should look like the other fetchers and use the existing done stamps mechanism. That may not be able to detect changes in files if the mtime is moved backwards, but the local filesystem is not a security boundary so this is probably ok. It's also better to get something in place and document the limitations in the bugzilla entry rather than continuing to iterate and add complexity.
There is also no need to test with different hash algorithms here. Two simple test cases, one for a matching checksum and one for a mismatch, should be enough, and they should use the same hash algorithm for consistency. The different hash algorithms should be tested in separate unit tests that focus on those features. AI tools really get carried away when adding test cases and some judgement is definitely needed to keep them under control.
Best regards,
--
Paul Barker
PATCH v8 1/1 fetch/local: verify checksums for file:// urls
[YOCTO #9993]
A checksum given in a file:// url (e.g. UNINATIVE_CHECKSUM when UNINATIVE_URL points at a local file://) was never verified, because Local disables the donestamp and so verify_checksum() was never called. Enable the donestamp for file:// urls that carry a checksum.
Based on a patch by Jhonata Poma-Hansen.
AI-Generated: Uses Claude Code (Claude Opus 5.5)
---
changes in v8:
- Dropped the local-checksums stamps and re-used the regular donestamp, enabled only for file:// urls with a checksum. local.py is unchanged again.
- Tests reduced to a checksum match and a mismatch test.
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetch
changes in v7:
- We shouldn't assume that anything coming from a file:// URL is "typically small", or that it is on fast local storage (it could be on a HDD array accessed over NFS).
* Rework the code to avoid re-hashing the file on every fetch, and instead only re-hash it when the checksum is actually expected to be verified.
- You don't need to mention this if it didn't result in actual change.
* Wont do in the future.
- Such as? This is a completely different approach so it's useful to know which bits of feedback were actually relevant and which were made obsolete by the change in approach.
* Hoped I did better this time.
- What does "edited with an old mtime" mean?
* In that test the file attributes are modified to have an old mtime, which is a corner case that was not handled correctly before. This test was removed in v6 because the new approach doesn't have that issue anymore.
- Why complicate this by using different hash algorithms for the different tests?
The test wants to cover the case where we have a valid md5sum but an invalid sha256sum or a invalid md5sum but a valid sha256sum.
- Added a test to covern new functionality.
changes in v6:
- Rebased on top of master
- Fixed issues reported by reviewer
- Check the file directly in Local.verify_donestamp(), via a new FetchData.verify_checksum_if_expected() helper, instead of enabling the donestamp. Fixes stale checksums for files edited with an old mtime and DL_DIR stamp/lock path collisions
- Drop the rename_bad_checksum() refactor, no longer needed
- Document the cost of re-hashing on every fetch
- Tests: use a correct md5sum and a wrong sha256sum in the mismatch test, add a test for a file edited with an old mtime, and check that nothing is written to DL_DIR
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetch
changes in v5:
- Rebased on top of master, added changelog entry
changes in v4:
- Rebased on top of master
changes in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen
---
--- lib/bb/fetch/__init__.py | 16 ++++++++++++++++ lib/bb/tests/fetch.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)
lib/bb/fetch/__init__.py +16 -0
@@ -1087,6 +1087,10 @@ def rename_bad_checksum(ud, suffix, localpath=None): if ud.localpath is None: return + # A file:// url refers to the user's own file, leave it in place + if isinstance(ud.method, local.Local): + return + if localpath is None: localpath = ud.localpath
@@ -1389,6 +1393,9 @@ class FetchData(object): for checksum_id in CHECKSUM_LIST: configure_checksum(checksum_id) + if isinstance(self.method, local.Local): + self.needdonestamp = self.checksum_expected() + self.ignore_checksums = False if "localpath" in self.parm:
@@ -1416,6 +1423,15 @@ class FetchData(object): self.donestamp = basepath + '.done' self.lockfile = basepath + '.lock' + def checksum_expected(self): + """ + Is any checksum given for this url? + """ + for checksum_id in CHECKSUM_LIST: + if getattr(self, "%s_expected" % checksum_id): + return True + return False + def setup_revisions(self, d): self.revision = srcrev_internal_helper(self, d, self.name)
lib/bb/tests/fetch.py +30 -0
@@ -876,6 +876,36 @@ class FetcherLocalTest(FetcherTest): with self.subTest(striplevel=repr(value)): self.assertInvalidStriplevel(value) + def test_local_checksum_match(self): + # Correct sha256 must run verify_checksum to completion; donestamp + # lands in DL_DIR. + import hashlib + content = b"file:// checksum match test\n" + with open(os.path.join(self.localsrcdir, 'sumfile'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + good], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp)) + + def test_local_checksum_mismatch(self): + # Bad sha256 raises ChecksumError without renaming the user's + # source file to the <localpath>_bad-checksum_<sha> sibling. + content = b"file:// checksum mismatch test\n" + srcpath = os.path.join(self.localsrcdir, 'sumfile') + with open(srcpath, 'wb') as f: + f.write(content) + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + bad], self.d) + with self.assertRaises(bb.fetch2.FetchError): + fetcher.download() + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content) + self.assertFalse(os.path.exists(srcpath + '_bad-checksum_' + bad)) + def dummyGitTest(self, suffix): # Create dummy local Git repo src_dir = tempfile.mkdtemp(dir=self.tempdir,
Signature · 1 line
2.34.1
Re: [PATCH v8] fetch/local: verify checksums for file:// urls
Quoted 77 lines · Savvas Etairidis
[YOCTO #9993]
A checksum given in a file:// url (e.g.
UNINATIVE_CHECKSUMwhenUNINATIVE_URLpoints at a local file://) was never verified, because Local disables the donestamp and soverify_checksum()was never called. Enable the donestamp for file:// urls that carry a checksum.Based on a patch by Jhonata Poma-Hansen.
AI-Generated: Uses Claude Code (Claude Opus 5.5)
Signed-off-by Savvas Etairidis <[email protected]>---
changes in v8:
- Dropped the local-checksums stamps and re-used the regular donestamp, enabled only for file:// urls with a checksum.local.pyis unchanged again.
- Tests reduced to a checksum match and a mismatch test.
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetchchanges in v7:
- We shouldn't assume that anything coming from a file:// URL is "typically small", or that it is on fast local storage (it could be on a HDD array accessed over NFS).
* Rework the code to avoid re-hashing the file on every fetch, and instead only re-hash it when the checksum is actually expected to be verified.
- You don't need to mention this if it didn't result in actual change.
* Wont do in the future.
- Such as? This is a completely different approach so it's useful to know which bits of feedback were actually relevant and which were made obsolete by the change in approach.
* Hoped I did better this time.
- What does "edited with an old mtime" mean?
* In that test the file attributes are modified to have an old mtime, which is a corner case that was not handled correctly before. This test was removed in v6 because the new approach doesn't have that issue anymore.
- Why complicate this by using different hash algorithms for the different tests?
The test wants to cover the case where we have a valid md5sum but an invalid sha256sum or a invalid md5sum but a valid sha256sum.
- Added a test to covern new functionality.changes in v6:
- Rebased on top of master
- Fixed issues reported by reviewer
- Check the file directly inLocal.verify_donestamp(), via a newFetchData.verify_checksum_if_expected()helper, instead of enabling the donestamp. Fixes stale checksums for files edited with an old mtime andDL_DIRstamp/lock path collisions
- Drop therename_bad_checksum()refactor, no longer needed
- Document the cost of re-hashing on every fetch
- Tests: use a correct md5sum and a wrong sha256sum in the mismatch test, add a test for a file edited with an old mtime, and check that nothing is written toDL_DIR
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetchchanges in v5:
- Rebased on top of master, added changelog entrychanges in v4:
- Rebased on top of masterchanges in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen---
--- lib/bb/fetch/__init__.py | 16 ++++++++++++++++ lib/bb/tests/fetch.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)lib/bb/fetch/__init__.py +0
-0
lib/bb/fetch/__init__.py def rename_bad_checksum(ud, suffix, localpath=None): · line 1092
Quoted 6 lines
lib/bb/fetch/__init__.py +3
-0@@ -1087,6 +1087,10 @@ def rename_bad_checksum(ud, suffix, localpath=None): if ud.localpath is None: return + # A file:// url refers to the user's own file, leave it in place + if isinstance(ud.method, local.Local): + return
Adding special cases in the base fetcher code is poor design and complicates maintenance. Why have you taken this approach instead of making rename_bad_checksum() a method of the Fetch class and overriding it as you did in v4 of this patch? I'm looking for an explanation not a v9 patch.
Best regards,
Signature · 1 line
Paul Barker
Re: [PATCH v8] fetch/local: verify checksums for file:// urls
Hi,
Path of least resistance: I noticed the isinstance check above and thought it was acceptable.
My bad I sends this as a personal response previously.
Regards,
Savvas
Στις Δευ 5 Οκτ 2026 στις 4:36 μ.μ., ο/η Paul Barker <[email protected]> έγραψε:
Quoted 101 lines · Savvas Etairidis
Quoted 20 lines
[YOCTO #9993]
A checksum given in a file:// url (e.g.
UNINATIVE_CHECKSUMwhenUNINATIVE_URLpoints at a local file://) was never verified, because Local disables the donestamp and soverify_checksum()was never called. Enable the donestamp for file:// urls that carry a checksum.Based on a patch by Jhonata Poma-Hansen.
AI-Generated: Uses Claude Code (Claude Opus 5.5)
Signed-off-by Savvas Etairidis <[email protected]>---
changes in v8:
- Dropped the local-checksums stamps and re-used the regular donestamp, enabled only for file:// urls with a checksum.local.pyis unchanged again.
- Tests reduced to a checksum match and a mismatch test.
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetchQuoted 3 lines
changes in v7:
- We shouldn't assume that anything coming from a file:// URL is "typically small", or that it is on fast local storage (it could be ona
Quoted 3 lines
HDD array accessed over NFS).
* Rework the code to avoid re-hashing the file on every fetch, and instead only re-hash it when the checksum is actually expected to beverified.
Quoted 4 lines
- You don't need to mention this if it didn't result in actual change.
* Wont do in the future.
- Such as? This is a completely different approach so it's useful to know which bits of feedback were actually relevant and which were madeQuoted 8 lines
obsolete by the change in approach.
* Hoped I did better this time.
- What does "edited with an old mtime" mean?
* In that test the file attributes are modified to have an old mtime, which is a corner case that was not handled correctly before. This test was removed in v6 because the new approach doesn't have that issue anymore.
- Why complicate this by using different hash algorithms for thedifferent
lib/bb/fetch/__init__.py def rename_bad_checksum(ud, suffix,
Quoted 39 lines
tests?
The test wants to cover the case where we have a valid md5sum but an invalid sha256sum or a invalid md5sum but a valid sha256sum.
- Added a test to covern new functionality.changes in v6:
- Rebased on top of master
- Fixed issues reported by reviewer
- Check the file directly inLocal.verify_donestamp(), via a newFetchData.verify_checksum_if_expected()helper, instead of enabling the donestamp. Fixes stale checksums for files edited with an old mtime andDL_DIRstamp/lock path collisions
- Drop therename_bad_checksum()refactor, no longer needed
- Document the cost of re-hashing on every fetch
- Tests: use a correct md5sum and a wrong sha256sum in the mismatch test, add a test for a file edited with an old mtime, and check that nothing is written toDL_DIR
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetchchanges in v5:
- Rebased on top of master, added changelog entrychanges in v4:
- Rebased on top of masterchanges in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen---
--- lib/bb/fetch/__init__.py | 16 ++++++++++++++++ lib/bb/tests/fetch.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)lib/bb/fetch/__init__.py +0
-0@@ -1087,6 +1087,10 @@ def rename_bad_checksum(ud, suffix,localpath=None):
lib/bb/fetch/__init__.py def rename_bad_checksum(ud, suffix, localpath=None): · line 1092
Quoted 6 lines
lib/bb/fetch/__init__.py +3
-0if ud.localpath is None: return + # A file:// url refers to the user's own file, leave it in place + if isinstance(ud.method, local.Local): + returnAdding special cases in the base fetcher code is poor design and complicates maintenance. Why have you taken this approach instead of making
rename_bad_checksum()a method of the Fetch class and overriding it as you did in v4 of this patch? I'm looking for an explanation not a v9 patch.Best regards,
--
Paul Barker
Re: [PATCH v8] fetch/local: verify checksums for file:// urls
Hi Paul,
Since it some time since the last message,
I will post a v9 patch where the special handling is moved in the local.py additionally the rename_bad_checksum is made a class method.
Regards,
Savvas
PATCH v9 1/1 fetch/local: verify checksums for file:// urls
[YOCTO #9993]
A checksum given in a file:// url (e.g. UNINATIVE_CHECKSUM when UNINATIVE_URL points at a local file://) was never verified, because Local disables the donestamp and so verify_checksum() was never called. Enable the donestamp in Local.urldata_init() for file:// urls that carry a checksum parameter.
Based on a patch by Jhonata Poma-Hansen.
AI-Generated: Uses Claude Code (Claude Opus 5.5)
---
changes in v9:
- Moved the donestamp decision from FetchData.__init__() into Local.urldata_init(), with a Local.checksum_expected() helper that checks the url parameters (the *_expected attributes are not yet set when urldata_init() runs).
- Made rename_bad_checksum() a FetchMethod method, overridden in Local, instead of special-casing Local in the base fetcher code.
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetch
changes in v8:
- Dropped the local-checksums stamps and re-used the regular donestamp, enabled only for file:// urls with a checksum. local.py is unchanged again.
- Tests reduced to a checksum match and a mismatch test.
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetch
changes in v7:
- We shouldn't assume that anything coming from a file:// URL is "typically small", or that it is on fast local storage (it could be on a HDD array accessed over NFS).
* Rework the code to avoid re-hashing the file on every fetch, and instead only re-hash it when the checksum is actually expected to be verified.
- You don't need to mention this if it didn't result in actual change.
* Wont do in the future.
- Such as? This is a completely different approach so it's useful to know which bits of feedback were actually relevant and which were made obsolete by the change in approach.
* Hoped I did better this time.
- What does "edited with an old mtime" mean?
* In that test the file attributes are modified to have an old mtime, which is a corner case that was not handled correctly before. This test was removed in v6 because the new approach doesn't have that issue anymore.
- Why complicate this by using different hash algorithms for the different tests?
The test wants to cover the case where we have a valid md5sum but an invalid sha256sum or a invalid md5sum but a valid sha256sum.
- Added a test to covern new functionality.
changes in v6:
- Rebased on top of master
- Fixed issues reported by reviewer
- Check the file directly in Local.verify_donestamp(), via a new FetchData.verify_checksum_if_expected() helper, instead of enabling the donestamp. Fixes stale checksums for files edited with an old mtime and DL_DIR stamp/lock path collisions
- Drop the rename_bad_checksum() refactor, no longer needed
- Document the cost of re-hashing on every fetch
- Tests: use a correct md5sum and a wrong sha256sum in the mismatch test, add a test for a file edited with an old mtime, and check that nothing is written to DL_DIR
- Tested with BB_SKIP_NETTESTS=yes bin/bitbake-selftest bb.tests.fetch
changes in v5:
- Rebased on top of master, added changelog entry
changes in v4:
- Rebased on top of master
changes in v3:
- Cherry-picked the patch from Jhonata Poma-Hansen
---
--- lib/bb/fetch/__init__.py | 41 ++++++++++++++++++++-------------------- lib/bb/fetch/local.py | 20 +++++++++++++++++++- lib/bb/fetch/wget.py | 2 +- lib/bb/tests/fetch.py | 30 +++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 23 deletions(-)
lib/bb/fetch/__init__.py +20 -21
@@ -742,7 +742,7 @@ def verify_donestamp(ud, d, origud=None): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) return False
@@ -775,7 +775,7 @@ def update_stamp(ud, d): logger.warning("Checksum mismatch for local file %s\n" "Cleaning and trying again." % ud.localpath) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) bb.utils.remove(ud.donestamp) raise
@@ -1079,23 +1079,6 @@ def build_mirroruris(origud, mirrors, ld): return uris, uds -def rename_bad_checksum(ud, suffix, localpath=None): - """ - Renames files to have suffix from parameter - """ - - if ud.localpath is None: - return - - if localpath is None: - localpath = ud.localpath - - new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) - bb.warn("Renaming %s to %s" % (localpath, new_localpath)) - if not bb.utils.movefile(localpath, new_localpath): - bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (localpath, new_localpath)) - - def try_mirror_url(fetch, origud, ud, ld, check = False): # Return of None or a value means we're finished # False means try another url
@@ -1166,7 +1149,7 @@ def try_mirror_url(fetch, origud, ud, ld, check = False): logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url)) logger.warning(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
@@ -1482,6 +1465,22 @@ class FetchMethod(object): """ return True + def rename_bad_checksum(self, ud, suffix, localpath=None): + """ + Renames files to have suffix from parameter + """ + + if ud.localpath is None: + return + + if localpath is None: + localpath = ud.localpath + + new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) + bb.warn("Renaming %s to %s" % (localpath, new_localpath)) + if not bb.utils.movefile(localpath, new_localpath): + bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (localpath, new_localpath)) + def verify_donestamp(self, ud, d): """ Verify the donestamp file
@@ -1946,7 +1945,7 @@ class Fetch(object): logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u) logger.debug(str(e)) if os.path.exists(ud.localpath): - rename_bad_checksum(ud, e.checksum) + ud.method.rename_bad_checksum(ud, e.checksum) elif isinstance(e, NoChecksumError): raise else:
lib/bb/fetch/local.py +19 -1
@@ -31,11 +31,29 @@ class Local(FetchMethod): # We don't set localfile as for this fetcher the file is already local! ud.basename = os.path.basename(ud.path) ud.basepath = ud.path - ud.needdonestamp = False + # Only use a donestamp when a checksum is given in the url, so that + # verify_checksum() gets called for the file + ud.needdonestamp = self.checksum_expected(ud) if "*" in ud.path: raise bb.fetch.ParameterError("file:// urls using globbing are no longer supported. Please place the files in a directory and reference that instead.", ud.url) return + def checksum_expected(self, ud): + """ + Is any checksum given in the url parameters? + """ + for checksum_id in bb.fetch.CHECKSUM_LIST: + if "%ssum" % checksum_id in ud.parm or \ + "%s.%ssum" % (ud.name, checksum_id) in ud.parm: + return True + return False + + def rename_bad_checksum(self, ud, suffix, localpath=None): + """ + A file:// url refers to the user's own file, leave it in place + """ + return + def localpath(self, urldata, d): """ Return the local filename of a given url assuming a successful fetch.
lib/bb/fetch/wget.py +1 -1
@@ -181,7 +181,7 @@ class Wget(FetchMethod): except bb.fetch.ChecksumError as e: # The download is resumed with --continue, so a bad file left in place # would be reused by the mirrors and by every later attempt - bb.fetch.rename_bad_checksum(ud, e.checksum, localpath=localpath) + self.rename_bad_checksum(ud, e.checksum, localpath=localpath) raise # Remove the ".tmp" and move the file into position atomically
lib/bb/tests/fetch.py +30 -0
@@ -876,6 +876,36 @@ class FetcherLocalTest(FetcherTest): with self.subTest(striplevel=repr(value)): self.assertInvalidStriplevel(value) + def test_local_checksum_match(self): + # Correct sha256 must run verify_checksum to completion; donestamp + # lands in DL_DIR. + import hashlib + content = b"file:// checksum match test\n" + with open(os.path.join(self.localsrcdir, 'sumfile'), 'wb') as f: + f.write(content) + good = hashlib.sha256(content).hexdigest() + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + good], self.d) + ud = fetcher.ud[fetcher.urls[0]] + self.assertTrue(ud.needdonestamp) + fetcher.download() + self.assertTrue(os.path.exists(ud.donestamp)) + + def test_local_checksum_mismatch(self): + # Bad sha256 raises ChecksumError without renaming the user's + # source file to the <localpath>_bad-checksum_<sha> sibling. + content = b"file:// checksum mismatch test\n" + srcpath = os.path.join(self.localsrcdir, 'sumfile') + with open(srcpath, 'wb') as f: + f.write(content) + bad = "0" * 64 + fetcher = bb.fetch.Fetch(['file://sumfile;sha256sum=' + bad], self.d) + with self.assertRaises(bb.fetch2.FetchError): + fetcher.download() + self.assertTrue(os.path.exists(srcpath)) + with open(srcpath, 'rb') as f: + self.assertEqual(f.read(), content) + self.assertFalse(os.path.exists(srcpath + '_bad-checksum_' + bad)) + def dummyGitTest(self, suffix): # Create dummy local Git repo src_dir = tempfile.mkdtemp(dir=self.tempdir,
Signature · 1 line
2.53.0