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 | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)

diff --git a/qa/qa_utils.py b/qa/qa_utils.py
index 3dfe03f..f2b2c2e 100644
--- a/qa/qa_utils.py
+++ b/qa/qa_utils.py
@@ -473,23 +473,32 @@ def UploadData(node, data, mode=0600, filename=None):
 
   """
   if filename:
-    tmp = "tmp=%s" % utils.ShellQuote(filename)
+    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)])
   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)
+    cmd = " && ".join([
+      'tmp=$(mktemp --tmpdir gnt.XXXXXX)',
+      'chmod %o "${tmp}"' % mode,
+      'cat > "${tmp}"',
+      'echo "${tmp}"'])
+
+  p = subprocess.Popen(GetSSHCommand(node, cmd),
+                       shell=False,
+                       stdin=subprocess.PIPE,
+                       stdout=subprocess.PIPE)
   p.stdin.write(data)
   p.stdin.close()
   AssertEqual(p.wait(), 0)
 
-  # Return temporary filename
-  return _GetCommandStdout(p).strip()
+  if filename:
+    return filename
+  else:
+    return _GetCommandStdout(p).strip()
 
 
 def BackupFile(node, path):
-- 
2.1.4

Reply via email to