File: reset.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (185 lines) | stat: -rw-r--r-- 5,961 bytes parent folder | download | duplicates (3)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2020-2022 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 preseed

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/snapcore/snapd/cmd/snaplock/runinhibit"
	"github.com/snapcore/snapd/dirs"
	"github.com/snapcore/snapd/osutil"
	apparmor_sandbox "github.com/snapcore/snapd/sandbox/apparmor"
)

// bash-completion symlinks; note there are symlinks that point at
// completer, and symlinks that point at the completer symlinks.
// e.g.
// lxd.lxc -> /snap/core/current/usr/lib/snapd/complete.sh
// lxc -> lxd.lxc
func resetCompletionSymlinks(completersPath string) error {
	files, err := os.ReadDir(completersPath)
	if err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("error reading %s: %v", completersPath, err)
	}
	completeShSymlinks := make(map[string]string)
	var otherSymlinks []string

	// pass 1: find all symlinks pointing at complete.sh
	for _, fileInfo := range files {
		if fileInfo.Type()&os.ModeSymlink == 0 {
			continue
		}
		fullPath := filepath.Join(completersPath, fileInfo.Name())
		if dirs.IsCompleteShSymlink(fullPath) {
			if err := os.Remove(fullPath); err != nil {
				return fmt.Errorf("error removing symlink %s: %v", fullPath, err)
			}
			completeShSymlinks[fileInfo.Name()] = fullPath
		} else {
			otherSymlinks = append(otherSymlinks, fullPath)
		}
	}
	// pass 2: find all symlinks that point at the symlinks found in pass 1.
	for _, other := range otherSymlinks {
		target, err := os.Readlink(other)
		if err != nil {
			return fmt.Errorf("error reading symlink target of %s: %v", other, err)
		}
		if _, ok := completeShSymlinks[target]; ok {
			if err := os.Remove(other); err != nil {
				return fmt.Errorf("error removing symlink %s: %v", other, err)
			}
		}
	}

	return nil
}

// ResetPreseededChroot removes all preseeding artifacts from preseedChroot
// (classic Ubuntu only).
var ResetPreseededChroot = func(preseedChroot string) error {
	var err error
	preseedChroot, err = filepath.Abs(preseedChroot)
	if err != nil {
		return err
	}

	exists, isDir, err := osutil.DirExists(preseedChroot)
	if err != nil {
		return fmt.Errorf("cannot reset %q: %v", preseedChroot, err)
	}
	if !exists {
		return fmt.Errorf("cannot reset non-existing directory %q", preseedChroot)
	}
	if !isDir {
		return fmt.Errorf("cannot reset %q, it is not a directory", preseedChroot)
	}

	// globs that yield individual files
	globs := []string{
		dirs.SnapStateFile,
		dirs.SnapSystemKeyFile,
		filepath.Join(dirs.SnapBlobDir, "*.snap"),
		filepath.Join(dirs.SnapUdevRulesDir, "*-snap.*.rules"),
		filepath.Join(dirs.SnapDBusSystemPolicyDir, "snap.*.*.conf"),
		filepath.Join(dirs.SnapServicesDir, "snap.*.service"),
		filepath.Join(dirs.SnapServicesDir, "snap.*.timer"),
		filepath.Join(dirs.SnapServicesDir, "snap.*.socket"),
		filepath.Join(dirs.SnapServicesDir, "snap-*.mount"),
		filepath.Join(dirs.SnapServicesDir, "multi-user.target.wants", "snap-*.mount"),
		filepath.Join(dirs.SnapServicesDir, "default.target.wants", "snap-*.mount"),
		filepath.Join(dirs.SnapServicesDir, "snapd.mounts.target.wants", "snap-*.mount"),
		filepath.Join(dirs.SnapUserServicesDir, "snap.*.service"),
		filepath.Join(dirs.SnapUserServicesDir, "snap.*.socket"),
		filepath.Join(dirs.SnapUserServicesDir, "snap.*.timer"),
		filepath.Join(dirs.SnapUserServicesDir, "default.target.wants", "snap.*.service"),
		filepath.Join(dirs.SnapUserServicesDir, "sockets.target.wants", "snap.*.socket"),
		filepath.Join(dirs.SnapUserServicesDir, "timers.target.wants", "snap.*.timer"),
		filepath.Join(runinhibit.InhibitDir, "*.lock"),
	}

	for _, gl := range globs {
		matches, err := filepath.Glob(filepath.Join(preseedChroot, gl))
		if err != nil {
			// the only possible error from Glob() is ErrBadPattern
			return err
		}
		for _, path := range matches {
			if err := os.Remove(path); err != nil {
				return fmt.Errorf("error removing %s: %v", path, err)
			}
		}
	}

	// directories that need to be removed recursively (but
	// leaving parent directory intact).
	globs = []string{
		filepath.Join(dirs.SnapDataDir, "*"),
		filepath.Join(dirs.SnapCacheDir, "*"),
		filepath.Join(apparmor_sandbox.CacheDir, "*"),
		filepath.Join(dirs.SnapDesktopFilesDir, "*"),
		filepath.Join(dirs.SnapDBusSessionServicesDir, "*"),
		filepath.Join(dirs.SnapDBusSystemServicesDir, "*"),
	}

	for _, gl := range globs {
		matches, err := filepath.Glob(filepath.Join(preseedChroot, gl))
		if err != nil {
			// the only possible error from Glob() is ErrBadPattern
			return err
		}
		for _, path := range matches {
			if err := os.RemoveAll(path); err != nil {
				return fmt.Errorf("error removing %s: %v", path, err)
			}
		}
	}

	// directories removed entirely
	paths := []string{
		dirs.SnapAssertsDBDir,
		dirs.FeaturesDir,
		dirs.SnapDesktopIconsDir,
		dirs.SnapDeviceDir,
		dirs.SnapCookieDir,
		dirs.SnapMountPolicyDir,
		dirs.SnapAppArmorDir,
		dirs.SnapSeqDir,
		dirs.SnapMountDir,
		dirs.SnapSeccompBase,
	}

	for _, path := range paths {
		if err := os.RemoveAll(filepath.Join(preseedChroot, path)); err != nil {
			// report the error and carry on
			return fmt.Errorf("error removing %s: %v", path, err)
		}
	}

	for _, completersPath := range []string{dirs.CompletersDir, dirs.LegacyCompletersDir} {
		if err := resetCompletionSymlinks(filepath.Join(preseedChroot, completersPath)); err != nil {
			return err
		}
	}

	return nil
}