Skip to content

On Windows, SFTP file download fails with PathNotFoundException and RangeError #992

@GT-610

Description

@GT-610

Describe the bug

SFTP file download functionality fails on Windows due to multiple path handling issues:

  1. Invalid folder name: Uses spi.oldId (format: user@ip:port) as folder name, which contains characters illegal on Windows (@ and :)
  2. Path separator mismatch: Mixes Unix-style forward slashes (/) with Windows backslashes (\), resulting in malformed paths like C:\Users\...\root@127.0.0.1:22\//root\compose
  3. RangeError: Substring operations use hardcoded / separator while localPath uses platform-specific separators, causing lastIndexOf to return -1

This only happens on Windows, as other platforms use / as seperator, which won't cause any issue.

To Reproduce

Steps to reproduce the behavior:

  1. Run the application on Windows
  2. Navigate to the SFTP file browser for a remote server
  3. Attempt to download a file from the remote server
  4. Observe the error messages in the logs
Image

This error also exists in File page, while opening a folder.

Image

Error Messages

[package:server_box/main.dart:73] [WARNING] App: Exception: sftp worker event: PathNotFoundException: Exists failed, path = 'C:\Users\...\AppData\Roaming\ServerBox\file\root@127.0.0.1:22\\\root\compose' (OS Error: 文件名、目录名或卷标语法不正确。, errno = 123)
[package:server_box/main.dart:73] [WARNING] App: Exception: sftp worker event: RangeError (end): Invalid value: Not in inclusive range 0..95: -1

Desired Results

  • Files should download successfully on Windows
  • Folder names should use valid characters for all platforms (e.g., use spi.id instead of spi.oldId)
  • Path separators should be consistent and platform-appropriate
  • No RangeError exceptions during file operations

Actual Results

  • File download fails with PathNotFoundException
  • Invalid folder names are created on Windows
  • Path separators are mixed, creating malformed paths
  • RangeError occurs due to incorrect separator usage in substring operations

Root Cause Analysis

The issues stem from inconsistent path handling across the codebase:

  1. sftp.dart:589: Uses spi.oldId instead of spi.id for folder naming
  2. sftp.dart:589: Does not strip leading separator from normalizedPath, causing triple separators
  3. worker.dart:73: Uses hardcoded / instead of Pfs.seperator in lastIndexOf
  4. req.dart:39: Uses hardcoded / instead of Pfs.seperator in split

Proposed Fixes

Device

  • OS: Windows 11
  • Platform: Windows
  • ServerBox version: v1276

Additional context

ServerBox generates server IDs using ShortId.generate() (e.g., ptPQHje00), which are valid folder names on all platforms. However, the code currently uses spi.oldId (format: user@ip:port) for folder naming.

I'm investigating into this issue and have applied some fixes, but I'm checking if there are more problems about this.

Image Image

After that I'll create a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions