Hi, I try to write a blob to a file and then execute it. My code:
source:
ProgramLauncher
::ProgramLauncher ( 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";
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 );
}
}
{
QString path
= QDir::tempPath() + "/" + file
->fileName
();
qDebug() << "Launching filename " << path;
qDebug
() <<
QProcess::startDetached ( path
);
}
ProgramLauncher::~ProgramLauncher()
{
}
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()
{
}
To copy to clipboard, switch view to plain text mode
header:
class ProgramLauncher
: public QObject{
Q_OBJECT
public:
ProgramLauncher
( QObject *parent
= 0 );
~ProgramLauncher();
public slots:
void launch ( int id );
private:
BinaryLoader *loader;
};
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;
};
To copy to clipboard, switch view to plain text mode
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"
false
5
tmpFile: "XN1408" opened
wrote 24791336 into the file
Launching filename "/tmp/XN1408"
false
To copy to clipboard, switch view to plain text mode
Why 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
Bookmarks