Skip to content

Commit

Permalink
Merge pull request #6 from romainneutron/race-conditions
Browse files Browse the repository at this point in the history
Fix warnings and exceptions that could be thrown in some race conditions
  • Loading branch information
henrikbjorn committed Sep 22, 2013
2 parents 123f9fa + e3661f1 commit ee649c7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
18 changes: 13 additions & 5 deletions src/Lurker/Resource/DirectoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class DirectoryResource extends BaseDirectoryResource implements ResourceInterfa

public function exists()
{
clearstatcache(true, $this->getResource());

return is_dir($this);
}

Expand All @@ -21,11 +23,12 @@ public function getModificationTime()
return -1;
}

$resource = $this->getResource();
clearstatcache(true, $resource);
$newestMTime = filemtime($resource);
clearstatcache(true, $this->getResource());
if (false === $mtime = @filemtime($this->getResource())) {
return -1;
}

return $newestMTime;
return $mtime;
}

public function isFresh($timestamp)
Expand Down Expand Up @@ -65,7 +68,12 @@ public function getFilteredResources()
return array();
}

$iterator = new \DirectoryIterator($this->getResource());
// race conditions
try {
$iterator = new \DirectoryIterator($this->getResource());
} catch (\UnexpectedValueException $e) {
return array();
}

$resources = array();
foreach ($iterator as $file) {
Expand Down
12 changes: 7 additions & 5 deletions src/Lurker/Resource/FileResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ public function getModificationTime()
return -1;
}

$resource = $this->getResource();

clearstatcache(true, $resource);
clearstatcache(true, $this->getResource());
if (false === $mtime = @filemtime($this->getResource())) {
return -1;
}

return filemtime($resource);
return $mtime;
}

public function getId()
Expand All @@ -38,7 +39,8 @@ public function isFresh($timestamp)

public function exists()
{
clearstatcache(true, $this->getResource());

return is_file($this);
}

}

0 comments on commit ee649c7

Please sign in to comment.