How does the apt-get purge command work?

How does 'it' (ie. purge) know where the dependencies and configuration files are located?

How does purge find out that a particular piece of software created a certain file?

Is there some sort of configuration file that records the creation of all files by programs, so that purge can access the records and match up the files to the program during removal?


Solution 1:

Yes, there are such files. In particular, *.list, *.postrm , and *.conffiles, which are all stored in /var/lib/dpkg/info directory.

The .postrm files are post-remove scripts, that run after a package is removed. Those scripts are responsibility of the package maintainers, and each script is tailored to the specific package. If you examine /var/lib/dpkg/info/usb-modeswitch-data.postrm for example , you will see something like this:

#!/bin/sh
set -e
# Automatically added by dh_installdeb
dpkg-maintscript-helper rm_conffile "/etc/usb_modeswitch.d/230d:0001" 20101222-3 -- "$@"
# End automatically added section

There's also *.list and *.conffiles files, which are basically lists. gnome-terminal.list, for example, will list all files and directories created when gnome-terminal was installed. The *.list files are very useful for determining what installed what. *.conffiles list all the config files for a package. For instance, while xterm.list gives all the files , `xterm.conffiles* gives only conffiles. In other words, the contents of conffile will also be present in list file.

$ cat /var/lib/dpkg/info/xterm.conffiles                                       
/etc/X11/app-defaults/XTerm
/etc/X11/app-defaults/KOI8RXTerm-color
/etc/X11/app-defaults/XTerm-color
/etc/X11/app-defaults/UXTerm-color
/etc/X11/app-defaults/UXTerm
/etc/X11/app-defaults/KOI8RXTerm

I don't quite know how *.conffiles work. My assumption is that the post-removal scripts take priority, and apt-get just uses those *.conffiles to double check that files listed there were removed.

As @A.B. properly reminded me, there are also *.prerm files, some of which do remove the .conf files, but not necessarily, for instance the one for xpdf app was removing the files in /etc/xpdf/includes directory