Bug 89902

Summary: xdg-open does not exit if it does not recognize the mimetype
Product: Portland Reporter: Danny Arnold <despair.blue>
Component: xdg-utilsAssignee: Portland Bugs <portland-bugs>
Status: RESOLVED MOVED QA Contact:
Severity: critical    
Priority: medium    
Version: 1.1.0 rc3   
Hardware: x86-64 (AMD64)   
OS: Linux (All)   
Whiteboard:
i915 platform: i915 features:

Description Danny Arnold 2015-04-04 12:47:29 UTC
How to reproduce:

```
$ touch index.html
$ xdg-open index.html#test
xdg-mime: file 'index.html#test' does not exist
xdg-mime: mimetype argument missing
Try 'xdg-mime --help' for more information.
Could not determine mimetype for file: index.html#test
```

The process simply never returns, which breaks any app that calls xdg-open synchronously (like github atom).

Version: xdg-open 1.1.0 rc3
Comment 1 Rex Dieter 2015-04-04 15:44:34 UTC
I'd argue that's not a fair test,

what happens if you try:

xdg-open file://`pwd`/index.html#test
Comment 2 Rex Dieter 2015-04-04 15:50:04 UTC
Interestingly,

$ DE="generic" XDG_CURRENT_DESKTOP="" xdg-open index.html#test
xdg-mime: file 'index.html#test' does not exist
xdg-mime: mimetype argument missing
Try 'xdg-mime --help' for more information.

but then it opens the file ok in firefox for me.


In KDE, I get an error:
The file or folder ".../index.html#test" does not exist.
(the same error as if I run by hand:
kde-open index.html#test
)

but as per my initial comment, Qt/KDE is picky about URIs, it insists you give a protocol (ie, file://)

Either way, there's still a problem with xdg-mime failing somewhere here, I'll see if I can find more what's going on there.
Comment 3 Danny Arnold 2015-04-04 15:52:42 UTC
Same thing happens:

```
xdg-mime: file '/home/despairblue/index.html#test' does not exist
xdg-mime: mimetype argument missing
Try 'xdg-mime --help' for more information.
Could not determine mimetype for file: /home/despairblue/index.html#test
```

And then it just never exits :/

I'm not using any desktop environment, just i3.
Comment 4 Rex Dieter 2015-04-06 12:40:34 UTC
I think this should help:

--- /usr/bin/xdg-open.orig      2015-04-06 07:39:33.572166692 -0500
+++ /usr/bin/xdg-open   2015-04-06 07:36:27.569417647 -0500
@@ -707,7 +707,7 @@
         file_check=${file_check%%\?*}
         check_input_file "$file_check"
 
-        open_generic_xdg_file_mime "$file"
+        open_generic_xdg_file_mime "$file_check"
 
         if which run-mailcap 2>/dev/null 1>&2; then
             run-mailcap --action=view "$file"


Can you verify ?
Comment 5 Rex Dieter 2015-04-06 13:19:02 UTC
actually, that's the wrong fix, it needs to be fixed inside open_generic_xdg_file_mime

But, I'd like to know where the hang occurs for you, can you insert a:

set -x 

somewhere near the top of xdg-open, and re-run the test case?  I'd like to see the last few lines of output
Comment 6 Rex Dieter 2015-04-06 13:37:36 UTC
OK, this should be a better fix (than comment #4):

diff -u /usr/bin/xdg-open.orig /usr/bin/xdg-open
--- /usr/bin/xdg-open.orig      2015-04-06 08:27:10.396932209 -0500
+++ /usr/bin/xdg-open   2015-04-06 08:29:31.534667495 -0500
@@ -706,7 +706,8 @@
         file_check=${file_check%%\?*}
         check_input_file "$file_check"
 
-        open_generic_xdg_file_mime "$file"
+        filetype=`xdg-mime query filetype "$file_check" | sed "s/;.*//"`
+        open_generic_xdg_mime "$file" "$filetype"
 
         if which run-mailcap 2>/dev/null 1>&2; then
             run-mailcap --action=view "$file"
Comment 7 Rex Dieter 2015-04-06 14:36:48 UTC
Went ahead and committed the immediate fix,
http://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=3e9a91b44d8d4639000fbe2b49ee303cd3f836e0

I'd still be interested in output per comment #5 if you don't mind (to help find out why xdg-open would not exit properly).
Comment 8 Danny Arnold 2015-04-07 19:33:05 UTC
Sure, thanks, I'll try that out when I'm at home and will get back to you.
Comment 9 Danny Arnold 2015-04-08 09:44:01 UTC
Here is the complete output: https://gist.github.com/despairblue/c95abf709787faa41bef

The problem seems to be that it starts lynx if I add an anchor. Without the anchor it starts google-chrome-stable.
Comment 10 Danny Arnold 2015-04-08 09:45:19 UTC
Or maybe it tries to find out if lynx is installed by executing it. I'll have to take a closer look at xdg-open itself.
Comment 11 Danny Arnold 2015-04-11 00:49:15 UTC
Ok the problem is that xdg-mime and mimeopen think that the anchor is part of the file name, thus they fail to return a mime type for it, so xdg-open goes through the list of known browsers and falls through to lynx. Which it then runs with eval.

1. I don't think lynx should be part of that list unless it's started with a new terminal emulator, otherwise it just opens and blocks xdg-open.
2. The list should be more exhaustive, if it'd contain chromium or google-chrome-stable it would work even on archlinux.
3. It might be better to use hash and exec instead of eval when going through the browser, like this:
```
for browser in $BROWSER; do
        if [ x"$browser" != x"" ]; then
            browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
            if [ $? -ne 0 ]; then
                browser_with_arg=$browser;
            fi

            hash $browser 2>/dev/null
            if [ $? -ne 1 ]; then
                if [ x"$browser_with_arg" = x"$browser" ]; then
                    exec $browser "$1" #$xdg_redirect_output;
                else exec $browser_with_arg #$xdg_redirect_output;
                fi
            fi
        fi
    done
```
That way lynx could stay in the list and would work.

So, should I write a patch adding more known browsers, removing lynx, changing the for loop or do you see another solution?

regards
Comment 12 rkfg 2015-04-11 22:52:01 UTC
(In reply to Rex Dieter from comment #7)
> Went ahead and committed the immediate fix,
> http://cgit.freedesktop.org/xdg/xdg-utils/commit/
> ?id=3e9a91b44d8d4639000fbe2b49ee303cd3f836e0
> 
> I'd still be interested in output per comment #5 if you don't mind (to help
> find out why xdg-open would not exit properly).

Typo:

--- a/scripts/xdg-open.in
+++ b/scripts/xdg-open.in
@@ -276,7 +276,7 @@ open_generic()
         file_check=${file_check%%\?*}
         check_input_file "$file_check"
 
-        filetype=`xdg-mime query filetype "$filecheck" | sed "s/;.*//"`
+        filetype=`xdg-mime query filetype "$file_check" | sed "s/;.*//"`
         open_generic_xdg_mime "$file" "$filetype"
 
         if which run-mailcap 2>/dev/null 1>&2; then
Comment 13 Rex Dieter 2015-04-11 23:51:17 UTC
Thanks, typo fixed.

As far as browser handling goes, my hope is that code path never gets reached.  I'm tempted to remove it (though probably will wait for 1.2 to do that).

In the meantime, if you have any simple, portable ways to make this better, I'd welcome it.
Comment 14 GitLab Migration User 2019-02-16 13:36:37 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to freedesktop.org's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.freedesktop.org/xdg/xdg-utils/issues/80.

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.