listDirectory method

Future<void> listDirectory(
  1. String path
)

Implementation

Future<void> listDirectory(String path) async {
  if (!await openDataConnection()) {
    return;
  }

  try {
    transferInProgress = true;

    var dirContents = await fileOperations.listDirectory(path);
    logger.generalLog('Listing directory: $path');

    for (FileSystemEntity entity in dirContents) {
      if (!transferInProgress) break; // Abort if transfer is cancelled

      var stat = await entity.stat();
      String permissions = _formatPermissions(stat);
      String fileSize = stat.size.toString();
      String modificationTime = _formatModificationTime(stat.modified);
      String fileName = entity.path.split(Platform.pathSeparator).last;
      String entry =
          '$permissions 1 ftp ftp $fileSize $modificationTime $fileName\r\n';
      dataSocket!.write(entry);
    }

    if (transferInProgress) {
      transferInProgress = false;
      await _closeDataSocket();
      sendResponse('226 Transfer complete');
    }
  } catch (e) {
    sendResponse('550 Failed to list directory');
    logger.generalLog('Error listing directory: $e');
    transferInProgress = false;
    await _closeDataSocket();
  }
}