Transactional File System

Warning!

Microsoft strongly recommends developers investigate utilizing the discussed alternatives (or in some cases, investigate other alternatives) rather than adopting an API platform which may not be available in future versions of Windows.
– MSDN

What?

Microsoft introduced Transactional NTFS (TxF) with Windows Vista. This enabled developers to make a number of file system operations and they would need to complete all or everything is rolled back (like in database transactions)

It uses unmanaged C++ operations from the Kernel32.dll. Also the Distributed Transaction Coordinator is used for managing the transaction.

Why?

Ever wanted to process a set of files as a whole? What if the last of 100 files is not moved / deleted / written? You would need to do a corrective action. What if the corrective action fails?

How?

I’m investigating the TxF for reading and moving a set of files. The files need to be parsed, loaded into a database and archived as a set. When a file fails every file needs to be left in the input folder. This can be achieved by starting a transactionscope and completing it when everything succeeds.

using (var scope = new TransactionScope()) {
    foreach (var file in files) {
        var source = Path.Combine(directory, file);
        using(var stream = TransactedFile.Open(source, 
                    FileMode.Open, FileAccess.Read, FileShare.None))
        {
            // read file and put in database
        }
        var destination = Path.Combine(archive, file);
        TransactedFile.Move(source, destination);
    }
    scope.Complete();
}

Again: Microsoft is planning to deprecate the TxF API’s. Maybe if we all start using it Microsoft will hang on to it.

References

Transaction File System wrappers on github
Alternatives to using Transactional NTFS on MSDN

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s