Files
FlClash/lib/common/file.dart
T
chen08209 5589c76921 fix(core,profile,ui): keep files inside their owner and layouts usable when narrow
The elevated Core no longer leaves root-owned files behind on macOS, and
provider files stay inside the profile that owns them.

Cards, form rows, and narrow layouts stay usable at small widths.

Changelog: Elevated runs and profile providers stop leaving files outside their owner, and narrow windows stay usable
2026-08-28 10:07:17 +08:00

47 lines
1.1 KiB
Dart

import 'dart:io';
extension FileExt on File {
Future<void> safeCopy(String newPath) async {
if (!await exists()) {
await create(recursive: true);
return;
}
final targetFile = File(newPath);
if (!await targetFile.exists()) {
await targetFile.create(recursive: true);
}
await copy(newPath);
}
Future<File> safeWriteAsString(String str) async {
if (!await exists()) {
await create(recursive: true);
}
return writeAsString(str);
}
Future<File> safeWriteAsBytes(List<int> bytes) async {
if (!await exists()) {
await create(recursive: true);
}
return writeAsBytes(bytes);
}
}
extension FileSystemEntityExt on FileSystemEntity {
Future<void> safeDelete({bool recursive = false}) async {
if (!await exists()) {
return;
}
await delete(recursive: recursive);
}
}
Future<void> safeDeletePath(String path) async {
final entity = switch (FileSystemEntity.typeSync(path)) {
FileSystemEntityType.directory => Directory(path),
_ => File(path),
};
await entity.safeDelete(recursive: true);
}