Skip to content

Commit bcb8a06

Browse files
committed
bootstrap: write texts to a .tmp file first for atomicity
If you are using a hard-linked file as your config.toml, this change will affect the way other instances of the file is modified. The original version would modify all other instances whereas the new version will leave others unchanged, reducing the ref count by one. Signed-off-by: NODA, Kai <[email protected]>
1 parent fc49152 commit bcb8a06

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/bootstrap/bootstrap.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ def default_build_triple():
303303
return "{}-{}".format(cputype, ostype)
304304

305305

306+
@contextlib.contextmanager
307+
def output(filepath):
308+
tmp = filepath + '.tmp'
309+
with open(tmp, 'w') as f:
310+
yield f
311+
os.rename(tmp, filepath)
312+
313+
306314
class RustBuild(object):
307315
"""Provide all the methods required to build Rust"""
308316
def __init__(self):
@@ -346,7 +354,7 @@ def download_stage0(self):
346354
self._download_stage0_helper(filename, "rustc")
347355
self.fix_executable("{}/bin/rustc".format(self.bin_root()))
348356
self.fix_executable("{}/bin/rustdoc".format(self.bin_root()))
349-
with open(self.rustc_stamp(), 'w') as rust_stamp:
357+
with output(self.rustc_stamp()) as rust_stamp:
350358
rust_stamp.write(self.date)
351359

352360
# This is required so that we don't mix incompatible MinGW
@@ -363,7 +371,7 @@ def download_stage0(self):
363371
filename = "cargo-{}-{}.tar.gz".format(cargo_channel, self.build)
364372
self._download_stage0_helper(filename, "cargo")
365373
self.fix_executable("{}/bin/cargo".format(self.bin_root()))
366-
with open(self.cargo_stamp(), 'w') as cargo_stamp:
374+
with output(self.cargo_stamp()) as cargo_stamp:
367375
cargo_stamp.write(self.date)
368376

369377
def _download_stage0_helper(self, filename, pattern):
@@ -776,7 +784,7 @@ def bootstrap(help_triggered):
776784
if build.use_vendored_sources:
777785
if not os.path.exists('.cargo'):
778786
os.makedirs('.cargo')
779-
with open('.cargo/config', 'w') as cargo_config:
787+
with output('.cargo/config') as cargo_config:
780788
cargo_config.write("""
781789
[source.crates-io]
782790
replace-with = 'vendored-sources'

src/bootstrap/configure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def configure_section(lines, config):
432432
# order that we read it in.
433433
p("")
434434
p("writing `config.toml` in current directory")
435-
with open('config.toml', 'w') as f:
435+
with bootstrap.output('config.toml') as f:
436436
for section in section_order:
437437
if section == 'target':
438438
for target in targets:
@@ -442,7 +442,7 @@ def configure_section(lines, config):
442442
for line in sections[section]:
443443
f.write(line + "\n")
444444

445-
with open('Makefile', 'w') as f:
445+
with bootstrap.output('Makefile') as f:
446446
contents = os.path.join(rust_dir, 'src', 'bootstrap', 'mk', 'Makefile.in')
447447
contents = open(contents).read()
448448
contents = contents.replace("$(CFG_SRC_DIR)", rust_dir + '/')

0 commit comments

Comments
 (0)