getFileSystemStats method
Gets file system statistics for the specified paths.
Collects information about disk usage, file counts, and other relevant metrics for reporting and validation.
Parameters:
paths: List of paths to analyze
Returns a map containing file system statistics.
Implementation
Map<String, dynamic> getFileSystemStats(List<String> paths) {
final stats = <String, dynamic>{};
var totalFiles = 0;
var totalDirectories = 0;
var totalSizeBytes = 0;
final pathStats = <String, Map<String, dynamic>>{};
for (final path in paths) {
final pathStat = <String, dynamic>{};
if (!exists(path)) {
pathStat['exists'] = false;
pathStats[path] = pathStat;
continue;
}
pathStat['exists'] = true;
if (Directory(path).existsSync()) {
pathStat['type'] = 'directory';
try {
final dir = Directory(path);
final entities = dir.listSync(recursive: true);
final files = entities.whereType<File>().toList();
final directories = entities.whereType<Directory>().toList();
pathStat['fileCount'] = files.length;
pathStat['directoryCount'] = directories.length;
var pathSize = 0;
for (final file in files) {
try {
pathSize += file.lengthSync();
} catch (e) {
// Skip files we can't read
}
}
pathStat['sizeBytes'] = pathSize;
totalFiles += files.length;
totalDirectories += directories.length;
totalSizeBytes += pathSize;
} catch (e) {
pathStat['error'] = e.toString();
}
} else {
pathStat['type'] = 'file';
try {
final fileSize = File(path).lengthSync();
pathStat['sizeBytes'] = fileSize;
totalSizeBytes += fileSize;
totalFiles++;
} catch (e) {
pathStat['error'] = e.toString();
}
}
pathStats[path] = pathStat;
}
stats['totalFiles'] = totalFiles;
stats['totalDirectories'] = totalDirectories;
stats['totalSizeBytes'] = totalSizeBytes;
stats['totalSizeMB'] = (totalSizeBytes / (1024 * 1024)).toStringAsFixed(2);
stats['pathStats'] = pathStats;
return stats;
}