3

I was browsing the Win32 API functions for file and directory management operations. I saw that some of those functions has their so called "transactional" counterparts.

Examples:
CreateDirectory and CreateDirectoryTransacted
RemoveDirectory and RemoveDirectoryTransacted
CreateFile and CreateFileTransacted
CopyFile and CopyFileTransacted

I read explanations of these transacted functions, the Wikipedia article Transactional NTFS and this MSDN Magazine page. But because of the heavy terminology (for me) in these pages, I didn't clearly understand these explanations. They all come to a common consensus that these functions are "atomic". But as far as I understand from the word "atom", it is a nucleus with spinning electrons around it...

Can you please explain me in basic and simple English sentences, what are the purposes and operations of these functions? Why and when would one prefer transacted version of an API function?

hkBattousai
  • 9,613
  • 17
  • 66
  • 114
  • 1
    "Atomic" in this context refers to the original meaning of the (greek) word, which is about "indivisible". Atomic operations are like this. Such an operation either finishes successfully or, in case of an error, if fails as a whole, so that, whatever happens, there will be no half-way exectued result, which could for instance be a corrupted file system. In other words: When an atomic operation is executed on a consistent state it will *always* result in a consistent state; either the old consistent state or a new consistent state, but nothing in between. – JimmyB Jul 11 '13 at 12:27
  • Maybe look at this: http://msdn.microsoft.com/en-us/library/hh802690%28v=vs.85%29.aspx, which states "atomicity here is that the changes either are completely applied or not applied at all, as an inconsistent state would render the file corrupt". – JimmyB Jul 11 '13 at 12:35
  • This website is only for its expert members because even if they are not experts in the topic they are asking about, they know how to ask questions that doesn't get marked as 'off-topic'. – Ameen Jul 19 '15 at 15:38

3 Answers3

2

In short, a transaction (be it a file system, database or bank) will only be completed if no errors occurred in the process.

Using a non-transactional file system and API, say you have a file containing:

AAAA

Now you want to fill the file with all B's, but while doing so in the middle the power is lost and not all data is committed to the disk. Now you have an inconsistent state when you read the file back (after power returns):

BBAA

Remember FAT and scandisk?

Now with transactions, the file system basically first write the changes to a different location on the disk, and change the "file data location pointers" inodes towards the new location of the data only when finished, marking the space the old data occupied as 'available' again.

You don't need Transactional NTFS (TxF) for this, as 'standard' NTFS also promises to ensure consistency:

NTFS is a recoverable file system that guarantees the consistency of the volume by using standard transaction logging and recovery techniques. In the event of a system failure, NTFS runs a recovery procedure that accesses information stored in a transaction log file. The NTFS recovery procedure guarantees that the volume is restored to a consistent state. Transaction logging requires very little overhead.

Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236
2

Why and when would one prefer transacted version of an API function?

There are a couple of scenarios given in the link I quoted above.

One of these is the use case of an installer application, which needs to copy/install several files to different locations and then maybe perform some updates to the registry. Before the installer runs the system can be considered consistent. Once the installer has done all its work, the software is completely installed and the system is again in a consistent state.

If, however, the computer crashes during the installation process it may not be trivial to determine which steps of the installation procedure have already been performed successfully before the crash and which have not. Transactional operations can give support in this situation by 'automatically' restoring the consistent system state as it was before the installer ran, if for any reason the installation fails in the process.

As Microsoft states, the transactional file system operations have never been adopted much by developers, which may serve as an indication that the functionality is not really needed for the vast majority of applications, or, that there are easier ways to achieve the desired result in an application-specific way, for which MS gives examples too.

Besides, the concept of "atomic" operations is present in different areas of software development, for instance in concurrent programming or in database management systems. See the Wikipedia article.

JimmyB
  • 11,178
  • 1
  • 23
  • 42
2

IMPORTANT

Note that Microsoft marked the entire "Transactional NTFS" API as deprecated and strongly discourages its usage.

See https://docs.microsoft.com/cs-cz/windows/win32/fileio/deprecation-of-txf (Alternatives to using Transactional NTFS)

Jaromír Adamec
  • 539
  • 4
  • 13