fix: atomic writes for posts and users.toml
Write to a .tmp file and then File.rename into place, so a concurrent read or a mid-write crash never sees a torn file.
This commit is contained in:
@@ -37,7 +37,9 @@ module Volumen
|
||||
def save(post)
|
||||
target = post.path || default_path_for(post)
|
||||
FileUtils.mkdir_p(File.dirname(target))
|
||||
File.write(target, post.to_file)
|
||||
tmp = "#{target}.tmp"
|
||||
File.write(tmp, post.to_file)
|
||||
File.rename(tmp, target)
|
||||
post.path = target
|
||||
@all = nil
|
||||
post
|
||||
|
||||
@@ -118,7 +118,9 @@ module Volumen
|
||||
|
||||
def persist(users)
|
||||
FileUtils.mkdir_p(File.dirname(@path))
|
||||
File.write(@path, TomlRB.dump("users" => users.map { |user| user_hash(user) }))
|
||||
tmp = "#{@path}.tmp"
|
||||
File.write(tmp, TomlRB.dump("users" => users.map { |user| user_hash(user) }))
|
||||
File.rename(tmp, @path)
|
||||
end
|
||||
|
||||
def user_hash(user)
|
||||
|
||||
Reference in New Issue
Block a user