2
This commit is contained in:
1 parent
27cdb00af2
commit
f78f4aeb4a
20 files changed
+3549
-120
No files matched your search
@@ -161,12 +161,9 @@ class DistroManager {
|
||||
break;
|
||||
|
||||
case ContainerRuntimeMode.chroot:
|
||||
bool hasRoot = false;
|
||||
if (RootService.instance.isDeviceRootCapable()) {
|
||||
hasRoot = await RootService.instance.isRootAvailablePassive();
|
||||
if (!hasRoot) {
|
||||
hasRoot = await RootService.instance.promptRequestRootExplicit();
|
||||
}
|
||||
bool hasRoot = await RootService.instance.isRootAvailablePassive();
|
||||
if (!hasRoot) {
|
||||
hasRoot = await RootService.instance.promptRequestRootExplicit();
|
||||
}
|
||||
if (hasRoot && rootDir.existsSync()) {
|
||||
final chrootReady = await ChrootMountManager.instance.prepareChrootEnvironment(
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
import 'dart:io';
|
||||
import 'dart:isolate';
|
||||
import 'package:code_editor/models/search_model.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
class ProjectSearchService {
|
||||
static final ProjectSearchService instance = ProjectSearchService._();
|
||||
ProjectSearchService._();
|
||||
|
||||
static const Set<String> ignoredDirectories = {
|
||||
'.git',
|
||||
'.dart_tool',
|
||||
'.idea',
|
||||
'.vscode',
|
||||
'build',
|
||||
'node_modules',
|
||||
'.gradle',
|
||||
'.svn',
|
||||
'.hg',
|
||||
'dist',
|
||||
'target',
|
||||
'out',
|
||||
'__pycache__',
|
||||
'.vs',
|
||||
'.settings',
|
||||
};
|
||||
|
||||
static const Set<String> binaryExtensions = {
|
||||
'.png', '.jpg', '.jpeg', '.gif', '.ico', '.webp', '.bmp',
|
||||
'.mp3', '.mp4', '.wav', '.avi', '.mov', '.flac',
|
||||
'.zip', '.tar', '.gz', '.7z', '.rar', '.bz2',
|
||||
'.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx',
|
||||
'.exe', '.dll', '.so', '.dylib', '.class', '.jar', '.apk', '.aab', '.bin',
|
||||
'.pyc', '.o', '.a', '.obj',
|
||||
'.ttf', '.otf', '.woff', '.woff2', '.eot',
|
||||
};
|
||||
|
||||
static const int maxFileSizeBytes = 2 * 1024 * 1024; // 2MB
|
||||
|
||||
/// 在后台 Isolate 中执行工程全文或文件名异步检索
|
||||
Future<SearchResult> search({
|
||||
required String rootPath,
|
||||
required SearchOptions options,
|
||||
int maxResults = 2000,
|
||||
}) async {
|
||||
final query = options.query.trim();
|
||||
if (query.isEmpty) {
|
||||
return const SearchResult.empty();
|
||||
}
|
||||
|
||||
final regExp = options.buildRegExp();
|
||||
if (regExp == null) {
|
||||
return const SearchResult.error('无效的正则表达式', errorCode: SearchErrorCode.invalidRegex);
|
||||
}
|
||||
|
||||
final dir = Directory(rootPath);
|
||||
if (!await dir.exists()) {
|
||||
return const SearchResult.error('项目目录不存在', errorCode: SearchErrorCode.projectDirNotFound);
|
||||
}
|
||||
|
||||
// 采用 Isolate.run 彻底隔离耗时文件读取与正则计算,UI 线程 0 阻塞
|
||||
try {
|
||||
return await Isolate.run(() => _executeSearchInIsolate(
|
||||
rootPath: rootPath,
|
||||
options: options,
|
||||
maxResults: maxResults,
|
||||
));
|
||||
} catch (e) {
|
||||
debugPrint('ProjectSearchService 搜索异常: $e');
|
||||
return SearchResult.error(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
static SearchResult _executeSearchInIsolate({
|
||||
required String rootPath,
|
||||
required SearchOptions options,
|
||||
required int maxResults,
|
||||
}) {
|
||||
final sw = Stopwatch()..start();
|
||||
final regExp = options.buildRegExp();
|
||||
if (regExp == null) {
|
||||
return const SearchResult.error('无效的正则表达式', errorCode: SearchErrorCode.invalidRegex);
|
||||
}
|
||||
|
||||
final rootDir = Directory(rootPath);
|
||||
if (!rootDir.existsSync()) {
|
||||
return const SearchResult.error('项目目录不存在', errorCode: SearchErrorCode.projectDirNotFound);
|
||||
}
|
||||
|
||||
final List<FileSearchResult> fileResults = [];
|
||||
int totalMatchCount = 0;
|
||||
|
||||
void processDirectory(Directory dir) {
|
||||
final List<FileSystemEntity> entities;
|
||||
try {
|
||||
entities = dir.listSync(followLinks: false);
|
||||
} catch (_) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (final entity in entities) {
|
||||
if (totalMatchCount >= maxResults) return;
|
||||
|
||||
final basename = p.basename(entity.path);
|
||||
|
||||
if (entity is Directory) {
|
||||
if (ignoredDirectories.contains(basename) || basename.startsWith('.')) {
|
||||
continue;
|
||||
}
|
||||
if (options.mode == SearchMode.fileName) {
|
||||
final match = regExp.firstMatch(basename);
|
||||
if (match != null) {
|
||||
final relPath = p.relative(entity.path, from: rootPath).replaceAll('\\', '/');
|
||||
fileResults.add(FileSearchResult(
|
||||
filePath: entity.path,
|
||||
relativePath: relPath,
|
||||
fileName: basename,
|
||||
isDirectory: true,
|
||||
matches: [
|
||||
LineMatch(
|
||||
lineNumber: 1,
|
||||
lineContent: basename,
|
||||
matchStart: match.start,
|
||||
matchEnd: match.end,
|
||||
),
|
||||
],
|
||||
));
|
||||
totalMatchCount++;
|
||||
}
|
||||
}
|
||||
processDirectory(entity);
|
||||
} else if (entity is File) {
|
||||
final ext = p.extension(basename).toLowerCase();
|
||||
if (binaryExtensions.contains(ext)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final relPath = p.relative(entity.path, from: rootPath).replaceAll('\\', '/');
|
||||
|
||||
if (options.mode == SearchMode.fileName) {
|
||||
// 文件名称匹配(仅对文件名/文件夹名本身匹配,不含上层相对路径)
|
||||
final match = regExp.firstMatch(basename);
|
||||
if (match != null) {
|
||||
fileResults.add(FileSearchResult(
|
||||
filePath: entity.path,
|
||||
relativePath: relPath,
|
||||
fileName: basename,
|
||||
isDirectory: false,
|
||||
matches: [
|
||||
LineMatch(
|
||||
lineNumber: 1,
|
||||
lineContent: basename,
|
||||
matchStart: match.start,
|
||||
matchEnd: match.end,
|
||||
),
|
||||
],
|
||||
));
|
||||
totalMatchCount++;
|
||||
}
|
||||
} else {
|
||||
// 文本内容匹配
|
||||
try {
|
||||
final stat = entity.statSync();
|
||||
if (stat.size > maxFileSizeBytes) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final content = entity.readAsStringSync();
|
||||
final lines = content.split('\n');
|
||||
final List<LineMatch> lineMatches = [];
|
||||
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
final line = lines[i].replaceAll('\r', '');
|
||||
final matches = regExp.allMatches(line);
|
||||
|
||||
for (final m in matches) {
|
||||
lineMatches.add(LineMatch(
|
||||
lineNumber: i + 1,
|
||||
lineContent: line,
|
||||
matchStart: m.start,
|
||||
matchEnd: m.end,
|
||||
));
|
||||
totalMatchCount++;
|
||||
if (totalMatchCount >= maxResults) break;
|
||||
}
|
||||
if (totalMatchCount >= maxResults) break;
|
||||
}
|
||||
|
||||
if (lineMatches.isNotEmpty) {
|
||||
fileResults.add(FileSearchResult(
|
||||
filePath: entity.path,
|
||||
relativePath: relPath,
|
||||
fileName: basename,
|
||||
matches: lineMatches,
|
||||
));
|
||||
}
|
||||
} catch (_) {
|
||||
// 忽略编码不可读等非文本异常文件
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
processDirectory(rootDir);
|
||||
sw.stop();
|
||||
|
||||
return SearchResult(
|
||||
query: options.query,
|
||||
fileResults: fileResults,
|
||||
totalMatches: totalMatchCount,
|
||||
durationMs: sw.elapsedMilliseconds,
|
||||
isFileNameSearch: options.mode == SearchMode.fileName,
|
||||
);
|
||||
}
|
||||
|
||||
/// 替换单行匹配项并返回更新后的整篇文本内容
|
||||
String replaceSingleMatchInContent({
|
||||
required String fileContent,
|
||||
required LineMatch match,
|
||||
required String replacement,
|
||||
}) {
|
||||
final hasCrLf = fileContent.contains('\r\n');
|
||||
final normalized = fileContent.replaceAll('\r\n', '\n');
|
||||
final lines = normalized.split('\n');
|
||||
|
||||
final lineIndex = match.lineNumber - 1;
|
||||
if (lineIndex < 0 || lineIndex >= lines.length) {
|
||||
return fileContent;
|
||||
}
|
||||
|
||||
final targetLine = lines[lineIndex];
|
||||
if (match.matchStart >= 0 &&
|
||||
match.matchEnd <= targetLine.length &&
|
||||
match.matchStart <= match.matchEnd) {
|
||||
final updatedLine = targetLine.replaceRange(
|
||||
match.matchStart,
|
||||
match.matchEnd,
|
||||
replacement,
|
||||
);
|
||||
lines[lineIndex] = updatedLine;
|
||||
}
|
||||
|
||||
final result = lines.join('\n');
|
||||
return hasCrLf ? result.replaceAll('\n', '\r\n') : result;
|
||||
}
|
||||
|
||||
/// 替换文本中所有匹配项并返回更新后的内容
|
||||
String replaceAllInContent({
|
||||
required String fileContent,
|
||||
required RegExp regExp,
|
||||
required String replacement,
|
||||
}) {
|
||||
return fileContent.replaceAll(regExp, replacement);
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ class RootService {
|
||||
@visibleForTesting
|
||||
bool? mockRootGranted;
|
||||
|
||||
/// 常见 Linux/Android su 二进制搜索路径
|
||||
/// 常见 Linux/Android su 二进制搜索路径(包含现代 KernelSU / APatch / Magisk 扩展路径)
|
||||
static const List<String> _commonSuPaths = [
|
||||
'/system/bin/su',
|
||||
'/system/xbin/su',
|
||||
@@ -26,17 +26,38 @@ class RootService {
|
||||
'/system/sd/xbin/su',
|
||||
'/data/local/xbin/su',
|
||||
'/data/local/bin/su',
|
||||
'/data/adb/ksu/bin/su',
|
||||
'/data/adb/ap/bin/su',
|
||||
'/data/adb/magisk/su',
|
||||
'/data/adb/magisk/busybox',
|
||||
'/apex/com.android.runtime/bin/su',
|
||||
'/system_ext/bin/su',
|
||||
'/product/bin/su',
|
||||
'/odm/bin/su',
|
||||
'/vendor/bin/su',
|
||||
'/system/bin/.ext/.su',
|
||||
'/system/usr/we-need-root/su',
|
||||
'/system/xbin/ku.sud',
|
||||
];
|
||||
|
||||
/// 静态检测当前设备是否具备 Root 能力(文件系统存在 su 二进制)
|
||||
/// 常见现代 Root 框架特征目录 (KernelSU / APatch / Magisk)
|
||||
static const List<String> _commonRootIndicatorDirs = [
|
||||
'/data/adb/ksu',
|
||||
'/data/adb/ap',
|
||||
'/data/adb/magisk',
|
||||
'/data/adb/modules',
|
||||
];
|
||||
|
||||
/// 静态检测当前设备是否具备 Root 能力(文件系统存在 su 二进制或现代 Root 管理器特征)
|
||||
///
|
||||
/// 该方法纯粹检查静态文件路径,绝不调用任何命令,因此绝对不会触发 Magisk / KernelSU / APatch 授权弹窗。
|
||||
/// 该方法纯粹检查静态文件路径与环境变量,绝不调用外部进程命令,因此不会触发授权弹窗。
|
||||
bool isDeviceRootCapable() {
|
||||
if (mockRootCapable != null) {
|
||||
return mockRootCapable!;
|
||||
}
|
||||
|
||||
if (!kIsWeb && Platform.isAndroid) {
|
||||
// 1. 遍历已知 su 二进制路径
|
||||
for (final path in _commonSuPaths) {
|
||||
try {
|
||||
if (File(path).existsSync()) {
|
||||
@@ -44,6 +65,29 @@ class RootService {
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
// 2. 遍历 PATH 环境变量中的所有目录进行动态检索
|
||||
final pathEnv = Platform.environment['PATH'];
|
||||
if (pathEnv != null && pathEnv.isNotEmpty) {
|
||||
final dirs = pathEnv.split(':');
|
||||
for (final dir in dirs) {
|
||||
if (dir.isEmpty) continue;
|
||||
try {
|
||||
if (File('$dir/su').existsSync()) {
|
||||
return true;
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 探测现代 Root 框架特征目录 (KernelSU / APatch / Magisk)
|
||||
for (final dir in _commonRootIndicatorDirs) {
|
||||
try {
|
||||
if (Directory(dir).existsSync()) {
|
||||
return true;
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -74,16 +118,12 @@ class RootService {
|
||||
|
||||
/// 被动检测当前是否已获得真实 Root 权限 (uid=0)
|
||||
///
|
||||
/// 仅在用户主动触发 Chroot 操作、或 Auto 模式下准备启动容器时被动调用。
|
||||
/// 直接执行最小命令探针 `su -c 'id -u'`,不受静态文件可见性(Root 隐藏/SELinux 隔离)限制。
|
||||
Future<bool> isRootAvailablePassive({Duration timeout = const Duration(seconds: 4)}) async {
|
||||
if (mockRootGranted != null) {
|
||||
return mockRootGranted!;
|
||||
}
|
||||
|
||||
if (!isDeviceRootCapable()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
final result = await Process.run(
|
||||
'su',
|
||||
|
||||
Reference in new issue
Block a user