repo = $repo ?? new Job_Repository(); } /** Artifact pool: scheduled / manual backups. */ public const POOL_REGULAR = 'regular'; /** Artifact pool: automatic pre-update backups (own cap, Phase K). */ public const POOL_PRE_UPDATE = 'pre_update'; /** * Which retention pool an artifact row belongs to (meta trigger=pre_update → pre_update). * * @param array $art Artifact row. */ public static function pool_of( array $art ): string { $meta = array(); if ( isset( $art['meta'] ) && is_array( $art['meta'] ) ) { $meta = $art['meta']; } elseif ( isset( $art['meta_json'] ) && is_string( $art['meta_json'] ) && '' !== $art['meta_json'] ) { $decoded = json_decode( $art['meta_json'], true ); $meta = is_array( $decoded ) ? $decoded : array(); } return 'pre_update' === (string) ( $meta['trigger'] ?? '' ) ? self::POOL_PRE_UPDATE : self::POOL_REGULAR; } /** * Prune pre-update artifacts so at most N *backup sets* remain (newest kept). * * Counts local rows only (a backup with remote copies is still one backup) and * removes sibling remote rows of a pruned set. Chain protection is applied via * `brverify_retention_can_delete` exactly like the regular pool. * * @param int|null $keep Override cap (null → settings). * @return array{ok:bool,kept:int,deleted:int,errors:list} */ public function prune_pre_update( ?int $keep = null ): array { $n = null === $keep ? Settings::pre_update_retention_count() : max( 1, min( 50, $keep ) ); $all = $this->repo->list_artifacts_oldest_first( 1000 ); $pool = array(); foreach ( $all as $art ) { if ( self::POOL_PRE_UPDATE === self::pool_of( $art ) && 'local' === (string) ( $art['adapter'] ?? 'local' ) ) { $pool[] = $art; } } $total = count( $pool ); if ( $total <= $n ) { return array( 'ok' => true, 'kept' => $total, 'deleted' => 0, 'errors' => array(), ); } $deleted = 0; $errors = array(); foreach ( array_slice( $pool, 0, $total - $n ) as $art ) { /** This filter is documented in prune_by_count(). */ $can = (bool) apply_filters( 'brverify_retention_can_delete', true, $art, $all, $n, self::POOL_PRE_UPDATE ); if ( ! $can ) { continue; } $res = $this->delete_artifact( (int) $art['id'], (string) ( $art['path'] ?? '' ) ); if ( ! $res['ok'] ) { $errors[] = (string) ( $res['error'] ?? 'unknown' ); continue; } ++$deleted; // Drop sibling remote rows of the same backup set (row only; the file is gone). foreach ( $all as $sib ) { if ( (int) $sib['job_id'] === (int) $art['job_id'] && (int) $sib['id'] !== (int) $art['id'] && 'local' !== (string) ( $sib['adapter'] ?? 'local' ) ) { $this->repo->delete_artifact( (int) $sib['id'] ); } } } return array( 'ok' => empty( $errors ), 'kept' => $total - $deleted, 'deleted' => $deleted, 'errors' => $errors, ); } /** * Prune regular (scheduled/manual) local artifacts so at most N remain (newest kept). * * Pre-update artifacts (Phase K) are excluded from this count — they have their * own cap — but are passed to the can-delete filter so chain protection sees them. * * @param int|null $keep Override keep count (null → settings). * @return array{ok:bool,kept:int,deleted:int,errors:list} */ public function prune_by_count( ?int $keep = null ): array { $n = null === $keep ? Settings::retention_count() : max( 1, min( 100, $keep ) ); $everything = $this->repo->list_artifacts_oldest_first( 500 ); $all = array(); foreach ( $everything as $row ) { if ( self::POOL_REGULAR === self::pool_of( $row ) ) { $all[] = $row; } } $total = count( $all ); if ( $total <= $n ) { return array( 'ok' => true, 'kept' => $total, 'deleted' => 0, 'errors' => array(), ); } $to_delete = array_slice( $all, 0, $total - $n ); $deleted = 0; $errors = array(); foreach ( $to_delete as $art ) { $id = (int) $art['id']; $path = (string) ( $art['path'] ?? '' ); /** * Whether an artifact may be pruned by retention. * * extended edition incremental chains return false for parents still required * by a newer kept child. * * @param bool $can Default true. * @param array $art Artifact row. * @param list> $all ALL artifacts oldest-first (every pool, so chains that cross pools stay protected). * @param int $n Keep count for this pool. * @param string $pool 'regular' or 'pre_update' (Phase K). */ $can = (bool) apply_filters( 'brverify_retention_can_delete', true, $art, $everything, $n, self::POOL_REGULAR ); if ( ! $can ) { continue; } $res = $this->delete_artifact( $id, $path ); if ( $res['ok'] ) { ++$deleted; } else { $errors[] = (string) ( $res['error'] ?? 'unknown' ); } } return array( 'ok' => empty( $errors ), 'kept' => $total - $deleted, 'deleted' => $deleted, 'errors' => $errors, ); } /** * Delete one artifact (disk under storage jail + DB row). * * @param int $id Artifact id. * @param string $path Absolute path. * @return array{ok:bool,error?:string} */ public function delete_artifact( int $id, string $path ): array { $storage_real = realpath( Working_Dir::storage() ); if ( '' !== $path && is_file( $path ) ) { $path_real = realpath( $path ); if ( false === $storage_real || false === $path_real || ! Path_Guard::is_under( $path_real, $storage_real ) ) { return array( 'ok' => false, 'error' => sprintf( 'Refused to delete path outside storage (artifact #%d).', $id ), ); } wp_delete_file( $path_real ); if ( is_file( $path_real ) ) { return array( 'ok' => false, 'error' => sprintf( 'Failed to unlink artifact #%d.', $id ), ); } } elseif ( '' !== $path && false !== $storage_real ) { // Missing file is OK (already gone); still refuse obvious escapes without realpath. $norm = wp_normalize_path( $path ); $jail = trailingslashit( wp_normalize_path( Working_Dir::storage() ) ); if ( ! \brverify_str_starts_with( $norm, $jail ) ) { return array( 'ok' => false, 'error' => sprintf( 'Refused to delete path outside storage (artifact #%d).', $id ), ); } } if ( ! $this->repo->delete_artifact( $id ) ) { return array( 'ok' => false, 'error' => sprintf( 'Failed to delete artifact row #%d.', $id ), ); } return array( 'ok' => true ); } /** * Run prune after a successful backup and log on a job id. * * @param int $job_id Completed backup job id. */ public function after_backup_success( int $job_id ): void { $result = $this->prune_by_count(); $pre = $this->prune_pre_update(); if ( (int) $pre['deleted'] > 0 ) { Logger::info( $job_id, sprintf( 'Pre-update retention prune: kept=%d deleted=%d (N=%d).', (int) $pre['kept'], (int) $pre['deleted'], Settings::pre_update_retention_count() ) ); } if ( ! empty( $pre['errors'] ) ) { Logger::warning( $job_id, 'Pre-update retention warnings: ' . implode( '; ', $pre['errors'] ) ); } Logger::info( $job_id, sprintf( 'Retention prune: kept=%d deleted=%d (N=%d).', (int) $result['kept'], (int) $result['deleted'], Settings::retention_count() ) ); if ( ! empty( $result['errors'] ) ) { Logger::warning( $job_id, 'Retention prune warnings: ' . implode( '; ', $result['errors'] ) ); } } }