-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathwsl.dart
661 lines (594 loc) · 20.1 KB
/
wsl.dart
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import 'dart:async';
import 'dart:io';
import 'dart:convert' show Utf8Decoder, utf8;
import 'package:chunked_downloader/chunked_downloader.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:localization/localization.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:wsl2distromanager/api/app.dart';
import 'package:wsl2distromanager/api/safe_paths.dart';
import 'package:wsl2distromanager/components/constants.dart';
import 'package:wsl2distromanager/components/helpers.dart';
import 'package:wsl2distromanager/components/logging.dart';
/// Used to store the instances of WSL in a list.
class Instances {
List<String> running = [];
List<String> all = [];
Instances(this.all, this.running);
}
bool inited = false;
/// This class is used to interact with WSL. It contains all the functions
/// needed to interact with WSL based on Process.run and Process.start.
/// Most functions will return the UTF8 converted stdout of the process.
class WSLApi {
WSLApi() {
if (!inited) {
inited = true;
App().getDistroLinks();
}
}
/// Get distro size of [distroName] a string with a GB suffix.
/// Returns null if size is 0.
/// e.g. "2.00 GB"
String? getSize(String distroName) {
String ext4Path = getInstancePath(distroName).file('ext4.vhdx');
// Get size of distro
try {
File file = File(ext4Path);
int byteSize = file.lengthSync();
if (byteSize == 0) {
return null;
}
double size = byteSize / 1024 / 1024 / 1024; // Convert to GB
return '${'size-text'.i18n()}: ${size.toStringAsFixed(2)} GB';
} catch (error, stack) {
logDebug(error, stack, null);
return null;
}
}
/// Create directory
void mkRootDir({String path = defaultPath}) {
SafePath(path);
}
/// Install WSL
void installWSL() async {
Process.start(
'powershell',
[
'Start-Process cmd -ArgumentList "/c wsl --install" -Verb RunAs',
],
mode: ProcessStartMode.detached,
runInShell: true);
}
/// Start a WSL distro by name
void start(String distribution,
{String startPath = '',
String startUser = '',
String startCmd = ''}) async {
List<String> args = ['wsl', '-d', distribution];
if (startPath != '') {
args.addAll(['--cd', startPath]);
}
if (startUser != '') {
args.addAll(['--user', startUser]);
}
if (startCmd != '') {
for (String cmd in startCmd.split(' ')) {
args.add(cmd);
}
// Run shell to keep open
args.add(';/bin/sh');
}
await Process.start('start', args,
mode: ProcessStartMode.detached, runInShell: true);
if (kDebugMode) {
print("Done starting $distribution");
}
}
/// Stop a WSL distro by name
Future<String> stop(String distribution) async {
ProcessResult results =
await Process.run('wsl', ['--terminate', distribution]);
return results.stdout;
}
/// Open bashrc with notepad from WSL
Future<String> openBashrc(String distribution) async {
List<String> argsRc = ['wsl', '-d', distribution, 'notepad.exe', '.bashrc'];
Process results = await Process.start('start', argsRc,
mode: ProcessStartMode.normal, runInShell: true);
return results.stdout.toString();
}
/// Shutdown WSL
Future<String> shutdown() async {
ProcessResult results = await Process.run('wsl', ['--shutdown']);
return results.stdout;
}
/// Start VSCode
void startVSCode(String distribution, {String path = ''}) async {
List<String> args = ['wsl', '-d', distribution, 'code'];
if (path != '') {
args.add(path);
}
Process.start('start', args,
mode: ProcessStartMode.normal, runInShell: true);
}
/// Write wslconfig file
void writeConfig(String text) async {
File file = File(getWslConfigPath());
if (!file.existsSync()) {
file.createSync();
}
file.writeAsStringSync('[wsl2]\n\n$text');
}
/// Set wslconfig setting
void setConfig(String parent, String key, String value) async {
File file = File(getWslConfigPath());
if (!file.existsSync()) {
file.createSync();
}
String text = file.readAsStringSync();
// Check if parent exists
if (text.contains('[$parent]')) {
// Check if key exists with regeex
RegExp regex = RegExp('$key[ ]*=');
if (regex.hasMatch(text)) {
// Replace key value
text = text.replaceAll(RegExp('$key[ ]*=(.*)'), '$key = $value');
} else {
// Add key value
text = text.replaceAll('[$parent]', '[$parent]\n$key = $value');
}
} else {
// Add parent and key value
text += '\n[$parent]\n$key = $value';
}
// Write to file
file.writeAsStringSync(text);
}
/// Read wslconfig file
Future<Map<String, String>> readConfig() async {
File file = File(getWslConfigPath());
if (!file.existsSync()) {
file.createSync();
}
Map<String, String> config = {};
String key = '', value = '';
List<String> lines = await file.readAsLines();
for (var line in lines) {
if (line.isNotEmpty && line.contains('=')) {
key = line.substring(0, line.indexOf('='));
key = key.replaceAll(' ', '');
value = line.substring(line.indexOf('=') + 1, line.length);
value = value.replaceAll(' ', '');
config[key] = value;
}
}
return config;
}
/// Open wslconfig file
void editConfig() async {
Process.start('start', ['notepad.exe', getWslConfigPath()],
mode: ProcessStartMode.normal, runInShell: true);
}
/// Start Explorer
void startExplorer(String distribution) async {
await Process.start(
'start', ['explorer.exe', getInstancePath(distribution).path],
mode: ProcessStartMode.normal, runInShell: true);
}
/// Start Windows Terminal
void startWindowsTerminal(String distribution) async {
List<String> args = ['wt', 'wsl', '-d', distribution, '--cd', '~'];
Process.start('start', args,
mode: ProcessStartMode.normal, runInShell: true);
}
/// Copy a WSL distro by name
Future<String> copy(String distribution, String newName) async {
String exportPath =
getInstancePath(distribution).file('$distribution.ext4');
// Copy
String exportRes = await export(distribution, exportPath);
String importRes =
await import(newName, getInstancePath(newName).path, exportPath);
// Cleanup, delete file
File file = File(exportPath);
if (file.existsSync()) {
file.deleteSync();
}
return '$exportRes $importRes';
}
/// Copy a WSL distro by name and vhd
Future<String> copyVhd(String name, String newName) async {
String vhdPath = getInstancePath(name).file('ext4.vhdx');
String copyPath = getInstancePath(name).file('ext4.copy.vhdx');
// Copy path to new location so instance doesn't have to be stopped
File file = File(vhdPath);
if (file.existsSync()) {
file.copySync(copyPath);
} else {
return 'File not found';
}
String importRes = await import(
newName, getInstancePath(newName).path, copyPath,
isVhd: true);
// Cleanup, delete file
File file2 = File(copyPath);
if (file2.existsSync()) {
file2.deleteSync();
}
return importRes;
}
/// Export a WSL distro by name
Future<String> export(String distribution, String location) async {
ProcessResult results = await Process.run(
'wsl', ['--export', distribution, location],
stdoutEncoding: null);
return utf8Convert(results.stdout);
}
/// Remove a WSL distro by name
Future<String> remove(String distribution) async {
ProcessResult results =
await Process.run('wsl', ['--unregister', distribution]);
// Check if folder is empty and delete
String path = getInstancePath(distribution).path;
// Wait 10 seconds in async then delete for Windows to release file
Future.delayed(const Duration(seconds: 10), () {
Directory dir = Directory(path);
if (dir.existsSync()) {
if (dir.listSync().isEmpty) {
dir.deleteSync(recursive: true);
}
}
});
return results.stdout;
}
/// Install a WSL distro by name
Future<String> install(String distribution) async {
ProcessResult results =
await Process.run('wsl', ['--install', '-d', distribution]);
return results.stdout;
}
List<String> resultQueue = [];
/// Get the current cached output
String getCurrentOutput() {
String tmp = resultQueue.join('\n');
resultQueue = [];
return tmp;
}
/// Executes a command list in a WSL distro
Future<List<int>> execCmds(
String distribution,
List<String> cmds, {
String? user,
required Function(String) onMsg,
required Function onDone,
bool showOutput = true,
}) async {
List<int> processes = [];
Process result = await Process.start(
'wsl', ['-d', distribution, '-u', user ?? 'root'],
mode: ProcessStartMode.normal, runInShell: true);
Timer currentWaiter = Timer(const Duration(seconds: 60), () {
result.kill();
onDone();
});
result.stdout
.cast<List<int>>()
.transform(const Utf8Decoder())
.listen((String line) {
resultQueue.add(line);
onMsg(line);
currentWaiter.cancel();
// No new output within the last 30 seconds
currentWaiter = Timer(const Duration(seconds: 15), () {
result.kill();
onDone();
});
});
// Log output to file
result.stdin.writeln('script -B /tmp/currentsessionlog -f');
// Start windows with output
await Process.start(
'wsl',
[
'-d',
distribution,
'-u',
user ?? 'root',
'tail',
'-n',
'+1',
'-f',
'/tmp/currentsessionlog'
],
mode: showOutput ? ProcessStartMode.detached : ProcessStartMode.normal,
runInShell: true);
// Delay to allow tail to start
await Future.delayed(const Duration(milliseconds: 500));
for (var cmd in cmds) {
result.stdin.writeln(cmd);
}
return processes;
}
/// Executes a command list in a WSL distro and open a terminal
Future<Process> runCmds(
String distribution,
List<String> cmds, {
String? user,
}) async {
// Write commands to /tmp/cmds
Process fileProcess = await Process.start(
'wsl', ['-d', distribution, '-u', user ?? 'root'],
mode: ProcessStartMode.normal, runInShell: true);
fileProcess.stdin.writeln('echo "#!/bin/bash" > /tmp/wdmcmds');
for (var cmd in cmds) {
cmd = cmd.replaceAll('"', '\\"');
fileProcess.stdin.writeln('echo "$cmd" >> /tmp/wdmcmds');
}
var waitCmd = 'read -n1 -r -p \\"\n\nDone running the action. '
'Press any key to exit...\\" key';
fileProcess.stdin.writeln('echo "$waitCmd" >> /tmp/wdmcmds');
// Wait for commands to be written
await Future.delayed(const Duration(milliseconds: 500));
// Execute commands in /tmp/cmds
List<String> args = [
'-d',
distribution,
'-u',
user ?? 'root',
'/bin/bash',
'/tmp/wdmcmds'
];
Process results = await Process.start('wsl', args,
runInShell: true, mode: ProcessStartMode.detached);
return results;
}
/// Executes a command in a WSL distro and returns the output
Future<String> execCmdAsRoot(String distribution, String cmd) async {
List<String> args = ['--distribution', distribution, '-u', 'root'];
for (var arg in cmd.split(' ')) {
args.add(arg);
}
ProcessResult results = await Process.run('wsl', args,
runInShell: true, stdoutEncoding: utf8, stderrEncoding: utf8);
return results.stdout;
}
/// Executes a command in a WSL distro. passwd will open a shell
Future<List<int>> exec(String distribution, List<String> cmds) async {
List<String> args;
List<int> processes = [];
int exitCode;
for (String cmd in cmds) {
if (cmd.contains('passwd')) {
args = ['wsl', '-d', distribution];
cmd.split(' ').forEach((String arg) {
args.add(arg);
});
Process result = await Process.start('start', args,
mode: ProcessStartMode.normal, runInShell: true);
exitCode = await result.exitCode;
processes.add(exitCode);
} else {
args = ['-d', distribution];
cmd.split(' ').forEach((String arg) {
args.add(arg);
});
ProcessResult result =
await Process.run('wsl', args, runInShell: false);
exitCode = result.exitCode;
processes.add(exitCode);
}
}
return processes;
}
/// Restart WSL
Future<String> restart() async {
ProcessResult results = await Process.run('wsl', ['--shutdown']);
results = await Process.run('wsl', ['--shutdown']);
return results.stdout;
}
/// Import a WSL distro by name
Future<String> import(
String distribution, String installLocation, String filename,
{bool isVhd = false}) async {
if (installLocation == '') {
installLocation = getInstancePath(distribution).path;
} else {
installLocation = SafePath(installLocation).path;
}
ProcessResult results;
if (isVhd) {
results = await Process.run(
'wsl', ['--import', distribution, installLocation, filename, '--vhd'],
stdoutEncoding: null);
} else {
results = await Process.run(
'wsl', ['--import', distribution, installLocation, filename],
stdoutEncoding: null);
}
return utf8Convert(results.stdout);
}
/// Import a WSL distro by name
Future<dynamic> create(String distribution, String filename,
String installPath, Function(String) status,
{bool image = false}) async {
if (installPath == '') {
installPath = getInstancePath(distribution).path;
} else {
installPath = SafePath(installPath).path;
}
// Download
String downloadPath = getDistroPath().file('$filename.tar.gz');
String downloadPathTmp = getDistroPath().file('$filename.tar.gz.tmp');
bool fileExists = await File(downloadPath).exists();
if (!image && distroRootfsLinks[filename] != null && !fileExists) {
String url = distroRootfsLinks[filename]!;
// Download file
try {
var downloader = ChunkedDownloader(
url: url,
saveFilePath: downloadPathTmp,
onProgress: (int count, int total, double speed) {
status('${'downloading-text'.i18n()}'
' ${(count / total * 100).toStringAsFixed(0)}%');
})
..start();
// Await download
while (!downloader.done) {
await Future.delayed(const Duration(milliseconds: 500));
}
File file = File(downloadPathTmp);
file.rename(downloadPath);
status('${'downloaded-text'.i18n()} $filename');
} catch (error) {
status('${'errordownloading-text'.i18n()} $filename');
}
}
// Downloaded or extracted
if (!image && distroRootfsLinks[filename] == null) {
downloadPath = filename;
}
// Create from local file
ProcessResult results = await Process.run(
'wsl', ['--import', distribution, installPath, downloadPath],
stdoutEncoding: null);
return results;
}
var lastDistroList = Instances([], []);
/// Returns list of WSL distros
Future<Instances> list(bool showDocker) async {
ProcessResult results =
await Process.run('wsl', ['--list', '--quiet'], stdoutEncoding: null);
String output = utf8Convert(results.stdout);
List<String> list = [];
bool wslInstalled = true;
// Check if wsl is installed
if (output.contains('wsl.exe') || output.contains('ProcessException')) {
wslInstalled = false;
}
if (wslInstalled) {
if (output.contains('ERROR_FILE_NOT_FOUND')) {
return lastDistroList;
}
output.split('\n').forEach((line) {
var dockerfilter = showDocker
? true
: (!line.startsWith('docker-desktop-data') &&
!line.startsWith('docker-desktop'));
// Filter out docker data
if (line != '' && dockerfilter) {
list.add(line);
}
});
List<String> running = await listRunning();
lastDistroList = Instances(list, running);
return Instances(list, running);
} else {
return Instances(['wslNotInstalled'], []);
}
}
/// Clean up WSL distros. Exporting, deleting, and importing.
Future<String> cleanup(String distribution) async {
var instancePath = getInstancePath(distribution);
var file = instancePath.file('export.tar.gz');
// Export, remove, and import
await export(distribution, file);
await remove(distribution);
return await import(distribution, instancePath.path, file);
}
/// Returns list of WSL distros
Future<List<String>> listRunning() async {
ProcessResult results = await Process.run(
'wsl', ['--list', '--running', '--quiet'],
stdoutEncoding: null);
String output = utf8Convert(results.stdout);
List<String> list = [];
output.split('\n').forEach((line) {
// Filter out docker data
if (line != '') {
list.add(line);
}
});
return list;
}
/// Returns list of downloadable WSL distros
Future<List<String>> getDownloadable(
String repo, Function(String) onError) async {
// Get list of distros from git
distroRootfsLinks = await App().getDistroLinks();
// Get list of distros from custom repo link and try to format
try {
await Dio().get(repo).then((value) => {
value.data.split('\n').forEach((line) {
if (line.contains('tar.gz"') &&
line.contains('href=') &&
(line.contains('debian-10') || line.contains('debian-11'))) {
String name = line
.split('href="')[1]
.split('"')[0]
.toString()
.replaceAll('.tar.gz', '')
.replaceAll('1_amd64', '')
.replaceAll(RegExp(r'-|_'), ' ')
.replaceAllMapped(RegExp(r' .|^.'),
(Match m) => m[0].toString().toUpperCase());
distroRootfsLinks.addAll({
name: repo + line.split('href="')[1].split('"')[0].toString()
});
}
})
});
} catch (e) {
onError(e.toString());
}
List<String> list = [];
list.addAll(distroRootfsLinks.keys);
return list;
}
/// Move WSL distro to another location by [distro] and [newPath].
/// Returns [ProcessResult] of the command.
Future<String> move(String distro, String newPath) async {
SafePath path = SafePath(newPath);
await export(distro, path.file('export.ext4'));
await remove(distro);
var res = await import(distro, newPath, path.file('export.ext4'));
await File(path.file('export.ext4')).delete();
return res;
}
/// Convert bytes to human readable string while removing non-ascii characters
String utf8Convert(List<int> bytes) {
List<int> utf8Lines = List<int>.from(bytes);
bool running = true;
int i = 0;
while (running) {
// Check end of string
if (utf8Lines.length == i) {
running = false;
break;
}
// Remove non-ascii/unnecessary utf8 characters but keep newline (10)
if (utf8Lines[i] != 10 && (utf8Lines[i] < 32 || utf8Lines[i] > 122)) {
utf8Lines.removeAt(i);
continue;
}
i++;
}
return utf8.decode(utf8Lines);
}
/// Change setting in wsl.conf with key and value
Future<bool> setSetting(
String distro, String parent, String key, String value) async {
// Read trigger script from assets
String script = await rootBundle.loadString('assets/scripts/settings.bash');
script = script.replaceAll(RegExp(r'^#.*\n', multiLine: true), '');
script = script.replaceAll('PARENT', parent);
script = script.replaceAll('KEY', key);
script = script.replaceAll('VALUE', value);
List<String> scriptLines = script.split('\n');
// Execute trigger script
await execCmds(distro, scriptLines,
onMsg: (msg) {}, onDone: () {}, showOutput: false);
return true;
}
}