Restore

O Operator
Restore

Restore file to captured state

Every captured change carries its SHA-256-addressed content in BLOB_STORE. Restoring writes that content back to the original path — current file gets backed up to <path>.drift_restore_backup_<epoch> before overwrite.

What will change
Preview -- nothing has been written yet.
Target file/var/www/vhosts/3dshawn.com/site1/MODS/CompileCheck.pm
SiteDriftSense self-monitor on local
Kindlocal
Captured at2026-07-12 00:48:41
Captured SHA6616dcca750f879fe56c29d31263f409a4d28df59d3e7d115ea04e4cdb68472b
Current state on disk
Live check just now. If SHAs match, the restore is a no-op.
File presentyes
Size4609 bytes
Current SHAf24af278b0c0
Same as captured?no -- restore will change it
What restore will change — live diff
Left column shows current on-disk content; right shows what restore will write. +1 additions, -2 deletions, 131 unchanged context lines.
11package MODS::CompileCheck;
22#======================================================================
33# DriftSense -- syntax check for captured blob content.
44#
55# Test-compiles content using system-native tools (no CPAN, no libs):
66# Perl (.pl .pm .cgi) -> perl -c
77# JavaScript (.js .mjs) -> node --check (skipped gracefully if no node)
88# Shell (.sh .bash) -> bash -n
99# Python (.py) -> python3 -m py_compile (skipped if no python)
1010#
1111# All checks run under a short timeout with content written to a
1212# mode-0600 tempfile that's unlinked immediately after.
1313#
1414# my $r = MODS::CompileCheck::check($content, $file_name);
1515# # $r = { status => 'ok' | 'error' | 'unchecked',
1616# # language => 'perl',
1717# # message => 'perl -c output on error' }
1818#======================================================================
1919use strict;
2020use warnings;
2121use IPC::Open3;
2222use Symbol 'gensym';
2323use Fcntl ();
2424
2525my $TIMEOUT_S = 8;
2626
2727sub check {
2828 my ($content, $file_name) = @_;
2929 $file_name //= '';
3030 return { status => 'unchecked', language => 'unknown', message => 'no content' }
3131 unless defined $content && length $content;
3232
3333 my $ext = lc(($file_name =~ /\.([a-zA-Z0-9]+)$/)[0] // '');
3434 my $lang;
3535 my @argv;
3636 if ($ext =~ /^(pl|pm|cgi|t)$/) { $lang = 'perl'; @argv = (_which('perl'), '-c'); }
3737 elsif ($ext =~ /^(js|mjs)$/) { $lang = 'javascript'; @argv = (_which('node'), '--check'); }
3838 elsif ($ext =~ /^(sh|bash)$/) { $lang = 'shell'; @argv = (_which('bash'), '-n'); }
3939 elsif ($ext =~ /^py$/) { $lang = 'python'; @argv = (_which('python3'), '-m', 'py_compile'); }
4040 elsif ($ext =~ /^(json)$/) { $lang = 'json'; @argv = (_which('python3'), '-c', 'import sys,json; json.load(sys.stdin)'); }
4141 else {
4242 return { status => 'unchecked', language => 'unsupported', message => "no compiler wired for .$ext" };
4343 }
4444
4545 return { status => 'unchecked', language => $lang, message => "$argv[0] not installed" }
4646 unless $argv[0] && -x $argv[0];
4747
4848 # Write content to a temp file and pass its path to the compiler.
4949 # Except for JSON where we pipe via stdin.
5050 my $tmp;
5151 if ($lang ne 'json') {
5252 $tmp = _write_tmp($content, $ext);
5353 return { status => 'unchecked', language => $lang, message => "tempfile failed" }
5454 unless $tmp;
5555 push @argv, $tmp;
5656 }
5757
5858 my ($wtr, $rdr, $err_fh);
5959 $err_fh = gensym;
6060 my $pid = eval { open3($wtr, $rdr, $err_fh, @argv) };
6161 if ($@ || !$pid) {
6262 unlink $tmp if $tmp;
6363 return { status => 'unchecked', language => $lang, message => "spawn failed: $@" };
6464 }
6565 if ($lang eq 'json') {
6666 binmode $wtr; print {$wtr} $content;
6767 }
6868 close $wtr;
6969
7070 # Read with timeout via alarm
7171 my $out = '';
7272 my $err = '';
7373 eval {
7474 local $SIG{ALRM} = sub { die "timeout\n" };
7575 alarm($TIMEOUT_S);
7676 local $/;
7777 $out = <$rdr> // '';
7878 $err = <$err_fh> // '';
7979 alarm(0);
8080 };
8181 my $timed_out = $@ =~ /timeout/ ? 1 : 0;
8282 close $rdr; close $err_fh;
8383 kill 'TERM', $pid if $timed_out;
8484 waitpid $pid, 0;
8585 my $rc = $? >> 8;
8686 unlink $tmp if $tmp;
8787
8888 if ($timed_out) {
8989 return { status => 'unchecked', language => $lang, message => "timed out after ${TIMEOUT_S}s" };
9090 }
9191
9292 my $status = $rc == 0 ? 'ok' : 'error';
9393 my $msg = _pretty_message($out, $err, $status, $lang);
9494 return { status => $status, language => $lang, message => $msg };
9595}
9696
9797#---------------------------------------------------------------------
9898sub _which {
9999 my ($bin) = @_;
100100 return "/usr/bin/$bin" if -x "/usr/bin/$bin";
101101 return "/usr/local/bin/$bin" if -x "/usr/local/bin/$bin";
102102 return "/bin/$bin" if -x "/bin/$bin";
103103 return '';
104104}
105105
106106sub _write_tmp {
107107 my ($content, $ext) = @_;
108108 my $t = time();
109 my $rand = int(rand(1_000_000));
110 my $path = '/tmp/.ds_check_' . $t . '_' . $$ . '_' . $rand . '.' . $ext;
109 my $path = "/tmp/.ds_check_${t}_$$_" . int(rand(1_000_000)) . ".$ext";
111110 sysopen(my $fh, $path, Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_EXCL(), 0600)
112111 or return undef;
113112 binmode $fh;
114113 print $fh $content;
115114 close $fh;
116115 return $path;
117116}
118117
119118sub _pretty_message {
120119 my ($out, $err, $status, $lang) = @_;
121120 my $m = length($err) ? $err : $out;
122121 $m //= '';
123122 # Strip our temp path noise
124123 $m =~ s{/tmp/\.ds_check_\d+_\d+_\d+\.\w+}{<tempfile>}g;
125124 # Cap length
126125 $m = substr($m, 0, 2000) if length($m) > 2000;
127126 if ($status eq 'ok' && !length $m) {
128127 return "$lang compiles cleanly";
129128 }
130129 return $m;
131130}
132131
1331321;
Confirm restore
Backs current content to <target>.drift_restore_backup_<epoch>, writes the captured content, verifies SHA post-write.
Cancel Logged to RESTORE_LOG regardless of outcome.