1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Ensure that when transferring a file to a directory the name of the file is the correct basename and not 'source'.

This commit is contained in:
Michael DeHaan 2013-03-25 22:19:35 -04:00
parent 7044e53e07
commit 3c5890f42b
3 changed files with 13 additions and 9 deletions

View file

@ -111,7 +111,7 @@ class ActionModule(object):
return ReturnData(conn=conn, result=dict(changed=True), diff=diff) return ReturnData(conn=conn, result=dict(changed=True), diff=diff)
# transfer the file to a remote tmp location # transfer the file to a remote tmp location
tmp_src = tmp + os.path.basename(source) tmp_src = tmp + 'source'
conn.put_file(source, tmp_src) conn.put_file(source, tmp_src)
if content is not None: if content is not None:
os.remove(tmp_content) os.remove(tmp_content)
@ -120,7 +120,7 @@ class ActionModule(object):
self.runner._low_level_exec_command(conn, "chmod a+r %s" % tmp_src, tmp) self.runner._low_level_exec_command(conn, "chmod a+r %s" % tmp_src, tmp)
# run the copy module # run the copy module
module_args = "%s src=%s" % (module_args, pipes.quote(tmp_src)) module_args = "%s src=%s original_basename=%s" % (module_args, pipes.quote(tmp_src), pipes.quote(os.path.basename(source)))
return self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject, complex_args=complex_args) return self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject, complex_args=complex_args)
else: else:

View file

@ -105,7 +105,7 @@ class ActionModule(object):
self.runner._low_level_exec_command(conn, "chmod a+r %s" % xfered, tmp) self.runner._low_level_exec_command(conn, "chmod a+r %s" % xfered, tmp)
# run the copy module # run the copy module
module_args = "%s src=%s dest=%s" % (module_args, pipes.quote(xfered), pipes.quote(dest)) module_args = "%s src=%s dest=%s original_basename=%s" % (module_args, pipes.quote(xfered), pipes.quote(dest), pipes.quote(os.path.basename(source)))
if self.runner.check: if self.runner.check:
return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before_header=dest, after_header=source, before=dest_contents, after=resultant)) return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before_header=dest, after_header=source, before=dest_contents, after=resultant))

View file

@ -82,11 +82,12 @@ def main():
module = AnsibleModule( module = AnsibleModule(
# not checking because of daisy chain to file module # not checking because of daisy chain to file module
argument_spec = dict( argument_spec = dict(
src = dict(required=False), src = dict(required=False),
content = dict(required=False), original_basename = dict(required=False), # used to handle 'dest is a directory' via template, a slight hack
dest = dict(required=True), content = dict(required=False),
backup = dict(default=False, type='bool'), dest = dict(required=True),
force = dict(default=True, aliases=['thirsty'], type='bool'), backup = dict(default=False, type='bool'),
force = dict(default=True, aliases=['thirsty'], type='bool'),
), ),
add_file_common_args=True, add_file_common_args=True,
) )
@ -95,6 +96,7 @@ def main():
dest = os.path.expanduser(module.params['dest']) dest = os.path.expanduser(module.params['dest'])
backup = module.params['backup'] backup = module.params['backup']
force = module.params['force'] force = module.params['force']
original_basename = module.params.get('original_basename',None)
if not os.path.exists(src): if not os.path.exists(src):
module.fail_json(msg="Source %s failed to transfer" % (src)) module.fail_json(msg="Source %s failed to transfer" % (src))
@ -109,6 +111,8 @@ def main():
module.exit_json(msg="file already exists", src=src, dest=dest, changed=False) module.exit_json(msg="file already exists", src=src, dest=dest, changed=False)
if (os.path.isdir(dest)): if (os.path.isdir(dest)):
basename = os.path.basename(src) basename = os.path.basename(src)
if original_basename:
basename = original_basename
dest = os.path.join(dest, basename) dest = os.path.join(dest, basename)
if os.access(dest, os.R_OK): if os.access(dest, os.R_OK):
md5sum_dest = module.md5(dest) md5sum_dest = module.md5(dest)
@ -128,7 +132,7 @@ def main():
if os.path.islink(dest): if os.path.islink(dest):
os.unlink(dest) os.unlink(dest)
open(dest, 'w').close() open(dest, 'w').close()
#TODO:pid + epoch should avoid most collisions, hostname/mac for those using nfs? # TODO:pid + epoch should avoid most collisions, hostname/mac for those using nfs?
# might be an issue with exceeding path length # might be an issue with exceeding path length
dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.time()) dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.time())
shutil.copyfile(src, dest_tmp) shutil.copyfile(src, dest_tmp)