Results 1 to 6 of 6

Thread: Open/Executing Files on Linux

  1. #1
    Join Date
    Feb 2009
    Location
    Germany, Cologne
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Open/Executing Files on Linux

    Hello,

    actually i have a problem opening or executing a file on Linux. This means if the user choose maybe a text file, i need to open the text file. If the user choose a binary like the QtCreator i need to start the application.

    On Windows its just easy by checking QFileInfo().isExecutable() and then calling QDesktopService:penUrl() or QProcess::execute().

    But on Linux that does not work as i expect. isExecutable() is in this case just useless on linux. Calling QDesktopService:penUrl() for a binary does obviously not work and QProcess::execute() cant open a text file.

    Does anyone have a suggestion how i can handle both situations correctly?

    greets

  2. #2
    Join Date
    Mar 2009
    Location
    Belchatow, Poland
    Posts
    38
    Thanks
    11
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: Open/Executing Files on Linux

    Maybe you could check permissions, executable files will have the "x" permission and text files won't.

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Open/Executing Files on Linux

    Quote Originally Posted by arturo182 View Post
    Maybe you could check permissions, executable files will have the "x" permission and text files won't.
    Any file can have the exexcutable bit set under linux; it doesn't guarantee that the file is an exectable program.

    Linux does, however, have the excellent 'file' utitlity which can provide detailed information on thousands of file types.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Open/Executing Files on Linux

    The UNIX "file" command is not that reliable on its own, and certainly not reliably portable given that strings change with versions and Linux flavours. First a binary executable:
    Qt Code:
    1. $ cp /bin/ls /tmp
    2. $ ls -l /tmp/ls
    3. -rwxr-xr-x 1 chrisw users 92072 Jun 3 08:11 /tmp/ls
    4. $ file /tmp/ls
    5. /tmp/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped
    6. # Remove execute permission
    7. $ chmod a-x /tmp/ls
    8. $ ls -l /tmp/ls
    9. -rw-r--r-- 1 chrisw users 92072 Jun 3 08:11 /tmp/ls
    10. $ file /tmp/ls
    11. /tmp/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped
    12. # Same description but NOT executable
    To copy to clipboard, switch view to plain text mode 
    Same problem with shell scripts.
    Qt Code:
    1. $ ls -l /tmp/verifyCD.sh
    2. -rwxr--r-- 1 chrisw users 611 Jun 3 08:06 verifyCD.sh
    3. $ file /tmp/verifyCD.sh
    4. /tmp/verifyCD.sh: Bourne-Again shell script text executable
    5. # ^^ good ID, and file is executable
    6. $ chmod a-x /tmp/verifyCD.sh
    7. $ ls -l /tmp/verifyCD.sh
    8. -rw-r--r-- 1 chrisw users 611 Jun 3 08:06 verifyCD.sh
    9. $ file /tmp/verifyCD.sh
    10. /tmp/verifyCD.sh: Bourne-Again shell script text executable
    11. # Attempting to execute this will fail. Huh? ^^^
    To copy to clipboard, switch view to plain text mode 
    If the execute bit is set in the file permissions (and it is a regular file) then you should try to execute it. If the user has set the bit on something that is not executable then that, I believe, is the user's problem.

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Open/Executing Files on Linux

    Quote Originally Posted by ChrisW67 View Post
    If the user has set the bit on something that is not executable then that, I believe, is the user's problem.
    Sort of. Directories always have the execute bit set. And I've seen more than my share of applications that have had their execute bits turned off for one reason or another.

    I agree, though, that the application should simply try to launch whatever file is selected, since there's also no guarantee that the file will run correctly even if it is a legitimate executable file if there are missing dynamic libraries or other resources. This is true on Windows as well.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Open/Executing Files on Linux

    Quote Originally Posted by SixDegrees View Post
    Directories always have the execute bit set.
    Indeed, that's why I stipulated that it be a regular file. This also precludes trying to execute character and block device nodes, named pipes (sockets) etc.

Similar Threads

  1. Open odt files with Qt
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 21st April 2011, 09:29
  2. Open Microsoft excel files in QT --linux
    By qixier in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2010, 09:58
  3. executing a audio player code on Embedded linux
    By sureshlohith in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 21st April 2010, 05:17
  4. Open Ms Word, Ms excel, ppt files in QT -Linux
    By amankamboj1 in forum Qt Programming
    Replies: 3
    Last Post: 5th September 2009, 08:55

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.