From 8646df0a1fce1bd6c7aeaea4208901066f8af98b Mon Sep 17 00:00:00 2001
From: Patrik Lundin <patrik.lundin.swe@gmail.com>
Date: Wed, 10 Jul 2013 18:19:01 +0200
Subject: [PATCH] openbsd_pkg: Handle another pkg_add gotcha

* Add '-m' to pkg_add incovation to get access to the "packagename-1.0: ok"
  message.
* Watch for that message if we are about to fail because of stderr in
  package_present().
---
 packaging/openbsd_pkg | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/packaging/openbsd_pkg b/packaging/openbsd_pkg
index 16892283a70..b16a0c0648f 100644
--- a/packaging/openbsd_pkg
+++ b/packaging/openbsd_pkg
@@ -104,9 +104,9 @@ def get_package_state(name, specific_version):
 # Function used to make sure a package is present.
 def package_present(name, installed_state, specific_version, module):
     if module.check_mode:
-        install_cmd = 'pkg_add -In'
-    else: 
-        install_cmd = 'pkg_add -I'
+        install_cmd = 'pkg_add -Imn'
+    else:
+        install_cmd = 'pkg_add -Im'
 
     if installed_state is False:
 
@@ -124,10 +124,21 @@ def package_present(name, installed_state, specific_version, module):
             if rc: 
                 changed=False
         else:
-            # Depend on stderr instead and fake the return code.
+            # Depend on stderr instead.
             if stderr:
-                rc = 1
-                changed=False
+                # There is a corner case where having an empty directory in
+                # installpath prior to the right location will result in a
+                # "file:/local/package/directory/ is empty" message on stderr
+                # while still installing the package, so we need to look for
+                # for a message like "packagename-1.0: ok" just in case.
+                match = re.search("\W%s-[^:]+: ok\W" % name, stdout)
+                if match:
+                    # It turns out we were able to install the package.
+                    pass
+                else:
+                    # We really did fail, fake the return code.
+                    rc = 1
+                    changed=False
 
         if rc == 0:
             if module.check_mode: