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)
|
def save(post)
|
||||||
target = post.path || default_path_for(post)
|
target = post.path || default_path_for(post)
|
||||||
FileUtils.mkdir_p(File.dirname(target))
|
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
|
post.path = target
|
||||||
@all = nil
|
@all = nil
|
||||||
post
|
post
|
||||||
|
|||||||
@@ -118,7 +118,9 @@ module Volumen
|
|||||||
|
|
||||||
def persist(users)
|
def persist(users)
|
||||||
FileUtils.mkdir_p(File.dirname(@path))
|
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
|
end
|
||||||
|
|
||||||
def user_hash(user)
|
def user_hash(user)
|
||||||
|
|||||||
Reference in New Issue
Block a user