Results 1 to 12 of 12

Thread: QTemporaryFile problems

  1. #1
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTemporaryFile problems

    Hi, I try to write a blob to a file and then execute it. My code:
    source:
    Qt Code:
    1. ProgramLauncher::ProgramLauncher ( QObject *parent )
    2. : QObject ( parent )
    3. {
    4. loader = new BinaryLoader ( );
    5. connect ( loader, SIGNAL ( dataLoaded ( int, QByteArray ) ), this, SLOT ( loaded ( int, QByteArray ) ) );
    6. }
    7.  
    8. void ProgramLauncher::launch ( int id )
    9. {
    10. qDebug() << id;
    11. loader->getData ( "bin_programs", id );
    12. }
    13.  
    14. void ProgramLauncher::loaded ( int handle, QByteArray data )
    15. {
    16. Q_UNUSED ( handle );
    17. // QString path = QDir::tempPath() + "XXXXXX";
    18. QTemporaryFile *tmpFile = new QTemporaryFile ( QString ( "XXXXXX" ) );
    19. if ( !tmpFile->open ( ) )
    20. {
    21. return;
    22. }
    23. qDebug() << "tmpFile: " << tmpFile->fileName() << " opened";
    24. qint64 size = tmpFile->write ( data );
    25. if ( size != -1 )
    26. {
    27. qDebug() << "wrote " << size << " into the file";
    28. execute ( tmpFile );
    29. }
    30. }
    31.  
    32. void ProgramLauncher::execute ( QTemporaryFile *file )
    33. {
    34. QString path = QDir::tempPath() + "/" + file->fileName();
    35. qDebug() << "Launching filename " << path;
    36. qDebug() << QProcess::startDetached ( path );
    37. }
    38. ProgramLauncher::~ProgramLauncher()
    39. {
    40. }
    To copy to clipboard, switch view to plain text mode 
    header:
    Qt Code:
    1. class ProgramLauncher : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. ProgramLauncher ( QObject *parent = 0 );
    6. ~ProgramLauncher();
    7. public slots:
    8. void launch ( int id );
    9. void loaded ( int handle, QByteArray data );
    10. private:
    11. void execute ( QTemporaryFile *file );
    12. BinaryLoader *loader;
    13. };
    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:
    Qt Code:
    1. 5
    2. tmpFile: "XN1408" opened
    3. wrote 24791336 into the file
    4. Launching filename "/tmp/XN1408"
    5. 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

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTemporaryFile problems

    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.
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTemporaryFile problems

    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:
    Qt Code:
    1. ProgramLauncher::ProgramLauncher ( QObject *parent )
    2. : QObject ( parent )
    3. {
    4. loader = new BinaryLoader ( );
    5. connect ( loader, SIGNAL ( dataLoaded ( int, QByteArray ) ), this, SLOT ( loaded ( int, QByteArray ) ) );
    6. }
    7.  
    8. void ProgramLauncher::launch ( int id )
    9. {
    10. qDebug() << id;
    11. loader->getData ( "bin_programs", id );
    12. }
    13.  
    14. void ProgramLauncher::loaded ( int handle, QByteArray data )
    15. {
    16. Q_UNUSED ( handle );
    17.  
    18. tmpFile = new QTemporaryFile ( QString ( QDir::tempPath() + "/XXXXXX" ) );
    19. if ( !tmpFile->open ( ) )
    20. {
    21. return;
    22. }
    23. qDebug() << "tmpFile: " << tmpFile->fileName() << " opened";
    24. qint64 size = tmpFile->write ( data );
    25. if ( size != -1 )
    26. {
    27. qDebug() << "wrote " << size << " into the file";
    28. QString fname = tmpFile->fileName();
    29. qDebug() << tmpFile->autoRemove();
    30. tmpFile->close();
    31. qDebug() << QFile::copy ( fname, fname + "_bak" );
    32. qDebug() << QProcess::startDetached ( fname );
    33. delete tmpFile;
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 
    And it does not launch the binaries although they are valid linux executables like xterm and konsole (just for testing)

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTemporaryFile problems

    You can't delete the file while the process is executing, that's why the delete doesn;t succeed.

  5. #5
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTemporaryFile problems

    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...

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTemporaryFile problems

    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.

  7. #7
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTemporaryFile problems

    Thanks
    Okay, i'm using this code now:
    Qt Code:
    1. if ( size != -1 )
    2. {
    3. qDebug() << "wrote " << size << " into the file";
    4. QFile file ( tmpFile->fileName() );
    5. tmpFile->close();
    6. if ( file.open( QIODevice::ReadOnly ) && file.exists() )
    7. {
    8. qDebug() << file.fileName();
    9. file.setPermissions ( QFile::ReadOwner | QFile::WriteOwner |
    10. QFile::ExeOwner | QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther );
    11. qDebug() << QProcess::startDetached ( file.fileName() );
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by C167; 21st June 2008 at 15:08.

  8. #8
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTemporaryFile problems

    any ideas? on windows, its the same problem and the program must be finished by tomorrow (need some time to test it etc.)

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTemporaryFile problems

    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.

  10. #10
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTemporaryFile problems

    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)

  11. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTemporaryFile problems

    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.

  12. #12
    Join Date
    Jan 2008
    Posts
    40
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTemporaryFile problems

    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
    Last edited by C167; 22nd June 2008 at 10:02.

Similar Threads

  1. flicker and wierd resize problems ...
    By momesana in forum Qt Programming
    Replies: 1
    Last Post: 12th May 2008, 18:00
  2. Problems compiling hello world with edyuk
    By mck182 in forum Newbie
    Replies: 1
    Last Post: 7th May 2008, 16:45
  3. Replies: 2
    Last Post: 8th March 2007, 22:22
  4. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 15:39
  5. problems with Opengl
    By SlawQ in forum Qt Programming
    Replies: 4
    Last Post: 12th February 2006, 22:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.