Blame SOURCES/0006-Use-temp-file-objects-for-temporary-storage-area.patch

e40f95
From 4f6611ed208b6029b4523ca7760a802e82b6a199 Mon Sep 17 00:00:00 2001
e40f95
From: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
e40f95
Date: Fri, 3 Apr 2020 20:06:48 +0530
e40f95
Subject: [PATCH] Use temp file objects for temporary storage area
e40f95
e40f95
happen to see yet another issue after we switch to stringIO/bytesIO:
e40f95
e40f95
$ targetcli ls
e40f95
'unicode' does not have the buffer interface
e40f95
e40f95
After which I felt like dealing with StringIO/BytesIO is like a
e40f95
wild goose chase and we are after all attempting to deal with python
e40f95
incompatible apis.
e40f95
e40f95
Technically speaking, the rtslib and configshell libraries are expecting a
e40f95
string IO stream, but then for python2 with ByteIO() we are passing a
e40f95
bytestream, and stringIO() is behaving a bit different in python2 than
e40f95
expected as explained in my previous patch.
e40f95
e40f95
Lets simply switch to temporary file in the most secure manner possible,
e40f95
opening files in string mode will assure the compatibility across the platforms.
e40f95
e40f95
At the end we wish to have a consistent behaviour across all python versions.
e40f95
e40f95
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
e40f95
---
e40f95
 daemon/targetclid | 26 +++++++++++++-------------
e40f95
 scripts/targetcli |  3 ++-
e40f95
 2 files changed, 15 insertions(+), 14 deletions(-)
e40f95
e40f95
diff --git a/daemon/targetclid b/daemon/targetclid
e40f95
index 5df6ca2..329cede 100755
e40f95
--- a/daemon/targetclid
e40f95
+++ b/daemon/targetclid
e40f95
@@ -26,19 +26,16 @@ from configshell_fb import ConfigShell
e40f95
 from os import getuid, getenv, unlink
e40f95
 from threading import Thread
e40f95
 
e40f95
+import os
e40f95
 import sys
e40f95
 import socket
e40f95
 import struct
e40f95
 import fcntl
e40f95
 import signal
e40f95
 import errno
e40f95
+import tempfile
e40f95
 
e40f95
 
e40f95
-if sys.version_info < (3, 0):
e40f95
-    from io import BytesIO as StringIO
e40f95
-else:
e40f95
-    from io import StringIO
e40f95
-
e40f95
 err = sys.stderr
e40f95
 
e40f95
 class TargetCLI:
e40f95
@@ -158,26 +155,29 @@ class TargetCLI:
e40f95
                 connection.close()
e40f95
                 still_listen = False
e40f95
             else:
e40f95
-                self.con._stdout = self.con._stderr = f = StringIO()
e40f95
+                self.con._stdout = self.con._stderr = f = tempfile.NamedTemporaryFile(mode='w', delete=False)
e40f95
                 try:
e40f95
                     # extract multiple commands delimited with '%'
e40f95
                     list_data = data.decode().split('%')
e40f95
                     for cmd in list_data:
e40f95
                         self.shell.run_cmdline(cmd)
e40f95
                 except Exception as e:
e40f95
-                    print(str(e).encode(), file=f) # push error to stream
e40f95
+                    print(str(e), file=f) # push error to stream
e40f95
 
e40f95
                 # Restore
e40f95
                 self.con._stdout = self.con_stdout_
e40f95
                 self.con._stderr = self.con_stderr_
e40f95
-                output = f.getvalue().encode()
e40f95
-                var = struct.pack('i', len(output))
e40f95
-                connection.sendall(var) # length of string
e40f95
-                if len(output):
e40f95
-                    connection.sendall(output) # actual string
e40f95
-
e40f95
                 f.close()
e40f95
 
e40f95
+                with open(f.name, 'r') as f:
e40f95
+                    output = f.read()
e40f95
+                    var = struct.pack('i', len(output))
e40f95
+                    connection.sendall(var) # length of string
e40f95
+                    if len(output):
e40f95
+                        connection.sendall(output.encode()) # actual string
e40f95
+
e40f95
+                os.unlink(f.name)
e40f95
+
e40f95
 
e40f95
 def usage():
e40f95
     print("Usage: %s [--version|--help]" % sys.argv[0], file=err)
e40f95
diff --git a/scripts/targetcli b/scripts/targetcli
e40f95
index 04e5aba..f11ece4 100755
e40f95
--- a/scripts/targetcli
e40f95
+++ b/scripts/targetcli
e40f95
@@ -154,8 +154,9 @@ def call_daemon(shell, req):
e40f95
     # get the actual data in chunks
e40f95
     while amount_received < amount_expected:
e40f95
         data = sock.recv(1024)
e40f95
+        data = data.decode()
e40f95
         amount_received += len(data)
e40f95
-        print(data.decode(), end ="")
e40f95
+        print(data, end ="")
e40f95
 
e40f95
     sock.send(b'-END@OF@DATA-')
e40f95
     sock.close()
e40f95
-- 
e40f95
2.21.0
e40f95