@@ -7,23 +7,29 @@ package gowsl
7
7
// This file contains utilities to interact with a Distro and its configuration
8
8
9
9
import (
10
+ "context"
10
11
"errors"
11
12
"fmt"
12
13
"sort"
13
14
14
15
"github.com/google/uuid"
15
16
"github.com/ubuntu/decorate"
17
+ "github.com/ubuntu/gowsl/internal/flags"
16
18
)
17
19
18
20
// Distro is an abstraction around a WSL distro.
19
21
type Distro struct {
22
+ ctx context.Context
20
23
name string
21
24
}
22
25
23
26
// NewDistro declares a new distribution, but does not register it nor
24
27
// check if it exists.
25
- func NewDistro (name string ) Distro {
26
- return Distro {name : name }
28
+ func NewDistro (ctx context.Context , name string ) Distro {
29
+ return Distro {
30
+ ctx : ctx ,
31
+ name : name ,
32
+ }
27
33
}
28
34
29
35
// Name is a getter for the DistroName as shown in "wsl.exe --list".
@@ -35,7 +41,7 @@ func (d Distro) Name() string {
35
41
func (d * Distro ) GUID () (id uuid.UUID , err error ) {
36
42
defer decorate .OnError (& err , "could not obtain GUID of %s" , d .name )
37
43
38
- distros , err := registeredDistros ()
44
+ distros , err := registeredDistros (d . ctx )
39
45
if err != nil {
40
46
return id , err
41
47
}
@@ -50,38 +56,38 @@ func (d *Distro) GUID() (id uuid.UUID, err error) {
50
56
// Equivalent to:
51
57
//
52
58
// wsl --terminate <distro>
53
- func (d Distro ) Terminate () error {
54
- return terminate (d .Name ())
59
+ func (d Distro ) Terminate (ctx context. Context ) error {
60
+ return selectBackend ( ctx ). Terminate (d .Name ())
55
61
}
56
62
57
63
// Shutdown powers off all of WSL, including all other distros.
58
64
// Equivalent to:
59
65
//
60
66
// wsl --shutdown
61
- func Shutdown () error {
62
- return shutdown ()
67
+ func Shutdown (ctx context. Context ) error {
68
+ return selectBackend ( ctx ). Shutdown ()
63
69
}
64
70
65
71
// SetAsDefault sets a particular distribution as the default one.
66
72
// Equivalent to:
67
73
//
68
74
// wsl --set-default <distro>
69
- func (d Distro ) SetAsDefault () error {
70
- return setAsDefault (d .Name ())
75
+ func (d Distro ) SetAsDefault (ctx context. Context ) error {
76
+ return selectBackend ( ctx ). SetAsDefault (d .Name ())
71
77
}
72
78
73
79
// DefaultDistro gets the current default distribution.
74
- func DefaultDistro () (d Distro , err error ) {
80
+ func DefaultDistro (ctx context. Context ) (d Distro , err error ) {
75
81
defer decorate .OnError (& err , "could not obtain the default distro" )
76
82
77
83
// First, we find out the GUID of the default distro
78
- r , err := openRegistry ( lxssPath )
84
+ r , err := selectBackend ( ctx ). OpenLxssRegistry ( )
79
85
if err != nil {
80
86
return d , err
81
87
}
82
- defer r .close ()
88
+ defer r .Close ()
83
89
84
- guid , err := r .field ("DefaultDistribution" )
90
+ guid , err := r .Field ("DefaultDistribution" )
85
91
if err != nil {
86
92
return d , err
87
93
}
@@ -92,37 +98,20 @@ func DefaultDistro() (d Distro, err error) {
92
98
}
93
99
94
100
// Last, we find out the name of the distro
95
- r , err = openRegistry ( lxssPath , guid )
101
+ r , err = selectBackend ( ctx ). OpenLxssRegistry ( guid )
96
102
if err != nil {
97
103
return d , err
98
104
}
99
- defer r .close ()
105
+ defer r .Close ()
100
106
101
- name , err := r .field ("DistributionName" )
107
+ name , err := r .Field ("DistributionName" )
102
108
if err != nil {
103
109
return d , err
104
110
}
105
111
106
- return NewDistro (name ), err
112
+ return NewDistro (ctx , name ), err
107
113
}
108
114
109
- // Windows' WSL_DISTRIBUTION_FLAGS
110
- // https://learn.microsoft.com/en-us/windows/win32/api/wslapi/ne-wslapi-wsl_distribution_flags
111
- type wslFlags int
112
-
113
- // Allowing underscores in names to keep it as close to Windows as possible.
114
- const (
115
- flag_NONE wslFlags = 0x0 //nolint: revive
116
- flag_ENABLE_INTEROP wslFlags = 0x1 //nolint: revive
117
- flag_APPEND_NT_PATH wslFlags = 0x2 //nolint: revive
118
- flag_ENABLE_DRIVE_MOUNTING wslFlags = 0x4 //nolint: revive
119
-
120
- // Per the conversation at https://github.com/microsoft/WSL-DistroLauncher/issues/96
121
- // the information about version 1 or 2 is on the 4th bit of the flags, which is
122
- // currently referenced neither by the API nor the documentation.
123
- flag_undocumented_WSL_VERSION wslFlags = 0x8 //nolint: revive
124
- )
125
-
126
115
// Configuration is the configuration of the distro.
127
116
type Configuration struct {
128
117
Version uint8 // Type of filesystem used (lxfs vs. wslfs, relevant only to WSL1)
@@ -192,9 +181,9 @@ func (d Distro) GetConfiguration() (c Configuration, err error) {
192
181
defer decorate .OnError (& err , "could not access configuration for %s" , d .name )
193
182
194
183
var conf Configuration
195
- var flags wslFlags
184
+ var flags flags. WslFlags
196
185
197
- err = wslGetDistributionConfiguration (
186
+ err = selectBackend ( d . ctx ). WslGetDistributionConfiguration (
198
187
d .Name (),
199
188
& conf .Version ,
200
189
& conf .DefaultUID ,
@@ -280,55 +269,55 @@ func (d *Distro) configure(config Configuration) error {
280
269
return err
281
270
}
282
271
283
- return wslConfigureDistribution (d .Name (), config .DefaultUID , flags )
272
+ return selectBackend ( d . ctx ). WslConfigureDistribution (d .Name (), config .DefaultUID , flags )
284
273
}
285
274
286
275
// unpackFlags examines a winWslFlags object and stores its findings in the Configuration.
287
- func (conf * Configuration ) unpackFlags (flags wslFlags ) {
276
+ func (conf * Configuration ) unpackFlags (f flags. WslFlags ) {
288
277
conf .InteropEnabled = false
289
- if flags & flag_ENABLE_INTEROP != 0 {
278
+ if flags . ENABLE_INTEROP != 0 {
290
279
conf .InteropEnabled = true
291
280
}
292
281
293
282
conf .PathAppended = false
294
- if flags & flag_APPEND_NT_PATH != 0 {
283
+ if f & flags . APPEND_NT_PATH != 0 {
295
284
conf .PathAppended = true
296
285
}
297
286
298
287
conf .DriveMountingEnabled = false
299
- if flags & flag_ENABLE_DRIVE_MOUNTING != 0 {
288
+ if f & flags . ENABLE_DRIVE_MOUNTING != 0 {
300
289
conf .DriveMountingEnabled = true
301
290
}
302
291
303
292
conf .undocumentedWSLVersion = 1
304
- if flags & flag_undocumented_WSL_VERSION != 0 {
293
+ if f & flags . Undocumented_WSL_VERSION != 0 {
305
294
conf .undocumentedWSLVersion = 2
306
295
}
307
296
}
308
297
309
298
// packFlags generates a winWslFlags object from the Configuration.
310
- func (conf Configuration ) packFlags () (wslFlags , error ) {
311
- flags := flag_NONE
299
+ func (conf Configuration ) packFlags () (flags. WslFlags , error ) {
300
+ f := flags . NONE
312
301
313
302
if conf .InteropEnabled {
314
- flags = flags | flag_ENABLE_INTEROP
303
+ f = f | flags . ENABLE_INTEROP
315
304
}
316
305
317
306
if conf .PathAppended {
318
- flags = flags | flag_APPEND_NT_PATH
307
+ f = f | flags . APPEND_NT_PATH
319
308
}
320
309
321
310
if conf .DriveMountingEnabled {
322
- flags = flags | flag_ENABLE_DRIVE_MOUNTING
311
+ f = f | flags . ENABLE_DRIVE_MOUNTING
323
312
}
324
313
325
314
switch conf .undocumentedWSLVersion {
326
315
case 1 :
327
316
case 2 :
328
- flags = flags | flag_undocumented_WSL_VERSION
317
+ f = f | flags . Undocumented_WSL_VERSION
329
318
default :
330
- return flags , fmt .Errorf ("unknown WSL version %d" , conf .undocumentedWSLVersion )
319
+ return f , fmt .Errorf ("unknown WSL version %d" , conf .undocumentedWSLVersion )
331
320
}
332
321
333
- return flags , nil
322
+ return f , nil
334
323
}
0 commit comments