From 14d802b8f36ed8dbf1621050bc704cfac821339f Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Sun, 7 Oct 2018 18:12:38 +0200 Subject: [PATCH 1/3] Refactor method to throw exception instead of true/false Signed-off-by: Daniel Kesselberg --- core/Command/Base.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/core/Command/Base.php b/core/Command/Base.php index dbf6c71b8f4..0695582ef50 100644 --- a/core/Command/Base.php +++ b/core/Command/Base.php @@ -129,15 +129,19 @@ class Base extends Command implements CompletionAwareInterface { } /** - * @return bool + * Throw InterruptedException when interrupted by user + * + * @throws InterruptedException */ protected function hasBeenInterrupted() { - // return always false if pcntl_signal functions are not accessible - if ($this->php_pcntl_signal) { - pcntl_signal_dispatch(); - return $this->interrupted; - } else { - return false; + if ($this->php_pcntl_signal === false) { + return; + } + + pcntl_signal_dispatch(); + + if ($this->interrupted === true) { + throw new InterruptedException('Command interrupted by user'); } } From 752f63720791383ff656c599a0f67d94e1ce3cdd Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Sun, 7 Oct 2018 18:17:29 +0200 Subject: [PATCH 2/3] Adjust code for changed method signature Signed-off-by: Daniel Kesselberg --- apps/files/lib/Command/Scan.php | 26 ++++++++++---------------- apps/files/lib/Command/ScanAppData.php | 20 ++++++-------------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/apps/files/lib/Command/Scan.php b/apps/files/lib/Command/Scan.php index 36fa39e043d..9d0dc266c5f 100644 --- a/apps/files/lib/Command/Scan.php +++ b/apps/files/lib/Command/Scan.php @@ -129,33 +129,25 @@ class Scan extends Base { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $output->writeln("\tFile $path"); $this->filesCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new InterruptedException(); - } + $this->hasBeenInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { $output->writeln("\tFolder $path"); $this->foldersCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new InterruptedException(); - } + $this->hasBeenInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { - $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); + $output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')'); }); # count only } else { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { $this->filesCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new InterruptedException(); - } + $this->hasBeenInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { $this->foldersCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new InterruptedException(); - } + $this->hasBeenInterrupted(); }); } $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { @@ -173,7 +165,7 @@ class Scan extends Base { } } catch (ForbiddenException $e) { $output->writeln("Home storage for user $user not writable"); - $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); + $output->writeln('Make sure you\'re running the scan command only as the user the web server runs as'); } catch (InterruptedException $e) { # exit the function if ctrl-c has been pressed $output->writeln('Interrupted by user'); @@ -250,8 +242,10 @@ class Scan extends Base { } else { $output->writeln("Unknown user $user_count $user"); } - # check on each user if there was a user interrupt (ctrl-c) and exit foreach - if ($this->hasBeenInterrupted()) { + + try { + $this->hasBeenInterrupted(); + } catch(InterruptedException $e) { break; } } diff --git a/apps/files/lib/Command/ScanAppData.php b/apps/files/lib/Command/ScanAppData.php index 1eb22d5f68a..24944dad64a 100644 --- a/apps/files/lib/Command/ScanAppData.php +++ b/apps/files/lib/Command/ScanAppData.php @@ -102,33 +102,25 @@ class ScanAppData extends Base { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $output->writeln("\tFile $path"); $this->filesCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new InterruptedException(); - } + $this->hasBeenInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { $output->writeln("\tFolder $path"); $this->foldersCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new InterruptedException(); - } + $this->hasBeenInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { - $output->writeln("Error while scanning, storage not available (" . $e->getMessage() . ")"); + $output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')'); }); # count only } else { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { $this->filesCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new InterruptedException(); - } + $this->hasBeenInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { $this->foldersCounter += 1; - if ($this->hasBeenInterrupted()) { - throw new InterruptedException(); - } + $this->hasBeenInterrupted(); }); } $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { @@ -142,7 +134,7 @@ class ScanAppData extends Base { $scanner->scan($appData->getPath()); } catch (ForbiddenException $e) { $output->writeln("Storage not writable"); - $output->writeln("Make sure you're running the scan command only as the user the web server runs as"); + $output->writeln('Make sure you\'re running the scan command only as the user the web server runs as'); } catch (InterruptedException $e) { # exit the function if ctrl-c has been pressed $output->writeln('Interrupted by user'); From 311de17730174dae1f951bdf38a657e6c5453574 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Mon, 8 Oct 2018 13:05:00 +0200 Subject: [PATCH 3/3] Rename method to abortIfInterrupted Signed-off-by: Daniel Kesselberg --- apps/files/lib/Command/Scan.php | 10 +++++----- apps/files/lib/Command/ScanAppData.php | 8 ++++---- core/Command/Base.php | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/apps/files/lib/Command/Scan.php b/apps/files/lib/Command/Scan.php index 9d0dc266c5f..a50d9abfc3d 100644 --- a/apps/files/lib/Command/Scan.php +++ b/apps/files/lib/Command/Scan.php @@ -129,12 +129,12 @@ class Scan extends Base { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $output->writeln("\tFile $path"); $this->filesCounter += 1; - $this->hasBeenInterrupted(); + $this->abortIfInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { $output->writeln("\tFolder $path"); $this->foldersCounter += 1; - $this->hasBeenInterrupted(); + $this->abortIfInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { $output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')'); @@ -143,11 +143,11 @@ class Scan extends Base { } else { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { $this->filesCounter += 1; - $this->hasBeenInterrupted(); + $this->abortIfInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { $this->foldersCounter += 1; - $this->hasBeenInterrupted(); + $this->abortIfInterrupted(); }); } $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { @@ -244,7 +244,7 @@ class Scan extends Base { } try { - $this->hasBeenInterrupted(); + $this->abortIfInterrupted(); } catch(InterruptedException $e) { break; } diff --git a/apps/files/lib/Command/ScanAppData.php b/apps/files/lib/Command/ScanAppData.php index 24944dad64a..988bcd1e62f 100644 --- a/apps/files/lib/Command/ScanAppData.php +++ b/apps/files/lib/Command/ScanAppData.php @@ -102,12 +102,12 @@ class ScanAppData extends Base { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) { $output->writeln("\tFile $path"); $this->filesCounter += 1; - $this->hasBeenInterrupted(); + $this->abortIfInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) { $output->writeln("\tFolder $path"); $this->foldersCounter += 1; - $this->hasBeenInterrupted(); + $this->abortIfInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) { $output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')'); @@ -116,11 +116,11 @@ class ScanAppData extends Base { } else { $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function () use ($output) { $this->filesCounter += 1; - $this->hasBeenInterrupted(); + $this->abortIfInterrupted(); }); $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function () use ($output) { $this->foldersCounter += 1; - $this->hasBeenInterrupted(); + $this->abortIfInterrupted(); }); } $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) { diff --git a/core/Command/Base.php b/core/Command/Base.php index 0695582ef50..4eca5bcaab7 100644 --- a/core/Command/Base.php +++ b/core/Command/Base.php @@ -133,7 +133,7 @@ class Base extends Command implements CompletionAwareInterface { * * @throws InterruptedException */ - protected function hasBeenInterrupted() { + protected function abortIfInterrupted() { if ($this->php_pcntl_signal === false) { return; }