ls method
Lists the contents of the directory at the given path.
path may be an MFS path or an /ipfs/<cid>[/sub] path. If the target
is a file, a single entry describing it is returned — matching Kubo
files ls on a non-directory path.
long mirrors files ls -l: entries get their Type (0=file,
1=directory), Size (logical file size; 0 for directories) and Hash
populated, plus UnixFS 1.5 Mode/Mtime when stored. Without it only
Name is populated, matching Kubo's ListNames response. u requests
unsorted order (Kubo compatibility flag).
Implementation
Future<List<MFSListEntry>> ls(
String path, {
bool long = false,
bool u = false,
}) async {
final cid = await _resolveAny(path);
if (cid == null) throw Exception('Path not found: $path');
final block = await _blockStore.getBlock(cid.encode());
if (!block.found) throw Exception('Block not found for CID: $cid');
final type = await _unixfsType(cid);
if (type != Data_DataType.Directory && type != Data_DataType.HAMTShard) {
// Kubo lists the file itself when the target is not a directory.
if (!long) {
return [
MFSListEntry(name: _basename(path), type: 0, size: 0, hash: ''),
];
}
final node = _tryParseNode(block.block.data);
return [
_entryFor(
_basename(path),
cid,
node,
dataLength: block.block.data.length,
),
];
}
final node = PBNode.fromBuffer(block.block.data);
var entries = node.links.toList();
if (!u) {
entries.sort((a, b) => compareEntryNamesUtf8(a.name, b.name));
}
if (!long) {
// Kubo `files ls` without -l resolves only the names.
return [
for (final link in entries)
MFSListEntry(name: link.name, type: 0, size: 0, hash: ''),
];
}
final result = <MFSListEntry>[];
for (final link in entries) {
final childCid = CID.fromBytes(Uint8List.fromList(link.hash));
final childBlock = await _blockStore.getBlock(childCid.encode());
PBNode? childNode;
var dataLength = 0;
if (childBlock.found) {
dataLength = childBlock.block.data.length;
childNode = _tryParseNode(childBlock.block.data);
}
result.add(
_entryFor(link.name, childCid, childNode, dataLength: dataLength),
);
}
return result;
}