Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # Local file checksum cache implementation |
| 2 | # |
| 3 | # Copyright (C) 2012 Intel Corporation |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 8 | import glob |
| 9 | import operator |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | import os |
| 11 | import stat |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 12 | import pickle |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 13 | import bb.utils |
| 14 | import logging |
| 15 | from bb.cache import MultiProcessCache |
| 16 | |
| 17 | logger = logging.getLogger("BitBake.Cache") |
| 18 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | # mtime cache (non-persistent) |
| 20 | # based upon the assumption that files do not change during bitbake run |
| 21 | class FileMtimeCache(object): |
| 22 | cache = {} |
| 23 | |
| 24 | def cached_mtime(self, f): |
| 25 | if f not in self.cache: |
| 26 | self.cache[f] = os.stat(f)[stat.ST_MTIME] |
| 27 | return self.cache[f] |
| 28 | |
| 29 | def cached_mtime_noerror(self, f): |
| 30 | if f not in self.cache: |
| 31 | try: |
| 32 | self.cache[f] = os.stat(f)[stat.ST_MTIME] |
| 33 | except OSError: |
| 34 | return 0 |
| 35 | return self.cache[f] |
| 36 | |
| 37 | def update_mtime(self, f): |
| 38 | self.cache[f] = os.stat(f)[stat.ST_MTIME] |
| 39 | return self.cache[f] |
| 40 | |
| 41 | def clear(self): |
| 42 | self.cache.clear() |
| 43 | |
| 44 | # Checksum + mtime cache (persistent) |
| 45 | class FileChecksumCache(MultiProcessCache): |
| 46 | cache_file_name = "local_file_checksum_cache.dat" |
| 47 | CACHE_VERSION = 1 |
| 48 | |
| 49 | def __init__(self): |
| 50 | self.mtime_cache = FileMtimeCache() |
| 51 | MultiProcessCache.__init__(self) |
| 52 | |
| 53 | def get_checksum(self, f): |
| 54 | entry = self.cachedata[0].get(f) |
| 55 | cmtime = self.mtime_cache.cached_mtime(f) |
| 56 | if entry: |
| 57 | (mtime, hashval) = entry |
| 58 | if cmtime == mtime: |
| 59 | return hashval |
| 60 | else: |
| 61 | bb.debug(2, "file %s changed mtime, recompute checksum" % f) |
| 62 | |
| 63 | hashval = bb.utils.md5_file(f) |
| 64 | self.cachedata_extras[0][f] = (cmtime, hashval) |
| 65 | return hashval |
| 66 | |
| 67 | def merge_data(self, source, dest): |
| 68 | for h in source[0]: |
| 69 | if h in dest: |
| 70 | (smtime, _) = source[0][h] |
| 71 | (dmtime, _) = dest[0][h] |
| 72 | if smtime > dmtime: |
| 73 | dest[0][h] = source[0][h] |
| 74 | else: |
| 75 | dest[0][h] = source[0][h] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 76 | |
| 77 | def get_checksums(self, filelist, pn): |
| 78 | """Get checksums for a list of files""" |
| 79 | |
| 80 | def checksum_file(f): |
| 81 | try: |
| 82 | checksum = self.get_checksum(f) |
| 83 | except OSError as e: |
| 84 | bb.warn("Unable to get checksum for %s SRC_URI entry %s: %s" % (pn, os.path.basename(f), e)) |
| 85 | return None |
| 86 | return checksum |
| 87 | |
| 88 | def checksum_dir(pth): |
| 89 | # Handle directories recursively |
Brad Bishop | 220d553 | 2018-08-14 00:59:39 +0100 | [diff] [blame] | 90 | if pth == "/": |
| 91 | bb.fatal("Refusing to checksum /") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 92 | dirchecksums = [] |
| 93 | for root, dirs, files in os.walk(pth): |
| 94 | for name in files: |
| 95 | fullpth = os.path.join(root, name) |
| 96 | checksum = checksum_file(fullpth) |
| 97 | if checksum: |
| 98 | dirchecksums.append((fullpth, checksum)) |
| 99 | return dirchecksums |
| 100 | |
| 101 | checksums = [] |
| 102 | for pth in filelist.split(): |
| 103 | exist = pth.split(":")[1] |
| 104 | if exist == "False": |
| 105 | continue |
| 106 | pth = pth.split(":")[0] |
| 107 | if '*' in pth: |
| 108 | # Handle globs |
| 109 | for f in glob.glob(pth): |
| 110 | if os.path.isdir(f): |
| 111 | if not os.path.islink(f): |
| 112 | checksums.extend(checksum_dir(f)) |
| 113 | else: |
| 114 | checksum = checksum_file(f) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 115 | if checksum: |
| 116 | checksums.append((f, checksum)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 117 | elif os.path.isdir(pth): |
| 118 | if not os.path.islink(pth): |
| 119 | checksums.extend(checksum_dir(pth)) |
| 120 | else: |
| 121 | checksum = checksum_file(pth) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 122 | if checksum: |
| 123 | checksums.append((pth, checksum)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 124 | |
| 125 | checksums.sort(key=operator.itemgetter(1)) |
| 126 | return checksums |