Moving bundled directories to SNAP_DATA

I’m currently trying to bundle an Electron app using electron-builder. I’m making use of the “extraResources” flag for the configuration which takes the specified directories and bundles them along with the application. I need to be able to write to the resources/ directory it creates, and on Windows it’s placed in %APPDATA%. On Linux however, the files are bundled inside /snap/$app/$version/resources which is read only.

I’ve tried looking into using the install hook to see if I can move / copy the resources over to SNAP_DATA but after putting echo $(ls) > "$SNAP_DATA/output.txt" , it looks like the hook is in the root directory and I don’t know how I can access resources/ to copy it over.

Is it possible to do what I’m trying to accomplish via hooks? Or is there another approach I could take?

there are more variables you can use in an install hook, regardless if it is running in / :wink:

#! /bin/sh

# if $SNAP_DATA/resources does not exist ...
if ! ls -d $SNAP_DATA/resources >/dev/null 2>&1; then
  # copy the dir from the snap to the writable dir
  cp -a $SNAP/resources $SNAP_DATA/
fi

Snap packages are distributed as read-only file systems, which is why you can’t write to that location. $SNAP_DATA likely wouldn’t help either, since only root will be able to write to that location. I’d expect you’d run into the same sort of problems on other platforms when running as the a normal user with an application installed as the administrator.

The usual way to get a location to store user configuration in a platform independent way would be to use app.getPath, probably with "userData" as the argument.

Using what @orga mentioned, would it be bad practice to instead make the hook

#! /bin/sh

# if $SNAP_DATA/resources does not exist ...
if ! ls -d $SNAP_DATA/resources >/dev/null 2>&1; then
  # copy the dir from the snap to the writable dir
  cp -a $SNAP/resources $SNAP_DATA/
  chmod -R 777 $SNAP_DATA/resources
fi

To make resources/ writable?

it shold be writable by root already … making it 777 wont gain you much since the parent dir is also only writable by root …

this only actually works well for daemons as @jamesh mentioned above … to make this usable for an enduser app you instead want to use a command-chain wrapper like:

 #! /bin/sh

# if $SNAP_USER_DATA/resources does not exist ...
if ! ls -d $SNAP_USER_DATA/resources >/dev/null 2>&1; then
  # copy the dir from the snap to the writable dir
  cp -a $SNAP/resources $SNAP_USER_DATA/
fi

exec "$@"

this will copy the resources dir to /home/$USER/snap/<snap name>/current which is fully writable for the user…

So would there be no way to make a shared folder for every user? My thought process was 777 would change the permissions for other to rwe.

So after trying what you suggested, it doesn’t actually appear like anything is in the directory you mentioned. Is there somewhere else resources could have moved to?