mountDirectory method

Future<MountRecord> mountDirectory({
  1. required String localDir,
  2. required String nodeId,
  3. required String remotePath,
  4. String? name,
  5. bool readWrite = false,
  6. bool initialSync = true,
  7. DriveProgress? onProgress,
})

Mounts a local directory onto remotePath of nodeId.

Implementation

Future<MountRecord> mountDirectory({
  required String localDir,
  required String nodeId,
  required String remotePath,
  String? name,
  bool readWrite = false,
  bool initialSync = true,
  DriveProgress? onProgress,
}) async {
  final dir = Directory(localDir);
  if (!await dir.exists()) {
    throw DriveException('local directory not found: $localDir');
  }
  final localAbs = dir.absolute.path;
  final mountName = name ?? _basename(localAbs);
  final endpoint = _endpointId(nodeId);
  final driveId = DriveId.scoped(endpoint: endpoint, name: mountName);
  final id = _mountId(nodeId, mountName);

  var record = MountRecord(
    id: id,
    nodeId: nodeId,
    name: mountName,
    kind: 'dir',
    remotePath: remotePath,
    localPath: localAbs,
    readWrite: readWrite,
    driveId: driveId.value,
    mountedAt: DateTime.now(),
    syncState: SyncState(baselineRef: _emptyDirRef, status: SyncStatus.clean),
  );

  record = await _withSession(record, (rpc) async {
    // Anchor the baseline at the node's current content before the first push.
    final originRef = (await ChannelContentSource(rpc).manifest()).hash();
    var r = record.copyWith(
      syncState: SyncState(baselineRef: originRef, status: SyncStatus.clean),
    );
    if (initialSync) {
      r = (await _syncDirectory(
        r,
        rpc,
        SyncDirection.push,
        onProgress: onProgress,
      )).record;
    }
    return r;
  });

  store.mounts[id] = record;
  await store.save();
  return record;
}