diff options
author | Joshua Watt <JPEWhacker@gmail.com> | 2023-09-13 13:04:24 -0600 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2023-09-14 15:08:21 +0100 |
commit | b2ab9bd4a394a60bfb2b21f5cc31b6f3185fc7e5 (patch) | |
tree | 581d328ebdc75d6b13237da400d18e7196babc24 | |
parent | a96f735ff9a8aea9997c4db8613158699c7abb72 (diff) | |
download | poky-b2ab9bd4a394a60bfb2b21f5cc31b6f3185fc7e5.tar.gz |
bitbake: utils: Add path_is_descendant()
Adds a utility that checks if one path is an descendant of another. This
check uses os.path.samestat() to make it immune to symlinks and bind
mounts.
(Bitbake rev: c3ae45946886ee2049939dd5a205790657a7de32)
Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r-- | bitbake/lib/bb/utils.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index 0624a4f3e9..b401fa5ec7 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py | |||
@@ -1828,6 +1828,29 @@ def mkstemp(suffix=None, prefix=None, dir=None, text=False): | |||
1828 | prefix = tempfile.gettempprefix() + entropy | 1828 | prefix = tempfile.gettempprefix() + entropy |
1829 | return tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir, text=text) | 1829 | return tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir, text=text) |
1830 | 1830 | ||
1831 | def path_is_descendant(descendant, ancestor): | ||
1832 | """ | ||
1833 | Returns True if the path `descendant` is a descendant of `ancestor` | ||
1834 | (including being equivalent to `ancestor` itself). Otherwise returns False. | ||
1835 | Correctly accounts for symlinks, bind mounts, etc. by using | ||
1836 | os.path.samestat() to compare paths | ||
1837 | |||
1838 | May raise any exception that os.stat() raises | ||
1839 | """ | ||
1840 | |||
1841 | ancestor_stat = os.stat(ancestor) | ||
1842 | |||
1843 | # Recurse up each directory component of the descendant to see if it is | ||
1844 | # equivalent to the ancestor | ||
1845 | check_dir = os.path.abspath(descendant).rstrip("/") | ||
1846 | while check_dir: | ||
1847 | check_stat = os.stat(check_dir) | ||
1848 | if os.path.samestat(check_stat, ancestor_stat): | ||
1849 | return True | ||
1850 | check_dir = os.path.dirname(check_dir).rstrip("/") | ||
1851 | |||
1852 | return False | ||
1853 | |||
1831 | # If we don't have a timeout of some kind and a process/thread exits badly (for example | 1854 | # If we don't have a timeout of some kind and a process/thread exits badly (for example |
1832 | # OOM killed) and held a lock, we'd just hang in the lock futex forever. It is better | 1855 | # OOM killed) and held a lock, we'd just hang in the lock futex forever. It is better |
1833 | # we exit at some point than hang. 5 minutes with no progress means we're probably deadlocked. | 1856 | # we exit at some point than hang. 5 minutes with no progress means we're probably deadlocked. |