Skip to content

Commit

Permalink
Verify statefile integrity in cache (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp authored Dec 4, 2024
1 parent 2c37d19 commit 6b6aa01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
9 changes: 2 additions & 7 deletions src/Actions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ using Base64
using FromFile

@from "./MoreAnalysis.jl" import bound_variable_connections_graph
@from "./Export.jl" import try_get_exact_pluto_version, try_fromcache, try_tocache
@from "./Export.jl" import try_get_exact_pluto_version,
try_fromcache, try_tocache, write_statefile
@from "./Types.jl" import NotebookSession, RunningNotebook, FinishedNotebook, RunResult
@from "./Configuration.jl" import PlutoDeploySettings, is_glob_match
@from "./FileHelpers.jl" import find_notebook_files_recursive
Expand Down Expand Up @@ -271,12 +272,6 @@ function generate_static_export(
@debug "Written to $(export_html_path)"
end

function write_statefile(path, state)
data = Pluto.pack(state)
write(path, data)
@assert read(path) == data
end

tryrm(x) = isfile(x) && rm(x)

function remove_static_export(path; settings, output_dir)
Expand Down
17 changes: 14 additions & 3 deletions src/Export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import Pluto: Pluto, ServerSession
using HTTP
import Pkg

export write_statefile

function write_statefile(path, state; verify::Bool=true)
data = Pluto.pack(state)
write(path, data)
if verify
input_data = read(path)
@assert input_data == data
input_state = Pluto.unpack(input_data)
@assert sort(collect(keys(state))) == sort(collect(keys(input_state)))
end
end


## CACHE

Expand Down Expand Up @@ -32,9 +45,7 @@ try_fromcache(cache_dir::Nothing, current_hash) = nothing
function try_tocache(cache_dir::String, current_hash::String, state)
mkpath(cache_dir)
try
open(cache_filename(cache_dir, current_hash), "w") do io
Pluto.pack(io, state)
end
write_statefile(cache_filename(cache_dir, current_hash), state)
catch e
@warn "Failed to write to cache file" current_hash exception =
(e, catch_backtrace())
Expand Down

0 comments on commit 6b6aa01

Please sign in to comment.