Skip to content

Commit d1a71dc

Browse files
committed
include internal data to RP
1 parent 8b8aa24 commit d1a71dc

File tree

6 files changed

+167
-35
lines changed

6 files changed

+167
-35
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
/**
7+
* Nextcloud - Backup
8+
*
9+
* This file is licensed under the Affero General Public License version 3 or
10+
* later. See the COPYING file.
11+
*
12+
* @author Maxence Lange <maxence@artificial-owl.com>
13+
* @copyright 2019, Maxence Lange <maxence@artificial-owl.com>
14+
* @license GNU AGPL version 3 or any later version
15+
*
16+
* This program is free software: you can redistribute it and/or modify
17+
* it under the terms of the GNU Affero General Public License as
18+
* published by the Free Software Foundation, either version 3 of the
19+
* License, or (at your option) any later version.
20+
*
21+
* This program is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24+
* GNU Affero General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Affero General Public License
27+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28+
*
29+
*/
30+
31+
32+
namespace OCA\Backup\Exceptions;
33+
34+
35+
use Exception;
36+
37+
38+
/**
39+
* Class RestoringDataNotFoundException
40+
*
41+
* @package OCA\Backup\Exceptions
42+
*/
43+
class RestoringDataNotFoundException extends Exception {
44+
45+
}
46+

