Giter Club home page Giter Club logo

safe-stream's Introduction

Nette SafeStream: Atomic Operations

Downloads this Month Build Status Latest Stable Version License

The Nette\Utils\SafeStram protocol for file manipulation guarantees atomicity and isolation of every file operation. Why is it actually good? Let's start with a simple example, where we repeatedly write the same string to the file and then read it:

$s = str_repeat('Long String', 10000);

$counter = 1000;
while ($counter--) {
	file_put_contents('file', $s);       // write it
	$read = file_get_contents('file');   // read it
	if ($s !== $read) {                    // check it
		echo 'Strings are different!';
	}
}

It may seem that the echo 'Strings are different!' command can't ever get executed. The opposite is true. Try to run this script in two browsers simultaneously. The error occurs almost immediately.

It's because the code is not safe when performed repeatedly at the same time (ie, in multiple threads). And that is nothing unusual on the Internet, where several people often connect to one website at the same time. Therefore, it's very important to ensure that your application can handle multiple threads at once - that it's thread-safe, because native PHP functions are not. Otherwise, you can expect data loss and strange errors occuring.

How to ensure, that functions like file_get_contets or fwrite behave atomically? The SafeStream protocol offers a secure solution, so we can atomically manipulate files through standard PHP functions. To register this protocol install SafeStream via Composer or use:

Nette\Utils\SafeStream::register();

After that, you just need prefix the filename with nette.safe://:

$handle = fopen('nette.safe://test.txt', 'x'); // prefix the filename with nette.safe://

fwrite($handle, 'Nette Framework'); // for now, the data is written into a temporary file

fclose($handle); // and only now the file is renamed to test.txt

You can of course use all the familiar functions, such as:

file_put_contents('nette.safe://test.txt', $content);

$ini = parse_ini_file('nette.safe://autoload.ini');

SafeStream guarantees:

  • Atomicity: The file is written either as a whole or not written at all.
  • Isolation: No one can start to read a file that is not yet fully written.

If you write to an existing file in the 'a' mode (append), SafeStream creates it's copy and only after successfully writing it renames it to the original name. Write in this mode is therefore more resource-consuming than in other modes.

safe-stream's People

Contributors

besanek avatar dg avatar fprochazka avatar milo avatar mishak87 avatar salatos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.