sizeOf method

Future<int> sizeOf(
  1. String path
)

Implementation

Future<int> sizeOf(String path) async {
  final type = FileSystemEntity.typeSync(path, followLinks: false);
  if (type == FileSystemEntityType.notFound) return 0;
  if (type == FileSystemEntityType.file) return File(path).lengthSync();
  if (type != FileSystemEntityType.directory) return 0;

  var total = 0;
  final directory = Directory(path);
  await for (final entity
      in directory
          .list(recursive: true, followLinks: false)
          .handleError((_) {})) {
    if (entity is File) {
      try {
        total += await entity.length();
      } on FileSystemException {
        // Files can vanish while scanning caches; ignore and continue.
      }
    }
  }
  return total;
}