Globally disable GNOME's Trash in Debian-based distributions

From The IT Community
Jump to: navigation, search

GNOME's built-in Trash has one particularly annoying feature: every time one deletes a file on a mounted drive, a ".Trash-<UID>" folder is created. This happens regardless of whether the storage device in question is formatted with a Linux filesystem, or not (e.g. USB flash drives or Windows shared folders).

If you are like me and want the Trash gone for good, here is a solution that will disable this "feature" globally, system-wide for everyone:

  1. BEWARE: this guide proposes to replace GNOME's libgio with a custom-compiled version which re-routes all calls of g_file_trash() to its pendant g_file_delete().
    Modifying system libraries is not without risks – I cannot accept any responsibility for any loss, disruption or damage to your data or computer system which may occur as a result of following the steps provided in this guide!
    Also, the following procedure will need to be repeated every time a system update causes a new libglib to be installed:

    $ sudo apt-get dist-upgrade
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Calculating upgrade... Done
    The following packages will be upgraded:

  2. Before getting started, make sure the latest updates are installed:

    $ sudo apt-get dist-upgrade

    Also, it would be wise to get answers to some basic questions first:
    • Which package contains GIO?

      $ apt-file search -x 'libgio-.*?\.so'
      libglib2.0-0: /usr/lib/x86_64-linux-gnu/
      libglib2.0-0: /usr/lib/x86_64-linux-gnu/

      See also: Glib
    • Where are these files located?

      $ locate

      Note: all commands given in the remainder of this guide will refer to this specific version of libgio – on other systems, some adjustments will be necessary!

  3. Download source package for libglib-2.0.0:

    $ sudo apt-get source libglib2.0-0
    $ ls -l
    drwxr-xr-x 14 root root 4096 2015-05-01 11:03 glib2.0-2.42.1
    -rw-r--r-- 1 root root 68072 2014-11-12 23:24 glib2.0_2.42.1-1.debian.tar.xz
    -rw-r--r-- 1 root root 3119 2014-11-12 23:24 glib2.0_2.42.1-1.dsc
    -rw-r--r-- 1 root root 6985120 2014-11-12 23:24 glib2.0_2.42.1.orig.tar.xz

  4. Install build dependencies for libglib-2.0.0:

    $ sudo apt-get build-dep libglib2.0-0

  5. Extract glib2.0_2.42.1.orig.tar.xz into a newly created subdirectory ./extr:

    $ mkdir -p extr && tar xf glib2.0_2.42.1.orig.tar.xz -C extr/ && cd extr

  6. Open the file „glib-2.42.1/gio/gfile.c“ in an editor of your choice, locate the function g_file_trash() and replace its body with a
    single return (g_file_delete (...)) statement, as shown below (any existing code code should be commented out with /* ... */):

    $ sudo gedit glib-2.42.1/gio/gfile.c

g_file_trash (GFile         *file,
              GCancellable  *cancellable,
              GError       **error)
  /* (Unconditionally) bypass the trash: */
  return (g_file_delete (file, cancellable, error));

/*// Existing code commented out:
  GFileIface *iface;

  g_return_val_if_fail (G_IS_FILE (file), FALSE);

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return FALSE;

  iface = G_FILE_GET_IFACE (file);

  if (iface->trash == NULL)
      g_set_error_literal (error,
                          G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                          _("Trash not supported"));
      return FALSE;

  return (* iface->trash) (file, cancellable, error);
    • A comparison of this (modified) gfile.c with the one in the glib2.0-2.42.1 directory downloaded in step 2 should now reveal a single change:

      $ diff -b glib-2.42.1/gio/gfile.c ../glib2.0-2.42.1/gio/gfile.c
      < /* (Unconditionally) bypass the trash: */
      < return (g_file_delete (file, cancellable, error));<
      < /*// Existing code commented out:
      < */

    • If the comparison in the previous step checks out, we're ready to proceed:

      # Copy the modified gfile.c over the one extracted in step 2:
      $ sudo cp glib-2.42.1/gio/gfile.c ../glib2.0-2.42.1/gio/gfile.c

      # Repack the glib2.0_2.42.1.orig.tar.xz archive:
      $ sudo tar cJf ../glib2.0_2.42.1.orig.tar.xz . && cd ..
      $ ls -l glib2.0_2.42.1.orig.tar.xz
      -rw-r--r-- 1 root root 7312816 glib2.0_2.42.1.orig.tar.xz

      # Remove the temporary extr directory:
      sudo rm -rf extr/

  1. Change into the glib2.0-2.42.1 directory downloaded in step 2, log in as the root user and run dpkg-buildpackage:

    $ cd glib2.0-2.42.1/
    $ sudo su root
    # dpkg-buildpackage

    1. In some cases, one of GLib's automated tests may "hang", thus preventing dpkg-buildpackage from completing the build:

      PASS: gsettings 23 /gsettings/no-read-binding
      ^Cmake[9]: *** Deleting file 'gsettings.log'
      Makefile:4163: recipe for target 'gsettings.log' failed
      make[9]: *** [gsettings.log] Interrupt

    2. If one is willing to skip the test suite, it's possible to solve this by invoking dpkg-buildpackage with the nocheck option (after having aborted the hanging test with <Ctrl-C>):

      # DEB_BUILD_OPTIONS=nocheck dpkg-buildpackage

  2. A successful run of dpkg-buildpackage will result in a set of newly created .DEB archives, which could be installed with dpkg:

    # exit
    $ ls -l ../*.deb
    -rw-r--r-- 1 root root 2400624 ../libglib2.0-0_2.42.1-1_amd64.deb
    -rw-r--r-- 1 root root 6827444 ../libglib2.0-0-dbg_2.42.1-1_amd64.deb
    -rw-r--r-- 1 root root 1674442 ../libglib2.0-0-refdbg_2.42.1-1_amd64.deb
    -rw-r--r-- 1 root root 1335798 ../libglib2.0-bin_2.42.1-1_amd64.deb
    -rw-r--r-- 1 root root 2172456 ../libglib2.0-data_2.42.1-1_all.deb
    -rw-r--r-- 1 root root 2645490 ../libglib2.0-dev_2.42.1-1_amd64.deb
    -rw-r--r-- 1 root root 2677670 ../libglib2.0-doc_2.42.1-1_all.deb
    -rw-r--r-- 1 root root 2256398 ../

  3. For our purposes, however, it is better to simply replace the systems with the newly compiled one (after making a backup copy):
    # Backup current `libgio':

    $ sudo cp /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/
    $ sudo touch -r /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/

    After that it's necessary to reboot the system into the recovery mode, as the last step won't work while the GUI is running:
    # Install libgio's replacement:

    root@deb:~# cd /home/<your user>/libglib2.0-0/glib2.0-2.42.1/
    root@deb:/.../glib2.0-2.42.1# find ./debian/libglib2.0-0/usr/lib/ -name '*' | xargs ls -l
    -rw-r--r-- 1 root root 1541864 ./debian/libglib2.0-0/usr/lib/x86_64-linux-gnu/
    root@deb:/.../glib2.0-2.42.1# cp ./debian/libglib2.0-0/usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/

    Please also make sure that all* symlinks under /usr/lib/x86_64-linux-gnu actually point to this updated!

  4. Reboot your system - the Trash will now no longer be used!

Was this article helpful? Then please donate to keep The IT Community alive...

If you found this article helpful please share it, comment and help others by writing your own article.

Translate this page: