parseStatLines function

List<StatEntry> parseStatLines(
  1. String stdout
)

Parses the TYPE|SIZE|PATH lines emitted by the node's find/stat run.

The type field is a human word — GNU %F (directory/regular file/ symbolic link) or BSD %HT (Directory/Regular File/Symbolic Link). Only the first two | separate the fields, so a | in a path is preserved.

Implementation

List<StatEntry> parseStatLines(String stdout) {
  final out = <StatEntry>[];
  for (final line in const LineSplitter().convert(stdout)) {
    if (line.isEmpty) continue;
    final i1 = line.indexOf('|');
    if (i1 < 0) continue;
    final i2 = line.indexOf('|', i1 + 1);
    if (i2 < 0) continue;
    final type = line.substring(0, i1).toLowerCase();
    final size = int.tryParse(line.substring(i1 + 1, i2).trim()) ?? 0;
    final path = line.substring(i2 + 1);
    final isLink = type.contains('link');
    out.add((
      path: path,
      size: size,
      isDir: !isLink && type.contains('dir'),
      isLink: isLink,
    ));
  }
  return out;
}