134 lines
4.2 KiB
Dart
134 lines
4.2 KiB
Dart
import 'dart:io';
|
|
import 'package:archive/archive.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:path/path.dart' as p;
|
|
|
|
/// 安装进度回调
|
|
/// [progress] 0.0 ~ 1.0
|
|
/// [message] 当前阶段说明
|
|
typedef InstallProgressCallback = void Function(double progress, String message);
|
|
|
|
/// Linux 发行版 Rootfs 安装与解压服务
|
|
class DistroInstaller {
|
|
/// 解压并安装 tar.gz 格式的 Rootfs 到目标目录
|
|
///
|
|
/// [tarGzBytes] .tar.gz 压缩包字节流
|
|
/// [targetDir] 目标解压目录 (即容器的 rootfs 路径)
|
|
/// [onProgress] 进度回调
|
|
static Future<void> installFromBytes({
|
|
required Uint8List tarGzBytes,
|
|
required Directory targetDir,
|
|
InstallProgressCallback? onProgress,
|
|
}) async {
|
|
onProgress?.call(0.05, '正在校验与解析系统镜像...');
|
|
|
|
if (!targetDir.existsSync()) {
|
|
targetDir.createSync(recursive: true);
|
|
}
|
|
|
|
// 1. 在后台解压 Gzip 与 Tar(避免 UI 线程卡顿)
|
|
onProgress?.call(0.15, '正在解压内核与文件系统结构...');
|
|
final archive = await compute(_decodeTarGz, tarGzBytes);
|
|
|
|
// 2. 写入文件与目录
|
|
final total = archive.length;
|
|
int processed = 0;
|
|
|
|
for (final file in archive) {
|
|
processed++;
|
|
final filename = file.name;
|
|
// 避免路径穿越漏洞 (Directory Traversal)
|
|
if (filename.contains('..')) continue;
|
|
|
|
final outPath = p.join(targetDir.path, filename);
|
|
|
|
if (file.isFile) {
|
|
final parentDir = Directory(p.dirname(outPath));
|
|
if (!parentDir.existsSync()) {
|
|
parentDir.createSync(recursive: true);
|
|
}
|
|
|
|
// 处理符号链接与常规文件
|
|
if (file.isSymbolicLink) {
|
|
try {
|
|
final link = Link(outPath);
|
|
if (link.existsSync()) link.deleteSync();
|
|
link.createSync(file.nameOfLinkedFile);
|
|
} catch (_) {
|
|
// Windows 或受限环境降级为写入目标路径文本或忽略
|
|
}
|
|
} else {
|
|
final content = file.content as List<int>;
|
|
File(outPath).writeAsBytesSync(content, flush: false);
|
|
}
|
|
} else {
|
|
Directory(outPath).createSync(recursive: true);
|
|
}
|
|
|
|
if (processed % 100 == 0 || processed == total) {
|
|
final percent = 0.20 + (processed / total) * 0.70; // 20% ~ 90%
|
|
onProgress?.call(percent, '正在释放系统文件 ($processed/$total)...');
|
|
}
|
|
}
|
|
|
|
// 3. 执行系统配置与网络补丁
|
|
onProgress?.call(0.92, '正在配置网络 DNS 与系统环境...');
|
|
await postInstallConfigure(targetDir);
|
|
|
|
onProgress?.call(1.0, '系统初始化就绪!');
|
|
}
|
|
|
|
/// 后台 Isolate 解压 tar.gz
|
|
static Archive _decodeTarGz(Uint8List bytes) {
|
|
final tarData = GZipDecoder().decodeBytes(bytes);
|
|
return TarDecoder().decodeBytes(tarData);
|
|
}
|
|
|
|
/// 自动化环境补丁与配置
|
|
/// 1. 注入 DNS (resolv.conf) 防止容器内 apk/apt 无法联网
|
|
/// 2. 注入 hosts
|
|
/// 3. 创建挂载点 /workspace 和 /tmp
|
|
static Future<void> postInstallConfigure(Directory rootfsDir) async {
|
|
final etcDir = Directory(p.join(rootfsDir.path, 'etc'));
|
|
if (!etcDir.existsSync()) {
|
|
etcDir.createSync(recursive: true);
|
|
}
|
|
|
|
// 1. 配置 DNS 解析器
|
|
final resolvFile = File(p.join(etcDir.path, 'resolv.conf'));
|
|
try {
|
|
resolvFile.writeAsStringSync(
|
|
'# Generated by CodeEditor DistroManager\n'
|
|
'nameserver 1.1.1.1\n'
|
|
'nameserver 8.8.8.8\n'
|
|
'nameserver 114.114.114.114\n',
|
|
);
|
|
} catch (e) {
|
|
debugPrint('[DistroInstaller] 写入 resolv.conf 失败: $e');
|
|
}
|
|
|
|
// 2. 配置 hosts
|
|
final hostsFile = File(p.join(etcDir.path, 'hosts'));
|
|
try {
|
|
hostsFile.writeAsStringSync(
|
|
'127.0.0.1 localhost\n'
|
|
'::1 localhost ip6-localhost ip6-loopback\n',
|
|
);
|
|
} catch (e) {
|
|
debugPrint('[DistroInstaller] 写入 hosts 失败: $e');
|
|
}
|
|
|
|
// 3. 确保 /workspace 挂载点目录存在
|
|
final workspaceDir = Directory(p.join(rootfsDir.path, 'workspace'));
|
|
if (!workspaceDir.existsSync()) {
|
|
workspaceDir.createSync(recursive: true);
|
|
}
|
|
|
|
// 4. 确保 /tmp 目录存在
|
|
final tmpDir = Directory(p.join(rootfsDir.path, 'tmp'));
|
|
if (!tmpDir.existsSync()) {
|
|
tmpDir.createSync(recursive: true);
|
|
}
|
|
}
|
|
}
|