Skip to content

Commit 295bc89

Browse files
cq-zlimingxinleosy-records
authored
Optimized FindDriver which can support alpine image. (#2611)
Co-authored-by: 李铭昕 <715557344@qq.com> Co-authored-by: sy-records <52o@qq52o.cn>
1 parent 499a499 commit 295bc89

1 file changed

Lines changed: 41 additions & 21 deletions

File tree

src/Driver/FindDriver.php

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,26 @@ class FindDriver implements DriverInterface
2727
/**
2828
* @var bool
2929
*/
30-
protected $isDarwin;
30+
protected $isDarwin = false;
31+
32+
/**
33+
* @var bool
34+
*/
35+
protected $isSupportFloatMinutes = true;
36+
37+
/**
38+
* @var int
39+
*/
40+
protected $startTime;
3141

3242
public function __construct(Option $option)
3343
{
3444
$this->option = $option;
35-
$this->isDarwin = PHP_OS === 'Darwin';
45+
if (PHP_OS === 'Darwin') {
46+
$this->isDarwin = true;
47+
} else {
48+
$this->isDarwin = false;
49+
}
3650
if ($this->isDarwin) {
3751
$ret = System::exec('which gfind');
3852
if (empty($ret['output'])) {
@@ -43,21 +57,27 @@ public function __construct(Option $option)
4357
if (empty($ret['output'])) {
4458
throw new \InvalidArgumentException('find not exists.');
4559
}
60+
$ret = System::exec('find --help', true);
61+
$this->isSupportFloatMinutes = (strpos($ret['output'] ?? '', 'BusyBox')) === false;
4662
}
4763
}
4864

4965
public function watch(Channel $channel): void
5066
{
67+
$this->startTime = time();
5168
$ms = $this->option->getScanInterval();
52-
Timer::tick($ms, function () use ($channel, $ms) {
69+
$seconds = ceil(($ms + 1000) / 1000);
70+
if ($this->isSupportFloatMinutes) {
71+
$minutes = sprintf('-%.2f', $seconds / 60);
72+
} else {
73+
$minutes = sprintf('-%d', ceil($seconds / 60));
74+
}
75+
Timer::tick($ms, function () use ($channel, $minutes) {
5376
global $fileModifyTimes;
5477
if (is_null($fileModifyTimes)) {
5578
$fileModifyTimes = [];
5679
}
5780

58-
$seconds = ceil(($ms + 1000) / 1000);
59-
$minutes = sprintf('-%.2f', $seconds / 60);
60-
6181
[$fileModifyTimes, $changedFiles] = $this->scan($fileModifyTimes, $minutes);
6282

6383
foreach ($changedFiles as $file) {
@@ -70,27 +90,27 @@ protected function find(array $fileModifyTimes, array $targets, string $minutes,
7090
{
7191
$changedFiles = [];
7292
$dest = implode(' ', $targets);
73-
$ret = System::exec($this->getBin() . ' ' . $dest . ' -mmin ' . $minutes . ' -type f -printf "%p %T+' . PHP_EOL . '"');
93+
$ret = System::exec($this->getBin() . ' ' . $dest . ' -mmin ' . $minutes . ' -type f -print');
7494
if ($ret['code'] === 0 && strlen($ret['output'])) {
7595
$stdout = $ret['output'];
7696

7797
$lineArr = explode(PHP_EOL, $stdout);
7898
foreach ($lineArr as $line) {
79-
$fileArr = explode(' ', $line);
80-
if (count($fileArr) == 2) {
81-
$pathName = $fileArr[0];
82-
$modifyTime = $fileArr[1];
83-
84-
if (! empty($ext) && ! Str::endsWith($pathName, $ext)) {
85-
continue;
86-
}
87-
88-
if (isset($fileModifyTimes[$pathName]) && $fileModifyTimes[$pathName] == $modifyTime) {
89-
continue;
90-
}
91-
$fileModifyTimes[$pathName] = $modifyTime;
92-
$changedFiles[] = $pathName;
99+
$pathName = $line;
100+
$modifyTime = fileatime($pathName);
101+
// modifyTime less than or equal to startTime continue
102+
if ($modifyTime <= $this->startTime) {
103+
continue;
104+
}
105+
if (! empty($ext) && ! Str::endsWith($pathName, $ext)) {
106+
continue;
107+
}
108+
109+
if (isset($fileModifyTimes[$pathName]) && $fileModifyTimes[$pathName] == $modifyTime) {
110+
continue;
93111
}
112+
$fileModifyTimes[$pathName] = $modifyTime;
113+
$changedFiles[] = $pathName;
94114
}
95115
}
96116

0 commit comments

Comments
 (0)