root = untrailingslashit( $root_dir ); $this->exclusions = new Exclusions( $root_key, $root_dir ); } /** * Lists entries after a cursor. * * @param string $after Last path from the previous call ('' to start). * @param int $max Max entries. * @param float $deadline microtime(true) deadline. * @return array{entries: array[], next: string|null, excluded: int} */ public function walk( $after, $max, $deadline ) { $this->out = array(); $this->stopped = false; $this->last = (string) $after; $this->max = max( 1, (int) $max ); $this->deadline = (float) $deadline; $this->excluded = 0; $parts = '' === (string) $after ? array() : explode( '/', (string) $after ); $this->walk_dir( '', $parts ); return array( 'entries' => $this->out, 'next' => $this->stopped ? $this->last : null, 'excluded' => $this->excluded, ); } /** * Walks one directory, skipping names up to the cursor. * * @param string $rel_dir Relative directory ('' for root). * @param string[] $after_parts Remaining cursor components at this depth. */ private function walk_dir( $rel_dir, array $after_parts ) { $abs = '' === $rel_dir ? $this->root : $this->root . '/' . $rel_dir; $names = @scandir( $abs ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( false === $names ) { if ( '' !== $rel_dir ) { $this->emit( array( 'p' => $rel_dir, 't' => 'd', 'err' => 'unreadable' ), $rel_dir ); } return; } $names = array_values( array_diff( $names, array( '.', '..' ) ) ); sort( $names, SORT_STRING ); $first = $after_parts ? $after_parts[0] : null; foreach ( $names as $name ) { if ( $this->stopped ) { return; } $rel = '' === $rel_dir ? $name : $rel_dir . '/' . $name; if ( null !== $first ) { $cmp = strcmp( $name, $first ); if ( $cmp < 0 ) { continue; } if ( 0 === $cmp ) { $first = null; $path = $this->root . '/' . $rel; if ( is_dir( $path ) && ! is_link( $path ) && ! $this->exclusions->excluded( $rel, $name ) ) { // The cursor is this directory (children not yet sent) or something inside it. $this->walk_dir( $rel, array_slice( $after_parts, 1 ) ); } continue; } $first = null; } $this->visit( $rel, $name ); } } /** * Emits one entry (and walks into directories). * * @param string $rel Relative path. * @param string $name Base name. */ private function visit( $rel, $name ) { if ( $this->exclusions->excluded( $rel, $name ) ) { ++$this->excluded; return; } $path = $this->root . '/' . $rel; $stat = @lstat( $path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( false === $stat ) { return; // Vanished since the listing. } $type = $stat['mode'] & 0170000; if ( 0120000 === $type ) { $this->emit( array( 'p' => $rel, 't' => 'l', 'target' => (string) @readlink( $path ), // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged ), $rel ); return; } if ( 0040000 === $type ) { $this->emit( array( 'p' => $rel, 't' => 'd', 'm' => (int) $stat['mtime'], ), $rel ); if ( ! $this->stopped ) { $this->walk_dir( $rel, array() ); } return; } if ( 0100000 === $type ) { $entry = array( 'p' => $rel, 't' => 'f', 's' => (int) $stat['size'], 'm' => (int) $stat['mtime'], ); if ( ! is_readable( $path ) ) { $entry['err'] = 'unreadable'; } $this->emit( $entry, $rel ); } // Sockets, FIFOs and devices are ignored. } /** * Records an entry and checks the budget. * * @param array $entry Entry. * @param string $rel Relative path (becomes the cursor). */ private function emit( array $entry, $rel ) { if ( ! preg_match( '//u', $entry['p'] ) ) { // Not valid UTF-8: send the raw bytes base64-encoded so JSON can carry them. $entry['p64'] = base64_encode( $entry['p'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode $entry['p'] = ''; } $this->out[] = $entry; $this->last = $rel; if ( count( $this->out ) >= $this->max || microtime( true ) >= $this->deadline ) { $this->stopped = true; } } }