UploadData works incorrectly if both filename and mode
are specified. This patch fixes this issue.

If filename is specified, and the file does not exist,
the file is then created.

Signed-off-by: Bhimanavajjula Aditya <[email protected]>
---
 qa/qa_utils.py | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/qa/qa_utils.py b/qa/qa_utils.py
index 3dfe03f..80de54d 100644
--- a/qa/qa_utils.py
+++ b/qa/qa_utils.py
@@ -472,24 +472,27 @@ def UploadData(node, data, mode=0600, filename=None):
   anymore.
 
   """
-  if filename:
-    tmp = "tmp=%s" % utils.ShellQuote(filename)
-  else:
-    tmp = ('tmp=$(mktemp --tmpdir gnt.XXXXXX) && '
-           'chmod %o "${tmp}"') % mode
-  cmd = ("%s && "
-         "[[ -f \"${tmp}\" ]] && "
-         "cat > \"${tmp}\" && "
-         "echo \"${tmp}\"") % tmp
-
-  p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False,
-                       stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+  if not filename:
+    # pylint: disable=E0602
+    r = ''.join(random.choice(string.ascii_letters) for i in range(6))
+    filename = "/tmp/gnt.%s" % r
+
+  quoted_filename = utils.ShellQuote(filename)
+  directory = utils.ShellQuote(os.path.dirname(filename))
+
+  cmd = " && ".join([
+    "mkdir -p %s" % directory,
+    "cat > %s" % quoted_filename,
+    "chmod %o %s" % (mode, quoted_filename)])
+
+  p = subprocess.Popen(GetSSHCommand(node, cmd),
+                       shell=False,
+                       stdin=subprocess.PIPE)
   p.stdin.write(data)
   p.stdin.close()
   AssertEqual(p.wait(), 0)
 
-  # Return temporary filename
-  return _GetCommandStdout(p).strip()
+  return filename
 
 
 def BackupFile(node, path):
-- 
2.6.0.rc2.230.g3dd15c0

Reply via email to