File based "transactions"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

File based "transactions"

Post by kaisellgren »

Hello,

Just like SQL transactions, but in file based level. I have a pack of content that should be written into multiple different files, if any of them fail (the md5 doesn't match), then rollback all files to previous state.

The only reasonable way of accomplishing this seems to be looping through the content and check the md5's of the files and if any md5 match fails during loop then cancel whole operation otherwise loop through them again and this time make the actual changes to the files not just reading the content and comparing md5s.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: File based "transactions"

Post by alex.barylski »

If it's mission critical, do whatever it takes to use a transaction supported database.

Otherwise, your algorithm sounds about right, yes.

Save the MD5/CRC32 of the original files. Copy them into place or upload, whatever, then check each file CRC32 and compare that against the newly updated/uploaded files. On failure, restore the temp buffered files to their original glory.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: File based "transactions"

Post by requinix »

How about making a temporary copy of the file(s) to be modified?
Make your changes, check that everything worked, and then copy the temporary back over the original.
Post Reply