sindresorhus / trash

Move files and directories to the trash
MIT License
2.58k stars 79 forks source link

Fix trashing on Linux #114

Closed gsittyz closed 3 years ago

gsittyz commented 3 years ago

Fixes #56 .

The changes are:

  1. DeletionDate should be written like %Y-%m-%dT%H:%M:%S in the local time.
  2. Write the trash info file completely before the file is moved to trash.

You can check the trash implementation in GNOME: https://gitlab.gnome.org/GNOME/glib/blob/glib-2-56/gio/glocalfile.c .

A code starting at line 2195 shows the appropriate style of trash info:

  {
    time_t t;
    struct tm now;
    t = time (NULL);
    localtime_r (&t, &now);
    delete_time[0] = 0;
    strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
  }

  data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
              original_name_escaped, delete_time);

As for the second change, a comment starting at line 2181 is helpful. This comment and discussion ( https://bugzilla.gnome.org/show_bug.cgi?id=749314 ) shows the appropriate order of moveFile and writeFile.

  /* Write the full content of the info file before trashing to make
   * sure someone doesn't read an empty file.  See #749314
   */

I confirmed that the fixed code worked fine in Ubuntu 18.04. When I reverse the order of moveFile and writeFile in js, I always reproduce the hashed filename issue.


IssueHunt Summary ### Referenced issues This pull request has been submitted to: - [#56: Trashing a file on Ubuntu Linux deletes it unrecoverably](https://issuehunt.io/repos/19789032/issues/56) ---
sindresorhus commented 3 years ago

Thanks for fixing this :)