add comment and warning

This commit is contained in:
Nikos Atlas 2022-05-20 12:05:02 +03:00 committed by Brian May
parent 1def53e085
commit 93200f7095

View File

@ -34,6 +34,7 @@ def rewrite_etc_hosts(hostmap, port):
try: try:
os.link(HOSTSFILE, BAKFILE) os.link(HOSTSFILE, BAKFILE)
except OSError: except OSError:
# file is locked - performing non-atomic copy
shutil.copyfile(HOSTSFILE, BAKFILE) shutil.copyfile(HOSTSFILE, BAKFILE)
tmpname = "%s.%d.tmp" % (HOSTSFILE, port) tmpname = "%s.%d.tmp" % (HOSTSFILE, port)
f = open(tmpname, 'w') f = open(tmpname, 'w')
@ -54,6 +55,9 @@ def rewrite_etc_hosts(hostmap, port):
try: try:
os.rename(tmpname, HOSTSFILE) os.rename(tmpname, HOSTSFILE)
except OSError: except OSError:
# file is locked - performing non-atomic copy
log('Warning: Using a non-atomic way to overwrite %s that can corrupt the file if '
'multiple processes write to it simultaneously.' % HOSTSFILE)
shutil.move(tmpname, HOSTSFILE) shutil.move(tmpname, HOSTSFILE)