Skip to content

Commit 42a11c3

Browse files
authored
Merge pull request #60594 from nextcloud/backport/60286/stable32
[stable32] fix(AppStore/Fetcher): catch GenericFileException when reading cache file in Fetcher
2 parents 26f9a08 + b06d294 commit 42a11c3

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;
@@ -161,7 +162,16 @@ public function get($allowUnstable = false) {
161162
}
162163
}
163164
} catch (NotFoundException $e) {
164-
// File does not already exists
165+
// File does not already exist
166+
$file = $rootFolder->newFile($this->fileName);
167+
} catch (GenericFileException $e) {
168+
try {
169+
$file->delete();
170+
} catch (\Exception) {
171+
$this->logger->error('Could not read appstore cache file', ['app' => 'appstoreFetcher', 'exception' => $e]);
172+
return [];
173+
}
174+
$this->logger->warning('Could not read appstore cache file, it will be refreshed', ['app' => 'appstoreFetcher', 'exception' => $e]);
165175
$file = $rootFolder->newFile($this->fileName);
166176
}
167177

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

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

0 commit comments

Comments
 (0)