lib/Model/RestoringChunk.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,26 @@ class RestoringChunk implements JsonSerializable, IDeserializable {
7171
/** @var bool */
7272
private $encrypted = false;
7373

74+
/** @var bool */
75+
private $staticName = false;
76+
7477
/** @var string */
7578
private $encryptedChecksum = '';
7679

7780

7881
/**
82+
* only add $name for specific Chunk.
83+
*
7984
* RestoringChunk constructor.
8085
*/
81-
public function __construct() {
82-
$this->name = $this->uuid();
86+
public function __construct(string $name = '') {
87+
if ($name === '') {
88+
$name = $this->uuid();
89+
} else {
90+
$this->staticName = true;
91+
}
92+
93+
$this->name = $name;
8394
}
8495

8596

@@ -107,10 +118,34 @@ public function setName(string $name): RestoringChunk {
107118
return $this;
108119
}
109120

121+
122+
/**
123+
* @param bool $staticName
124+
*
125+
* @return RestoringChunk
126+
*/
127+
public function setStaticName(bool $staticName): self {
128+
$this->staticName = $staticName;
129+
130+
return $this;
131+
}
132+
133+
/**
134+
* @return bool
135+
*/
136+
public function isStaticName(): bool {
137+
return $this->staticName;
138+
}
139+
140+
110141
/**
111142
* @return string
112143
*/
113144
public function getFilename(): string {
145+
if ($this->isStaticName()) {
146+
return $this->getName();
147+
}
148+
114149
if ($this->isEncrypted()) {
115150
return $this->getName();
116151
}
@@ -235,6 +270,18 @@ public function setChecksum(string $checksum): RestoringChunk {
235270
return $this;
236271
}
237272

273+
/**
274+
* @param string $checksum
275+
*
276+
* @return bool
277+
*/
278+
public function compareChecksum(string $checksum): bool {
279+
if ($this->isEncrypted()) {
280+
return ($this->getEncryptedChecksum() === $checksum);
281+
}
282+
283+
return ($this->getChecksum() === $checksum);
284+
}
238285

239286
/**
240287
* @return string
@@ -285,6 +332,7 @@ public function import(array $data): IDeserializable {
285332
->setSize($this->getInt('size', $data))
286333
->setContent($this->get('content', $data))
287334
->setEncrypted($this->getBool('encrypted', $data))
335+
->setStaticName($this->getBool('staticName', $data))
288336
->setChecksum($this->get('checksum', $data))
289337
->setEncryptedChecksum($this->get('encryptedChecksum', $data));
290338

@@ -311,6 +359,7 @@ public function jsonSerialize(): array {
311359
'count' => $this->getCount(),
312360
'size' => $this->getSize(),
313361
'encrypted' => $this->isEncrypted(),
362+
'staticName' => $this->isStaticName(),
314363
'checksum' => $this->getChecksum(),
315364
'encryptedChecksum' => $this->getEncryptedChecksum()
316365
];

lib/Model/RestoringData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class RestoringData implements IDeserializable, JsonSerializable {
5353
use TNC23Deserialize;
5454

5555

56+
const INTERNAL_DATA = 0;
5657
const ROOT_DISK = 1;
5758
const ROOT_NEXTCLOUD = 2;
5859
const ROOT_DATA = 3;
@@ -63,6 +64,7 @@ class RestoringData implements IDeserializable, JsonSerializable {
6364
// value > 1000 is for content that are not 'file'
6465
const SQL_DUMP = 1001;
6566

67+
const INTERNAL = 'internal';
6668
const DATA = 'data';
6769
const APPS = 'apps';
6870
const CONFIG = 'config';

lib/Model/RestoringPoint.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Logger;
4242
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
4343
use JsonSerializable;
44+
use OCA\Backup\Exceptions\RestoringDataNotFoundException;
4445
use OCP\Files\SimpleFS\ISimpleFolder;
4546

4647

@@ -238,11 +239,9 @@ public function getBaseFolder(): ISimpleFolder {
238239

239240

240241
/**
241-
* @param bool $filtered
242-
*
243242
* @return RestoringData[]
244243
*/
245-
public function getRestoringData(bool $filtered = false): array {
244+
public function getRestoringData(): array {
246245
return $this->restoringData;
247246
// $options = $this->getOptions();
248247
// if (!$filtered || $options->getChunk() === '') {
@@ -271,16 +270,32 @@ public function setRestoringData(array $restoringData): self {
271270
}
272271

273272
/**
274-
* @param RestoringData $chunk
273+
* @param RestoringData $data
275274
*
276275
* @return RestoringPoint
277276
*/
278-
public function addRestoringData(RestoringData $chunk): self {
279-
$this->restoringData[] = $chunk;
277+
public function addRestoringData(RestoringData $data): self {
278+
$this->restoringData[] = $data;
280279

281280
return $this;
282281
}
283282

283+
/**
284+
* @param string $dataName
285+
*
286+
* @return RestoringData
287+
* @throws RestoringDataNotFoundException
288+
*/
289+
public function getData(string $dataName): RestoringData {
290+
foreach ($this->restoringData as $restoringData) {
291+
if ($restoringData->getName() === $dataName) {
292+
return $restoringData;
293+
}
294+
}
295+
296+
throw new RestoringDataNotFoundException();
297+
}
298+
284299

285300
/**
286301
* @return bool

lib/Service/ArchiveService.php

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ArchiveService {
6969

7070
const MAX_ZIP_SIZE = 100000000;
7171
const BACKUP_SCRIPT = 'restore.php';
72-
72+
const APP_ZIP = 'app.zip';
7373

7474
/** @var FilesService */
7575
private $filesService;
@@ -105,7 +105,11 @@ public function __construct(
105105
* @throws ArchiveNotFoundException
106106
*/
107107
public function createChunks(RestoringPoint $point): void {
108-
foreach ($point->getRestoringData(false) as $data) {
108+
foreach ($point->getRestoringData() as $data) {
109+
if ($data->getType() === RestoringData::INTERNAL_DATA) {
110+
continue;
111+
}
112+
109113
$this->filesService->initRestoringData($data);
110114
$this->filesService->fillRestoringData($data, $data->getUniqueFile());
111115

@@ -226,7 +230,7 @@ public function extractContentFromZip(ZipArchive $zip, string $file) {
226230
* @throws ArchiveNotFoundException
227231
*/
228232
public function verifyChecksum(Backup $backup, RestoringChunk $archive, bool $encrypted): bool {
229-
$sum = $this->getChecksum($backup, $archive, $encrypted);
233+
$sum = $this->getChecksum($backup, $archive);
230234

231235
if (!$encrypted && $sum === $archive->getChecksum()) {
232236
return true;
@@ -283,7 +287,7 @@ public function createContentChunk(
283287

284288
$this->createContentZip($point, $chunk, $filename, $content);
285289

286-
$this->updateChecksum($point, $chunk, false);
290+
$this->updateChecksum($point, $chunk);
287291
// $this->encryptArchive($backup, $archive, true);
288292
// $this->updateChecksum($backup, $archive, true);
289293
}
@@ -398,17 +402,12 @@ public function finalizeZip(ZipStreamer $zip, RestoringChunk $archive): void {
398402
/**
399403
* @param RestoringPoint $point
400404
* @param RestoringChunk $chunk
401-
* @param bool $encrypted
402405
*
403406
* @throws ArchiveNotFoundException
404407
*/
405-
private function updateChecksum(
406-
RestoringPoint $point,
407-
RestoringChunk $chunk,
408-
bool $encrypted
409-
): void {
410-
$sum = $this->getChecksum($point, $chunk, $encrypted);
411-
if ($encrypted) {
408+
private function updateChecksum(RestoringPoint $point, RestoringChunk $chunk): void {
409+
$sum = $this->getChecksum($point, $chunk);
410+
if ($chunk->isEncrypted()) {
412411
$chunk->setEncryptedChecksum($sum);
413412
} else {
414413
$chunk->setChecksum($sum);
@@ -419,33 +418,28 @@ private function updateChecksum(
419418
/**
420419
* @param RestoringPoint $point
421420
* @param RestoringChunk $chunk
422-
* @param bool $encrypted
423421
*
424422
* @return string
425423
* @throws ArchiveNotFoundException
426424
*/
427-
public function getChecksum(
428-
RestoringPoint $point,
429-
RestoringChunk $chunk,
430-
bool $encrypted
431-
): string {
425+
public function getChecksum(RestoringPoint $point, RestoringChunk $chunk): string {
432426
try {
433427
if ($point->isPackage()) {
434-
if (!file_exists('./' . $chunk->getName(($encrypted) ? '' : 'zip'))) {
428+
if (!file_exists('./' . $chunk->getFilename())) {
435429
throw new ArchiveNotFoundException('Archive not found');
436430
}
437-
$stream = fopen('./' . $chunk->getName(($encrypted) ? '' : 'zip'), 'r');
431+
$stream = fopen('./' . $chunk->getFilename(), 'r');
438432
} else {
439433
$folder = $point->getBaseFolder();
440-
$file = $folder->getFile($chunk->getName(($encrypted) ? '' : 'zip'));
434+
$file = $folder->getFile($chunk->getFilename());
441435
$stream = $file->read();
442436
}
443437
} catch (Exception $e) {
444-
throw new ArchiveNotFoundException('Chunk not found');
438+
throw new ArchiveNotFoundException('Chunk ' . $chunk->getFilename() . ' not found');
445439
}
446440

447441
if (is_bool($stream)) {
448-
throw new ArchiveNotFoundException('Chunk not found');
442+
throw new ArchiveNotFoundException('Chunk ' . $chunk->getFilename() . ' not valid');
449443
}
450444

451445
return $this->getChecksumFromStream($stream);
@@ -556,11 +550,12 @@ public function decryptArchive(Backup $backup, RestoringChunk $archive) {
556550
*
557551
* @throws BackupAppCopyException
558552
* @throws BackupScriptNotFoundException
553+
* @throws ArchiveNotFoundException
559554
*/
560555
public function copyApp(RestoringPoint $point): void {
561556
$folder = $point->getBaseFolder();
562557
try {
563-
$file = $folder->newFile('app.zip');
558+
$file = $folder->newFile(self::APP_ZIP);
564559
$zip = new ZipStreamer(
565560
[
566561
'outstream' => $file->write(),
@@ -569,9 +564,8 @@ public function copyApp(RestoringPoint $point): void {
569564
'level' => $this->assignCompressionLevel()
570565
]
571566
);
572-
573567
} catch (Exception $e) {
574-
throw new BackupAppCopyException('Could not generate app.zip');
568+
throw new BackupAppCopyException('Could not generate ' . self::APP_ZIP);
575569
}
576570

577571
$appFiles = $this->filesService->getFilesFromApp();
@@ -596,6 +590,30 @@ public function copyApp(RestoringPoint $point): void {
596590
}
597591

598592

593+
/**
594+
* @param RestoringPoint $point
595+
*
596+
* @throws ArchiveNotFoundException
597+
*/
598+
public function generateInternalData(RestoringPoint $point): void {
599+
$data = new RestoringData(
600+
RestoringData::INTERNAL_DATA,
601+
'',
602+
RestoringData::INTERNAL
603+
);
604+
605+
$chunk = new RestoringChunk(self::APP_ZIP);
606+
$this->updateChecksum($point, $chunk);
607+
$data->addChunk($chunk);
608+
609+
$chunk = new RestoringChunk(self::BACKUP_SCRIPT);
610+
$this->updateChecksum($point, $chunk);
611+
$data->addChunk($chunk);
612+
613+
$point->addRestoringData($data);
614+
}
615+
616+
599617
/**
600618
* @return int
601619
*/

lib/Service/PointService.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ public function getRPByInstance(string $instance): array {
156156
public function create(bool $complete): RestoringPoint {
157157
$point = $this->initRestoringPoint($complete);
158158
$this->chunkService->copyApp($point);
159+
$this->chunkService->generateInternalData($point);
159160

160161
// $backup->setEncryptionKey('12345');
161162
$this->chunkService->createChunks($point);
163+
162164
$this->backupSql($point);
163165
$this->saveMetadata($point);
164166

@@ -434,7 +436,7 @@ public function generateHealth(RestoringPoint $point, bool $updateDb = false): v
434436
*/
435437
private function generateChunkHealthStatus(RestoringPoint $point, RestoringChunk $chunk): int {
436438
try {
437-
$checksum = $this->chunkService->getChecksum($point, $chunk, false);
439+
$checksum = $this->chunkService->getChecksum($point, $chunk);
438440
if ($checksum !== $chunk->getChecksum()) {
439441
return RestoringChunkHealth::STATUS_CHECKSUM;
440442
}

0 commit comments

Comments
 (0)