listSync method
Lists the sub-directories and files of this Directory. Optionally recurses into sub-directories.
If followLinks
is false
, then any symbolic links found
are reported as Link objects, rather than as directories or files,
and are not recursed into.
If followLinks
is true
, then working links are reported as
directories or files, depending on what they point to,
and links to directories are recursed into if recursive
is true
.
Broken links are reported as Link objects.
If a link makes a loop in the file system, then a recursive listing will not follow a link twice in the same recursive descent, but will report it as a Link the second time it is seen.
Returns a List containing FileSystemEntity objects for the
directories, files, and links. The List will be in an arbitrary order
and does not include the special entries '.'
and '..'
.
Implementation
@override
List<FileSystemEntity> listSync(
{bool recursive = false, bool followLinks = true}) {
assertIsDirectory(_fs, path);
final map = _fs.toMap();
final entities = <FileSystemEntity>[];
FileSystemEntity toEntity(MapBasedFsImplementation impl) {
if (impl is MapBasedFsFileImplementation) {
return ArchiveFile._(_fs, path);
} else if (impl is MapBasedFsDirectoryImplementation) {
return ArchiveDirectory._(_fs, path);
} else {
throw StateError("Unknown entity type");
}
}
// TODO(ehesp): Implement followLinks
// TODO could move to MapBasedFileSystem as utility method
if (recursive) {
for (final entry in map.entries) {
if (entry.key.startsWith(absolute.path) && entry.key != absolute.path) {
entities.add(toEntity(entry.value!));
}
}
} else {
for (final entry in map.entries) {
if (entry.key.startsWith(absolute.path) &&
entry.key.split("/").length ==
absolute.path.split("/").length + 1) {
entities.add(toEntity(entry.value!));
}
}
}
return entities;
}