[v3,4/9] oe.path: Add copyhardlink() helper function

Message ID 20200907162018.16636-1-Vijaikumar_Kanagarajan@mentor.com
State Superseded, archived
Headers show
Series WIC update | expand

Commit Message

Vijai Kumar K Sept. 7, 2020, 8:20 a.m. UTC
From: Paul Barker <paul@betafive.co.uk>

This function creates hard links if possible, falling back to copying
the file if the destination is on a different volume to the source.

The docstring for copyhardlinktree() is also updated to make the
difference between the two functions a little clearer.

Signed-off-by: Paul Barker <paul@betafive.co.uk>
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
[Vijai: Imported from OE-core 5437efa16f9bec914e417c6c939a39c247084f52]
Signed-off-by: Vijai Kumar K <Vijaikumar_Kanagarajan@mentor.com>
---
 meta/lib/oe/path.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

Patch

diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py
index 90c87f5..c6bb604 100644
--- a/meta/lib/oe/path.py
+++ b/meta/lib/oe/path.py
@@ -94,7 +94,7 @@  def copytree(src, dst):
     subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
 
 def copyhardlinktree(src, dst):
-    """ Make the hard link when possible, otherwise copy. """
+    """Make a tree of hard links when possible, otherwise copy."""
     bb.utils.mkdirhier(dst)
     if os.path.isdir(src) and not len(os.listdir(src)):
         return
@@ -118,6 +118,17 @@  def copyhardlinktree(src, dst):
     else:
         copytree(src, dst)
 
+def copyhardlink(src, dst):
+    """Make a hard link when possible, otherwise copy."""
+
+    # We need to stat the destination directory as the destination file probably
+    # doesn't exist yet.
+    dstdir = os.path.dirname(dst)
+    if os.stat(src).st_dev == os.stat(dstdir).st_dev:
+        os.link(src, dst)
+    else:
+        shutil.copy(src, dst)
+
 def remove(path, recurse=True):
     """
     Equivalent to rm -f or rm -rf