Skip to content

Commit afe61fe

Browse files
authored
Merge pull request #60286 from nextcloud/fix/noid/appstore-fetcher-generic-file-exception
fix(AppStore/Fetcher): catch GenericFileException when reading cache file in Fetcher
2 parents 69d0b7e + c4356e1 commit afe61fe

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

lib/private/App/AppStore/Fetcher/Fetcher.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OC\Files\AppData\Factory;
1313
use OCP\AppFramework\Http;
1414
use OCP\AppFramework\Utility\ITimeFactory;
15+
use OCP\Files\GenericFileException;
1516
use OCP\Files\IAppData;
1617
use OCP\Files\NotFoundException;
1718
use OCP\Http\Client\IClientService;
@@ -167,7 +168,16 @@ public function get($allowUnstable = false): array {
167168
}
168169
}
169170
} catch (NotFoundException $e) {
170-
// File does not already exists
171+
// File does not already exist
172+
$file = $rootFolder->newFile($this->fileName);
173+
} catch (GenericFileException $e) {
174+
try {
175+
$file->delete();
176+
} catch (\Exception) {
177+
$this->logger->error('Could not read appstore cache file', ['app' => 'appstoreFetcher', 'exception' => $e]);
178+
return [];
179+
}
180+
$this->logger->warning('Could not read appstore cache file, it will be refreshed', ['app' => 'appstoreFetcher', 'exception' => $e]);
171181
$file = $rootFolder->newFile($this->fileName);
172182
}
173183

tests/lib/App/AppStore/Fetcher/FetcherBase.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OC\Files\AppData\AppData;
1414
use OC\Files\AppData\Factory;
1515
use OCP\AppFramework\Utility\ITimeFactory;
16+
use OCP\Files\GenericFileException;
1617
use OCP\Files\IAppData;
1718
use OCP\Files\NotFoundException;
1819
use OCP\Files\SimpleFS\ISimpleFile;
@@ -684,4 +685,89 @@ public function testFetchAfterUpgradeNoETag(): void {
684685
];
685686
$this->assertSame($expected, $this->fetcher->get());
686687
}
688+
689+
public function testGetWithUnreadableCacheFileRecreatesAndFetches(): void {
690+
$this->config
691+
->method('getSystemValueString')
692+
->willReturnCallback(function ($var, $default) {
693+
if ($var === 'appstoreurl') {
694+
return 'https://apps.nextcloud.com/api/v1';
695+
} elseif ($var === 'version') {
696+
return '11.0.0.2';
697+
}
698+
return $default;
699+
});
700+
$this->config->method('getSystemValueBool')
701+
->willReturn(true);
702+
703+
$folder = $this->createMock(ISimpleFolder::class);
704+
$corruptedFile = $this->createMock(ISimpleFile::class);
705+
$freshFile = $this->createMock(ISimpleFile::class);
706+
$this->appData
707+
->expects($this->once())
708+
->method('getFolder')
709+
->with('/')
710+
->willReturn($folder);
711+
$folder
712+
->expects($this->once())
713+
->method('getFile')
714+
->with($this->fileName)
715+
->willReturn($corruptedFile);
716+
$corruptedFile
717+
->expects($this->once())
718+
->method('getContent')
719+
->willThrowException(new GenericFileException());
720+
$corruptedFile
721+
->expects($this->once())
722+
->method('delete');
723+
$folder
724+
->expects($this->once())
725+
->method('newFile')
726+
->with($this->fileName)
727+
->willReturn($freshFile);
728+
729+
$client = $this->createMock(IClient::class);
730+
$this->clientService
731+
->expects($this->once())
732+
->method('newClient')
733+
->willReturn($client);
734+
$response = $this->createMock(IResponse::class);
735+
$client
736+
->expects($this->once())
737+
->method('get')
738+
->with($this->endpoint)
739+
->willReturn($response);
740+
$response
741+
->expects($this->once())
742+
->method('getBody')
743+
->willReturn('[{"id":"MyNewApp", "foo": "foo"}, {"id":"bar"}]');
744+
$response->method('getHeader')
745+
->with($this->equalTo('ETag'))
746+
->willReturn('"myETag"');
747+
748+
$fileData = '{"data":[{"id":"MyNewApp","foo":"foo"},{"id":"bar"}],"timestamp":1502,"ncversion":"11.0.0.2","ETag":"\"myETag\""}';
749+
$freshFile
750+
->expects($this->once())
751+
->method('putContent')
752+
->with($fileData);
753+
$freshFile
754+
->expects($this->once())
755+
->method('getContent')
756+
->willReturn($fileData);
757+
$this->timeFactory
758+
->expects($this->once())
759+
->method('getTime')
760+
->willReturn(1502);
761+
762+
$expected = [
763+
[
764+
'id' => 'MyNewApp',
765+
'foo' => 'foo',
766+
],
767+
[
768+
'id' => 'bar',
769+
],
770+
];
771+
$this->assertSame($expected, $this->fetcher->get());
772+
}
687773
}

0 commit comments

Comments
 (0)