= 80100; } /** * Does up to $seconds of hashing and reports where it stands. * * @return array{status:string,size:int,mtime:int,done:int,percent:float,sha256?:string,computed_at?:int} * status "ready" (with sha256), or "working" (call again; done and percent say how far), or "error". */ public static function step( string $archive, float $seconds ): array { clearstatcache( true, $archive ); $size = (int) filesize( $archive ); $mtime = (int) filemtime( $archive ); $cached = self::cached( $archive, $size, $mtime ); if ( null !== $cached ) { return $cached; } // One worker at a time. A second request (a double click, another tab) is told how far the first has got. $part = fopen( self::partPath( $archive ), 'c+' ); if ( false === $part ) { return self::state( 'error', $size, $mtime, 0 ); } if ( ! flock( $part, LOCK_EX | LOCK_NB ) ) { $state = self::savedState( $part, $size, $mtime ); fclose( $part ); return self::state( 'working', $size, $mtime, (int) ( $state['offset'] ?? 0 ) ); } try { return self::work( $archive, $part, $size, $mtime, $seconds ); } finally { flock( $part, LOCK_UN ); fclose( $part ); } } /** * @param resource $part * @return array{status:string,size:int,mtime:int,done:int,percent:float,sha256?:string,computed_at?:int} */ private static function work( string $archive, $part, int $size, int $mtime, float $seconds ): array { $resumable = self::resumable(); $saved = $resumable ? self::savedState( $part, $size, $mtime ) : null; $context = null; $offset = 0; if ( null !== $saved && isset( $saved['ctx'] ) ) { $restored = @unserialize( (string) base64_decode( (string) $saved['ctx'], true ), array( 'allowed_classes' => array( 'HashContext' ) ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize -- only our own saved hash state, and only a HashContext can come out. if ( $restored instanceof \HashContext ) { $context = $restored; $offset = (int) $saved['offset']; } } if ( null === $context ) { $context = hash_init( 'sha256' ); } $handle = fopen( $archive, 'rb' ); if ( false === $handle || 0 !== fseek( $handle, $offset ) ) { return self::state( 'error', $size, $mtime, $offset ); } $deadline = microtime( true ) + $seconds; while ( $offset < $size ) { // Each block gets a fresh allowance, so a one-pass hash of a very large backup is never cut short. if ( function_exists( 'set_time_limit' ) ) { @set_time_limit( 300 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,Squiz.PHP.DiscouragedFunctions.Discouraged -- renewed per block of one long-running task, and only where the host allows it. } $read = hash_update_stream( $context, $handle, min( self::BLOCK, $size - $offset ) ); if ( $read < 1 ) { break; } $offset += $read; if ( $resumable && $offset < $size && microtime( true ) >= $deadline ) { fclose( $handle ); self::save( $part, $context, $size, $mtime, $offset ); return self::state( 'working', $size, $mtime, $offset ); } } fclose( $handle ); // The file must be the one that was measured: if it shrank, grew or was replaced meanwhile, start again. clearstatcache( true, $archive ); if ( $offset !== $size || (int) filesize( $archive ) !== $size || (int) filemtime( $archive ) !== $mtime ) { ftruncate( $part, 0 ); return self::state( 'working', (int) filesize( $archive ), (int) filemtime( $archive ), 0 ); } $hash = hash_final( $context ); $now = time(); SafeFile::writeAtomic( self::metaPath( $archive ), (string) wp_json_encode( array( 'file' => basename( $archive ), 'size' => $size, 'mtime' => $mtime, 'created' => $mtime, 'algo' => 'sha256', 'sha256' => $hash, 'computed_at' => $now, ) ) ); ftruncate( $part, 0 ); @unlink( self::partPath( $archive ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- the lock is released when the caller closes it. return self::state( 'ready', $size, $mtime, $size ) + array( 'sha256' => $hash, 'computed_at' => $now, ); } /** * The remembered hash, if it is still the hash of this very file. * * @return array{status:string,size:int,mtime:int,done:int,percent:float,sha256:string,computed_at:int}|null */ private static function cached( string $archive, int $size, int $mtime ): ?array { $meta = SafeFile::readJson( self::metaPath( $archive ) ); if ( is_array( $meta ) && 'sha256' === ( $meta['algo'] ?? '' ) && (int) ( $meta['size'] ?? -1 ) === $size && (int) ( $meta['mtime'] ?? -1 ) === $mtime && is_string( $meta['sha256'] ?? null ) && 1 === preg_match( '/^[a-f0-9]{64}$/', $meta['sha256'] ) ) { return self::state( 'ready', $size, $mtime, $size ) + array( 'sha256' => $meta['sha256'], 'computed_at' => (int) ( $meta['computed_at'] ?? 0 ), ); } // The earlier format: " " in .sha256. Adopted once, then retired. $legacy = $archive . '.sha256'; if ( is_file( $legacy ) ) { $parts = explode( ' ', trim( (string) SafeFile::readText( $legacy ) ) ); if ( 3 === count( $parts ) && 1 === preg_match( '/^[a-f0-9]{64}$/', $parts[0] ) && (int) $parts[1] === $size && (int) $parts[2] === $mtime ) { $now = time(); SafeFile::writeAtomic( self::metaPath( $archive ), (string) wp_json_encode( array( 'file' => basename( $archive ), 'size' => $size, 'mtime' => $mtime, 'created' => $mtime, 'algo' => 'sha256', 'sha256' => $parts[0], 'computed_at' => $now ) ) ); @unlink( $legacy ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged return self::state( 'ready', $size, $mtime, $size ) + array( 'sha256' => $parts[0], 'computed_at' => $now, ); } } return null; } /** * What an earlier request saved, if it is for this exact file. * * @param resource $part * @return array|null */ private static function savedState( $part, int $size, int $mtime ): ?array { rewind( $part ); $raw = (string) stream_get_contents( $part ); $state = '' === $raw ? null : json_decode( $raw, true ); if ( is_array( $state ) && (int) ( $state['size'] ?? -1 ) === $size && (int) ( $state['mtime'] ?? -1 ) === $mtime && (int) ( $state['offset'] ?? -1 ) >= 0 && (int) $state['offset'] <= $size ) { return $state; } return null; } /** * @param resource $part */ private static function save( $part, \HashContext $context, int $size, int $mtime, int $offset ): void { $json = (string) wp_json_encode( array( 'size' => $size, 'mtime' => $mtime, 'offset' => $offset, 'ctx' => base64_encode( serialize( $context ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- a hash in progress, read back only by this class. ) ); ftruncate( $part, 0 ); rewind( $part ); fwrite( $part, $json ); fflush( $part ); } /** * @return array{status:string,size:int,mtime:int,done:int,percent:float} */ private static function state( string $status, int $size, int $mtime, int $done ): array { return array( 'status' => $status, 'size' => $size, 'mtime' => $mtime, 'done' => $done, 'percent' => $size > 0 ? round( min( 100.0, $done / $size * 100 ), 1 ) : 100.0, ); } }