In fixing a system crash bug related to writing to a temporary file and then renaming/replacing an existing file to update it, you must call fsync/sync_data before the rename. My question is, does the call to fsync/sync_data make calling flush redundant? This is applicable to all languages fyi. In File in std::fs - Rust, there is no flush, so I'm leaning towards not needing to call flush. Can anyone more knowledgeable in OSes shed some light?
Example
let mut file = NamedTempFile::new_in(MY_DIR)?;
file.write_all(&json)?;
file.flush()?; // is this redundant now that sync_data is being called?
file.as_file_mut().sync_data()?;
file.persist(PATH)?;
From my python app
try:
tmp_file = SETTINGS_FILE.with_suffix('.json.tmp')
with open(tmp_file, 'w', encoding='utf-8') as outfile:
json.dump(settings, outfile, indent=2, escape_forward_slashes=False)
outfile.flush() # is this redundant because of fsync?
os.fsync(outfile.fileno()) # I made a post on Python forum asking for macos support
# assume the system crashes before the file is closed
os.replace(tmp_file, SETTINGS_FILE)