defmodule Nugitea.Accounts.JsonFile do@moduledoc """A JSON object in the state dir, for the small secret stores(`Nugitea.Accounts.Passwords`, `Nugitea.Accounts.Sessions`): read whole,updated under a lock held across every connected node, written to a tempfile and renamed into place, readable by its owner only."""@doc "The file's object, or `%{}` when it's missing or unreadable."def load(state_dir, name) dowith {:ok, json} <- File.read(Path.join(state_dir, name)),{:ok, %{} = map} <- Jason.decode(json) domapelse_ -> %{}endend@doc """Replaces the object with `fun.(object)`, or, when `fun` returns`{object, value}`, with `object`, returning `value`. Returns `:ok` or thevalue."""def update(state_dir, name, fun) dopath = Path.join(state_dir, name):global.trans({{__MODULE__, path}, self()}, fn ->{map, result} =case fun.(load(state_dir, name)) do{%{} = map, value} -> {map, value}%{} = map -> {map, :ok}endFile.mkdir_p!(state_dir)tmp = path <> ".tmp"File.write!(tmp, Jason.encode!(map, pretty: true))File.chmod!(tmp, 0o600)File.rename!(tmp, path)resultend)endend