1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
<?php
/**
* Copyright (c) 2013 ownCloud, Inc.
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC\Files\Storage\Wrapper;
use OC\Files\Filesystem;
use OCP\Files\LockNotAcquiredException;
use OCP\Files\Lock;
/**
* Class LockingWrapper
* A Storage Wrapper used to lock files at the system level
* @package OC\Files\Storage\Wrapper
*
* Notes: Does the $locks array need to be global to all LockingWrapper instances, such as in the case of two paths
* that point to the same physical file? Otherwise accessing the file from a different path the second time would show
* the file as locked, even though this process is the one locking it.
*/
class LockingWrapper extends Wrapper {
/** @var array $locks Holds an array of lock instances indexed by path for this storage */
protected $locks = array();
/**
* Acquire a lock on a file
* @param string $path Path to file, relative to this storage
* @param integer $lockType A Lock class constant, Lock::READ/Lock::WRITE
* @return bool|\OCP\Files\Lock Lock instance on success, false on failure
*/
protected function getLock($path, $lockType){
$path = Filesystem::normalizePath($path);
if(!isset($this->locks[$path])) {
$this->locks[$path] = new Lock($path);
}
$this->locks[$path]->addLock($lockType);
return $this->locks[$path];
}
/**
* Release an existing lock
* @param string $path Path to file, relative to this storage
* @return bool true on success, false on failure
*/
protected function releaseLock($path, $lockType, $releaseAll = false){
$path = Filesystem::normalizePath($path);
if(isset($this->locks[$path])) {
if($releaseAll) {
return $this->locks[$path]->releaseAll();
}
else {
return $this->locks[$path]->release($lockType);
}
}
return true;
}
/**
* see http://php.net/manual/en/function.file_get_contents.php
* @param string $path
* @return string
* @throws \Exception
*/
public function file_get_contents($path) {
try {
if (!$this->getLock($path, Lock::READ)) {
throw new LockNotAcquiredException($path, Lock::READ);
}
$result = $this->storage->file_get_contents($path);
}
catch(\Exception $originalException) {
// Need to release the lock before more operations happen in upstream exception handlers
$this->releaseLock($path, Lock::READ);
throw $originalException;
}
return $result;
}
public function file_put_contents($path, $data) {
try {
if (!$this->getLock($path, Lock::WRITE)) {
throw new LockNotAcquiredException($path, Lock::WRITE);
}
$result = $this->storage->file_put_contents($path, $data);
}
catch(\Exception $originalException) {
// Release lock, throw original exception
$this->releaseLock($path, Lock::WRITE);
throw $originalException;
}
return $result;
}
public function copy($path1, $path2) {
try {
if (!$this->getLock($path1, Lock::READ)) {
throw new LockNotAcquiredException($path1, Lock::READ);
}
if (!$this->getLock($path2, Lock::WRITE)) {
throw new LockNotAcquiredException($path2, Lock::WRITE);
}
$result = $this->storage->copy($path1, $path2);
}
catch(\Exception $originalException) {
// Release locks, throw original exception
$this->releaseLock($path1, Lock::READ);
$this->releaseLock($path2, Lock::WRITE);
throw $originalException;
}
return $result;
}
public function rename($path1, $path2) {
try {
if (!$this->getLock($path1, Lock::READ)) {
throw new LockNotAcquiredException($path1, Lock::READ);
}
if (!$this->getLock($path2, Lock::WRITE)) {
throw new LockNotAcquiredException($path2, Lock::WRITE);
}
$result = $this->storage->rename($path1, $path2);
}
catch(\Exception $originalException) {
// Release locks, throw original exception
$this->releaseLock($path1, Lock::READ);
$this->releaseLock($path2, Lock::WRITE);
throw $originalException;
}
return $result;
}
}
|