Skip to content

Commit f8dc36a

Browse files
authored
Upd. Scan. Improve Surface flow to count files. (#652)
* Upd. Scan. Improve Surface flow to count files. * Upd. Scan. Improve Surface flow to count files. * Upd. Scan. Improve Surface flow to count files. * Upd. Scan. Improve Surface flow to count files.
1 parent 8671729 commit f8dc36a

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

lib/CleantalkSP/SpbctWP/Scanner/ScannerQueue.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,9 @@ public function signature_analysis($status = 'UNKNOWN,MODIFIED,OK,INFECTED,ERROR
15521552
}
15531553
}
15541554

1555+
if ( ! isset($spbc->data['scanner']['scanned_total']) ) {
1556+
$spbc->data['scanner']['scanned_total'] = 0;
1557+
}
15551558
$spbc->data['scanner']['scanned_total'] += $scanned;
15561559
$spbc->save('data');
15571560

lib/CleantalkSP/SpbctWP/Scanner/Surface.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -601,26 +601,25 @@ public function getFileStructure($main_path, $iterator_result, $is_root_dir)
601601
return $iterator_result;
602602
}
603603

604-
try {
605-
$it = new \FilesystemIterator(
606-
$main_path,
607-
\FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::KEY_AS_FILENAME
608-
);
604+
if ( ! is_readable($main_path) ) {
605+
return $iterator_result;
606+
}
609607

610-
if (!$is_root_dir) {
611-
$iterator_last_dir_offset = 0;
608+
try {
609+
$scan = @scandir($main_path, SCANDIR_SORT_NONE);
610+
if ( $scan === false ) {
611+
return $iterator_result;
612612
}
613+
sort($scan, SORT_STRING);
613614

614-
foreach ( $it as $file_name => $path ) {
615-
if ( ! $path ) {
616-
continue;
617-
}
615+
$iterator_last_dir_offset = 0;
618616

619-
if ( $file_name === ".." || $file_name === "." ) {
617+
foreach ( $scan as $file_name ) {
618+
if ( $file_name === '..' || $file_name === '.' ) {
620619
continue;
621620
}
622621

623-
$path = (string) $path;
622+
$path = $main_path . DIRECTORY_SEPARATOR . $file_name;
624623

625624
if ( is_file($path) && !is_link($path) ) {
626625
// Extensions filter
@@ -658,21 +657,19 @@ public function getFileStructure($main_path, $iterator_result, $is_root_dir)
658657

659658
$this->output_counter_files++;
660659

661-
// Skip if start is not reached for inner last dir
662-
if ( !$is_root_dir ) {
663-
$iterator_last_dir_offset++;
664-
if ( $it->getPath() === $this->last_dir_on_exit && $iterator_last_dir_offset - 1 < $this->output_files_offset ) {
665-
continue;
666-
}
660+
// Resume in this directory (root or subdir): skip first on_exit_dir_offset matching files.
661+
$iterator_last_dir_offset++;
662+
if ( $main_path === $this->last_dir_on_exit && $iterator_last_dir_offset - 1 < $this->output_files_offset ) {
663+
continue;
667664
}
668665

669666
$this->output_files[]['path'] = $path;
670667

671668
// Return if file limit is reached
672669
if ($this->output_counter_files >= $this->output_files_maximum ) {
673670
$iterator_result->max_files_interrupt = true;
674-
$iterator_result->on_exit_dir_path = $it->getPath();
675-
$iterator_result->on_exit_dir_offset = isset($iterator_last_dir_offset) ? $iterator_last_dir_offset : 0;
671+
$iterator_result->on_exit_dir_path = $main_path;
672+
$iterator_result->on_exit_dir_offset = $iterator_last_dir_offset;
676673
return $iterator_result;
677674
}
678675
} elseif ( is_dir($path) && !is_link($path) ) {

0 commit comments

Comments
 (0)