receive-file: add --output-file to override local output filename

This commit is contained in:
Brian Warner 2015-03-16 00:18:53 -07:00
parent 5fd85fd884
commit 84aa7ff248
2 changed files with 16 additions and 10 deletions

View File

@ -44,15 +44,18 @@ def receive_file(so):
print("Receiving %d bytes for '%s' (%s).." % (filesize, filename,
transit_receiver.describe()))
# only write to the current directory, and never overwrite anything
here = os.path.abspath(os.getcwd())
target = os.path.abspath(os.path.join(here, filename))
if os.path.dirname(target) != here:
print("Error: suggested filename (%s) would be outside current directory"
% (filename,))
record_pipe.send_record("bad filename\n")
record_pipe.close()
return 1
target = so["output-file"]
if not target:
# allow the sender to specify the filename, but only write to the
# current directory, and never overwrite anything
here = os.path.abspath(os.getcwd())
target = os.path.abspath(os.path.join(here, filename))
if os.path.dirname(target) != here:
print("Error: suggested filename (%s) would be outside current directory"
% (filename,))
record_pipe.send_record("bad filename\n")
record_pipe.close()
return 1
if os.path.exists(target):
print("Error: refusing to overwrite existing file %s" % (filename,))
record_pipe.send_record("file already exists\n")
@ -79,7 +82,7 @@ def receive_file(so):
os.rename(tmp, target)
print("Received file written to %s" % filename)
print("Received file written to %s" % target)
record_pipe.send_record("ok\n")
record_pipe.close()
return 0

View File

@ -17,6 +17,9 @@ class SendFileOptions(usage.Options):
synopsis = "FILENAME"
class ReceiveFileOptions(usage.Options):
optParameters = [
["output-file", "o", None, "File to create"],
]
def parseArgs(self, code=None):
self["code"] = code
synopsis = "[CODE]"