PDA

View Full Version : QTemporaryFile problems



C167
20th June 2008, 17:09
Hi, I try to write a blob to a file and then execute it. My code:
source:
ProgramLauncher::ProgramLauncher ( QObject *parent )
: QObject ( parent )
{
loader = new BinaryLoader ( );
connect ( loader, SIGNAL ( dataLoaded ( int, QByteArray ) ), this, SLOT ( loaded ( int, QByteArray ) ) );
}

void ProgramLauncher::launch ( int id )
{
qDebug() << id;
loader->getData ( "bin_programs", id );
}

void ProgramLauncher::loaded ( int handle, QByteArray data )
{
Q_UNUSED ( handle );
// QString path = QDir::tempPath() + "XXXXXX";
QTemporaryFile *tmpFile = new QTemporaryFile ( QString ( "XXXXXX" ) );
if ( !tmpFile->open ( ) )
{
return;
}
qDebug() << "tmpFile: " << tmpFile->fileName() << " opened";
qint64 size = tmpFile->write ( data );
if ( size != -1 )
{
qDebug() << "wrote " << size << " into the file";
execute ( tmpFile );
}
}

void ProgramLauncher::execute ( QTemporaryFile *file )
{
QString path = QDir::tempPath() + "/" + file->fileName();
qDebug() << "Launching filename " << path;
qDebug() << QProcess::startDetached ( path );
}
ProgramLauncher::~ProgramLauncher()
{
}header:
class ProgramLauncher : public QObject
{
Q_OBJECT
public:
ProgramLauncher ( QObject *parent = 0 );
~ProgramLauncher();
public slots:
void launch ( int id );
void loaded ( int handle, QByteArray data );
private:
void execute ( QTemporaryFile *file );
BinaryLoader *loader;
};My problem is that it doesn't create the file where it should. According to the doc, it should be in QDir::tempPath(), but instead it is where the program was started. So, starting the binary fails cause my functions look into /tmp:
5
tmpFile: "XN1408" opened
wrote 24791336 into the file
Launching filename "/tmp/XN1408"
falseWhy that?

The program is developed on linux, target platform will be windows where it should run from CD (so no write access).

And: I thought the file will be removed upon destruction of the object? it stays where it is, including content

C167

mcosta
20th June 2008, 17:44
From Qt documentation



QTemporaryFile::QTemporaryFile ( const QString & templateName )
Constructs a QTemporaryFile with a template filename of templateName. Upon opening the temporary file this will be used to create a unique filename. If the templateName does not contain XXXXXX it will automatically be appended and used as the dynamic portion of the filename.
If templateName is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath() to construct templateName if you want use the system's temporary directory.

C167
20th June 2008, 18:24
Oh, i see i misunderstood the last two sentences, i thought it would automatically go to /tmp if not set to anything else, sorry
It now generates the files below /tmp :-)

Okay, as this works, i still have the problem that it does not auto-delete the files after the destruction. I changed my code, it now looks like this:
ProgramLauncher::ProgramLauncher ( QObject *parent )
: QObject ( parent )
{
loader = new BinaryLoader ( );
connect ( loader, SIGNAL ( dataLoaded ( int, QByteArray ) ), this, SLOT ( loaded ( int, QByteArray ) ) );
}

void ProgramLauncher::launch ( int id )
{
qDebug() << id;
loader->getData ( "bin_programs", id );
}

void ProgramLauncher::loaded ( int handle, QByteArray data )
{
Q_UNUSED ( handle );

tmpFile = new QTemporaryFile ( QString ( QDir::tempPath() + "/XXXXXX" ) );
if ( !tmpFile->open ( ) )
{
return;
}
qDebug() << "tmpFile: " << tmpFile->fileName() << " opened";
qint64 size = tmpFile->write ( data );
if ( size != -1 )
{
qDebug() << "wrote " << size << " into the file";
QString fname = tmpFile->fileName();
qDebug() << tmpFile->autoRemove();
tmpFile->close();
qDebug() << QFile::copy ( fname, fname + "_bak" );
qDebug() << QProcess::startDetached ( fname );
delete tmpFile;
}
}And it does not launch the binaries although they are valid linux executables like xterm and konsole (just for testing)

marcel
20th June 2008, 18:28
You can't delete the file while the process is executing, that's why the delete doesn;t succeed.

C167
20th June 2008, 18:57
sorry for double-post, firefox is bit unstable here

Hm... but QProcess::startDetached returns false, and no window opens. Qt Documentation sais it gets executed like a daemon, so maybe i should just write a two-liner that touches a file...

marcel
20th June 2008, 19:30
Don't worry, I deleted it.
Starting detached means that the process won't be a child of your process, therefore you won't be able to monitor its events (like exit). Indeed it starts as a daemon, but that doesn;t mean you can delete the binary.

As for startDetached returning false, maybe you don;'t have the right permissions set on the temp file. See QFile::setPermissions.

C167
21st June 2008, 14:36
Thanks
Okay, i'm using this code now:
if ( size != -1 )
{
qDebug() << "wrote " << size << " into the file";
QFile file ( tmpFile->fileName() );
tmpFile->close();
if ( file.open( QIODevice::ReadOnly ) && file.exists() )
{
qDebug() << file.fileName();
file.setPermissions ( QFile::ReadOwner | QFile::WriteOwner |
QFile::ExeOwner | QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther );
qDebug() << QProcess::startDetached ( file.fileName() );
}
}you see, i've set the permissions to execute for everyone, but it still gives a false
€dit: even if i close the file before launching. strange

C167
21st June 2008, 21:33
any ideas? on windows, its the same problem and the program must be finished by tomorrow (need some time to test it etc.)

marcel
21st June 2008, 21:41
why don't you manually check the temporary file? put a break point before startDetached and go where the temp file is. try to execute it in a terminal and see what happens.

C167
22nd June 2008, 01:51
gdb tells me that the file i want to load does not exist, even if i use kdevelop to debug (which is not able to start the program)

marcel
22nd June 2008, 02:04
probably the file already got deleted.
I meant trying to manually execute the temporary program. just open a console, go to the location where the temp file was created and try to open it.

I suggested the breakpoint before startDetached because that would prevent the file from being deleted.

C167
22nd June 2008, 10:54
sorry, i meant the file where ther startDetached is, i cannot load the cpp file. I now simply make a QFile::copy to copy it to another filename ;)

Hm... seems that i broke something in the database that contains the blobs, but older, not deleted version work, they start konsole, xterm or wine
€dit: oh wait, the konsole doesn't start, i get no output, but it is a valid binary according to file