diff --git a/lib/data/model/file/file_ref.dart b/lib/data/model/file/file_ref.dart index ee90a37ea9..5132c2967f 100644 --- a/lib/data/model/file/file_ref.dart +++ b/lib/data/model/file/file_ref.dart @@ -1,4 +1,5 @@ import 'package:fl_lib/fl_lib.dart'; +import 'package:flutter/foundation.dart' show visibleForTesting; import 'package:server_box/core/ssh/host_keys.dart'; import 'package:server_box/core/ssh/identities.dart'; import 'package:server_box/core/ssh/jump_chain.dart'; @@ -27,10 +28,9 @@ sealed class FileRef { /// The last component. What a transfer list calls the job. String get name { - final normalized = path.replaceAll(r'\', '/'); - final trimmed = normalized.endsWith('/') && normalized.length > 1 - ? normalized.substring(0, normalized.length - 1) - : normalized; + final trimmed = this is LocalFileRef + ? normalizeLocalPath(path) + : _trimPosixTrailing(path); // The root has no last component but is still somewhere; without this it // would name itself the empty string. if (trimmed == '/') return '/'; @@ -49,20 +49,57 @@ final class LocalFileRef extends FileRef { @override final String path; + /// Normalized path for equality and [child]: on Windows, `\` is folded to `/` + /// so a ref built with `Pfs.seperator` and one built by [child] (`/`) read as + /// the same place; on POSIX, `\` is a valid filename character and stays + /// unchanged. + String get _normalizedPath => normalizeLocalPath(path); + @override - LocalFileRef child(String name) => - LocalFileRef(path.joinPath(name, separator: '/')); + LocalFileRef child(String name) { + final dir = _normalizedPath; + return LocalFileRef(dir.endsWith('/') ? '$dir$name' : '$dir/$name'); + } @override - bool operator ==(Object other) => other is LocalFileRef && other.path == path; + bool operator ==(Object other) => + other is LocalFileRef && other._normalizedPath == _normalizedPath; @override - int get hashCode => Object.hash(LocalFileRef, path); + int get hashCode => Object.hash(LocalFileRef, _normalizedPath); @override String toString() => 'LocalFileRef($path)'; } +/// A local path as it compares: on Windows `\` is folded to `/`, while on +/// POSIX platforms `\` is preserved as a literal filename character. Duplicate +/// `/` are collapsed and trailing `/` stripped — except the root `/`. +@visibleForTesting +String normalizeLocalPath(String path, {bool? windows}) { + var out = (windows ?? isWindows) ? path.replaceAll(r'\', '/') : path; + while (out.contains('//')) { + out = out.replaceAll('//', '/'); + } + if (out.length > 1 && out.endsWith('/')) { + out = out.substring(0, out.length - 1); + } + return out; +} + +String _trimPosixTrailing(String dir) { + var base = dir; + while (base.length > 1 && base.endsWith('/')) { + base = base.substring(0, base.length - 1); + } + return base; +} + +String _joinPosixChild(String dir, String name) { + final base = _trimPosixTrailing(dir); + return base.endsWith('/') ? '$base$name' : '$base/$name'; +} + /// A server, over its SSH connection. /// /// Which protocol runs on top of that connection — SFTP or `scp` — is the @@ -91,9 +128,11 @@ final class SshFileRef extends FileRef { spi.ssh?.fileTransport ?? SshFileTransport.sftp; /// Always `/`: the far side is a Linux-like system, whatever this device is. + /// A root (`/`) joining a name is the name inside the root, not a doubled + /// slash — same answer [BrowsePath.join] gives a browser. @override SshFileRef child(String name) => - SshFileRef(creds: creds, path: path.joinPath(name, separator: '/')); + SshFileRef(creds: creds, path: _joinPosixChild(path, name)); // The whole [Spi], not its id. A queued transfer names where it is going, and // an id outlives an edit that moved the server somewhere else — so two refs @@ -147,12 +186,13 @@ final class MonitorFileRef extends FileRef { final String path; /// Always `/`: the agent describes a POSIX-shaped filesystem whatever it is - /// running on. + /// running on. A root (`/`) joining a name is the name inside the root, not + /// a doubled slash — same answer [BrowsePath.join] gives a browser. @override MonitorFileRef child(String name) => MonitorFileRef( spi: spi, monitor: monitor, - path: path.joinPath(name, separator: '/'), + path: _joinPosixChild(path, name), ); @override diff --git a/test/unit/file/file_transfer_test.dart b/test/unit/file/file_transfer_test.dart index eff0fad911..b04f82f97e 100644 --- a/test/unit/file/file_transfer_test.dart +++ b/test/unit/file/file_transfer_test.dart @@ -32,10 +32,94 @@ void main() { expect(const LocalFileRef('/a/b').child('c.txt').path, '/a/b/c.txt'); }); + test('a root or a trailing slash joins without doubling', () { + expect(const LocalFileRef('/').child('a').path, '/a'); + expect(const LocalFileRef('/a/').child('b').path, '/a/b'); + expect(const LocalFileRef('/a///').child('b').path, '/a/b'); + if (Platform.isWindows) { + expect(const LocalFileRef(r'C:\').child('a').path, 'C:/a'); + } + }); + + test('duplicate and trailing slashes do not make two places', () { + expect(const LocalFileRef('/a//b') == const LocalFileRef('/a/b'), isTrue); + expect(const LocalFileRef('/a/b/') == const LocalFileRef('/a/b'), isTrue); + expect( + const LocalFileRef('/a/b/').hashCode, + const LocalFileRef('/a/b').hashCode, + ); + }); + test('two ends are the same place only when both halves match', () { expect(const LocalFileRef('/a/b') == const LocalFileRef('/a/b'), isTrue); expect(const LocalFileRef('/a/b') == const LocalFileRef('/a/c'), isFalse); }); + + test('backslash normalization applies only on Windows', () { + // Windows: backslashes fold to forward slashes, duplicate/trailing + // slashes collapse. + expect(normalizeLocalPath(r'C:\a\b', windows: true), 'C:/a/b'); + expect(normalizeLocalPath(r'C:\', windows: true), 'C:'); + expect(normalizeLocalPath(r'C:\\a\\b\\', windows: true), 'C:/a/b'); + expect(normalizeLocalPath('/', windows: true), '/'); + + // POSIX: backslashes are valid filename characters and stay untouched, + // while duplicate/trailing forward slashes still normalize. + expect(normalizeLocalPath(r'/a\b', windows: false), r'/a\b'); + expect(normalizeLocalPath(r'/a\b/', windows: false), r'/a\b'); + expect(normalizeLocalPath(r'/a\\b//c/', windows: false), r'/a\\b/c'); + expect(normalizeLocalPath('/', windows: false), '/'); + + if (Platform.isWindows) { + expect( + const LocalFileRef(r'C:\a\b') == const LocalFileRef('C:/a/b'), + isTrue, + ); + expect( + const LocalFileRef(r'C:\a\b').hashCode, + const LocalFileRef('C:/a/b').hashCode, + ); + expect(const LocalFileRef(r'C:\a\b').name, 'b'); + } else { + expect( + const LocalFileRef(r'/tmp/a\b') == const LocalFileRef('/tmp/a/b'), + isFalse, + ); + expect( + const LocalFileRef(r'/tmp/a\b').child('c').path, + r'/tmp/a\b/c', + ); + expect(const LocalFileRef(r'/tmp/a\b').name, r'a\b'); + } + }); + + test('remote refs join root and trailing slashes without doubling', () { + final monitor = MonitorFileRef.forServer( + const Spi( + name: 'agent', + id: 'agent', + monitorHttp: MonitorHttpCredential(addr: 'https://10.0.0.3:3770'), + ), + '/', + ); + expect(monitor.child('etc').path, '/etc'); + expect( + MonitorFileRef( + spi: monitor.spi, + monitor: monitor.monitor, + path: '/var///', + ).child('log').path, + '/var/log', + ); + expect( + MonitorFileRef( + spi: monitor.spi, + monitor: monitor.monitor, + path: r'/tmp/a\b', + ).name, + r'a\b', + ); + }); }); group('where a transfer runs', () {