The implementation principle of file association under Linux
Reprint, please indicate the source: http://blog.csdn.net/absurd/
We know that under Windows, when you double-click an executable file, the File Manager automatically runs the application. When you double-click a data file, the File Manager opens it with the application associated with it. The association between the data file and the application is achieved through the registry: The File Manager queries the registry, finds the application for the data file, and then runs the application and passes the file name of the data file as a command-line argument to it.
The way this file is associated is very easy to use, eliminating the hassle of starting the application before opening the file. The desktop environment under Linux has a similar function, and the implementation is more reasonable. Recently responsible for developing a resource manager, GNOME has a powerful resource manager Nautilus, but it is too large, not only more than 100,000 lines of code, but also rely on Libgnome, GNOME-VFS and CORBA, it is not suitable for embedded applications. Ultimately we decided to develop a simplified resource manager for ourselves, but try to be compatible with existing applications, which is one of the ways to understand the relevant standards. Take these days to learn the knowledge to make a note, for interested friends for reference:
first let's look at what the file association is going to do.
1. The data file is associated with the application. An application can usually open only certain data files, such as the Image browsing tool to open picture files such as PNG, BMP, and JPEG. Opening a word has a broad meaning, including: Open, play, install, edit, print, and so on.
2. File type information. When a resource manager lists data files, it usually identifies such files with an icon, plus a short name so that users can easily distinguish it from other types of files.
Now let's look at Linux how to achieve the next.
1. Determine the type of file. The number of files is unlimited and we can only handle them by file type. How can I tell which file type a file belongs to? One might say, quite simply, to differentiate by extension. Yes, it can be done with an extension, but there are two flaws in this approach: on the one hand it's not very precise, and the file of the same extension can be completely different, such as dat files, maybe a video file, or a normal data file. On the other hand it is not very accurate, the extension can be any change, for some purpose, can be changed to the exe extension of the HTM extension.
And in Linux, the extension is only an option, many files do not have extensions, so simply using the file name extension to judge the way is certainly not. In order to better judge the file type, there are two ways of using Linux in the same way: giving priority to the Magic method, followed by the file extension method. The so-called Magic Way, is based on the contents of the document to judge. Most of the files, the internal has some specific tags, such as magic, such as BMP picture file with bm Two characters, BM is a magic. Although the adoption of the double insurance mechanism also has the possibility of miscalculation, but the probability has been greatly reduced.
2. Presentation of file types.
How the file type is represented. We say that JPEG is a picture file, that txt is a text file, and WML is an XML file. This classification is intuitive, but there are several problems: for JPEG files, it is too general to call the picture file. Some of the image browsing tool can open most picture files, but not necessarily open all picture files, it requires more detailed file type information. For both TXT and WML, they are actually text files, and some editors may be able to handle them in the same way. To avoid the classification being too thin or too coarse, Linux uses a MIME (which can refer to the relevant RFC) specification, which is categorized in a hierarchical fashion, such as:
JPEG File: Image/jpeg
Text file: Text/plain
XML file: Text/xml
This type of classification can be balanced.
3. File type of data information.
Under Linux, information about file types is usually placed in the/usr/share/mime,/usr/local/share/mime, and user directories, and all applications can share that information. In this directory, the following files are generally available:
L Aliases: Alias for file type. For example, Application/pdf is sometimes called application/x-pdf.
L Magic: Internal identification of various files used to determine file types from the contents of the file. such as BMP image file with BM beginning.
L Globs: The corresponding relationship between the extension and the file type. If the *.cpp file is text/x-c++src type.
L Packages Directory: Used to install new file types.
L Other subdirectories and the files under it: A more detailed description of the various file types. For example, the Jpeg.xml file under image describes the JPEG file type. To facilitate internationalization, these descriptive information is available in various languages.
4. icon file associated with the data file.
In Explorer, you usually use different icons to distinguish between different file types. Icons are also related to desktop themes, and the icons are different in size and appearance. Icon files are usually stored in the/usr/share/icons/theme/size/mimetypes directory.
The corresponding relationship between the file type and the icon file is implemented by the filename. For example, the JPEG file corresponding to the icon file is gnome-mime-image-jpeg.png.
(This piece is not very definite and needs further study)
5. The application is associated with the data file.
The application's association with the data file is implemented through the. Desktop file. The application needs to provide a desktop file, either in the Start menu or on the desktop. After the application is installed, desktop files are usually installed under/usr/share/applications.
You can indicate the type of file that it can manipulate in the desktop file. For example, the package installer can manipulate the rpm file, and its desktop file (system-install-packages.desktop) content is:
[Desktop Entry]
Name=install Packages
Genericname=install Packages
Comment=install new packages on the system
mimetype=application/x-rpm;
Exec=/usr/bin/system-install-packages%F
Terminal=false
Type=application
Icon=system-config-packages.png
Encoding=utf-8
Nodisplay=true
The MimeType item indicates that it can manipulate RPM-type files.
Linux than Windows the scientific place of practice.
1. Linux uses the double insurance mechanism, the file type judgment is more correct, the probability of error is less.
2. Linux separates file types to determine information and file associations so that file type information can be reused. For example, the file command can use this information to determine the type of files without having to open it.
reference materials:
Http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.13.html
Http://www.freedesktop.org/wiki/Standards_2fAddingMIMETutor
~~end~~