/usr/share/php/Nette/Utils/SafeStream.php is in php-nette 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | <?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace Nette\Utils;
use Nette;
/**
* Provides atomicity and isolation for thread safe file manipulation using stream safe://
*
* <code>
* file_put_contents('safe://myfile.txt', $content);
*
* $content = file_get_contents('safe://myfile.txt');
*
* unlink('safe://myfile.txt');
* </code>
*
* @author David Grudl
* @internal
*/
class SafeStream
{
/** Name of stream protocol - safe:// */
const PROTOCOL = 'safe';
/** @var resource orignal file handle */
private $handle;
/** @var resource temporary file handle */
private $tempHandle;
/** @var string orignal file path */
private $file;
/** @var string temporary file path */
private $tempFile;
/** @var bool */
private $deleteFile;
/** @var bool error detected? */
private $writeError = FALSE;
/**
* Registers protocol 'safe://'.
* @return bool
*/
public static function register()
{
@stream_wrapper_unregister(self::PROTOCOL); // intentionally @
return stream_wrapper_register(self::PROTOCOL, __CLASS__);
}
/**
* Opens file.
* @param string file name with stream protocol
* @param string mode - see fopen()
* @param int STREAM_USE_PATH, STREAM_REPORT_ERRORS
* @param string full path
* @return bool TRUE on success or FALSE on failure
*/
public function stream_open($path, $mode, $options, & $opened_path)
{
$path = substr($path, strlen(self::PROTOCOL)+3); // trim protocol safe://
$flag = trim($mode, 'crwax+'); // text | binary mode
$mode = trim($mode, 'tb'); // mode
$use_path = (bool) (STREAM_USE_PATH & $options); // use include_path?
// open file
if ($mode === 'r') { // provides only isolation
return $this->checkAndLock($this->tempHandle = fopen($path, 'r'.$flag, $use_path), LOCK_SH);
} elseif ($mode === 'r+') {
if (!$this->checkAndLock($this->handle = fopen($path, 'r'.$flag, $use_path), LOCK_EX)) {
return FALSE;
}
} elseif ($mode[0] === 'x') {
if (!$this->checkAndLock($this->handle = fopen($path, 'x'.$flag, $use_path), LOCK_EX)) {
return FALSE;
}
$this->deleteFile = TRUE;
} elseif ($mode[0] === 'w' || $mode[0] === 'a' || $mode[0] === 'c') {
if ($this->checkAndLock($this->handle = @fopen($path, 'x'.$flag, $use_path), LOCK_EX)) { // intentionally @
$this->deleteFile = TRUE;
} elseif (!$this->checkAndLock($this->handle = fopen($path, 'a+'.$flag, $use_path), LOCK_EX)) {
return FALSE;
}
} else {
trigger_error("Unknown mode $mode", E_USER_WARNING);
return FALSE;
}
// create temporary file in the same directory to provide atomicity
$tmp = '~~' . lcg_value() . '.tmp';
if (!$this->tempHandle = fopen($path . $tmp, (strpos($mode, '+') ? 'x+' : 'x').$flag, $use_path)) {
$this->clean();
return FALSE;
}
$this->tempFile = realpath($path . $tmp);
$this->file = substr($this->tempFile, 0, -strlen($tmp));
// copy to temporary file
if ($mode === 'r+' || $mode[0] === 'a' || $mode[0] === 'c') {
$stat = fstat($this->handle);
fseek($this->handle, 0);
if (stream_copy_to_stream($this->handle, $this->tempHandle) !== $stat['size']) {
$this->clean();
return FALSE;
}
if ($mode[0] === 'a') { // emulate append mode
fseek($this->tempHandle, 0, SEEK_END);
}
}
return TRUE;
}
/**
* Checks handle and locks file.
* @return bool
*/
private function checkAndLock($handle, $lock)
{
if (!$handle) {
return FALSE;
} elseif (!flock($handle, $lock)) {
fclose($handle);
return FALSE;
}
return TRUE;
}
/**
* Error destructor.
*/
private function clean()
{
flock($this->handle, LOCK_UN);
fclose($this->handle);
if ($this->deleteFile) {
unlink($this->file);
}
if ($this->tempHandle) {
fclose($this->tempHandle);
unlink($this->tempFile);
}
}
/**
* Closes file.
* @return void
*/
public function stream_close()
{
if (!$this->tempFile) { // 'r' mode
flock($this->tempHandle, LOCK_UN);
fclose($this->tempHandle);
return;
}
flock($this->handle, LOCK_UN);
fclose($this->handle);
fclose($this->tempHandle);
if ($this->writeError || !rename($this->tempFile, $this->file)) { // try to rename temp file
unlink($this->tempFile); // otherwise delete temp file
if ($this->deleteFile) {
unlink($this->file);
}
}
}
/**
* Reads up to length bytes from the file.
* @param int length
* @return string
*/
public function stream_read($length)
{
return fread($this->tempHandle, $length);
}
/**
* Writes the string to the file.
* @param string data to write
* @return int number of bytes that were successfully stored
*/
public function stream_write($data)
{
$len = strlen($data);
$res = fwrite($this->tempHandle, $data, $len);
if ($res !== $len) { // disk full?
$this->writeError = TRUE;
}
return $res;
}
/**
* Truncates a file to a given length.
* @param int The size to truncate to.
* @return bool
*/
public function stream_truncate($size)
{
return ftruncate($this->tempHandle, $size);
}
/**
* Returns the position of the file.
* @return int
*/
public function stream_tell()
{
return ftell($this->tempHandle);
}
/**
* Returns TRUE if the file pointer is at end-of-file.
* @return bool
*/
public function stream_eof()
{
return feof($this->tempHandle);
}
/**
* Sets the file position indicator for the file.
* @param int position
* @param int see fseek()
* @return int Return TRUE on success
*/
public function stream_seek($offset, $whence)
{
return fseek($this->tempHandle, $offset, $whence) === 0; // ???
}
/**
* Gets information about a file referenced by $this->tempHandle.
* @return array
*/
public function stream_stat()
{
return fstat($this->tempHandle);
}
/**
* Gets information about a file referenced by filename.
* @param string file name
* @param int STREAM_URL_STAT_LINK, STREAM_URL_STAT_QUIET
* @return array
*/
public function url_stat($path, $flags)
{
// This is not thread safe
$path = substr($path, strlen(self::PROTOCOL)+3);
return ($flags & STREAM_URL_STAT_LINK) ? @lstat($path) : @stat($path); // intentionally @
}
/**
* Deletes a file.
* On Windows unlink is not allowed till file is opened
* @param string file name with stream protocol
* @return bool TRUE on success or FALSE on failure
*/
public function unlink($path)
{
$path = substr($path, strlen(self::PROTOCOL)+3);
return unlink($path);
}
}
|