forked from snapcore/snapd
/
reset.go
152 lines (139 loc) · 4.79 KB
/
reset.go
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2020 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 main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/snapcore/snapd/dirs"
apparmor_sandbox "github.com/snapcore/snapd/sandbox/apparmor"
)
func resetPreseededChroot(preseedChroot string) error {
// 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.SnapBusPolicyDir, "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.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"),
}
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)
}
}
// 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
files, err := ioutil.ReadDir(filepath.Join(preseedChroot, dirs.CompletersDir))
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("error reading %s: %v", dirs.CompletersDir, err)
}
completeShSymlinks := make(map[string]string)
var otherSymlinks []string
// pass 1: find all symlinks pointing at complete.sh
for _, fileInfo := range files {
if fileInfo.Mode()&os.ModeSymlink == 0 {
continue
}
fullPath := filepath.Join(preseedChroot, dirs.CompletersDir, 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
}