read static method
The installed SKILL.md of revision: from the first directory in
directories order that holds revision, the check stale and
validation's profile-skill-stale note make. Null when none does, as
before get or after the lock moved on, and when that directory has
no readable UTF-8 SKILL.md. projectRoot is the directory of the
wayfinder.json that bound the bundle, which a selection names as
SelectedProfile.projectRoot. Returns nothing that a symbolic link
places outside it, and never writes.
Each file is confined by its real path, and that real path is what is read, so a symbolic link retargeted after the check does not move the read. Each file is read only when it is a regular file, checked without following links just before it is opened, and the text is returned only when its real path still holds no symbolic link after the read and the marker, read again, is still a regular file, so a file or marker replaced by a link meanwhile is not returned either. Dart opens a file only by following its path, and an open of a named pipe waits for a writer, so two swaps between that last check and the open remain out of reach: a link put in place and taken away again before the read ends, whose target is returned, and a named pipe, which holds the read until something writes to it. A host that must exclude them keeps writers out of the project while it reads.
With maxBytes, at most maxBytes + 1 bytes are read: a longer file
is cut at the last character boundary within its first maxBytes
bytes and returned with truncated set, so a host can bound what a
huge file costs it. The marker is read up to 64 KiB; a larger one
marks no revision.
It takes no lock. The marker is read again after SKILL.md, and the
text is returned only when it still names revision, so a get that
replaces the directory meanwhile yields the next directory or null,
never another revision's text.
Implementation
static Future<ProfileSkillText?> read(
String projectRoot,
SkillRevision revision, {
int? maxBytes,
}) async {
if (maxBytes != null) RangeError.checkNotNegative(maxBytes, 'maxBytes');
final String real;
try {
real = await Directory(projectRoot).resolveSymbolicLinks();
} on FileSystemException {
return null;
}
// The real path of [path] when it lies inside the project; null
// otherwise. Only this path is read afterwards, and only when it holds
// a regular file just before it is opened.
Future<String?> confined(String path) async {
final resolved = await _realPath(path);
return resolved != null && p.isWithin(real, resolved) ? resolved : null;
}
for (final directory in directories(revision.id)) {
final path = p.join(projectRoot, directory);
try {
final markerFile = await confined(p.join(path, marker));
final skillFile = await confined(p.join(path, 'SKILL.md'));
if (markerFile == null ||
skillFile == null ||
await _revisionIn(File(markerFile), followLinks: false) !=
revision ||
!await _isRegularFile(skillFile)) {
continue;
}
final text = await _readText(File(skillFile), maxBytes);
final now = await _realPath(skillFile);
if (now != null &&
p.equals(now, skillFile) &&
await _revisionIn(File(markerFile), followLinks: false) ==
revision) {
return text;
}
} on FileSystemException {
// Unreadable: the next directory may still hold the revision.
} on FormatException {
// Not UTF-8 text: the next directory may still hold the revision.
}
}
return null;
}