content_dir = rtrim( $content_dir, '/' ); } /** * @return array A fresh cursor. */ public static function newCursor( string $content_dir, ?ExportOptions $options = null ): array { $roots = Exclusions::roots( $content_dir, $options ); return array( 'stack' => array_reverse( $roots['dirs'] ), 'loose' => $roots['files'], 'cur' => null, 'files' => 0, 'bytes' => 0, 'skip' => 0, ); } /** * Lists files until the budget runs out. * * @param array $cursor Updated in place. * @param resource $out Inventory file, positioned at its committed end. * @return bool True when the whole tree has been listed. */ public function scan( array &$cursor, $out, Budget $budget ): bool { while ( ! empty( $cursor['loose'] ) ) { $relative = array_pop( $cursor['loose'] ); $this->emit( $cursor, $out, $relative, $this->content_dir . '/' . $relative ); } $counter = 0; while ( true ) { if ( null === $cursor['cur'] ) { if ( empty( $cursor['stack'] ) ) { return true; } $cursor['cur'] = array( 'dir' => array_pop( $cursor['stack'] ), 'seen' => 0, ); } $dir = (string) $cursor['cur']['dir']; $absolute = '' === $dir ? $this->content_dir : $this->content_dir . '/' . $dir; $handle = @opendir( $absolute ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- unreadable directories are skipped. if ( false === $handle ) { ++$cursor['skip']; $cursor['cur'] = null; continue; } $seen = 0; $from = (int) $cursor['cur']['seen']; while ( false !== ( $entry = readdir( $handle ) ) ) { if ( '.' === $entry || '..' === $entry ) { continue; } ++$seen; if ( $seen <= $from ) { continue; } $relative = '' === $dir ? $entry : $dir . '/' . $entry; $path = $absolute . '/' . $entry; if ( is_link( $path ) ) { continue; } if ( is_dir( $path ) ) { if ( ! Exclusions::isExcluded( $relative, true ) ) { $cursor['stack'][] = $relative; } } elseif ( is_file( $path ) && ! Exclusions::isExcluded( $relative, false ) ) { $this->emit( $cursor, $out, $relative, $path ); } $cursor['cur']['seen'] = $seen; if ( 0 === ++$counter % 200 ) { $budget->unit(); if ( $budget->exhausted() ) { closedir( $handle ); fflush( $out ); return false; } } } closedir( $handle ); $cursor['cur'] = null; } } /** * @param array $cursor * @param resource $out */ private function emit( array &$cursor, $out, string $relative, string $path ): void { clearstatcache( true, $path ); $size = @filesize( $path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged $mtime = @filemtime( $path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( false === $size || ! is_readable( $path ) ) { ++$cursor['skip']; return; } $line = wp_json_encode( array( 'a' => Format::CONTENT_DIR . '/' . $relative, 'f' => $path, 's' => $size, 'm' => (int) $mtime, ), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); // A name that is not valid UTF-8 cannot be stored in the archive header; skip it. if ( false === $line || ! Format::isSafeName( Format::CONTENT_DIR . '/' . $relative ) ) { ++$cursor['skip']; return; } SafeFile::writeAll( $out, $line . "\n" ); ++$cursor['files']; $cursor['bytes'] += $size; } }