forked from snapcore/snapd
/
aliases.go
111 lines (98 loc) · 3.03 KB
/
aliases.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2017 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 backend
import (
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/snapcore/snapd/dirs"
)
// Alias represents a command alias with a name and its application target.
type Alias struct {
Name string `json:"name"`
Target string `json:"target"`
}
// MatchingAliases returns the subset of aliases that exist on disk and have the expected targets.
// UpdateAliases adds and removes the given aliases.
func (b Backend) UpdateAliases(add []*Alias, remove []*Alias) error {
removed := make(map[string]bool, len(remove))
for _, alias := range remove {
err := os.Remove(filepath.Join(dirs.SnapBinariesDir, alias.Name))
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("cannot remove alias symlink: %v", err)
}
removed[alias.Name] = true
completer := filepath.Join(dirs.CompletersDir, alias.Name)
target, err := os.Readlink(completer)
if err != nil || target != alias.Target {
continue
}
os.Remove(completer)
}
for _, alias := range add {
p := filepath.Join(dirs.SnapBinariesDir, alias.Name)
if !removed[alias.Name] {
if err := os.Remove(p); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("cannot remove alias symlink: %v", err)
}
}
err := os.Symlink(alias.Target, p)
if err != nil {
return fmt.Errorf("cannot create alias symlink: %v", err)
}
if dirs.IsCompleteShSymlink(filepath.Join(dirs.CompletersDir, alias.Target)) {
os.Symlink(alias.Target, filepath.Join(dirs.CompletersDir, alias.Name))
}
}
return nil
}
// RemoveSnapAliases removes all the aliases targeting the given snap.
func (b Backend) RemoveSnapAliases(snapName string) error {
removeSymlinksTo(dirs.CompletersDir, snapName)
return removeSymlinksTo(dirs.SnapBinariesDir, snapName)
}
func removeSymlinksTo(dir, snapName string) error {
cands, err := filepath.Glob(filepath.Join(dir, "*"))
if err != nil {
return err
}
prefix := fmt.Sprintf("%s.", snapName)
var firstErr error
// best effort
for _, cand := range cands {
target, err := os.Readlink(cand)
if err, ok := err.(*os.PathError); ok && err.Err == syscall.EINVAL {
continue
}
if err != nil {
if firstErr == nil {
firstErr = err
}
continue
}
if target == snapName || strings.HasPrefix(target, prefix) {
err := os.Remove(cand)
if err != nil && firstErr == nil {
firstErr = fmt.Errorf("cannot remove alias symlink: %v", err)
}
}
}
return firstErr
}