protected function _getCmd() { $exclude = $this->_excludePatterns; foreach ($exclude as &$e) { $e = '*' . $e; } $cmd = "watchmedo log --recursive --ignore-directories "; if ($exclude) { $cmd .= "--ignore-patterns " . escapeshellarg(implode(';', $exclude)) . ' '; } $paths = $this->_paths; if ($this->_followLinks) { //watchmedo doesn't recurse into symlinks //so we add all symlinks to $paths $paths = LinksHelper::followLinks($paths, $this->_excludePatterns); } $cmd .= implode(' ', $paths); if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { //disble output bufferering $cmd = "PYTHONUNBUFFERED=1 {$cmd}"; } else { //on windows disable output buffering using -u //the above doesn't work $cmd = "python -u -m watchdog.{$cmd}"; } return $cmd; }
protected function _getCmd() { $excludeRegEx = array(); foreach ($this->_excludePatterns as $e) { if (substr($e, -1) == '*') { $e = substr($e, 0, -1); } //not needed $excludeRegEx[] = str_replace(array('.', '*'), array('\\.', '.*'), $e); } $excludeRegEx = implode('|', $excludeRegEx); $cmd = "inotifywait -e modify -e create -e delete -e move -e moved_to -e moved_from -e attrib -r --monitor "; if ($excludeRegEx) { $cmd .= "--exclude '{$excludeRegEx}' "; } $paths = $this->_paths; if ($this->_followLinks) { //inotifywait doesn't recurse into symlinks //so we add all symlinks to $paths $paths = LinksHelper::followLinks($paths, $this->_excludePatterns); } $cmd .= implode(' ', $paths); return $cmd; }
public function start() { $paths = $this->_paths; if ($this->_followLinks) { $paths = LinksHelper::followLinks($paths, $this->_excludePatterns); } $this->_fd = inotify_init(); $finder = new Finder(); $finder->directories(); foreach ($this->_excludePatterns as $excludePattern) { $finder->notName($excludePattern); } foreach ($paths as $p) { $finder->in($p); } $this->_watches = array(); foreach ($paths as $p) { $this->_addWatch($p); } foreach ($finder as $f) { $this->_addWatch($f->__toString()); } $this->_logger->info("Watches set up..."); $read = array($this->_fd); $write = null; $except = null; stream_select($read, $write, $except, 0); stream_set_blocking($this->_fd, 0); $events = array(); while (!$this->_stopped) { while ($inotifyEvents = inotify_read($this->_fd)) { foreach ($inotifyEvents as $details) { $file = $this->_watches[$details['wd']]; if ($details['name']) { $file .= '/' . $details['name']; } if ($details['mask'] & IN_MODIFY || $details['mask'] & IN_ATTRIB) { $events[] = new ModifyEvent($file); } if ($details['mask'] & IN_CREATE) { $events[] = new CreateEvent($file); } if ($details['mask'] & IN_DELETE) { $events[] = new DeleteEvent($file); } if ($details['mask'] & IN_MOVED_FROM) { $this->_previousMoveFromFile = $file; } if ($details['mask'] & IN_MOVED_TO) { if (!isset($this->_previousMoveFromFile)) { $this->_logger->error('MOVED_FROM event is not followed by a MOVED_TO'); } else { $events[] = new MoveEvent($this->_previousMoveFromFile, $file); unset($this->_previousMoveFromFile); } } if ($details['mask'] & IN_DELETE_SELF) { unset($this->_watches[$details['wd']]); } } } $events = $this->_compressEvents($events); if ($this->_queueSizeLimit && count($events) > $this->_queueSizeLimit) { $this->_dispatchEvent(QueueFullEvent::NAME, new QueueFullEvent($events)); $events = array(); } foreach ($events as $event) { $name = call_user_func(array(get_class($event), 'getEventName')); $this->_dispatchEvent($name, $event); } usleep(100 * 1000); } foreach ($this->_watches as $wd => $path) { inotify_rm_watch($this->_fd, (int) $wd); } fclose($this->_fd); return; }