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:
2026-06-25 22:06:49 +02:00
parent ad00872895
commit afd8ab4bfb
2 changed files with 6 additions and 2 deletions
+3 -1
View File
@@ -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
+3 -1
View File
@@ -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)