The issue #348 in the magic-wormhole repository discusses a problem where the program does not cleanly handle filenames containing invalid characters for the target filesystem during file transfers. This can cause the wormhole receive command to fail when the sender's filename cannot be created on the receiver's filesystem. The issue specifically highlights scenarios where the receiver's operating system (e.g., Windows) enforces stricter rules on filenames than the sender's system (e.g., Linux).
Key Points of the Issue:
Filename Compatibility Across Filesystems:
Some filesystems impose restrictions on valid characters in filenames (e.g., Windows disallows <>:"/\|?* and reserved names like CON, NUL, etc.).
If a sender transmits a file with unsupported characters, the receiver will encounter an error when trying to create the file on the target filesystem.
Lack of Automatic Handling:
The current implementation does not sanitize or rename files with incompatible filenames.
The transfer fails instead of gracefully handling the issue, leaving the user to address it manually.
Potential Solutions Mentioned:
A proposed solution involves renaming invalid filenames on the receiver's side to valid ones, either automatically or with user confirmation.
This could involve stripping or replacing invalid characters or appending a suffix if the filename collides with an existing file.
Concerns Raised:
Automatic renaming may lead to confusion if the receiver cannot easily identify the original file.
A more user-friendly approach might involve prompting the user with options to rename the file or abort the transfer.
Insights from the Repository Context:
Current Handling of Files and Directories:
The cmd_receive.py file handles the process of receiving files and directories. It determines the destination filename using the _decide_destname() function, which checks for existing files and ensures the basename of the filename is used.
However, there is no built-in mechanism to handle invalid filenames for the target filesystem.
Error Messaging:
If a transfer fails, the user is informed of the issue, but there is no attempt to automatically fix or provide alternatives for invalid filenames.
User Control:
The CLI provides options like --output-file to override filenames during the transfer, but this requires the user to know about the issue beforehand.
Possible Resolutions:
Automatic Filename Sanitization:
Modify the _decide_destname() function in cmd_receive.py to sanitize filenames by replacing invalid characters with an underscore (_) or other safe characters.
If an invalid filename is detected, prompt the user for a new filename instead of aborting the transfer. This keeps the user in control while ensuring compatibility.
Fallback to Temporary Filenames:
Use a temporary filename (e.g., file_1.tmp) when a conflict or invalid filename is detected. Inform the user of the change in the transfer summary.
Filesystem-Specific Handling:
Add logic to detect the target filesystem (e.g., using os.name or platform.system()) and apply relevant rules for filename validation.
Recommendations:
Short-Term Fix: Implement filename sanitization to replace invalid characters automatically, as it's the simplest and least intrusive solution.
Long-Term Fix: Enhance user experience by adding prompts for renaming invalid filenames during transfer, possibly as an optional feature (--prompt-rename).
These changes would improve the robustness of magic-wormhole for cross-platform file transfers, especially between systems with differing filesystem rules.