[Kde-pim] mbox resource

Ingo Klöcker kloecker at kde.org
Wed Feb 5 20:34:59 GMT 2014


On Wednesday 05 February 2014 10:41:21 laurent Montel wrote:
> On mardi 4 février 2014 17:09:52 Martin Koller wrote:
> > On Tuesday 04 February 2014 15:51:39 Daniel Vrátil wrote:
> > > On Monday 03 of February 2014 21:11:37 Martin Koller wrote:
> > > > Hi,
> > > > 
> > > > I'm debugging the mbox resource and find a strange thing.
> > > > When I append a new message from the shell (simulating an
> > > > external
> > > > program
> > > > appending a new message) then
> > > > SingleFileResourceBase::fileChanged() gets called which in turn
> > > > lands in MboxResource::writeToFile().
> > > > There the given filename is a temporary file, e.g.
> > > > /home/kdetrunk/.kde4/share/apps/akonadi_mbox_resource_0/mbox-1
> > > > to which the whole current mbox file is copied via mMBox->save(
> > > > fileName )
> > > > 
> > > > But then follows something I do not understand.
> > > > The comment in this file says something like
> > > > "... Therefore we
> > > > 
> > > >   // copy the original file and append pending changes to it but
> > > >   also
> > > >   add an
> > > > 
> > > > extra // '\n' to make sure that the hashes differ and the user
> > > > gets
> > > > notified. ..."
> > > > 
> > > > But in fact it destroys what it has just written via
> > > > 
> > > >     QFile file( fileName );
> > > >     file.open( QIODevice::WriteOnly );
> > > >     file.seek( file.size() );
> > > >     file.write( "\n" );
> > > > 
> > > > QIODevice::WriteOnly is not
> > > > QIODevice::WriteOnly|QIODevice::Append even with the seek() so
> > > > I assume that this is simply a bug, right ?> > 
> > > Hi,
> > > 
> > > I haven't checked the code, but I think that WriteOnly +
> > > seek(file.size()) is equal to Append, so this should be just
> > > fine.
> > 
> > It's not.
> > Try this:
> > 
> > #include <QFile>
> > 
> > int main(int argc, char **argv)
> > {
> > 
> >   QString fileName("/tmp/testfile");
> >   
> >   QFile file( fileName );
> >   file.open( QIODevice::WriteOnly );
> >   file.seek( file.size() );
> >   file.write( "\n" );
> >   
> >   return 0;
> > 
> > }

Good catch, Martin!


> I didn't test it.
> But if you test case is ok, create a patch for mbox resource.
> Indeed it's better to add QIODevice::Append if this code appends
> message.

I dug into Qt and found the following in QFSFileEnging::open()

  // WriteOnly implies Truncate if neither ReadOnly nor Append are sent.
  if ((openMode & QFile::WriteOnly) && !(openMode & (QFile::ReadOnly | 
QFile::Append)))
      openMode |= QFile::Truncate;

So, opening a file with mode WriteOnly truncates the file. This is bad.

The documentation of QFile [1] doesn't mention that opening a file in 
WriteOnly mode truncates it (although one should probably expect this 
behavior since it's in line with fopen(..., "w")).

Confusingly, the documentation of QFile::open(OpenMode mode) states

"The mode must be QIODevice::ReadOnly, QIODevice::WriteOnly, or 
QIODevice::ReadWrite. It may also have additional flags, such as 
QIODevice::Text and QIODevice::Unbuffered."

No word about Append. In fact, Append isn't mentioned anywhere on [1]. 
Weird.


Regards,

Ingo


[1] http://qt-project.org/doc/qt-4.8/qfile.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-pim/attachments/20140205/3f6ccaa2/attachment.sig>
-------------- next part --------------
_______________________________________________
KDE PIM mailing list kde-pim at kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/


More information about the kde-pim mailing list