write static method

Future<void> write(
  1. String projectRoot,
  2. SkillRevision revision,
  3. List<SkillFile> files
)

Replaces each of revision's directories with files and the marker. Each directory is staged as a hidden sibling and renamed into place, so an agent never reads a half-written skill. It works under the real path of each skill root, which escaping confines to the project.

Throws before writing anything when a root escapes the project, a ProfileSkillDirectoriesException naming its directories as ProfileSkillDirectoryState.outside, as get refuses it; when revision's marker would be larger than the 64 KiB a marker is read up to; when a file's path would land outside its directory; and when a directory it would replace holds no marker for its id, the same exception with ProfileSkillDirectoryState.unmarked. It checks each directory's marker again just before moving it aside, so a directory that appears without one meanwhile is never replaced either; that refusal comes after the roots before it were replaced, and leaves them so.

Implementation

static Future<void> write(
  String projectRoot,
  SkillRevision revision,
  List<SkillFile> files,
) async {
  final id = revision.id.value;
  final resolved = await _resolvedRoots(projectRoot);
  final escaped = [
    for (final (:root, :real) in resolved)
      if (real == null) root,
  ];
  if (escaped.isNotEmpty) {
    // As get's own check refuses it, so a root that a link moved outside
    // the project after that check fails the same way.
    throw ProfileSkillDirectoriesException([
      for (final root in escaped)
        ProfileSkillDirectory(
          profile: revision.id,
          root: root,
          path: '$root/$id',
          state: ProfileSkillDirectoryState.outside,
        ),
    ]);
  }
  if (!skillMarkerFits(revision)) {
    throw ArgumentError.value(
      id,
      'revision',
      'has a release too long for the $marker marker, which is read up to '
          '64 KiB',
    );
  }
  final writes = [
    for (final (:root, :real) in resolved)
      _StagedWrite(root, Directory(p.join(real!, id)), files),
  ];
  for (final write in writes) {
    for (final (:file, :path) in write.targets) {
      if (!p.isWithin(write.staged.path, path)) {
        throw ArgumentError.value(
          file.path,
          'files',
          'is not a path inside the skill',
        );
      }
    }
  }
  ProfileSkillDirectoriesException unmarked(_StagedWrite write) =>
      ProfileSkillDirectoriesException([
        ProfileSkillDirectory(
          profile: revision.id,
          root: write.root,
          path: '${write.root}/$id',
          state: ProfileSkillDirectoryState.unmarked,
        ),
      ]);
  for (final write in writes) {
    if (await _occupied(write.target) && !await _owned(write.target, id)) {
      throw unmarked(write);
    }
  }
  for (final write in writes) {
    final _StagedWrite(:target, :staged, :old, :targets) = write;
    await target.parent.create(recursive: true);
    try {
      await _createMarked(staged, revision);
      for (final (:file, :path) in targets) {
        final written = File(path);
        await written.parent.create(recursive: true);
        await written.writeAsBytes(file.bytes, flush: true);
      }
      final executables = [
        for (final (:file, :path) in targets)
          if (file.executable) path,
      ];
      if (executables.isNotEmpty && !Platform.isWindows) {
        final chmod = await Process.run('chmod', ['+x', ...executables]);
        if (chmod.exitCode != 0) {
          throw FileSystemException(
            'could not mark skill files executable: ${chmod.stderr}',
            staged.path,
          );
        }
      }
      Directory? previous;
      if (await _occupied(target)) {
        if (!await _owned(target, id)) throw unmarked(write);
        previous = await target.rename(old);
      }
      try {
        await staged.rename(target.path);
      } on Object {
        await previous?.rename(target.path);
        rethrow;
      }
      await previous?.delete(recursive: true);
    } on Object {
      if (await staged.exists()) await staged.delete(recursive: true);
      rethrow;
    }
  }
}