[Development] QFile: writing via a temporary file

David Faure faure at kde.org
Fri Jan 6 20:39:54 CET 2012


On Friday 06 January 2012 14:43:30 Artur Souza (MoRpHeUz) wrote:
> On Fri, Jan 6, 2012 at 2:36 PM, David Faure <faure at kde.org> wrote:
> > No, no, the whole idea of this feature is not to use the temp file as a
> > backup. If the app crashes, you lose your partially-writen temporary
> > file. That's fine, it was partial anyway, why would you want to use it?
> > 
> > I think you're confusing this with a backup feature, which is unrelated.
> 
> Yeah, I understand that the main use case is not using it as a backup :)
> 
> I was just thinking that the use cases are very similar and depending
> the way you implement it, it's possible to use it to restore i.e. your
> document if your text editor written in Qt crashes...

There is no need to "restore" anything in case of a crash, that's the whole 
point of this feature.

If the app crashes between open and close, i.e. in the middle of the writing 
-- then what you want to open in your text editor, is the existing untouched 
(older) version of the file, NOT the half-written temp file.

If the app crashes at any other point, i.e. after close(), and then the 
'target' file is the one you want to open anyway.

The saving happens into the temp file. So there is nothing "to restore" in case 
of a crash, that's the whole point of not touching the existing file until the 
final atomic renaming.

So in fact I think you're confused about where the writing happens ;)

The target file (say, foo.txt) is untouched until it's atomically replaced with 
the new (temp) file, so at any point in time, the target file as known by the 
user (foo.txt) is useable. You never need the temp file after a crash.

-- 
David Faure, faure at kde.org, http://www.davidfaure.fr
Sponsored by Nokia to work on KDE, incl. KDE Frameworks 5




More information about the Development mailing list