Skip to contentSkip to conversation

fetch2: git: Fix path check for git repos

PATCH In review Joshua Watt 2 participants 4 messages Sep 13, 2023 – Sep 14, 2023

lore
Share & tools
lore
#0 lore raw

cover fetch2: git: Fix path check for git repos

The original implementation of this didn't account for symlinks/bind mounts/etc. To simplify the code, make a new helper function called path_is_descendant() that correctly accounts for these, then have the git fetcher use it to determine if a git repo needs to be re-cloned.

Joshua Watt (2):
utils: Add path_is_descendant()
fetch2: git: Use path_is_descendant() instead of path for repo check

 bitbake/lib/bb/fetch2/git.py | 15 ++++++---------
 bitbake/lib/bb/utils.py      | 23 +++++++++++++++++++++++
 2 files changed, 29 insertions(+), 9 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 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/lib/bb/utils.py | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
bitbake/lib/bb/utils.py +23 -0
index 0624a4f3e9a..b401fa5ec7a 100644
@@ -1828,6 +1828,29 @@ def mkstemp(suffix=None, prefix=None, dir=None, text=False):
         prefix = tempfile.gettempprefix() + entropy
     return tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir, text=text)
 
+def path_is_descendant(descendant, ancestor):
+    """
+    Returns True if the path `descendant` is a descendant of `ancestor`
+    (including being equivalent to `ancestor` itself). Otherwise returns False.
+    Correctly accounts for symlinks, bind mounts, etc. by using
+    os.path.samestat() to compare paths
+
+    May raise any exception that os.stat() raises
+    """
+
+    ancestor_stat = os.stat(ancestor)
+
+    # Recurse up each directory component of the descendant to see if it is
+    # equivalent to the ancestor
+    check_dir = os.path.abspath(descendant).rstrip("/")
+    while check_dir:
+        check_stat = os.stat(check_dir)
+        if os.path.samestat(check_stat, ancestor_stat):
+            return True
+        check_dir = os.path.dirname(check_dir).rstrip("/")
+
+    return False
+
 # If we don't have a timeout of some kind and a process/thread exits badly (for example
 # OOM killed) and held a lock, we'd just hang in the lock futex forever. It is better
 # we exit at some point than hang. 5 minutes with no progress means we're probably deadlocked.
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/2 fetch2: git: Use path_is_descendant() instead of path for repo check

In reply to Joshua Watt

Using path prefixes to check if the git directory is a descendant of the clone directory can be easily confused with symlinkes and bind mounts, causing directories to be deleted unnecessarily. Instead, use bb.utils.path_is_descendant() which is immune to the these sorts of problems.

---
 bitbake/lib/bb/fetch2/git.py | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
bitbake/lib/bb/fetch2/git.py +6 -9
index e11271b757a..4385d0b37af 100644
@@ -373,20 +373,17 @@ class Git(FetchMethod):
             try:
                 # Since clones can be bare, use --absolute-git-dir instead of --show-toplevel
                 output = runfetchcmd("LANG=C %s rev-parse --absolute-git-dir" % ud.basecmd, d, workdir=ud.clonedir)
+                toplevel = output.rstrip()
 
-                toplevel = os.path.abspath(output.rstrip())
-                abs_clonedir = os.path.abspath(ud.clonedir).rstrip('/')
-                # The top level Git directory must either be the clone directory
-                # or a child of the clone directory. Any ancestor directory of
-                # the clone directory is not valid as the Git directory (and
-                # probably belongs to some other unrelated repository), so a
-                # clone is required
-                if os.path.commonprefix([abs_clonedir, toplevel]) != abs_clonedir:
-                    logger.warning("Top level directory '%s' doesn't match expected '%s'. Re-cloning", toplevel, ud.clonedir)
+                if not bb.utils.path_is_descendant(toplevel, ud.clonedir):
+                    logger.warning("Top level directory '%s' is not a descendant of '%s'. Re-cloning", toplevel, ud.clonedir)
                     needs_clone = True
             except bb.fetch2.FetchError as e:
                 logger.warning("Unable to get top level for %s (not a git directory?): %s", ud.clonedir, e)
                 needs_clone = True
+            except FileNotFoundError as e:
+                logger.warning("%s", e)
+                needs_clone = True
 
             if needs_clone:
                 shutil.rmtree(ud.clonedir)
Signature · 1 line
2.34.1
#3 lore raw

Re: [bitbake-devel][PATCH 2/2] fetch2: git: Use path_is_descendant() instead of path for repo check

Hi,

Quoted 5 lines · Joshua Watt

Using path prefixes to check if the git directory is a descendant of the clone directory can be easily confused with symlinkes and bind mounts, causing directories to be deleted unnecessarily. Instead, use bb.utils.path_is_descendant() which is immune to the these sorts of problems.

I'm confused by this. What is the original problem? Corrupt download cache git repo being confused with poky git tree when building in "poky/build" directory and downloads are in "poky/build/downloads"? And at the time of check we don't know if git commit, branch, or git remote have changed and we want to avoid full clone if possible?

Cheers,

-Mikko

Quoted 49 lines
---
 bitbake/lib/bb/fetch2/git.py | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
bitbake/lib/bb/fetch2/git.py +6 -9
index e11271b757a..4385d0b37af 100644
@@ -373,20 +373,17 @@ class Git(FetchMethod):
             try:
                 # Since clones can be bare, use --absolute-git-dir instead of --show-toplevel
                 output = runfetchcmd("LANG=C %s rev-parse --absolute-git-dir" % ud.basecmd, d, workdir=ud.clonedir)
+                toplevel = output.rstrip()
 
-                toplevel = os.path.abspath(output.rstrip())
-                abs_clonedir = os.path.abspath(ud.clonedir).rstrip('/')
-                # The top level Git directory must either be the clone directory
-                # or a child of the clone directory. Any ancestor directory of
-                # the clone directory is not valid as the Git directory (and
-                # probably belongs to some other unrelated repository), so a
-                # clone is required
-                if os.path.commonprefix([abs_clonedir, toplevel]) != abs_clonedir:
-                    logger.warning("Top level directory '%s' doesn't match expected '%s'. Re-cloning", toplevel, ud.clonedir)
+                if not bb.utils.path_is_descendant(toplevel, ud.clonedir):
+                    logger.warning("Top level directory '%s' is not a descendant of '%s'. Re-cloning", toplevel, ud.clonedir)
                     needs_clone = True
             except bb.fetch2.FetchError as e:
                 logger.warning("Unable to get top level for %s (not a git directory?): %s", ud.clonedir, e)
                 needs_clone = True
+            except FileNotFoundError as e:
+                logger.warning("%s", e)
+                needs_clone = True
 
             if needs_clone:
                 shutil.rmtree(ud.clonedir)
Signature · 11 lines
2.34.1



-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#15057): https://lists.openembedded.org/g/bitbake-devel/message/15057
Mute This Topic: https://lists.openembedded.org/mt/101344082/7159507
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/bitbake-devel/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-