Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, June 2007
QFile::copy() let the source file opened ?...


Message 1 in thread

Hi !

When QFile::copy() is called it seems that copy() do not close the source
file when the file operation has been done. I run the latest KUbuntu distro
with Qt 4.3 (but the same thing happened in 4.2) and I cannot open the
source file without beeing forced to close it manually before calling
QFile::open().

Is it :
* An undocumented behaviour (in this case you should update your help since
there is nothing about a such behaviour)
* Something I missed (OK, it can be my fault ;-)
* A bug ?

I created a demo program to reproduce what I experienced :

#include <iostream>
#include <iostream>
#include <QFile>
#include <QIODevice>
#include <QApplication>


void die(const QString& msg)
{
    std::cout << qPrintable(msg) << std::endl;
    exit(1);
}

void testCopy()
{
    QFile src("./sourcefile");

    if (!src.exists())
      die(QString("The source file does not exists."));

    QFile target("./targetfile");

    if (target.exists())
        if (!target.remove())
          die(QString("Target file exists and cannot be removed.
Aborting."));

    if (!src.copy(target.fileName()))
      die(QString("The file cannot be copied."));
    else
      std::cout << "Copy succeeded, OK" << std::endl;

    //#define WORKAROUND

    #ifdef WORKAROUND
    src.close(); /* Should not be needed ?! */
    #endif

    if (src.open(QIODevice::ReadOnly))
    {
      std::cout << "All tests succeeded, good !!" << std::endl;
      src.close();
    } else
      std::cout << "Cannot open \"src\". Maybe it has not been closed in
QFile::copy() ?..." << std::endl;
}


int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    testCopy();

    return 0;
}

When WORKAROUND is not defined, I've got the following output :

adrien@adrien:~/copytest$ ./copytest
Copy succeeded, OK
QFile::open: File (./sourcefile) already open /* <- Qt's internal error
message /*
Cannot open "src". Maybe it has not been closed in QFile::copy() ?...

When WORKAROUND is defined, all works fine :
adrien@adrien:~/copytest$ ./copytest
Copy succeeded, OK
All tests succeeded, good !!

So, is it "as designed" or a bug ? Any hint appreciated !

Best regards,

A.R.

Message 2 in thread

> When QFile::copy() is called it seems that copy() do not close the
source
> file when the file operation has been done. I run the latest KUbuntu
> distro with Qt 4.3 (but the same thing happened in 4.2) and I cannot
open
> the source file without beeing forced to close it manually before
calling
> QFile::open().
> 
> Is it :
> * An undocumented behaviour (in this case you should update your help
> since there is nothing about a such behaviour)
> * Something I missed (OK, it can be my fault ;-)
> * A bug ?

i suggest to send this to qt-bugs@xxxxxxxxxxxxx this doesn't sound
right.

Cheers,
Peter

--
 [ signature omitted ] 

Message 3 in thread

Hi,


> i suggest to send this to qt-bugs@xxxxxxxxxxxxx this doesn't sound
> right.


Thanks, I'll do that.

Regards,

A.R.