Advertisement

Why download to a different file first and then copy?

Started by August 05, 2013 05:47 PM
2 comments, last by frob 11 years, 3 months ago

Hi,

Firefox as an example, but a bazillion of other programs that have the ability to download some file over the last decades, will, when you download a file, first save it to some different file, then copy it to the actual destination at the end.

Firefox first saves your file to "filename.part" and already makes a "filename" of 0KB. You can see the size of "filename.part" increase as it downloads. Then, when done, it gets copied or moved to "filename" and removed.

Other programs want to store the file in a temporary directory first, then in the end move it to your destination directory. CD or DVD copying programs also used to do this kind of stuff (I probably haven't touched an optical disk in the last 5 years tho so this is ancient stuff I'm talking about).

Do you know what I'm talking about, ever seen this behaviour?

Basically I'm wondering why, is there a technical reason? Why not just immediately download to the actual destination filename?

On occasions that the download dies halfway through, what file is left on the drive? The .part file is leftover, and is clearly incomplete. That's why. Burner programs are probably trying to avoid buffer under-run issues.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Advertisement

It also prevents you from doing anything with the file until the download is complete.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Copy and swap is a very common idiom.

Make a temporary object, then when the operation is complete, swap it in place; if the operation fails for any reason the temporary can be removed and the user notified of an error without corrupting any other data.

If the operation were to use the final object (in this case a file) then a partial file or a corrupt file would be indistinguishable from a completed file.

The pattern is used in software all the time, even if you are unaware of it.

Copying an object in memory in an exception-safe way is to create a temporary, do the assignments, and then, only if everything worked correctly, swapping the result into the right place. The alternative may be that you corrupt your existing object if any part of the copy fails.

Many times a game save feature will work the same way. Create a new temporary save file. If that worked, delete the original save file and rename the new one. If something went wrong, destroy the temporary and inform the user. The alternative of just erasing the original and writing a new file is that in the event of an error all data gets lost.

This topic is closed to new replies.

Advertisement