-
Notifications
You must be signed in to change notification settings - Fork 3
/
registration.go
214 lines (177 loc) · 5.2 KB
/
registration.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package gowsl
// This file contains utilities to create, destroy, stop WSL distros,
// as well as utilities to query this status.
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/google/uuid"
"github.com/ubuntu/decorate"
"github.com/ubuntu/gowsl/internal/backend"
)
// Register is a wrapper around Win32's WslRegisterDistribution.
// It creates a new distro with a copy of the given tarball as
// its filesystem.
func (d *Distro) Register(rootFsPath string) (err error) {
defer decorate.OnError(&err, "could not register %s from rootfs in %s", d.name, rootFsPath)
rootFsPath, err = fixPath(rootFsPath)
if err != nil {
return err
}
r, err := d.isRegistered()
if err != nil {
return err
}
if r {
return errors.New("already registered")
}
return d.backend.WslRegisterDistribution(d.Name(), rootFsPath)
}
// RegisteredDistros returns a slice of the registered distros.
func RegisteredDistros(ctx context.Context) (distros []Distro, err error) {
defer decorate.OnError(&err, "could not obtain registered distros")
names, err := registeredDistros(selectBackend(ctx))
if err != nil {
return distros, err
}
for name := range names {
distros = append(distros, NewDistro(ctx, name))
}
return distros, nil
}
// RegisteredDistros returns a map of the registered distros and their GUID.
func registeredDistros(backend backend.Backend) (distros map[string]uuid.UUID, err error) {
r, err := backend.OpenLxssRegistry(".")
if err != nil {
return nil, err
}
defer r.Close()
subkeys, err := r.SubkeyNames()
if err != nil {
return distros, err
}
distros = make(map[string]uuid.UUID, len(subkeys))
for _, key := range subkeys {
guid, err := uuid.Parse(key)
if err != nil {
continue // Not a WSL distro
}
r, err = backend.OpenLxssRegistry(key)
if err != nil {
return nil, err
}
defer r.Close()
name, err := r.Field("DistributionName")
if err != nil {
return nil, err
}
distros[name] = guid
}
return distros, nil
}
// IsRegistered returns a boolean indicating whether a distro is registered or not.
func (d Distro) IsRegistered() (registered bool, err error) {
r, err := d.isRegistered()
if err != nil {
return false, fmt.Errorf("%s: %v", d.name, err)
}
return r, nil
}
// isRegistered is the internal way of detecting whether a distro is registered or
// not. Use this one internally to avoid repeating error information.
func (d Distro) isRegistered() (registered bool, err error) {
defer decorate.OnError(&err, "could not determine if distro is registered")
distros, err := registeredDistros(d.backend)
if err != nil {
return false, err
}
for name := range distros {
if strings.EqualFold(name, d.Name()) {
return true, nil
}
}
return false, nil
}
// Unregister is a wrapper around Win32's WslUnregisterDistribution.
// It irreparably destroys a distro and its filesystem.
func (d *Distro) Unregister() (err error) {
defer decorate.OnError(&err, "could not unregister %q", d.name)
if err := d.mustBeRegistered(); err != nil {
return err
}
return d.backend.WslUnregisterDistribution(d.Name())
}
// Install installs a new distro from the Windows store.
func Install(ctx context.Context, appxName string) error {
return selectBackend(ctx).Install(ctx, appxName)
}
// Uninstall removes the distro's associated AppxPackage (if there is one)
// and unregisters the distro.
func (d *Distro) Uninstall(ctx context.Context) (err error) {
defer decorate.OnError(&err, "Distro %q uninstall", d.name)
guid, err := d.GUID()
if err != nil {
return err
}
k, err := d.backend.OpenLxssRegistry(fmt.Sprintf("{%s}", guid))
if err != nil {
return err
}
defer k.Close()
packageFamilyName, err := k.Field("PackageFamilyName")
if errors.Is(err, fs.ErrNotExist) {
// Distro was imported, so there is no Appx associated
return d.backend.WslUnregisterDistribution(d.Name())
}
if err != nil {
return err
}
if err := d.backend.RemoveAppxFamily(ctx, packageFamilyName); err != nil {
return err
}
return d.backend.WslUnregisterDistribution(d.Name())
}
// Import creates a new distro from a source root filesystem.
func Import(ctx context.Context, distributionName, sourcePath, destinationPath string) (Distro, error) {
err := os.MkdirAll(destinationPath, 0700)
if err != nil {
return Distro{}, fmt.Errorf("could not create destination path: %v", err)
}
stat, err := os.Stat(sourcePath)
if err != nil {
return Distro{}, fmt.Errorf("could not stat source path: %v", err)
} else if stat.IsDir() {
return Distro{}, errors.New("source path is a directory")
}
err = selectBackend(ctx).Import(ctx, distributionName, sourcePath, destinationPath)
if err != nil {
return Distro{}, err
}
return NewDistro(ctx, distributionName), nil
}
// fixPath deals with the fact that WslRegisterDistribuion is
// a bit picky with the path format.
func fixPath(relative string) (string, error) {
abs, err := filepath.Abs(filepath.FromSlash(relative))
if err != nil {
return "", err
}
if _, err := os.Stat(abs); errors.Is(err, os.ErrNotExist) {
return "", errors.New("file not found")
}
return abs, nil
}
func (d Distro) mustBeRegistered() error {
r, err := d.isRegistered()
if err != nil {
return err
}
if !r {
return ErrNotExist
}
return nil
}