5

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.

1
  • @ don_crissti Hello, I don't use DE. I use a bare WM Openbox. I know xdg-mime will retransmit the file type analysis to DE specific modules. But in case of WM, I don't know whether xdg-mime has internal analyzer.
    – Cyker
    Sep 9 '12 at 21:09
2

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
1
  • Great answer! Thank you. Finally I decide to use mimeo with it's own version of xdg-mime which is more accurate.
    – Cyker
    Sep 10 '12 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.