Openembedded Core Discussions
 help / color / mirror / Atom feed
* [PATCH] path.py: Deal with race issue
@ 2013-05-03 14:11 Richard Purdie
  0 siblings, 0 replies; only message in thread
From: Richard Purdie @ 2013-05-03 14:11 UTC (permalink / raw)
  To: openembedded-core

The change to use copyhardlinktree in some of the sstate code instead of
copytree exposed a race condition. This is due to cp failing if it finds
a directory doesn't exist yet some other process creates it while cp was
trying to create it itself. tar doesn't error in this case.

To fix this we need to create the directory structure with tar, then
use cp to hardlink the files. Messy but probably worth doing.

I also took the opportunity to remove src_bak since the code is neater
without it.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py
index 4f8b66c..0e2d8bc 100644
--- a/meta/lib/oe/path.py
+++ b/meta/lib/oe/path.py
@@ -87,16 +87,21 @@ def copytree(src, dst):
 def copyhardlinktree(src, dst):
     """ Make the hard link when possible, otherwise copy. """
     bb.utils.mkdirhier(dst)
-    src_bak = src
-    if os.path.isdir(src):
-        if not len(os.listdir(src)):
-            return	
-        src = src + "/*"
-    if (os.stat(src_bak).st_dev ==  os.stat(dst).st_dev):
+    if os.path.isdir(src) and not len(os.listdir(src)):
+        return	
+
+    if (os.stat(src).st_dev ==  os.stat(dst).st_dev):
+        # Need to copy directories only with tar first since cp will error if two 
+        # writers try and create a directory at the same time
+        cmd = 'cd %s; find . -type d -print | tar -cf - -C %s -ps --files-from - | tar -xf - -C %s' % (src, src, dst)
+        bb.warn(cmd)
+        check_output(cmd, shell=True, stderr=subprocess.STDOUT)
+        if os.path.isdir(src):
+            src = src + "/*"
         cmd = 'cp -afl %s %s' % (src, dst)
         check_output(cmd, shell=True, stderr=subprocess.STDOUT)
     else:
-        copytree(src_bak, dst)
+        copytree(src, dst)
 
 def remove(path, recurse=True):
     """Equivalent to rm -f or rm -rf"""





^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2013-05-03 14:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-03 14:11 [PATCH] path.py: Deal with race issue Richard Purdie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox