How to fix xdg-mime for text formats

freedesktopmime-typestext;

My xdg-mime makes mistakes. It recognizes an .html file containing Java code as a C++ file.

I don't quite know the internal mechanism of xdg (eg. how it determines filetype). And I want to know how to fix this problem by giving preference to judgement by filename extensions.

Of course other solutions are welcome. And explanation of mechanisms of xdg-* is even more welcome.

Best Answer

xdg-mime is part of a collection of scripts called xdg-utils, "a set of common interfaces for desktop environments (DE)". In your particular case, I'll just quote MestreLion's comment from here:

If you dont have any desktop enviroment, you should not use xdg-mime (or any other xdg tool). xdg is meant to provide interoperability between different desktop enviroments, but not when there is none. Think of xdg as desktop-agnostic, but not "desktop-atheist"

As to the internal mechanics of xdg-utils... They're just shell scripts checking for a DE and, IF found, calling that particular DE's file tools to perform a certain task. In GNOME xdg-open calls gvfs-open and xdg-mime calls gvfs-info (both gvfs-* tools giving preference to judgement by file extension). In your case, no DE is found so xdg-mime falls back to file[1] to get information about the file content type. Here is the code section from xdg-mime for "generic" DE (or rather unknown):

info_generic() {
DEBUG 1 "Running file -i \"$1\""
/usr/bin/file -i "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "//

if [ $? -eq 0 ]; then
    exit_success
else
    exit_failure_operation_failed
fi }

Though it is true file[1] results are not always 100% accurate, there's not much you can do about that (it's still the best tool for this job IMO). However, if you want to determine the mime types based only on files extensions (a la windows) you could write your own script. Here is a simple python example:

#!/usr/bin/python2

import os
import sys
import mimetypes

if len(sys.argv) != 2:
    print ("Usage: ./pymime filename")
    raise SystemExit

testfile = os.path.abspath(sys.argv[1])
mt = mimetypes.guess_type(testfile, strict=True)[0]
print ("%s" % mt)
sys.exit(0)

Name it pymime and run:

./pymime yourfile