File: dirs.go

package info (click to toggle)
snapd 2.74.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,428 kB
  • sloc: sh: 16,966; ansic: 16,788; python: 11,332; makefile: 1,897; exp: 190; awk: 58; xml: 22
file content (129 lines) | stat: -rw-r--r-- 3,386 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2025 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package syscheck

import (
	"fmt"

	"github.com/snapcore/snapd/dirs"
	"github.com/snapcore/snapd/release"
)

func init() {
	checks = append(checks, checkSnapMountDir, checkLibExecDir)
}

var (
	// distributions known to use /snap/
	defaultDirDistros = []string{
		"ubuntu",
		"ubuntu-core",
		"ubuntucoreinitramfs",
		"debian",
		"opensuse",
		"suse",
		"yocto",
	}

	// distributions known to use /var/lib/snapd/snap/
	altDirDistros = []string{
		"altlinux",
		"antergos",
		"arch",
		"archlinux",
		"fedora",
		"gentoo",
		"manjaro",
		"manjaro-arm",
	}

	// distributions which support migration from /snap to /var/lib/snapd/snap
	migratedAltDirDistros = []string{
		"opensuse", // openSUSE Tumbleweed, Slowroll, Leap, but not SLE
	}
)

func checkSnapMountDir() error {
	if err := dirs.SnapMountDirDetectionOutcome(); err != nil {
		return err
	}

	smd := dirs.StripRootDir(dirs.SnapMountDir)
	switch {
	case release.DistroLike(migratedAltDirDistros...) && smd == dirs.AltSnapMountDir:
		// some distributions support migration from /snap -> /var/lib/snapd/snap/
	case release.DistroLike(defaultDirDistros...) && smd != dirs.DefaultSnapMountDir:
		fallthrough
	case release.DistroLike(altDirDistros...) && smd != dirs.AltSnapMountDir:
		return fmt.Errorf("unexpected snap mount directory %v on %v", smd, release.ReleaseInfo.ID)
	}

	return nil
}

var (
	// distributions known to use /usr/lib/snapd/
	defaulLibExectDirDistros = []string{
		"ubuntu",
		"ubuntu-core",
		"ubuntucoreinitramfs",
		"debian",
		"yocto",
		"altlinux",
		"antergos",
		"arch",
		"archlinux",
		"gentoo",
		"manjaro",
		"manjaro-arm",
	}

	// distributions known to use /usr/libexec/snapd/
	altLibExecDirDistros = []string{
		"fedora",
		"opensuse-tumbleweed",
		"opensuse-slowroll",
	}

	bothLibExecDirDistros = []string{
		"opensuse-leap", // openSUSE Leap uses /usr/libexec/snapd
		// starting from 16.0, but used /usr/lib/snapd
		// in earlier versions
	}
)

func checkLibExecDir() error {
	d := dirs.StripRootDir(dirs.DistroLibExecDir)
	switch {
	case release.DistroLike(bothLibExecDirDistros...):
	// Distributions which use either location, likely depending on their
	// version, but doing version check is too much a hassle. openSUSE Leap is
	// one of those cases.
	case release.DistroLike(altLibExecDirDistros...) && d != dirs.AltDistroLibexecDir:
		// RHEL, CentOS, Fedora and derivatives, openSUSE Tumbleweed (since
		// snapshot 20200826) and Slowroll; both RHEL and CentOS list "fedora"
		// in ID_LIKE
		fallthrough
	case release.DistroLike(defaulLibExectDirDistros...) && d != dirs.DefaultDistroLibexecDir:
		return fmt.Errorf("unexpected snapd tooling directory %v on %v", d, release.ReleaseInfo.ID)
	}

	return nil
}