Bringing back the "New Document" button on right click by default

There is no such thing as “an empty plain text file”. :slight_smile:

Empty files have a application/x-zerosize MIME type—if you have a recent version of GLib and the shared-mime database—which is usually tied to the text editor. If I create a “empty-document.ods” file, I want LibreOffice Calc to open it. What I really don’t want to happen is gedit getting launched, of course, because that would make zero sense.

The current MIME type system has two tiers of operation:

  • if you pass the file name, and either the file does not exist or you explicit asked for a guess because you’re not really accessing a file on a local storage, the XDG shared-mime database will use its glob rules (which include the extension, because we are like Windows, regardless of what you may think) to determine the closest MIME type applicable
  • if you pass the file name and the file exist, or you pass a bunch of bytes from the file, the XDG shared-mime database will use its “magic” content matching rules, and return the MIME type of the file

Now, “empty-calc-file.ods” exists, but it’s empty. This means that the shared MIME database will return application/x-zerosize to Nautilus, which will launch the application that is registered for opening empty files—typically gedit, on a GNOME system. If the file didn’t exist, ironically, the MIME database would use the extension of .ods, and launch LibreOffice Calc.

Nautilus can detect the case of an empty file, and then decide to ignore it, but it needs to be modified to do so, and it’s kind of backwards to what the expected API model favours (first guess to avoid hitting the disk/network, then try to get an exact match) and it ends up in interesting edge cases like:

  1. create empty-file
  2. GIO opens empty-file and returns application/x-zerosize
  3. Nautilus queries the MIME type again, using only the file name to guess
  4. GIO does not open empty-file and returns application/octet-stream because there’s no extension

And application/octet-stream is either an executable binary or something that doesn’t have a default MIME handler.

Nautilus needs to remember that the file is empty, because if it doesn’t, it’ll need to check if the file has the execution bit set, and try and launch it as a binary. This is even less fun when dealing with volumes on NTFS/FAT file systems, because the x bit depends on the mount options.

1 Like