Using a temporary file in QFile (was "QSaveFile")
David Faure
faure at kde.org
Fri Dec 23 16:05:07 UTC 2011
On Sunday 16 October 2011 13:37:48 Oswald Buddenhagen wrote:
> so maybe filesaver should be just folded into qfile? how to
> activate that mode? setLatchErrors(bool)?
I had another look at this issue, and I need your input.
Were you thinking of having the whole functionality in QFile?
In that email thread from october I wasn't, but now I wanted to give it a try.
I added this API to QFile:
bool useTemporaryFile() const;
void setUseTemporaryFile(bool useTemp);
void rollback();
bool commit();
and sorted out "the internal name given to the file engine is different from the
publically visible name" by changing the implementation of fileName().
The main issue is opening the temp file in open(), rather than the target
filename. QTemporaryFile has the whole logic of "opening the file itself and
keeping it open in order to prevent race conditions", rather than the unsafe
mktemp approach of "just give me a filename". I found a way though: stealing
the fileEngine from the QTemporaryFile object after it opens the file, see
patch.
This leads to a WIP patch like the one attached -- it has TODOs and whitespace
issues and probably style issues, please try to overlook those. The question
is mainly: overall, does an API like this seem OK to you, given the
"interesting" implementation that it requires?
It also raises a few questions, like: should remove(), exists(), etc. be about
the final filename or the temporary file...
I would say: to the temp file while writing (between open() and commit()), and
to the target file the rest of the time (before open() and after commit()).
If this seems too weird to you, I'll just forget the idea and put creator's
savefile class in Qt... I kind of liked the idea of the above API though, so
that applications which often use QFile directly initially, can switch to the
safer "use of a temp file for saving" by calling two methods, instead of having
to port to another class altogether.
--
David Faure, faure at kde.org, http://www.davidfaure.fr
Sponsored by Nokia to work on KDE, incl. KDE Frameworks 5
-------------- next part --------------
A non-text attachment was scrubbed...
Name: qfile_temp.diff
Type: text/x-patch
Size: 6588 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-frameworks-devel/attachments/20111223/28dc5542/attachment.diff>
More information about the Kde-frameworks-devel
mailing list