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 https://nette.org
- 12: *
- 19: */
- 23: /**
- 24: * Thread safe / atomic file manipulation. Stream safe://
- 25: *
- 26: * <code>
- 27: * file_put_contents('safe://myfile.txt', $content);
- 28: *
- 29: * $content = file_get_contents('safe://myfile.txt');
- 30: *
- 31: * unlink('safe://myfile.txt');
- 32: * </code>
- 33: *
- 34: *
- 38: */
- 40: {
- 41: /**
- 42: * Name of stream protocol - safe://
- 43: */
- 46: /**
- 47: * Current file handle.
- 48: */
- 51: /**
- 52: * Renaming of temporary file.
- 53: */
- 57: /**
- 58: * Starting position in file (for appending).
- 59: */
- 62: /**
- 63: * Write-error detected?
- 64: */
- 69: /**
- 70: * Registers protocol 'safe://'.
- 71: *
- 73: */
- 75: {
- 77: }
- 81: /**
- 82: * Opens file.
- 83: *
- 89: */
- 91: {
- 103: // enter critical section: open and lock EXISTING file for reading/writing
- 109: }
- 117: // try enter critical section: open and lock EXISTING file for rewriting
- 127: }
- 130: }
- 132: }
- 133: // file doesn't exists, continue...
- 140: // create temporary file in the same directory
- 143: // enter critical section: create temporary file
- 149: // rename later - for windows
- 152: }
- 154: }
- 157: }
- 160: default:
- 169: /**
- 170: * Closes file.
- 171: *
- 173: */
- 175: {
- 178: }
- 182: // are we working with temporary file?
- 184: // try to rename temp file, otherwise delete temp file
- 187: }
- 188: }
- 189: }
- 193: /**
- 194: * Reads up to length bytes from the file.
- 195: *
- 198: */
- 200: {
- 202: }
- 206: /**
- 207: * Writes the string to the file.
- 208: *
- 211: */
- 213: {
- 219: }
- 222: }
- 226: /**
- 227: * Returns the position of the file.
- 228: *
- 230: */
- 232: {
- 234: }
- 238: /**
- 239: * Returns TRUE if the file pointer is at end-of-file.
- 240: *
- 242: */
- 244: {
- 246: }
- 250: /**
- 251: * Sets the file position indicator for the file.
- 252: *
- 256: */
- 258: {
- 260: }
- 264: /**
- 265: * Gets information about a file referenced by $this->handle.
- 266: *
- 268: */
- 270: {
- 272: }
- 276: /**
- 277: * Gets information about a file referenced by filename.
- 278: *
- 282: */
- 284: {
- 285: // This is not thread safe
- 288: }
- 292: /**
- 293: * Deletes a file.
- 294: * On Windows unlink is not allowed till file is opened
- 295: *
- 298: */
- 300: {
- 303: }
- 305: }