path = $path; } public function count(): int { clearstatcache(true, $this->path); return is_file($this->path) ? intdiv((int) filesize($this->path), 8) : 0; } /** @param int[] $indexes */ public function add(array $indexes): void { if ($indexes === []) { return; } $this->truncate($this->count()); $data = ''; foreach ($indexes as $i) { $data .= pack('J', (int) $i); } Fs::ensure_dir(dirname($this->path)); if (file_put_contents($this->path, $data, FILE_APPEND) !== strlen($data)) { throw new RelayError('files.write_failed', esc_html__('Could not write the file list. The disk may be full.', 'siterelay')); } } /** @return int[] up to $max numbers from position $from on */ public function slice(int $from, int $max): array { $count = $this->count(); if ($from < 0 || $from >= $count || $max <= 0) { return []; } $h = Fs::open($this->path, 'rb'); fseek($h, $from * 8); $data = (string) Fs::read($h, min($max, $count - $from) * 8); Fs::close($h); return array_values(unpack('J*', $data) ?: []); } public function truncate(int $count): void { clearstatcache(true, $this->path); if (is_file($this->path) && filesize($this->path) !== max(0, $count) * 8) { $h = Fs::open($this->path, 'r+b'); ftruncate($h, max(0, $count) * 8); Fs::close($h); clearstatcache(true, $this->path); } } public function delete(): void { wp_delete_file($this->path); } }