windows: fsync a temporary lock file before renaming it

Summary:
This helps to avoid the following problem:
1. hg creates a temporary lock file, writes some stuff there
2. os writes this stuff into its buffer
3. hg closes the file, the metadata is written out (or journaled)
4. hg renames the file, which is again a metadata-only operation
5. the buffer is still not flushed
6. the OS crashes
7. upon reload, the os has a file with a correct name and a correct length,
but unexpected contents

Reviewed By: quark-zju

Differential Revision: D7889111

fbshipit-source-id: a0a152c9e7efef34847fa2d2ab9b94191bde43f4
This commit is contained in:
Kostia Balytskyi 2018-05-08 03:10:31 -07:00 committed by Facebook Github Bot
parent dd7b147493
commit 88fbc4e6ee

View File

@ -531,6 +531,7 @@ def makelock(info, pathname):
prefix='%s.%i.' % (basename, os.getpid()),
dir=dirname)
os.write(fd, info)
os.fsync(fd)
os.close(fd)
try:
os.rename(tname, pathname)