]> sourceware.org Git - libabigail.git/commitdiff
Try to avoid a race condition when abipkgdiff extracts packages.
authorDodji Seketeli <dodji@redhat.com>
Sun, 19 Jul 2015 22:02:00 +0000 (00:02 +0200)
committerDodji Seketeli <dodji@redhat.com>
Sun, 19 Jul 2015 22:02:00 +0000 (00:02 +0200)
abipkgdiff extracts the content of the first package in a directory
named <tmpdir>/package1 and the content second package in
<tmpdir>/package2.  If two independant instances of abipkgdiff are
launched at the same time, they are going to walk on each others'
toes, to say the least.

This patch extracts the content of each package in directory named
<tmpdir>/<randomname>/package1, where randomname is supposed to be a
random number, and so should be unique, most of the time.

I guess we should try harder to generate a randomname that is unique
when we see that the directory <tmpdir>/<randomname> exists already,
but for now, what we have is good enough, or at least better than what
we have had so far.

* include/abg-tools-utils.h (get_random_number)
(get_random_number_as_string): Declare new functions.
* src/abg-tools-utils.cc (get_random_number)
(get_random_number_as_string): Define them.
* tools/abipkgdiff.cc
(package::extracted_package_parent_dir_path): New data member.
(package::package): Initialize
package::extracted_package_parent_dir_path to
<tmpdir>/<randomname>, with randomname being a random number
represented as a string.
(extract_rpm): Make sure to create a hierarchy of directories, not
just a directory.

Signed-off-by: Dodji Seketeli <dodji@redhat.com>
include/abg-tools-utils.h
src/abg-tools-utils.cc
tools/abipkgdiff.cc

index 16c1d38ed1e63e3526d82bd1d23c3e5476956816..f04a09a54e25dcd2eede871654eb08f649a836b3 100644 (file)
@@ -45,6 +45,13 @@ bool ensure_dir_path_created(const string&);
 bool ensure_parent_dir_created(const string&);
 bool check_file(const string& path, ostream& out);
 
+
+size_t
+get_random_number();
+
+string
+get_random_number_as_string();
+
 /// The different types of files understood the bi* suite of tools.
 enum file_type
 {
index 7d57a874365f19b1ab4e9b2dd4011909c6038693..2be7e6b49dad6b77b60cc035e5a1a4571e88bc95 100644 (file)
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <time.h>
 #include <cstdlib>
 #include <cstring>
 #include <libgen.h>
 #include <fstream>
 #include <iostream>
+#include <sstream>
 #include <abg-ir.h>
 #include "abg-tools-utils.h"
 
@@ -311,6 +313,36 @@ check_file(const string& path,
   return true;
 }
 
+
+/// Get a pseudo random number.
+///
+/// @return a pseudo random number.
+size_t
+get_random_number()
+{
+  static __thread bool initialized = false;
+
+  if (!initialized)
+    {
+      srand(time(NULL));
+      initialized = true;
+    }
+
+  return rand();
+}
+
+/// Get a pseudo random number as string.
+///
+/// @return a pseudo random number as string.
+string
+get_random_number_as_string()
+{
+  std::ostringstream o;
+  o << get_random_number();
+
+  return o.str();
+}
+
 ostream&
 operator<<(ostream& output,
           file_type r)
index 13c984f1ae7700e56a4abd18cccdf3c9f2a6b46e..bace67d40dd25ef0ffdbd09698f3b43c51407162 100644 (file)
@@ -158,6 +158,7 @@ struct abi_diff
 struct package
 {
   string                               path;
+  string                               extracted_package_parent_dir_path;
   string                               extracted_package_dir_path;
   abigail::tools_utils::file_type      type;
   bool                                 is_debug_info;
@@ -184,7 +185,13 @@ struct package
       extracted_package_dir_path = tmpdir;
     else
       extracted_package_dir_path = "/tmp";
-    extracted_package_dir_path = extracted_package_dir_path + "/" + dir;
+
+    using abigail::tools_utils::get_random_number_as_string;
+
+    extracted_package_parent_dir_path = extracted_package_dir_path
+      + "/libabigail-tmp-dir-" + get_random_number_as_string();
+
+    extracted_package_dir_path =  extracted_package_parent_dir_path + "/" + dir;
   }
 
   /// Erase the content of the temporary extraction directory that has
@@ -197,7 +204,7 @@ struct package
           << extracted_package_dir_path
           << " ...";
 
-    string cmd = "rm -rf " + extracted_package_dir_path;
+    string cmd = "rm -rf " + extracted_package_parent_dir_path;
     system(cmd.c_str());
 
     if (verbose)
@@ -260,7 +267,7 @@ extract_rpm(const string& package_path,
 
   system(cmd.c_str());
 
-  cmd = "mkdir " + extracted_package_dir_path + " && cd " +
+  cmd = "mkdir -p " + extracted_package_dir_path + " && cd " +
     extracted_package_dir_path + " && rpm2cpio " + package_path +
     " | cpio -dium --quiet";
 
This page took 0.043645 seconds and 5 git commands to generate.