Commit a0faf495 authored by Manfred Kutas's avatar Manfred Kutas

ZP-1261 Reduce amount of repeated glob calls.

Released under the Affero GNU General Public License (AGPL) version 3.
parent c9c700d9
...@@ -33,7 +33,8 @@ class FileStateMachine implements IStateMachine { ...@@ -33,7 +33,8 @@ class FileStateMachine implements IStateMachine {
private $userfilename; private $userfilename;
private $settingsfilename; private $settingsfilename;
private $statefiles; // List of the state files. Used by z-push-admin and scripts.
private $devicedatafiles; // List of the device data files. Used by z-push-admin and scripts.
/** /**
* Constructor * Constructor
* *
...@@ -59,6 +60,9 @@ class FileStateMachine implements IStateMachine { ...@@ -59,6 +60,9 @@ class FileStateMachine implements IStateMachine {
if ((!file_exists($this->userfilename) && !touch($this->userfilename)) || !is_writable($this->userfilename)) if ((!file_exists($this->userfilename) && !touch($this->userfilename)) || !is_writable($this->userfilename))
throw new FatalMisconfigurationException("Not possible to write to the configured state directory."); throw new FatalMisconfigurationException("Not possible to write to the configured state directory.");
Utils::FixFileOwner($this->userfilename); Utils::FixFileOwner($this->userfilename);
$this->statefiles = array();
$this->devicedatafiles = array();
} }
/** /**
...@@ -160,7 +164,8 @@ class FileStateMachine implements IStateMachine { ...@@ -160,7 +164,8 @@ class FileStateMachine implements IStateMachine {
* @throws StateInvalidException * @throws StateInvalidException
*/ */
public function CleanStates($devid, $type, $key, $counter = false, $thisCounterOnly = false) { public function CleanStates($devid, $type, $key, $counter = false, $thisCounterOnly = false) {
$matching_files = glob($this->getFullFilePath($devid, $type, $key). "*", GLOB_NOSORT); $filePath = $this->getFullFilePath($devid, $type, $key);
$matching_files = array_filter($this->getStateFiles(), function($var) use ($filePath) {return strpos($var, $filePath) !== false;});
if (is_array($matching_files)) { if (is_array($matching_files)) {
foreach($matching_files as $state) { foreach($matching_files as $state) {
$file = false; $file = false;
...@@ -287,7 +292,8 @@ class FileStateMachine implements IStateMachine { ...@@ -287,7 +292,8 @@ class FileStateMachine implements IStateMachine {
public function GetAllDevices($username = false) { public function GetAllDevices($username = false) {
$out = array(); $out = array();
if ($username === false) { if ($username === false) {
foreach (glob(STATE_DIR. "/*/*/*-".IStateMachine::DEVICEDATA, GLOB_NOSORT) as $devdata) foreach ($this->getDeviceDataFiles() as $devdata)
// TODO do we still need this check here?
if (preg_match('/\/([A-Za-z0-9]+)-'. IStateMachine::DEVICEDATA. '$/', $devdata, $matches)) if (preg_match('/\/([A-Za-z0-9]+)-'. IStateMachine::DEVICEDATA. '$/', $devdata, $matches))
$out[] = $matches[1]; $out[] = $matches[1];
return $out; return $out;
...@@ -368,8 +374,8 @@ class FileStateMachine implements IStateMachine { ...@@ -368,8 +374,8 @@ class FileStateMachine implements IStateMachine {
$types = array(IStateMachine::DEVICEDATA, IStateMachine::FOLDERDATA, IStateMachine::FAILSAVE, IStateMachine::HIERARCHY, IStateMachine::BACKENDSTORAGE); $types = array(IStateMachine::DEVICEDATA, IStateMachine::FOLDERDATA, IStateMachine::FAILSAVE, IStateMachine::HIERARCHY, IStateMachine::BACKENDSTORAGE);
$out = array(); $out = array();
$devdir = $this->getDirectoryForDevice($devid) . "/$devid-"; $devdir = $this->getDirectoryForDevice($devid) . "/$devid-";
$deviceFiles = array_filter($this->getStateFiles(), function($var) use ($devdir) {return strpos($var, $devdir) !== false;});
foreach (glob($devdir . "*", GLOB_NOSORT) as $devdata) { foreach ($deviceFiles as $devdata) {
// cut the device dir away and split into parts // cut the device dir away and split into parts
$parts = explode("-", substr($devdata, strlen($devdir))); $parts = explode("-", substr($devdata, strlen($devdir)));
...@@ -379,11 +385,10 @@ class FileStateMachine implements IStateMachine { ...@@ -379,11 +385,10 @@ class FileStateMachine implements IStateMachine {
if (isset($parts[0]) && in_array($parts[0], $types)) if (isset($parts[0]) && in_array($parts[0], $types))
$state['type'] = $parts[0]; $state['type'] = $parts[0];
if (isset($parts[0]) && strlen($parts[0]) == 8 && if (isset($parts[0], $parts[1], $parts[2], $parts[3], $parts[4]) &&
isset($parts[1]) && strlen($parts[1]) == 4 && strlen($parts[0]) == 8 && strlen($parts[1]) == 4 &&
isset($parts[2]) && strlen($parts[2]) == 4 && strlen($parts[2]) == 4 && strlen($parts[3]) == 4 &&
isset($parts[3]) && strlen($parts[3]) == 4 && strlen($parts[4]) == 12) {
isset($parts[4]) && strlen($parts[4]) == 12) {
$state['uuid'] = $parts[0]."-".$parts[1]."-".$parts[2]."-".$parts[3]."-".$parts[4]; $state['uuid'] = $parts[0]."-".$parts[1]."-".$parts[2]."-".$parts[3]."-".$parts[4];
} }
...@@ -482,4 +487,29 @@ class FileStateMachine implements IStateMachine { ...@@ -482,4 +487,29 @@ class FileStateMachine implements IStateMachine {
return false; return false;
} }
/**
* Returns the list of the state files.
*
* @access public
* @return array
*/
protected function getStateFiles() {
if (empty($this->statefiles)) {
$this->statefiles = glob(STATE_DIR. '*/*/*', GLOB_NOSORT);
}
return $this->statefiles;
}
/**
* Filters the list of the state files and returns the device data files only.
*
* @access public
* @return array
*/
protected function getDeviceDataFiles() {
if (empty($this->devicedatafiles)) {
$this->devicedatafiles = array_filter($this->GetStateFiles(), function($var) {return strpos($var, IStateMachine::DEVICEDATA) !== false;} );
}
return $this->devicedatafiles;
}
} }
...@@ -32,6 +32,7 @@ class ZPushAdmin { ...@@ -32,6 +32,7 @@ class ZPushAdmin {
const STATUS_DEVICE_SYNCED_AFTER_DAYSOLD = 1; const STATUS_DEVICE_SYNCED_AFTER_DAYSOLD = 1;
public static $status = self::STATUS_SUCCESS; public static $status = self::STATUS_SUCCESS;
public static $devices;
/** /**
* List devices known to Z-Push. * List devices known to Z-Push.
...@@ -773,7 +774,7 @@ class ZPushAdmin { ...@@ -773,7 +774,7 @@ class ZPushAdmin {
$dropedUsers = 0; $dropedUsers = 0;
$fixedUsers = 0; $fixedUsers = 0;
$devices = ZPush::GetStateMachine()->GetAllDevices(false); $devices = self::GetAllDevices();
foreach ($devices as $devid) { foreach ($devices as $devid) {
$users = self::ListUsers($devid); $users = self::ListUsers($devid);
$obsoleteUsers = array(); $obsoleteUsers = array();
...@@ -842,7 +843,7 @@ class ZPushAdmin { ...@@ -842,7 +843,7 @@ class ZPushAdmin {
static public function FixStatesDeviceToUserLinking() { static public function FixStatesDeviceToUserLinking() {
$seen = 0; $seen = 0;
$fixed = 0; $fixed = 0;
$devices = ZPush::GetStateMachine()->GetAllDevices(false); $devices = self::GetAllDevices();
ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::FixStatesDeviceToUserLinking(): found %d devices", count($devices))); ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::FixStatesDeviceToUserLinking(): found %d devices", count($devices)));
foreach ($devices as $devid) { foreach ($devices as $devid) {
...@@ -868,7 +869,7 @@ class ZPushAdmin { ...@@ -868,7 +869,7 @@ class ZPushAdmin {
static public function FixStatesUserToStatesLinking() { static public function FixStatesUserToStatesLinking() {
$processed = 0; $processed = 0;
$deleted = 0; $deleted = 0;
$devices = ZPush::GetStateMachine()->GetAllDevices(false); $devices = self::GetAllDevices();
ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::FixStatesUserToStatesLinking(): found %d devices", count($devices))); ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::FixStatesUserToStatesLinking(): found %d devices", count($devices)));
foreach ($devices as $devid) { foreach ($devices as $devid) {
...@@ -902,7 +903,7 @@ class ZPushAdmin { ...@@ -902,7 +903,7 @@ class ZPushAdmin {
$existingStates = ZPush::GetStateMachine()->GetAllStatesForDevice($devid); $existingStates = ZPush::GetStateMachine()->GetAllStatesForDevice($devid);
$processed = count($existingStates); $processed = count($existingStates);
ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::FixStatesUserToStatesLinking(): found %d valid uuids and %d states for device device '%s'", count($knownUuids), $processed, $devid)); ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::FixStatesUserToStatesLinking(): found %d valid uuids and %d states for device '%s'", count($knownUuids), $processed, $devid));
// remove states for all unknown uuids // remove states for all unknown uuids
foreach ($existingStates as $obsoleteState) { foreach ($existingStates as $obsoleteState) {
...@@ -932,7 +933,7 @@ class ZPushAdmin { ...@@ -932,7 +933,7 @@ class ZPushAdmin {
$seen = 0; $seen = 0;
$nouuid = 0; $nouuid = 0;
$fixed = 0; $fixed = 0;
$asdevices = ZPush::GetStateMachine()->GetAllDevices(false); $asdevices = self::GetAllDevices();
ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::FixStatesHierarchyFolderData(): found %d devices", count($asdevices))); ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::FixStatesHierarchyFolderData(): found %d devices", count($asdevices)));
foreach ($asdevices as $devid) { foreach ($asdevices as $devid) {
...@@ -1008,7 +1009,7 @@ class ZPushAdmin { ...@@ -1008,7 +1009,7 @@ class ZPushAdmin {
$devices = 0; $devices = 0;
$devicesWithAddFolders = 0; $devicesWithAddFolders = 0;
$fixed = 0; $fixed = 0;
$asdevices = ZPush::GetStateMachine()->GetAllDevices(false); $asdevices = self::GetAllDevices();
ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::FixStatesAdditionalFolders(): found %d devices", count($asdevices))); ZLog::Write(LOGLEVEL_DEBUG, sprintf("ZPushAdmin::FixStatesAdditionalFolders(): found %d devices", count($asdevices)));
foreach ($asdevices as $devid) { foreach ($asdevices as $devid) {
...@@ -1055,5 +1056,17 @@ class ZPushAdmin { ...@@ -1055,5 +1056,17 @@ class ZPushAdmin {
return array($devices, $devicesWithAddFolders, $fixed); return array($devices, $devicesWithAddFolders, $fixed);
} }
/**
* Returns the list of all devices.
*
* @access public
* @return array
*/
public static function GetAllDevices() {
if (empty(self::$devices)) {
self::$devices = ZPush::GetStateMachine()->GetAllDevices(false);
}
return self::$devices;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment