Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

1.6 KB
Raw Blame
import 'dart:io';
import 'dart:typed_data';

import 'package:path/path.dart' as p;

import 'folder_read.dart' show FolderFiles;

FolderFiles openFolder(String path) => IoFolderFiles(path);

/// [FolderFiles] by path, where the platform gives no way to open a file
/// relative to a folder without following links (Windows). Each name on the
/// way is checked not to be a link, and the file once open must still lie in
/// the folder; a link swapped in between that check and the open is not
/// caught.
final class IoFolderFiles implements FolderFiles {
  /// Refuses a dropped folder that is a link by now, as a link inside it is
  /// left out.
  IoFolderFiles(String path) : _root = _unlinked(path);

  static String _unlinked(String path) {
    if (FileSystemEntity.isLinkSync(path)) {
      throw FileSystemException('The folder is a link now', path);
    }
    return Directory(path).resolveSymbolicLinksSync();
  }

  final String _root;

  @override
  Uint8List? read(List<String> inside, int maxBytes) {
    var path = _root;
    for (final name in inside) {
      path = p.join(path, name);
      if (FileSystemEntity.isLinkSync(path)) return null;
    }
    final file = File(path).openSync();
    try {
      if (!p.isWithin(_root, File(path).resolveSymbolicLinksSync())) {
        return null;
      }
      final bytes = BytesBuilder(copy: false);
      while (bytes.length <= maxBytes) {
        final chunk = file.readSync(maxBytes + 1 - bytes.length);
        if (chunk.isEmpty) break;
        bytes.add(chunk);
      }
      return bytes.takeBytes();
    } finally {
      file.closeSync();
    }
  }

  @override
  void close() {}
}