Feature: Support upload_file and download_file on Linux and macOS hosts

Both tools used to reject LNX/MAC targets outright. The server now gates
on the target client type instead: a POSIX target must pass an absolute
POSIX remote path without a backslash or a ".." segment (-32602
otherwise), while Windows targets keep their exact previous behaviour.

upload_file needed no new client command; the POSIX receiver already
handles COMMAND_SEND_FILE_V2 and derives the destination from the packet
name. It now also reports a real receiver_ack: the upload session
records dstClientID/transferID, and OnC2CRecvResult matches the 176
acknowledgement from Linux targets, so a destination write failure
becomes -32011 and a missing ack (with bytes in flight) becomes -32001.

download_file needed two pieces on the POSIX side, both shared by Linux
and macOS through common/FileTransferV2.h: a FileCompletePacketV2 per
file with a streaming SHA-256 (new self-contained common/sha256_posix.h,
since BCrypt is Windows-only) so the server can count completed files
and verify digests, and a TOKEN_DOWNLOAD_ERROR reply when the requested
paths do not exist or are denied, which the server maps to
-32009/-32010 within milliseconds instead of waiting for the stall
watchdog.

Zero-byte files were lost when uploading to POSIX targets because the
worker sends no data chunk for them and the complete packet carries no
path, so the receiver had nothing to create. The server now injects a
zero-length final chunk for each empty file, only for POSIX targets;
Windows targets keep their existing behaviour.

Also fixes a download completion edge case found while testing: the
client decides entry order, so a directory may arrive after all files
(an empty subdirectory, for example), and the directory branch only
completed when the directory count alone covered totalFiles. The check
is now filesDone + directoryCount >= totalFiles.

Verified end to end against a WSL Linux client and a Windows client:
upload and download of a tree with a 3 MB binary, a Chinese filename, an
empty file and an empty subdirectory all round-trip with matching
SHA-256; the documented error paths (-32602, -32009, -32011, -32001) and
the Windows upload/download/C2C regressions were exercised as well.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-27 20:39:22 +01:00
1 parent 4146c73657
commit 3a158a241c
7 files changed
+451 -50

No files matched your search

+5 -4
View File
@@ -284,9 +284,10 @@ public:
bool overwrite);
// 登记上传会话(false = 该 host 已有文件会话或一次性挂起请求)。upload 走主连接、
// 工具线程同步驱动 FileBatchTransferWorkerV2,会话仅为单设备单传输互斥标记
// 工具线程同步驱动 FileBatchTransferWorkerV2。会话同时用于单设备单传输互斥与
// POSIX 目标端回执匹配(登记 dstClientID/transferID,见 OnC2CRecvResult)。
// (无 fmSubCtx/streamSubCtx,ClearFileTransfer 对空指针安全)。
bool BeginFileUpload(uint64_t device_id);
bool BeginFileUpload(uint64_t device_id, uint64_t transfer_id);
// ===== C2C 文件传输(copy_files_c2c:源客户端 → 目标客户端,服务端只做中继)=====
// 会话键控在「源」设备上:工具线程只等源客户端回传的 TOKEN_C2C_SEND_RESULT,
@@ -314,8 +315,8 @@ public:
void OnC2CResult(uint64_t src_id, const BYTE* buf, ULONG len);
// TOKEN_C2C_RECV_RESULT:目标客户端(目前只有 Linux)回传落盘回执。
// 目标端在服务端没有自己的会话条目(会话键在源端),所以按 dstClientID + transferID 反查;
// 回执可能早于源端结果到达,两条 latch 互相独立,这里绝不置 done。
// 会话匹配按 dstClientID + transferID:copy_files_c2c 的会话键在源端、只能反查;
// upload_file 的会话键就是目标端本身。回执可能早于源端结果到达,绝不置 done。
void OnC2CRecvResult(uint64_t dst_id, const BYTE* buf, ULONG len);
// 工具线程:等源客户端回传结果;true=全部发出(error=0)。调用方随后 ClearFileTransfer(src)。