[3/3] testbot/lib: Add a utility function to atomically create a new directory.

Francois Gouget fgouget at codeweavers.com
Mon Jun 16 09:11:25 CDT 2014


It ensures there are no race conditions with other processes and simplifies the calling code.
---
 testbot/bin/Engine.pl            | 14 ++------------
 testbot/lib/WineTestBot/Utils.pm | 17 ++++++++++++++++-
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/testbot/bin/Engine.pl b/testbot/bin/Engine.pl
index 29dc919..75a8851 100755
--- a/testbot/bin/Engine.pl
+++ b/testbot/bin/Engine.pl
@@ -412,12 +412,7 @@ sub HandleWinePatchMLSubmission()
     my $FullMessageFileName = "$DataDir/staging/$1";
 
     # Create a work directory
-    my $WorkDir = "$DataDir/staging/" . GenerateRandomString(32) . "_work";
-    while (-e $WorkDir)
-    {
-      $WorkDir = "$DataDir/staging/" . GenerateRandomString(32) . "_work";
-    }
-    mkdir $WorkDir;
+    my $WorkDir = CreateNewDir("$DataDir/staging", "_work");
 
     # Process the patch
     my $Parser = new MIME::Parser;
@@ -489,12 +484,7 @@ sub HandleWinePatchWebSubmission()
   foreach my $WebPatchId (sort { $a <=> $b } @WebPatchIds)
   {
     # Create a working dir
-    my $WorkDir = "$DataDir/staging/" . GenerateRandomString(32) . "_work";
-    while (-e $WorkDir)
-    {
-      $WorkDir = "$DataDir/staging/" . GenerateRandomString(32) . "_work";
-    }
-    mkdir $WorkDir;
+    my $WorkDir = CreateNewDir("$DataDir/staging", "_work");
 
     # Process the patch
     my $Parser = new MIME::Parser;
diff --git a/testbot/lib/WineTestBot/Utils.pm b/testbot/lib/WineTestBot/Utils.pm
index 1cea5d7..743e5ee 100644
--- a/testbot/lib/WineTestBot/Utils.pm
+++ b/testbot/lib/WineTestBot/Utils.pm
@@ -33,7 +33,8 @@ use vars qw (@ISA @EXPORT);
 require Exporter;
 @ISA = qw(Exporter);
 @EXPORT = qw(&MakeSecureURL &SecureConnection &GenerateRandomString
-             &OpenNewFile &CreateNewFile &CreateNewLink &BuildEMailRecipient);
+             &OpenNewFile &CreateNewFile &CreateNewLink &CreateNewDir
+             &BuildEMailRecipient);
 
 sub MakeSecureURL($)
 {
@@ -105,6 +106,20 @@ sub CreateNewLink($$$)
   }
 }
 
+sub CreateNewDir($$)
+{
+  my ($Dir, $Suffix) = @_;
+
+  while (1)
+  {
+    my $Path = "$Dir/" . GenerateRandomString(32) . $Suffix;
+    return $Path if (mkdir $Path);
+
+    # This is not an error that will be fixed by trying a different path
+    return undef if (!$!{EEXIST});
+  }
+}
+
 sub DateTimeToString($)
 {
   my ($Time) = @_;
-- 
2.0.0



More information about the wine-patches mailing list