Skip to content

Autosave Feature

Prakhar Agarwal requested to merge prakharagarwal1/purr-data:autosave into master

Autosave Feature

Description

The autosave feature solves the problem of data loss in case of application crashes. It automatically creates a backup copy of the dirty canvases in a hidden directory, which gets replaced with the latest changes after every specified interval of time(sec).

How to Use?

The Autosave feature is turned on by default and the autosave interval is set to 10 seconds which can be configured from the preferences dialog. Here's a screenshot of the setting (at the bottom of the dialog).

Preferences_Dialog

After an app crash, when the user opens up the app, a dialog will show up, listing all the files that can be recovered. If the user selects "OK" then all the listed files are recovered and opened up on the screen.

Recovery_Dialog

The user can then manually save those files.

Internal Working

  1. Once the engine is ready, we take the autosave interval value(say x) and call a setInterval() function, which makes calls to the autosave function in g_readwrite.c file after every x seconds.

  2. Now we generate a unique filename for the backup file by appending the current timestamp in "%Y-%m-%d_%H-%M-%S" format to the name of the canvas <name_of_canvas>_<timestamp>.pd and save it inside of a hidden directory which is ~/AppData/Roaming/Purr-Data/autosave in Windows and ~/.purr-data/ in Linux/macOS.

  3. We maintain a autosave.json file that stores key-value pairs that maps <original_canvas_file_path>:<autosaved_backup_file_path>. This helps in saving only a single backup copy of a canvas while recovering files after a crash. Here's some data inside the JSON file.

    { "/home/prakhar/purr-data/Untitled-1": "/home/prakhar/.purr-data/autosave/Untitled-1_2023-08-26_04-30-51.pd", 
      "/home/prakhar/purr-data/Untitled-2": "/home/prakhar/.purr-data/autosave/Untitled-2_2023-08-26_04-30-51.pd", 
      "/home/prakhar/purr-data/Untitled-3": "/home/prakhar/.purr-data/autosave/Untitled-3_2023-08-26_04-30-51.pd" 
    }
  4. When the user boots up after the crash, we check for any backup files present in the hidden folder by scanning the autosave.json file, If there are, then they get listed in a popup dialog which the user can then recover by clicking "OK".

  5. gl_recovered=1 flag is set to the newly opened backup files, to differentiate them from normal files and force the user to save as the file instead of just save on a new location.

Screencast

https://youtu.be/S3Tu2epynQY

Signed-off-by: prakharagarwal1 prakharagarwal3031@gmail.com

Merge request reports