Files
nextcloud-ng_backup/lib/UserMigration/RepositoryExportDestination.php
T
dillard aa541d58b2 Phase 0: fixes from review (torn files, pending blobs, exact sequences, UTF-8, storage checks)
- 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
2026-10-01 18:58:51 +02:00

96 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
// SPDX-FileCopyrightText: 2026 Dillard Blom
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\NgBackup\UserMigration;
use OCA\NgBackup\Repository\Repository;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\UserMigrationException;
/**
* Export destination for user_migration that writes straight into the encrypted repository:
* no zip, no copy in the user's folder. Every exported path becomes a blob stream; the list
* of paths is stored as one encrypted manifest (users/<uid>/<exportId>) when close() is called.
*/
final class RepositoryExportDestination implements IExportDestination {
/** @var array<string, array{t:string, b?:list<string>, s?:int}> path => entry */
private array $entries = [];
private array $versions = [];
private array $stats = ['newBlobs' => 0, 'dupBlobs' => 0, 'uploaded' => 0];
private string $exportId;
public function __construct(
private Repository $repo,
private string $uid,
) {
$this->exportId = gmdate('Ymd\THis\Z') . '-' . bin2hex(random_bytes(4));
}
public function addFileContents(string $path, string $content): void {
$writer = $this->repo->blobWriter($this->stats);
$writer->write($content);
$this->entries[self::norm($path)] = ['t' => 'f', 'b' => $writer->finish(), 's' => strlen($content)];
}
public function addFileAsStream(string $path, $stream): void {
$writer = $this->repo->blobWriter($this->stats);
while (!feof($stream)) {
$chunk = fread($stream, 1048576);
if ($chunk === false) {
throw new UserMigrationException('Read error while exporting ' . $path);
}
$writer->write($chunk);
}
$this->entries[self::norm($path)] = ['t' => 'f', 'b' => $writer->finish(), 's' => $writer->bytes()];
}
public function copyFolder(Folder $folder, string $destinationPath, ?callable $nodeFilter = null): void {
$base = self::norm($destinationPath);
$this->entries[$base] = ['t' => 'd'];
foreach ($folder->getDirectoryListing() as $node) {
if ($nodeFilter !== null && !$nodeFilter($node)) {
continue;
}
$target = $base . '/' . $node->getName();
if ($node instanceof Folder) {
$this->copyFolder($node, $target, $nodeFilter);
} elseif ($node instanceof File) {
$stream = $node->fopen('rb');
if ($stream === false) {
throw new \RuntimeException('Cannot read ' . $node->getPath());
}
$this->addFileAsStream($target, $stream);
fclose($stream);
}
}
}
public function setMigratorVersions(array $versions): void {
$this->versions = $versions;
}
public function close(): void {
$this->repo->flushPacks();
$manifest = ['uid' => $this->uid, 'time' => gmdate('c'), 'versions' => $this->versions, 'entries' => $this->entries];
$this->repo->putObject($this->manifestPath(), json_encode($manifest, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR));
}
public function manifestPath(): string {
return 'users/' . $this->uid . '/' . $this->exportId;
}
public function stats(): array {
return $this->stats;
}
private static function norm(string $path): string {
return trim($path, '/');
}
}