Skip to content

fix(torrent): save names Windows can't take under ones it can - #271

Merged
anmolkapil merged 1 commit into
anmolkapil:mainfrom
Arpit-Khandelwal:fix/torrent-windows-names
Nov 18, 2026
Merged

anmolkapil merged 1 commit into
anmolkapil:mainfrom
Arpit-Khandelwal:fix/torrent-windows-names

Conversation

@Arpit-Khandelwal

Copy link
Copy Markdown
Collaborator

Fixes #202

What was wrong

When a torrent is added, Plexo checks every path in it before writing anything (safeTorrentPaths in src/main/download/torrent/paths.ts). On Windows it refused the whole torrent if any part of a path was a name Windows can't save. That's where "Windows can't save a file named …" comes from.

In my first reply on #202 I said this was about names ending in a space or a dot. That was only part of it. Here's what happened on Windows for each kind of name:

Name in the torrent Before this PR (Windows)
A folder with <>:"|?* or a control character, including the torrent's own name (its top folder) refused
A file with <>:"|?* worked: webtorrent's storage drops those characters, on every system
A folder or file ending in . or a space refused
A device name (CON, NUL, COM1, LPT², CON.txt…) refused
A name over 255 bytes refused, on every system
A full path over 260 characters not checked, and it works (see Tests)

In #202 the error names [Trix] Blue Box S01 v3 (WEB 1080p AV1 Opus) [Multi Subs]. That's the torrent's own name, which differs from the magnet's dn (… [Multi Subs] | Ao no Hako (VOSTFR) (Batch)). Either a | after [Multi Subs] or a trailing space would produce that error. Both are covered by the tests below.

What it does now

Names that can be mapped are saved under one that works, the way qBittorrent and libtorrent handle it. Only Windows changes, apart from over-long names, which no system can save:

  • Forbidden characters in a folder name become _, so Blue Box | Ao no Hako is saved as Blue Box _ Ao no Hako. In a file name they're still dropped, as webtorrent has always done. I kept that so downloads already in progress on Windows resume against the same files.
  • Trailing dots and spaces are removed: Season 1. becomes Season 1. A name made only of dots and spaces becomes _.
  • Device names get a _ in front: CON.txt becomes _CON.txt and NUL becomes _NUL. HTTP downloads (claimName) and NZB files already do this, so I matched them instead of adding a suffix.
  • Names over 255 bytes are shortened, on every system. A file keeps its extension, and a multi-byte character is never cut in half.
  • Two names that end up the same are told apart as Name (2). This covers names in one folder, or in folders that differ only by case or by how an é is written, since those are one folder on Windows and macOS. The name that didn't need changing keeps its name: x./a and x/b are saved as x (2)/a and x/b. Each folder is renamed once, so every file in it ends up in the same place. The saved paths are then checked again for collisions.

A torrent is still refused when:

The mapping depends only on the torrent's file list, so the same torrent always gets the same paths. That matters because the paths are worked out again on restore and on relaunch.

How

  • safeTorrentPaths returns the paths the files will be saved at, instead of throwing. Everything that already reads the probe's files follows them automatically: the file list in the window, removing the files you didn't choose, and cancel and remove.
  • The store (storeNamed) used to split webtorrent's own file.path again. It now receives the checked paths in the torrent's order. Downloading, the hash check on resume and seeding all read and write through that store, so they all use the same files.

Tests

  • e2e/torrent.spec.ts, "names a system can't save": a torrent seeded from memory, so the test's seeder never has to write those names to disk.
    • The torrent's top folder is [Trix] Blue Box | Ao no Hako (a | and a trailing space). Inside: Season 1. /01 | Episode.mkv, Extras|Bonus/notes., CON.txt, report.txt (trailing space), NUL, and x./a.bin next to x/b.bin. The saved names expected on each platform are written out by hand.
    • Download, relaunch, resume: downloads partway, relaunches, resumes and completes. The tree on disk must match exactly. The bytes fetched after the resume must be no more than what was missing plus a few pieces; if the hash check on resume read the wrong files, this would fail.
    • Choosing files: only some files are chosen. The unchosen ones, partly written through shared pieces, must be removed under their saved names.
    • Cancelling partway: nothing may be left behind.
  • e2e/torrentPaths.spec.ts:
    • The property test now also checks that on Windows every saved name is one Windows accepts.
    • New cases cover the Getting file name error after adding magnet url/torrent file #202 name, folder and file characters, trailing dots and spaces, device names, renamed names meeting other names (also across folders that differ by case or Unicode form), macOS and Linux unchanged, and long names.

Results:

  • Windows CI on my fork, main plus only the new tests: 6 failed, as expected
    • "names a system can't save" fails as soon as the torrent is added, with Windows can't save a file named [Trix] Blue Box | Ao no Hako .
    • The new torrentPaths cases fail too.
  • Windows CI on my fork, this branch: all the torrent specs pass (torrentPaths, torrent, torrentProbe, torrentNative, torrentTrackers, torrentPeers, torrentFiles, removal); only the two-network tests skip, as they do on CI.
  • Full paths over 260 characters on Windows: a torrent with a 380-character path downloaded and verified on Windows CI. Nothing needs to check the full length.
  • macOS, locally: the same specs plus downloadActions all pass. Without the folder-case fix, the case/Unicode cases in torrentPaths fail.
  • Checks: typecheck:node, typecheck:web, typecheck:e2e, lint, format:check and electron-vite build all pass.

For you to decide

  • The file list shows saved names. On Windows the window now lists each file under the name it will be saved as (_CON.txt), not the torrent's own name. That seemed more honest than showing a name that won't exist on disk.

A torrent named with a "|" (or a folder ending in a dot or a space, or a
device name like CON) was refused on Windows as soon as it was added.
Such names are now saved the way other clients save them: a folder's
forbidden characters become "_", trailing dots and spaces go, and a
device name gets a "_" in front. A name too long for any file system is
shortened, its extension kept. A renamed name that meets another in its
folder is told apart as "Name (2)".

The storage is handed these paths, so the files are where the checks,
the file list and removal expect them, and the same torrent always maps
to the same names: a resumed download finds its pieces again.

Fixes anmolkapil#202

@anmolkapil anmolkapil left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Arpit, reviewed and tested locally. Agree on showing the saved names in the file list. Merging.

@anmolkapil
anmolkapil merged commit 0e5d922 into anmolkapil:main Nov 18, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Getting file name error after adding magnet url/torrent file

2 participants