@@ -11,6 +11,10 @@ import {
1111} from "./named-backup-export.js" ;
1212import { MODEL_FAMILIES , type ModelFamily } from "./prompts/codex.js" ;
1313import { AnyAccountStorageSchema , getValidationErrors } from "./schemas.js" ;
14+ import {
15+ describeAccountSnapshot ,
16+ statSnapshot ,
17+ } from "./storage/account-snapshot.js" ;
1418import {
1519 type BackupMetadataSection ,
1620 type BackupSnapshotMetadata ,
@@ -115,17 +119,6 @@ type AccountStorageWithMetadata = AccountStorageV3 & {
115119 restoreReason ?: RestoreReason ;
116120} ;
117121
118- type BackupSnapshotKind =
119- | "accounts-primary"
120- | "accounts-wal"
121- | "accounts-backup"
122- | "accounts-backup-history"
123- | "accounts-discovered-backup"
124- | "flagged-primary"
125- | "flagged-backup"
126- | "flagged-backup-history"
127- | "flagged-discovered-backup" ;
128-
129122export type BackupMetadata = {
130123 accounts : BackupMetadataSection ;
131124 flaggedAccounts : BackupMetadataSection ;
@@ -511,70 +504,6 @@ function isCacheLikeBackupArtifactName(entryName: string): boolean {
511504 return entryName . toLowerCase ( ) . includes ( ".cache" ) ;
512505}
513506
514- async function statSnapshot ( path : string ) : Promise < {
515- exists : boolean ;
516- bytes ?: number ;
517- mtimeMs ?: number ;
518- } > {
519- try {
520- const stats = await fs . stat ( path ) ;
521- return { exists : true , bytes : stats . size , mtimeMs : stats . mtimeMs } ;
522- } catch ( error ) {
523- const code = ( error as NodeJS . ErrnoException ) . code ;
524- if ( code !== "ENOENT" ) {
525- log . warn ( "Failed to stat backup candidate" , {
526- path,
527- error : String ( error ) ,
528- } ) ;
529- }
530- return { exists : false } ;
531- }
532- }
533-
534- async function describeAccountSnapshot (
535- path : string ,
536- kind : BackupSnapshotKind ,
537- index ?: number ,
538- ) : Promise < BackupSnapshotMetadata > {
539- const stats = await statSnapshot ( path ) ;
540- if ( ! stats . exists ) {
541- return { kind, path, index, exists : false , valid : false } ;
542- }
543- try {
544- const { normalized, schemaErrors, storedVersion } =
545- await loadAccountsFromPath ( path ) ;
546- return {
547- kind,
548- path,
549- index,
550- exists : true ,
551- valid : ! ! normalized ,
552- bytes : stats . bytes ,
553- mtimeMs : stats . mtimeMs ,
554- version : typeof storedVersion === "number" ? storedVersion : undefined ,
555- accountCount : normalized ?. accounts . length ,
556- schemaErrors : schemaErrors . length > 0 ? schemaErrors : undefined ,
557- } ;
558- } catch ( error ) {
559- const code = ( error as NodeJS . ErrnoException ) . code ;
560- if ( code !== "ENOENT" ) {
561- log . warn ( "Failed to inspect account snapshot" , {
562- path,
563- error : String ( error ) ,
564- } ) ;
565- }
566- return {
567- kind,
568- path,
569- index,
570- exists : true ,
571- valid : false ,
572- bytes : stats . bytes ,
573- mtimeMs : stats . mtimeMs ,
574- } ;
575- }
576- }
577-
578507async function loadFlaggedAccountsFromPath (
579508 path : string ,
580509) : Promise < FlaggedAccountStorageV1 > {
@@ -1241,10 +1170,24 @@ export async function getBackupMetadata(): Promise<BackupMetadata> {
12411170 flaggedPath,
12421171 getAccountsWalPath,
12431172 getAccountsBackupRecoveryCandidatesWithDiscovery,
1244- describeAccountSnapshot,
1173+ describeAccountSnapshot : ( path , kind , index ) =>
1174+ describeAccountSnapshot ( path , kind , {
1175+ index,
1176+ statSnapshot : ( targetPath ) =>
1177+ statSnapshot ( targetPath , {
1178+ stat : fs . stat ,
1179+ logWarn : ( message , meta ) => log . warn ( message , meta ) ,
1180+ } ) ,
1181+ loadAccountsFromPath,
1182+ logWarn : ( message , meta ) => log . warn ( message , meta ) ,
1183+ } ) ,
12451184 describeAccountsWalSnapshot : ( path ) =>
12461185 describeAccountsWalSnapshot ( path , {
1247- statSnapshot,
1186+ statSnapshot : ( targetPath ) =>
1187+ statSnapshot ( targetPath , {
1188+ stat : fs . stat ,
1189+ logWarn : ( message , meta ) => log . warn ( message , meta ) ,
1190+ } ) ,
12481191 readFile : fs . readFile ,
12491192 isRecord,
12501193 computeSha256,
@@ -1253,7 +1196,11 @@ export async function getBackupMetadata(): Promise<BackupMetadata> {
12531196 describeFlaggedSnapshot : ( path , kind , index ) =>
12541197 describeFlaggedSnapshot ( path , kind , {
12551198 index,
1256- statSnapshot,
1199+ statSnapshot : ( targetPath ) =>
1200+ statSnapshot ( targetPath , {
1201+ stat : fs . stat ,
1202+ logWarn : ( message , meta ) => log . warn ( message , meta ) ,
1203+ } ) ,
12571204 loadFlaggedAccountsFromPath,
12581205 logWarn : ( message , meta ) => log . warn ( message , meta ) ,
12591206 } ) ,
0 commit comments