Questions about flatpak and tmp files

Context: text editing application, GTK4, Flatpak and Python.

I need to periodically save the text being edited to avoid data loss in case of application or system crash or power outage. I have some doubts about the best way to do this:

  1. During development, I was unable to know exactly where the tmp directory is in the context of the application. I created a file in the directory, I can see the file in the application but I don’t know where it is in the general context of the system. (tmp, var/tmp, home/.var, etc…?)

  2. Gio.File has a temporary file creation function, which I am using, which says that the file is deleted when there are no more references to it. Is it the right choice? As Python doesn’t work with direct unref, I don’t know if the file will remain existing after a problem.

  3. How to simulate a crash to check whether or not the temporary file remains?


  1. What about using GLib.get_user_cache_dir() ?
  2. That’s actually a real issue, Python may keep the Gio.File object alive an undetermined amount of time before it gets collected and disposed. Maybe use something like concatenating GLib.get_user_cache_dir() with sha256sum(gfile.get_uri()) to create the backup file
  3. my favorite python killer is import ctypes ; ctypes.string_at(0), but that requires adding a way to call that when running the app. Alternatively, you can use flatpak kill <app-id> (see here).
1 Like

Thanks for the answer.

1 - The tmp directory within the directory returned by GLib.get_user_cache_dir() is empty, the tmp file is not there either (in fact, I had already looked in that directory and that’s where all my confusion comes from).

2 - I don’t know if I understood your suggestion. Are you suggesting not to use Gio.File.new_tmp and instead create a common file within the user_cache_dir and delete the file when exiting the application? Or manually create a second file as a backup of the temporary file created with Gio.File.new_tmp?

It is also important to explain that whenever the application is opened, it will look in the directory where the temporary files will be saved to see if there are any temporary files that have not been deleted, as this will indicate that the application exited unexpectedly and that the content of that file needs to be recovered.

My suggestion is to not use Gio.File.new_tmp because:

  • no guarantee of being persistent across reboots
  • no guarantee that Python will delete the tmp file after you close the original one, due to unpredictable garbage collection

Instead, I recommend to use Gio.File.new_for_path(os.path.join(GLib.get_user_cache_dir(), app_id, sha256sum(gfile.get_uri()))), so you have an agnostic, portable and reliable way to store, detect, restore and detete the backups.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.