Is the ReplaceFile Windows API a convenience function only?
Asked Answered
S

2

8

Is the ReplaceFile Windows API a convenience function only, or does it achieve anything beyond what could be coded using multiple calls to MoveFileEx?

I'm currently in the situation where I need to

  1. write a temporary file and then
  2. rename this temporary file to the original filename, possibly replacing the original file.

I thought about using MoveFileEx with MOVEFILE_REPLACE_EXISTING (since I don't need a backup or anything) but there is also the ReplaceFile API and since it is mentioned under Alternatives to TxF.

This got me thinking: Does ReplaceFile actually do anything special, or is it just a convenience wrapper for MoveFile(Ex)?

Speedometer answered 12/7, 2013 at 8:55 Comment(0)
E
11

I think the key to this can be found in this line from the documentation (my emphasis):

The replacement file assumes the name of the replaced file and its identity.

When you use MoveFileEx, the replacement file has a different identity. Its creation date is not preserved, the creator is not preserved, any ACLs are not preserved and so on. Using ReplaceFile allows you to make it look as though you opened the file, and modified its contents.

The documentation says it like this:

Another advantage is that ReplaceFile not only copies the new file data, but also preserves the following attributes of the original file:

  • Creation time
  • Short file name
  • Object identifier
  • DACLs
  • Security resource attributes
  • Encryption
  • Compression
  • Named streams not already in the replacement file

For example, if the replacement file is encrypted, but the replaced file is not encrypted, the resulting file is not encrypted.

Elope answered 12/7, 2013 at 10:9 Comment(4)
I read that but didn't really understand. Is "identity" here some Windows/NTFS/ACL term?Speedometer
I don't have a good definition of precisely what identity means. It is of course complicated by the fact that there are very many file systems in use that are different at the implementation level. But if you use MoveFileEx you will lose ACLs, attributes and so on. If you use ReplaceFile you replace the content but leave the metadata untouched.Elope
I think "identity" is spelled out further down in the same documentation: "Another advantage is that ReplaceFile not only copies the new file data, but also preserves the following attributes of the original file: •Creation time •Short file name •Object identifier •DACLs •Security resource attributes •Encryption •Compression •Named streams not already in the replacement file For example, if the replacement file is encrypted, but the replaced file is not encrypted, the resulting file is not encrypted."Varico
I assume, that internally nevertheless a Remove followed by a MoveFile takes place. What happens,if the OS decides to let an other app read access the file to be replaced, just in between these two components? Will the other app get a "Not existing" error, or does ReplaceFile employ a Lock mechanism around the command in some way, or does the other app never have any such issues, i.e. will it wait until ReplaceFile has come to an end?Tubman
F
1

Any app that wants to update a file by writing to a temp and doing the rename/rename/delete dance (handling all the various failure scenarios correctly), would have to change each time a new non-data attribute was added to the system. Rather than forcing all apps to change, they put in an API that is supposed to do this for you.

So you could "just do it yourself", but why? Do you correctly cover all the failure scenarios? Yes, MS may have a bug, but why try to invent the wheel?

NB, I have a number of issues with the programming model (better to do a "CreateUsingTemplate") but it's better than nothing.

Florey answered 3/8, 2013 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.