0f4a238414
Storage and repository:
- Listing skipped every name containing ".part-", directories too, so the
exports of a user like "john.part-time" were invisible to prune on S3,
SFTP, FTP and SMB and their data was deleted. Only real temporary uploads
("<name>.part-<8 hex>") are skipped now.
- prune refuses to run while a recorded per-user export's manifest is
missing, instead of freeing that export's data.
- forget() and untrash() copy first, switch the catalog, and delete the
originals last, so a crash can no longer leave the catalog pointing at a
missing snapshot or database dump.
- Writes are checked against the expected size, so a short upload is an
error instead of a truncated pack; raw restores check rename() and touch().
- A catalog write refuses to overwrite a generation another process just
wrote, and the catalog lock lasts 300 s, so opening a large repository
inside it no longer lets the lease run out.
Backup runs and scheduling:
- A file that keeps changing is read again at most three times; then the
last read is kept and reported, instead of restarting forever and
blocking cleanup.
- Unreadable files and folders are recorded, logged, shown by occ
backup:run, stored in the snapshot and notified to admins, instead of
being skipped silently.
- After a failed scheduled backup, at most three attempts a day, an hour
apart, instead of a new run and notification every five minutes.
- The job no longer spins on a run whose lease another process holds, and
the weekly cleanup catches up after a Sunday spent backing up.
- Notifications for the per-user export retention events.
132 lines
3.5 KiB
PHP
132 lines
3.5 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);
|
|
}
|
|
$expected = StorageBackend::remaining($stream);
|
|
if (stream_copy_to_stream($stream, $out) === false || !fflush($out)) {
|
|
throw new BackendException('Write failed for ' . $path);
|
|
}
|
|
fclose($out);
|
|
$out = false;
|
|
clearstatcache(true, $tmp);
|
|
if ($expected !== null && filesize($tmp) !== $expected) {
|
|
throw new BackendException('Incomplete write for ' . $path);
|
|
}
|
|
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() && !StorageBackend::isTemporary($file->getFilename())) {
|
|
$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;
|
|
}
|
|
}
|