Blame SOURCES/0047-python-Split-semanage-import-into-two-transactions.patch

4b0001
From 09c944561c76146b1fc11e99e95b6a674366cddf Mon Sep 17 00:00:00 2001
4b0001
From: Vit Mojzis <vmojzis@redhat.com>
4b0001
Date: Mon, 30 May 2022 14:20:21 +0200
4b0001
Subject: [PATCH] python: Split "semanage import" into two transactions
4b0001
4b0001
First transaction applies all deletion operations, so that there are no
4b0001
collisions when applying the rest of the changes.
4b0001
4b0001
Fixes:
4b0001
  # semanage port -a -t http_cache_port_t -r s0 -p tcp 3024
4b0001
  # semanage export | semanage import
4b0001
  ValueError: Port tcp/3024 already defined
4b0001
4b0001
Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
4b0001
---
4b0001
 python/semanage/semanage | 21 +++++++++++++++++++--
4b0001
 1 file changed, 19 insertions(+), 2 deletions(-)
4b0001
4b0001
diff --git a/python/semanage/semanage b/python/semanage/semanage
4b0001
index ebb93ea5..b8842d28 100644
4b0001
--- a/python/semanage/semanage
4b0001
+++ b/python/semanage/semanage
4b0001
@@ -841,10 +841,29 @@ def handleImport(args):
4b0001
     trans = seobject.semanageRecords(args)
4b0001
     trans.start()
4b0001
 
4b0001
+    deleteCommands = []
4b0001
+    commands = []
4b0001
+    # separate commands for deletion from the rest so they can be
4b0001
+    # applied in a separate transaction
4b0001
     for l in sys.stdin.readlines():
4b0001
         if len(l.strip()) == 0:
4b0001
             continue
4b0001
+        if "-d" in l or "-D" in l:
4b0001
+            deleteCommands.append(l)
4b0001
+        else:
4b0001
+            commands.append(l)
4b0001
+
4b0001
+    if deleteCommands:
4b0001
+        importHelper(deleteCommands)
4b0001
+        trans.finish()
4b0001
+        trans.start()
4b0001
+
4b0001
+    importHelper(commands)
4b0001
+    trans.finish()
4b0001
 
4b0001
+
4b0001
+def importHelper(commands):
4b0001
+    for l in commands:
4b0001
         try:
4b0001
             commandParser = createCommandParser()
4b0001
             args = commandParser.parse_args(mkargv(l))
4b0001
@@ -858,8 +877,6 @@ def handleImport(args):
4b0001
         except KeyboardInterrupt:
4b0001
             sys.exit(0)
4b0001
 
4b0001
-    trans.finish()
4b0001
-
4b0001
 
4b0001
 def setupImportParser(subparsers):
4b0001
     importParser = subparsers.add_parser('import', help=_('Import local customizations'))
4b0001
-- 
4b0001
2.35.3
4b0001