From b5dda254bdda01ebc42bbcb29eec11d469d49740 Mon Sep 17 00:00:00 2001 From: pwu2775-dev Date: Thu, 3 Dec 2026 22:01:31 +0800 Subject: [PATCH 1/2] fix: file: a local ref spells its path the same place either way A download's destination is built with the platform's own separator (serverLocalPath folds Paths.file.joinPath, which is a backslash on Windows), while a browser's child always joins with `/` - so on Windows a finished download into the directory a browser is showing never compared equal to where the browser stands, and _refreshOnArrival stayed silent: the list kept showing the directory as it was before the file landed. == and hashCode now compare the path the way name already reads it - backslashes folded to forward slashes, duplicate slashes collapsed, a trailing slash gone unless it is the root's own - and child() joins by the same rule BrowsePath.join does on all three ref types, so a root or a trailing slash names the name inside it rather than doubling. The local backends accept either spelling; nothing else reads the raw string. The file's line endings are normalized to LF like the rest of the repo. --- lib/data/model/file/file_ref.dart | 63 +++++++++++++++++++++----- test/unit/file/file_transfer_test.dart | 24 ++++++++++ 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/lib/data/model/file/file_ref.dart b/lib/data/model/file/file_ref.dart index ee90a37ea9..13c4cfecb5 100644 --- a/lib/data/model/file/file_ref.dart +++ b/lib/data/model/file/file_ref.dart @@ -49,20 +49,44 @@ final class LocalFileRef extends FileRef { @override final String path; + /// Forward slashes, whatever was written: a ref built by hand on Windows + /// (`Pfs.seperator`) and one built by [child] (`/`) must read as the same + /// place, or a transfer into the directory a browser is showing never tells + /// that browser to refresh. The local backends accept either. + 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: `\` folded to `/`, duplicate slashes +/// collapsed, and a trailing slash gone — except the root's own, which is +/// what says the root is the root. The local backends accept any of these +/// spellings, so two that do are one place. +String _normalizeLocalPath(String path) { + var out = path.replaceAll(r'\', '/'); + while (out.contains('//')) { + out = out.replaceAll('//', '/'); + } + if (out.length > 1 && out.endsWith('/')) { + out = out.substring(0, out.length - 1); + } + return out; +} + /// A server, over its SSH connection. /// /// Which protocol runs on top of that connection — SFTP or `scp` — is the @@ -91,9 +115,18 @@ 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 child(String name) { + final dir = path.length > 1 && path.endsWith('/') + ? path.substring(0, path.length - 1) + : path; + return SshFileRef( + creds: creds, + path: dir.endsWith('/') ? '$dir$name' : '$dir/$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,13 +180,19 @@ 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: '/'), - ); + MonitorFileRef child(String name) { + final dir = path.length > 1 && path.endsWith('/') + ? path.substring(0, path.length - 1) + : path; + return MonitorFileRef( + spi: spi, + monitor: monitor, + path: dir.endsWith('/') ? '$dir$name' : '$dir/$name', + ); + } @override bool operator ==(Object other) => diff --git a/test/unit/file/file_transfer_test.dart b/test/unit/file/file_transfer_test.dart index eff0fad911..9f88d06e3a 100644 --- a/test/unit/file/file_transfer_test.dart +++ b/test/unit/file/file_transfer_test.dart @@ -32,10 +32,34 @@ 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(r'C:\').child('a').path, 'C:/a'); + expect(const LocalFileRef('/a/').child('b').path, '/a/b'); + }); + + 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); + }); + 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('a separator written the other way is still the same place', () { + // A download's destination is built with the platform's own separator + // (`serverLocalPath`), while a browser's `child` always joins with `/`. + // On Windows the two spellings must still read as one place, or a + // finished download never refreshes the browser showing its directory. + 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); + }); }); group('where a transfer runs', () { From cd7b82b7dc69f3ea70b950d9c1b286daad559e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lollipopkit=F0=9F=8F=B3=EF=B8=8F=E2=80=8D=E2=9A=A7?= =?UTF-8?q?=EF=B8=8F?= <10864310+lollipopkit@users.noreply.github.com> Date: Fri, 4 Dec 2026 00:51:09 +0800 Subject: [PATCH 2/2] fix: convert backslashes in local file refs on Windows only --- lib/data/model/file/file_ref.dart | 69 +++++++++++----------- test/unit/file/file_transfer_test.dart | 80 ++++++++++++++++++++++---- 2 files changed, 105 insertions(+), 44 deletions(-) diff --git a/lib/data/model/file/file_ref.dart b/lib/data/model/file/file_ref.dart index 13c4cfecb5..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,11 +49,11 @@ final class LocalFileRef extends FileRef { @override final String path; - /// Forward slashes, whatever was written: a ref built by hand on Windows - /// (`Pfs.seperator`) and one built by [child] (`/`) must read as the same - /// place, or a transfer into the directory a browser is showing never tells - /// that browser to refresh. The local backends accept either. - String get _normalizedPath => _normalizeLocalPath(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) { @@ -72,12 +72,12 @@ final class LocalFileRef extends FileRef { String toString() => 'LocalFileRef($path)'; } -/// A local path as it compares: `\` folded to `/`, duplicate slashes -/// collapsed, and a trailing slash gone — except the root's own, which is -/// what says the root is the root. The local backends accept any of these -/// spellings, so two that do are one place. -String _normalizeLocalPath(String path) { - var out = path.replaceAll(r'\', '/'); +/// 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('//', '/'); } @@ -87,6 +87,19 @@ String _normalizeLocalPath(String path) { 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 @@ -118,15 +131,8 @@ final class SshFileRef extends FileRef { /// 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) { - final dir = path.length > 1 && path.endsWith('/') - ? path.substring(0, path.length - 1) - : path; - return SshFileRef( - creds: creds, - path: dir.endsWith('/') ? '$dir$name' : '$dir/$name', - ); - } + SshFileRef child(String name) => + 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 @@ -183,16 +189,11 @@ final class MonitorFileRef extends FileRef { /// 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) { - final dir = path.length > 1 && path.endsWith('/') - ? path.substring(0, path.length - 1) - : path; - return MonitorFileRef( - spi: spi, - monitor: monitor, - path: dir.endsWith('/') ? '$dir$name' : '$dir/$name', - ); - } + MonitorFileRef child(String name) => MonitorFileRef( + spi: spi, + monitor: monitor, + path: _joinPosixChild(path, name), + ); @override bool operator ==(Object other) => diff --git a/test/unit/file/file_transfer_test.dart b/test/unit/file/file_transfer_test.dart index 9f88d06e3a..b04f82f97e 100644 --- a/test/unit/file/file_transfer_test.dart +++ b/test/unit/file/file_transfer_test.dart @@ -34,13 +34,20 @@ void main() { test('a root or a trailing slash joins without doubling', () { expect(const LocalFileRef('/').child('a').path, '/a'); - expect(const LocalFileRef(r'C:\').child('a').path, 'C:/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', () { @@ -48,17 +55,70 @@ void main() { expect(const LocalFileRef('/a/b') == const LocalFileRef('/a/c'), isFalse); }); - test('a separator written the other way is still the same place', () { - // A download's destination is built with the platform's own separator - // (`serverLocalPath`), while a browser's `child` always joins with `/`. - // On Windows the two spellings must still read as one place, or a - // finished download never refreshes the browser showing its directory. + 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( - const LocalFileRef(r'C:\a\b') == const LocalFileRef('C:/a/b'), - isTrue, + 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', ); - expect(const LocalFileRef(r'C:\a\b').hashCode, - const LocalFileRef('C:/a/b').hashCode); }); });