4

I have two files: A and B. I need to replace B with A, while B is opened in another program.

The following approach:

MoveFile to rename B to C, then rename A to B, then delete C

works fine. However I need an atomic way to do that, because if one rename fails the data will be left in inconsistent state.

I tried MoveFileEx with MOVEFILE_REPLACE_EXISTING flag, but it doesn't work when file B is opened by another program. Same for ReplaceFile function. Any other ideas? Thank you

9
  • This isn't an atomic operation, so could you possibly do it in an atomic way? Why can't you just use proper error handling? Commented May 9, 2013 at 21:32
  • File locks exist for a reason. Try the change, fail if it can't be done. Commented May 9, 2013 at 21:33
  • 3
    I suspect the answer is no way, try reading this and see if it applies to you. Commented May 9, 2013 at 21:34
  • Error handling won't work. Let's say the file is on the network. First rename works, second fails, and the third (error handling) will also fail. So I'll leave the files in inconsistent state. Atomic operation is needed Commented May 9, 2013 at 21:40
  • @Kyle, atomic rename operation exists in Linux (of course, provided that the power supply is not abruptly interrupted, but there is the journal for handling that) and it's a totally legitimate request for any not-a-toy OS. Commented May 9, 2013 at 21:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.