magic-wormhole / magic-wormhole.rs

Rust implementation of Magic Wormhole, with new features and enhancements
European Union Public License 1.2
722 stars 78 forks source link

Check files before receiving #105

Closed piegamesde closed 3 years ago

piegamesde commented 3 years ago

We don't want to override random existing files on the receiver's host system, so we should check against that. The easiest way to do this is to simply check and bail out.

https://github.com/magic-wormhole/magic-wormhole.rs/blob/2b6605631d574f8556944d19837a25908e42eb60/src/transfer.rs#L329-L333

However, I'd prefer the CLI to ask me whether I want to override the file or not. Erroring out means that the transfer fails and the sender will have to re-send it and to retransmit a new code, which is annoying.

piegamesde commented 3 years ago

Discussion on the python side: https://github.com/magic-wormhole/magic-wormhole/issues/370

brightly-salty commented 3 years ago

I'd be willing to implement this. To check to see if I understand it correctly: We want to check if the file already exists. If it does, we query the user whether they would like to write over the existing file or not. If they do, we write over the existing file. If they don't, we get a new filename from stdin and start the check loop over again?

kamva9697 commented 3 years ago

@brightly-salty I'm sure that's is what @piegamesde is looking for. I think a safer way to go about overwriting a file is to first check if the file sizes are the same and if they are, they must be different files even if they are named similarly (and the latter would not be necessary) but if the files sizes match it would be thorough check for and compare the values of the first and the last 4 kb hashes of the files. And if the all those checks match out, warn the user about overwriting dissimilar files.

brightly-salty commented 3 years ago

@kamva9697 Okay, how would I check the first and last 4 kb hashes of the files? And I warn if they're different or if they're the same?

piegamesde commented 3 years ago

Okay, my description was a bit under-specified. I'm open to different solutions, but I had the following in mind:

  1. Check if the file path already exists
  2. If the file exists, ask the user if they want to overwrite it.
  3. Overwrite the file or abort, depending on the answer

with the following potential variations in mind:

I never thought about comparing file contents though. Also note that in the protocol the sequence is:

  1. Negotiate file transfer
  2. User ack
  3. Transfer the file

Therefore, we can't know the file's content at the point in time where we ask the user.

pickfire commented 3 years ago

I think we can just check the full file hash right? While receiving we can do a checksum while writing, we could even reuse the buffer like the optimization in cargo.