48ea6bb33d
CI / unit (8.2) (push) Has been cancelled
CI / unit (8.3) (push) Has been cancelled
CI / unit (8.4) (push) Has been cancelled
CI / integration (mariadb, 32) (push) Has been cancelled
CI / integration (mariadb, 33) (push) Has been cancelled
CI / integration (mariadb, 34) (push) Has been cancelled
CI / integration (mariadb, 35) (push) Has been cancelled
CI / integration (pgsql, 32) (push) Has been cancelled
CI / integration (pgsql, 33) (push) Has been cancelled
CI / integration (pgsql, 34) (push) Has been cancelled
CI / integration (pgsql, 35) (push) Has been cancelled
CI / integration (sqlite, 32) (push) Has been cancelled
CI / integration (sqlite, 33) (push) Has been cancelled
CI / integration (sqlite, 34) (push) Has been cancelled
CI / integration (sqlite, 35) (push) Has been cancelled
CI / integration-targets (push) Has been cancelled
Added tests/integration/targets-matrix.sh, which runs the full backup/restore round trip through files_external's AmazonS3 and SFTP backend classes (S3 via a disposable s3mock container, SFTP via atmoz/sftp), since the existing matrix only ever exercised the local backend. This caught a real bug: moving a snapshot to the trash read it from the location and streamed it straight back into a write on the same connection, which SFTP can't do reliably (phpseclib fails to interleave a read and a write on one connection). Backends now expose a native move() and the id-bound snapshot object is moved with that instead of a read+write copy; the path-bound database dump still goes through decrypt/re-encrypt, since its AEAD authentication is tied to its path.
127 lines
3.3 KiB
PHP
127 lines
3.3 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));
|
|
}
|
|
|
|
public function move(string $from, string $to): void {
|
|
$target = $this->abs($to);
|
|
$dir = dirname($target);
|
|
if (!is_dir($dir) && !mkdir($dir, 0700, true) && !is_dir($dir)) {
|
|
throw new BackendException('Cannot create ' . $dir);
|
|
}
|
|
if (!rename($this->abs($from), $target)) {
|
|
throw new BackendException("Cannot move $from to $to");
|
|
}
|
|
}
|
|
|
|
private function abs(string $path): string {
|
|
if (str_contains($path, '..') || str_starts_with($path, '/')) {
|
|
throw new BackendException('Invalid path: ' . $path);
|
|
}
|
|
return $this->root . '/' . $path;
|
|
}
|
|
}
|