How to make a snap that read only/access files in /usr/share/app_folder

Hi,

I’m quite newbie in snaps and was trying to make a snap that ls/cat/less files into a folder in /usr/share/app_name, but I always get permission denied. I tried using without success. Also try to ship the common-licenses folder into snap dir , but also , no success. I’m not sure what is the right way to achieve this.

snapcraft.yaml:
name: common-licenses
base: core18
version: ‘0.1’
summary: A bash command that list and reads/print common licenses available
description: |
A bash command that list and prints common licenses available in
the system.

grade: stable
confinement: strict

parts:
common-licenses:
source: https://github.com/kirotawa/common-licenses.git
plugin: nil
override-build: |
mkdir -p $SNAPCRAFT_PART_INSTALL/bin
cp common-licenses $SNAPCRAFT_PART_INSTALL/bin

apps:
common-licenses:
command: common-licenses

The shell script just does some ls/cat/less on common-licenes/ dir.

The confinement rules prohibit reading and writing files in /usr so you cannot access anything there. You need to ship everything your snap needs inside the snap itself, and access from there. The best thing would be to get your app to read from $SNAP, but you can use layouts to massage a troublesome app’s view of the world if you definitely cannot adjust it to read from $SNAP.

How to achieve the first approach?

Should I just copy the common-licenses folder to : project/snap/ ?
and call in the script ls/cat/less /snap/common-licenses/ ?
My attempts to it with ends up with: current x1 instead the content of common-licenses.

├── common-licenses
├── common-licenses_0.1_amd64.snap
├── LICENSE
├── Makefile
└── snap
├── common-licenses
│ ├── Apache-2.0
│ ├── Artistic
│ ├── BSD
│ ├── CC0-1.0
│ ├── GFDL
│ ├── GFDL-1.2
│ ├── GFDL-1.3
│ ├── GPL
│ ├── GPL-1
│ ├── GPL-2
│ ├── GPL-3
│ ├── LGPL
│ ├── LGPL-2
│ ├── LGPL-2.1
│ ├── LGPL-3
│ ├── MPL-1.1
│ └── MPL-2.0
└── snapcraft.yaml

Ok, I tried this now and it works - I just need to understand it now.

name: common-licenses base: core18 version: ‘0.1’ summary: A bash command that list and reads/print common licenses available description: | A bash command that list and prints common licenses available in the system.

grade: stable confinement: strict

parts: common-licenses: source: . plugin: nil override-build: | mkdir -p $SNAPCRAFT_PART_INSTALL/bin cp common-licenses $SNAPCRAFT_PART_INSTALL/bin stage-packages: - base-files apps: common-licenses: command: common-licenses

layout: /usr/share/common-licenses: bind: $SNAP/common-licenses/current/usr/share/common-licenses

and in my shell script I pass the path: /snap/common-licenses/current/usr/share/common-licenses

If is there any other/better approach please let me know :slight_smile:

Ok, I now understood.

So, base-files make the files available in the snap and can access through this path.

My last question is, how to achieve the same either adding the files into the snap or using layout , and so not changing the script path. Is that possible?

To make a path from your snap appear at the expected system location, you need to adjust your layout slightly to fix the path:

layout:
  /usr/share/common-licenses:
    bind: $SNAP/usr/share/common-licenses

The $SNAP variable is defined as /snap/[snap-name]/[snap-revision] so you only need to include the path from the top of your snap filesystem and prefix it with $SNAP/. So to reference /snap/common-licenses/current/usr/share/common-licenses you would specify $SNAP/usr/share/common-licenses

Thanks Daniel!!

And what if I wanted to ship files or directories into my snap. By example:
project_name/cmd.sh
project_name/files_dir/

so in short, is that possible to ship files_dir and access it from the snap , as by example, cmd.sh calling a ls to files_dir or other commands read only like: less, cat in files into this dir?