|null */ public static function readJson( string $path ): ?array { if ( ! is_file( $path ) ) { return null; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $raw = file_get_contents( $path ); if ( false === $raw ) { return null; } $decoded = json_decode( $raw, true ); return is_array( $decoded ) ? $decoded : null; } /** * Opens an append-only working file positioned at its last committed byte. * * Anything written after the last commit belongs to a tick that died before its * cursor was saved, so it is discarded. That is what makes every append-style * stage (dump, inventory, archive, upload) safe to repeat. * * @return resource * @throws \RuntimeException When the file is shorter than what was committed. */ public static function openAppend( string $path, int $committed ) { $handle = fopen( $path, 'c+b' ); if ( false === $handle ) { throw new \RuntimeException( sprintf( 'Cannot open %s for writing.', esc_html( basename( $path ) ) ) ); } return self::position( $handle, $path, $committed ); } /** * openAppend() that first takes an exclusive lock on the file itself, before it can truncate anything. * * The per-job lock normally keeps two ticks apart. This one sits on the data file, so a second * request that slips past the job lock (a retried request while the first is still running, or a * host where the job lock file is not honoured) cannot cut the file back under a writer. The lock * lasts until the handle is closed. * * @return resource|null Null when another process is writing the file right now. * @throws \RuntimeException */ public static function openAppendLocked( string $path, int $committed ) { $handle = fopen( $path, 'c+b' ); if ( false === $handle ) { throw new \RuntimeException( sprintf( 'Cannot open %s for writing.', esc_html( basename( $path ) ) ) ); } if ( ! flock( $handle, LOCK_EX | LOCK_NB ) ) { fclose( $handle ); return null; } return self::position( $handle, $path, $committed ); } /** * @param resource $handle * @return resource */ private static function position( $handle, string $path, int $committed ) { clearstatcache( true, $path ); $size = (int) filesize( $path ); if ( $size < $committed ) { fclose( $handle ); throw new \RuntimeException( sprintf( '%s is shorter than its recorded progress; the working file was lost.', esc_html( basename( $path ) ) ) ); } ftruncate( $handle, $committed ); fseek( $handle, 0, SEEK_END ); return $handle; } /** * Writes the whole string or fails. fwrite() may legitimately write less on a full disk. * * @param resource $handle * @throws \RuntimeException */ public static function writeAll( $handle, string $bytes ): void { $length = strlen( $bytes ); $written = 0; while ( $written < $length ) { $n = fwrite( $handle, $written ? substr( $bytes, $written ) : $bytes ); if ( false === $n || 0 === $n ) { throw new \RuntimeException( 'Write failed. The disk may be full.' ); } $written += $n; } } /** * Deletes a directory tree without following symlinks. * * Resumable: when the budget runs out it returns false having removed what it * could, and the next call carries on from what is left. * * @return bool True once $path no longer exists. * @throws \RuntimeException When something cannot be removed. */ public static function deleteTree( string $path, ?Budget $budget = null ): bool { if ( is_link( $path ) || is_file( $path ) ) { if ( ! @unlink( $path ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink throw new \RuntimeException( sprintf( 'Cannot delete %s.', esc_html( basename( $path ) ) ) ); } return true; } if ( ! is_dir( $path ) ) { return true; } $handle = opendir( $path ); if ( false === $handle ) { throw new \RuntimeException( sprintf( 'Cannot read directory %s.', esc_html( basename( $path ) ) ) ); } while ( false !== ( $entry = readdir( $handle ) ) ) { if ( '.' === $entry || '..' === $entry ) { continue; } if ( null !== $budget && $budget->exhausted() ) { closedir( $handle ); return false; } if ( ! self::deleteTree( $path . '/' . $entry, $budget ) ) { closedir( $handle ); return false; } } closedir( $handle ); if ( ! @rmdir( $path ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir throw new \RuntimeException( sprintf( 'Cannot remove directory %s.', esc_html( basename( $path ) ) ) ); } return true; } }