aa541d58b2
- detect files changing during reading or between resumable steps and re-read them (size/mtime/ctime/inode), spike F overwrites mid-run - drop pending blob ids when a pack or index upload fails - dump and restore PostgreSQL sequence state exactly, bound parameters, validated catalog names; spike C checks all sequences exactly - keep invalid UTF-8 byte-exact (tagged base64) - refuse large tables without a primary key - StorageBackend: check write/flush/close results, validate base path - user_migration import/export: root paths, intermediate folders, partial writes, read errors - spikes: never touch an existing account, own fixtures, 512M note
116 lines
2.9 KiB
PHP
116 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
// SPDX-FileCopyrightText: 2026 Dillard Blom
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
namespace OCA\NgBackup\Backend;
|
|
|
|
final class LocalBackend implements IBackend {
|
|
public function __construct(
|
|
private string $root,
|
|
) {
|
|
$this->root = rtrim($root, '/');
|
|
if (!is_dir($this->root) && !mkdir($this->root, 0700, true) && !is_dir($this->root)) {
|
|
throw new BackendException('Cannot create ' . $this->root);
|
|
}
|
|
}
|
|
|
|
public function put(string $path, $stream): void {
|
|
$tmp = null;
|
|
$out = false;
|
|
$done = false;
|
|
try {
|
|
$target = $this->abs($path);
|
|
$dir = dirname($target);
|
|
if (!is_dir($dir) && !mkdir($dir, 0700, true) && !is_dir($dir)) {
|
|
throw new BackendException('Cannot create ' . $dir);
|
|
}
|
|
// Write to a temporary name and rename, so a crash never leaves a partial object.
|
|
$tmp = $target . '.part-' . bin2hex(random_bytes(4));
|
|
$out = fopen($tmp, 'wb');
|
|
if ($out === false) {
|
|
throw new BackendException('Cannot write ' . $path);
|
|
}
|
|
if (stream_copy_to_stream($stream, $out) === false || !fflush($out)) {
|
|
throw new BackendException('Write failed for ' . $path);
|
|
}
|
|
fclose($out);
|
|
$out = false;
|
|
if (!rename($tmp, $target)) {
|
|
throw new BackendException('Cannot finalise ' . $path);
|
|
}
|
|
$done = true;
|
|
} finally {
|
|
if ($out !== false) {
|
|
fclose($out);
|
|
}
|
|
if (is_resource($stream)) {
|
|
fclose($stream);
|
|
}
|
|
if (!$done && $tmp !== null) {
|
|
@unlink($tmp);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function get(string $path) {
|
|
$fh = @fopen($this->abs($path), 'rb');
|
|
if ($fh === false) {
|
|
throw new BackendException('Not found: ' . $path);
|
|
}
|
|
return $fh;
|
|
}
|
|
|
|
public function getRange(string $path, int $offset, int $length): string {
|
|
$fh = $this->get($path);
|
|
try {
|
|
if (fseek($fh, $offset) !== 0) {
|
|
throw new BackendException('Seek failed in ' . $path);
|
|
}
|
|
$data = '';
|
|
while (strlen($data) < $length && !feof($fh)) {
|
|
$data .= (string)fread($fh, $length - strlen($data));
|
|
}
|
|
if (strlen($data) !== $length) {
|
|
throw new BackendException('Short read in ' . $path);
|
|
}
|
|
return $data;
|
|
} finally {
|
|
fclose($fh);
|
|
}
|
|
}
|
|
|
|
public function exists(string $path): bool {
|
|
return is_file($this->abs($path));
|
|
}
|
|
|
|
public function list(string $prefix): array {
|
|
$base = $this->abs($prefix);
|
|
if (!is_dir($base)) {
|
|
return [];
|
|
}
|
|
$result = [];
|
|
$it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($base, \FilesystemIterator::SKIP_DOTS));
|
|
foreach ($it as $file) {
|
|
if ($file->isFile() && !str_contains($file->getFilename(), '.part-')) {
|
|
$result[] = ltrim(substr($file->getPathname(), strlen($this->root)), '/');
|
|
}
|
|
}
|
|
sort($result);
|
|
return $result;
|
|
}
|
|
|
|
public function delete(string $path): void {
|
|
@unlink($this->abs($path));
|
|
}
|
|
|
|
private function abs(string $path): string {
|
|
if (str_contains($path, '..') || str_starts_with($path, '/')) {
|
|
throw new BackendException('Invalid path: ' . $path);
|
|
}
|
|
return $this->root . '/' . $path;
|
|
}
|
|
}
|