Skip to content

Commit fb29c56

Browse files
committedSep 23, 2015
Don't rely on clobbering renames when saving files; it makes Windows
sad.
1 parent 66ff620 commit fb29c56

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed
 

‎src/lua/fileio.lua

+11-4
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,19 @@ function SaveToStream(filename, object)
158158
r, e = fp:close()
159159
end
160160

161-
-- Once done, rename the new file over the top of the old one.
162-
-- Force the new one to be removed in case the rename fails.
161+
-- Once done, do a complicated series of renames so that we
162+
-- don't remove the old file until we're sure the new one has
163+
-- been written correctly. Note that accurséd Windows doesn't
164+
-- support clobbering renames...
163165

164166
if r then
165-
r, e = os.rename(filename..".new", filename)
166-
os.remove(filename..".new")
167+
r, e = os.rename(filename, filename..".old")
168+
if not e then
169+
r, e = os.rename(filename..".new", filename)
170+
end
171+
if not e then
172+
os.remove(filename..".old")
173+
end
167174
end
168175

169176
return r, e

0 commit comments

Comments
 (0)
Please sign in to comment.