Firefox temporary files

firefoxtmp

In Firefox, when I choose to open a file without saving it and without an internal plugin (like a pdf or a doc file) the default behavior is that the file is saved in /tmp as a temporary file and that the chosen application (like mupdf or libreoffice) is opened with the temporary file as input.
The filename of the temporary file is usually the original name of the downloaded file, unless a file with the same name is already present in /tmp. In such case, a suffix is appended to the filename (-1, -2, -3, etc) before its extension.
The temporary files are deleted from /tmp when Firefox is closed (unless the default behavior is changed).

I think it would be much more convenient to delete each temporary file when it is not needed anymore (i.e., when the application using it stops using it). Is there a way to implement this?

As an alternative, a better behavior for me would be to replace the existing file in /tmp with the new one in case a file with the same name already exists? (i.e., without changing the filename). Is this possible?

Best Answer

To delete the temporary file as soon as possible, you can write a wrapper, like:

#!/bin/sh
the-application "$1"
rm -f "$1"

replacing the-application by the name of the real executable, and ask Firefox to use this wrapper instead of the application. Or:

#!/bin/sh
the-application "$1"
case "$1" in
  /tmp/*) rm -f "$1" ;;
esac

This form is safer in case future Firefox versions do not create a new file for "file:" URL's (you don't want the file to be removed in this case).

Note that some applications return immediately and may fail to work correctly if the file is removed before the application is quit. In such a case, there isn't much you can do (at least in a reliable way).

Concerning the alternative (to replace an existing file with the same name), this can yield clashes with other applications that use /tmp, with possible security implications. This is not a good idea in general.

Related Question