Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cloudinit/atomic_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@


def write_file(filename, content, mode=_DEF_PERMS,
omode="wb", copy_mode=False):
omode="wb", preserve_mode=False):
# open filename in mode 'omode', write content, set permissions to 'mode'

if copy_mode:
if preserve_mode:
try:
file_stat = os.stat(filename)
mode = stat.S_IMODE(file_stat.st_mode)
Expand Down
2 changes: 1 addition & 1 deletion cloudinit/ssh_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def update_ssh_config(updates, fname=DEF_SSHD_CFG):
util.write_file(
fname, "\n".join(
[str(line) for line in lines]
) + "\n", copy_mode=True)
) + "\n", preserve_mode=True)
return len(changed) != 0


Expand Down
6 changes: 4 additions & 2 deletions cloudinit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ def chmod(path, mode):
os.chmod(path, real_mode)


def write_file(filename, content, mode=0o644, omode="wb", copy_mode=False):
def write_file(filename, content, mode=0o644, omode="wb", preserve_mode=False):
"""
Writes a file with the given content and sets the file mode as specified.
Restores the SELinux context if possible.
Expand All @@ -1809,9 +1809,11 @@ def write_file(filename, content, mode=0o644, omode="wb", copy_mode=False):
@param content: The content to write to the file.
@param mode: The filesystem mode to set on the file.
@param omode: The open mode used when opening the file (w, wb, a, etc.)
@param preserve_mode: If True and `filename` exists, preserve `filename`s
current mode instead of applying `mode`.
"""

if copy_mode:
if preserve_mode:
try:
file_stat = os.stat(filename)
mode = stat.S_IMODE(file_stat.st_mode)
Expand Down
12 changes: 6 additions & 6 deletions tests/unittests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,29 @@ def test_explicit_mode(self):
file_stat = os.stat(path)
self.assertEqual(0o666, stat.S_IMODE(file_stat.st_mode))

def test_copy_mode_no_existing(self):
"""Verify that file is created with mode 0o644 if copy_mode
def test_preserve_mode_no_existing(self):
"""Verify that file is created with mode 0o644 if preserve_mode
is true and there is no prior existing file."""
path = os.path.join(self.tmp, "NewFile.txt")
contents = "Hey there"

util.write_file(path, contents, copy_mode=True)
util.write_file(path, contents, preserve_mode=True)

self.assertTrue(os.path.exists(path))
self.assertTrue(os.path.isfile(path))
file_stat = os.stat(path)
self.assertEqual(0o644, stat.S_IMODE(file_stat.st_mode))

def test_copy_mode_with_existing(self):
def test_preserve_mode_with_existing(self):
"""Verify that file is created using mode of existing file
if copy_mode is true."""
if preserve_mode is true."""
path = os.path.join(self.tmp, "NewFile.txt")
contents = "Hey there"

open(path, 'w').close()
os.chmod(path, 0o666)

util.write_file(path, contents, copy_mode=True)
util.write_file(path, contents, preserve_mode=True)

self.assertTrue(os.path.exists(path))
self.assertTrue(os.path.isfile(path))
Expand Down