Source for file SafeStream.php
Documentation is available at SafeStream.php
- 1: <?php
- 3: /**
- 4: * Nette Framework
- 5: *
- 6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
- 7: *
- 8: * This source file is subject to the "Nette license" that is bundled
- 9: * with this package in the file license.txt.
- 10: *
- 11: * For more information please see http://nettephp.com
- 12: *
- 18: */
- 22: /**
- 23: * Thread safe / atomic file manipulation. Stream safe://
- 24: *
- 25: * <code>
- 26: * file_put_contents('safe://myfile.txt', $content);
- 27: *
- 28: * $content = file_get_contents('safe://myfile.txt');
- 29: *
- 30: * unlink('safe://myfile.txt');
- 31: * </code>
- 32: *
- 33: *
- 37: */
- 39: {
- 40: /**
- 41: * Name of stream protocol - safe://
- 42: */
- 45: /**
- 46: * Current file handle.
- 47: */
- 50: /**
- 51: * Renaming of temporary file.
- 52: */
- 56: /**
- 57: * Starting position in file (for appending).
- 58: */
- 61: /**
- 62: * Write-error detected?
- 63: */
- 68: /**
- 69: * Registers protocol 'safe://'.
- 70: *
- 72: */
- 74: {
- 76: }
- 80: /**
- 81: * Opens file.
- 82: *
- 88: */
- 90: {
- 102: // enter critical section: open and lock EXISTING file for reading/writing
- 108: }
- 116: // try enter critical section: open and lock EXISTING file for rewriting
- 126: }
- 129: }
- 131: }
- 132: // file doesn't exists, continue...
- 139: // create temporary file in the same directory
- 142: // enter critical section: create temporary file
- 148: // rename later - for windows
- 151: }
- 153: }
- 156: }
- 159: default:
- 168: /**
- 169: * Closes file.
- 170: *
- 172: */
- 174: {
- 177: }
- 181: // are we working with temporary file?
- 183: // try to rename temp file, otherwise delete temp file
- 186: }
- 187: }
- 188: }
- 192: /**
- 193: * Reads up to length bytes from the file.
- 194: *
- 197: */
- 199: {
- 201: }
- 205: /**
- 206: * Writes the string to the file.
- 207: *
- 210: */
- 212: {
- 218: }
- 221: }
- 225: /**
- 226: * Returns the position of the file.
- 227: *
- 229: */
- 231: {
- 233: }
- 237: /**
- 238: * Returns TRUE if the file pointer is at end-of-file.
- 239: *
- 241: */
- 243: {
- 245: }
- 249: /**
- 250: * Sets the file position indicator for the file.
- 251: *
- 255: */
- 257: {
- 259: }
- 263: /**
- 264: * Gets information about a file referenced by $this->handle.
- 265: *
- 267: */
- 269: {
- 271: }
- 275: /**
- 276: * Gets information about a file referenced by filename.
- 277: *
- 281: */
- 283: {
- 284: // This is not thread safe
- 287: }
- 291: /**
- 292: * Deletes a file.
- 293: * On Windows unlink is not allowed till file is opened
- 294: *
- 297: */
- 299: {
- 302: }
- 304: }