nextcloud/lib/public/files/lock.php

94 lines
2.4 KiB
PHP
Raw Normal View History

2014-05-21 01:44:57 +04:00
<?php
/**
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\Files;
2014-05-23 18:29:37 +04:00
use OC\Files\Filesystem;
2014-05-21 01:44:57 +04:00
/**
* Class Lock
* @package OC\Files
*/
class Lock {
const READ = 1;
const WRITE = 2;
/** @var string $path Filename of the file as represented in storage */
protected $path;
2014-05-23 18:29:37 +04:00
/** @var array $stack A stack of lock data */
protected $stack = array();
/** @var int $retries Number of lock retries to attempt */
public static $retries = 40;
/** @var int $retryInterval Milliseconds between retries */
public static $retryInterval = 50;
/**
* Constructor for the lock instance
* @param string $path Absolute pathname for a local file on which to obtain a lock
*/
2014-05-21 01:44:57 +04:00
public function __construct($path) {
2014-05-23 18:29:37 +04:00
$this->path = Filesystem::normalizePath($path);
2014-05-21 01:44:57 +04:00
}
2014-05-23 18:29:37 +04:00
/**
* @param integer $lockType A constant representing the type of lock to queue
*/
2014-05-21 01:44:57 +04:00
public function addLock($lockType) {
2014-05-23 18:29:37 +04:00
\OC_Log::write('lock', sprintf('INFO: Lock type %d requested for %s', $lockType, $this->path), \OC_Log::DEBUG);
$timeout = self::$retries;
if(!isset($this->stack[$lockType])) {
// does lockfile exist?
// yes
// Acquire exclusive lock on lockfile?
// yes
// Delete lockfile, release lock
// no
// Sleep for configurable milliseconds - start over
// no
// Acquire shared lock on original file?
// yes
// Capture handle, return for action
// no
// Sleep for configurable milliseconds - start over
$handle = 1;
$this->stack[$lockType] = array('handle' => $handle, 'count' => 0);
}
$this->stack[$lockType]['count']++;
2014-05-21 01:44:57 +04:00
}
/**
* Release locks on handles and files
*/
public function release($lockType) {
return true;
}
2014-05-23 18:29:37 +04:00
/**
* Release all queued locks on the file
* @return bool
*/
public function releaseAll() {
return true;
}
2014-05-21 01:44:57 +04:00
}