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

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