<?php
declare(strict_types=1);
$source=__DIR__.'/source';$target=__DIR__.'/appledouble-quarantine';if(is_dir($target))throw new RuntimeException('No quarantine replay');mkdir($target,0700);$records=[];
$files=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source,FilesystemIterator::SKIP_DOTS));
foreach($files as $file){if(!$file->isFile()||strpos($file->getFilename(),'._')!==0)continue;$path=$file->getPathname();$f=fopen($path,'rb');$head=fread($f,4);fclose($f);if($head!==hex2bin('00051607'))throw new RuntimeException('Not generated AppleDouble');$rel=substr($path,strlen($source)+1);$dest=$target.'/'.$rel;if(!is_dir(dirname($dest)))mkdir(dirname($dest),0700,true);$records[]=['path'=>$rel,'sha256'=>hash_file('sha256',$path),'bytes'=>filesize($path)];if(!rename($path,$dest))throw new RuntimeException('Exact recoverable move failed');chmod($dest,0600);}
file_put_contents(__DIR__.'/private/appledouble-quarantine.json',json_encode($records,JSON_PRETTY_PRINT));chmod(__DIR__.'/private/appledouble-quarantine.json',0600);echo json_encode(['recoverably_quarantined'=>count($records),'business_files_changed'=>0,'DB'=>0,'HTTP'=>0]),"\n";
