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

+141 -40
View File
@@ -1750,23 +1750,23 @@ std::string BuildToolsListResult(const Json::Value& id) {
}
}
// 16) download_file(P6:MCP 文件传输,仅 Windows,安全门:McpFileTransfer=1)
// 16) download_file(P6:MCP 文件传输,安全门:McpFileTransfer=1)
// 独立开关(不要求 McpReadonly=0):下载是只读方向(主控落盘),风险可控。
if (CMcpServer::Instance().IsFileTransferEnabled()) {
Json::Value tool(Json::objectValue);
tool["name"] = "download_file";
tool["description"] = u8"从指定在线 Windows 主机下载文件或目录到主控本机(V2 协议,逐文件 SHA-256 校验)。remote_path 为远程绝对路径(文件或目录,目录递归下载);local_dir 为本机保存目录;overwrite=false 时跳过已存在文件。返回 files[{path,size,sha256}] 与 total_bytes/skipped。";
tool["description"] = u8"从指定在线主机下载文件或目录到主控本机(V2 协议,逐文件 SHA-256 校验)。支持 Windows 与 Linux/macOS 主机;remote_path 为远程绝对路径(文件或目录,目录递归下载),Windows 用 D:\\data\\a.txt 这类盘符路径,Linux/macOS 用 /home/user/a.txt 这类绝对 POSIX 路径;local_dir 为本机保存目录;overwrite=false 时跳过已存在文件。返回 files[{path,size,sha256}] 与 total_bytes/skipped。";
tool["inputSchema"] = BuildDownloadFileInputSchema();
tool["outputSchema"] = BuildDownloadFileOutputSchema();
tools.append(tool);
}
// 17) upload_file(P2:MCP 文件上传,仅 Windows,安全门:McpFileTransfer=1 且 McpReadonly=0)
// 17) upload_file(P2:MCP 文件上传,安全门:McpFileTransfer=1 且 McpReadonly=0)
// 写远程盘,故额外要求关闭只读(与 terminal_* / remote_* 一致)。
if (CMcpServer::Instance().IsFileTransferEnabled() && !CMcpServer::Instance().IsReadonly()) {
Json::Value tool(Json::objectValue);
tool["name"] = "upload_file";
tool["description"] = u8"把主控本机文件或目录上传到指定在线 Windows 主机的目录(V2 协议,主连接发送)。local_path 为主控本机绝对路径(文件或目录,目录递归上传);remote_dir 为远程保存目录(不存在自动创建);overwrite=false 时跳过 remote_dir 下已存在的同名顶层项。返回 files[{path,size,sha256}] 与 total_bytes/skipped;sha256 恒为空(V2 无接收方回执,完整性由客户端本地校验、失败仅记日志)。";
tool["description"] = u8"把主控本机文件或目录上传到指定在线主机的目录(V2 协议,主连接发送)。支持 Windows 与 Linux/macOS 主机;local_path 为主控本机绝对路径(文件或目录,目录递归上传);remote_dir 为远程保存目录(不存在自动创建),Windows 用 C:\\uploads 这类盘符路径,Linux/macOS 用 /home/user/uploads 这类绝对 POSIX 路径;overwrite=false 时跳过 remote_dir 下已存在的同名顶层项。返回 files[{path,size,sha256}] 与 total_bytes/skipped/receiver_ack;sha256 恒为空(发送端不计算)。receiver_ack=true 表示目标端已确认写入(Linux 客户端回执),false 表示目标端没有回执(Windows 目标、老客户端或 macOS),此时需要确证请用 list_files 复核。";
tool["inputSchema"] = BuildUploadFileInputSchema();
tool["outputSchema"] = BuildUploadFileOutputSchema();
tools.append(tool);
@@ -4523,7 +4523,7 @@ Json::Value BuildDownloadFileInputSchema() {
Json::Value rp(Json::objectValue);
rp["type"] = "string";
rp["description"] = u8"远程绝对路径(文件或目录;目录递归下载),如 C:\\Users\\foo\\Pictures";
rp["description"] = u8"远程绝对路径(文件或目录;目录递归下载)。Windows 主机用 C:\\Users\\foo\\Pictures,Linux/macOS 主机用 /home/user/data 这类绝对 POSIX 路径";
props["remote_path"] = rp;
Json::Value ld(Json::objectValue);
@@ -4576,6 +4576,9 @@ Json::Value BuildDownloadFileOutputSchema() {
return schema;
}
// 词法检查路径是否含 ".." 段(定义在下方 upload_file 辅助区;download_file 先用)。
static bool HasDotDotSegment(const std::string& path);
// tools/call:download_file(下载远程文件/目录到本机,V2 协议)
std::string BuildDownloadFile(const Json::Value& id, const Json::Value& args, CMy2015RemoteDlg* parent) {
// 分派门控(独立开关 McpFileTransfer,不要求 McpReadonly=0):即便绕过 tools/list 直调也拒绝。
@@ -4591,10 +4594,10 @@ std::string BuildDownloadFile(const Json::Value& id, const Json::Value& args, CM
if (!ctx)
return BuildError(id, -32002, "Host not found or offline: " + std::to_string(devId));
// 仅 Windows 客户端实现 CMD_DOWN_FILES_V2(client/FileManager.cpp),LNX/MAC 提前拒绝。
// 目标端类型决定 remote_path 的写法:Windows 用盘符路径,LNX/MAC 用绝对 POSIX 路径。
// LNX/MAC 走 common/FileManager.h 里同一套 CMD_DOWN_FILES_V2(V2 发送器)。
CString clientType = ctx->GetAdditionalData(RES_CLIENT_TYPE);
if (clientType == "LNX" || clientType == "MAC")
return BuildError(id, -32005, "download_file is only supported on Windows hosts");
const bool posixTarget = (clientType == "LNX" || clientType == "MAC");
// 客户端须支持 V2 文件传输(否则 CMD_DOWN_FILES_V2 会被静默忽略 → 超时)。
if (!ctx->SupportsFileV2())
@@ -4630,6 +4633,17 @@ std::string BuildDownloadFile(const Json::Value& id, const Json::Value& args, CM
if (remotePathAnsi.empty())
return BuildError(id, -32602, "Invalid remote_path encoding");
// POSIX 端 WinPathToLinux 会把 "C:\x" 去盘符改写成 "/x",静默落到别处,
// 所以非 Windows 目标必须先按 POSIX 形态校验(\ 会被客户端归一化成 /,语义会漂)。
if (posixTarget) {
if (remotePathAnsi[0] != '/')
return BuildError(id, -32602, "remote_path must be an absolute POSIX path for Linux/macOS hosts, e.g. /home/user/file.txt");
if (remotePathAnsi.find('\\') != std::string::npos)
return BuildError(id, -32602, "remote_path must not contain backslashes on Linux/macOS hosts; use '/'");
if (HasDotDotSegment(remotePathAnsi))
return BuildError(id, -32602, "remote_path must not contain '..' path segments");
}
CMcpServer& mcp = CMcpServer::Instance();
if (!mcp.BeginFileTransferPending(devId, "download_file", localDirAnsi, remotePathAnsi, overwrite))
return BuildError(id, -32003, "Device busy: another request is pending for this host");
@@ -4835,7 +4849,7 @@ Json::Value BuildUploadFileInputSchema() {
Json::Value rd(Json::objectValue);
rd["type"] = "string";
rd["description"] = u8"远程主机的保存目录(不存在则自动创建),如 C:\\uploads";
rd["description"] = u8"远程主机的保存目录(不存在则自动创建)。Windows 主机如 C:\\uploads,Linux/macOS 主机用 /home/user/uploads 这类绝对 POSIX 路径";
props["remote_dir"] = rd;
Json::Value ow(Json::objectValue);
@@ -4876,6 +4890,7 @@ Json::Value BuildUploadFileOutputSchema() {
Json::Value tb(Json::objectValue); tb["type"] = "integer"; props["total_bytes"] = tb;
Json::Value sk(Json::objectValue); sk["type"] = "integer"; props["skipped"] = sk;
Json::Value ra(Json::objectValue); ra["type"] = "boolean"; props["receiver_ack"] = ra;
Json::Value schema(Json::objectValue);
schema["type"] = "object";
@@ -4900,10 +4915,11 @@ std::string BuildUploadFile(const Json::Value& id, const Json::Value& args, CMy2
if (!ctx)
return BuildError(id, -32002, "Host not found or offline: " + std::to_string(devId));
// 仅 Windows 客户端实现 V2 主连接接收链路(与 download_file 一致)。
// 目标端类型决定 remote_dir 的写法:Windows 用盘符路径,LNX/MAC 用绝对 POSIX 路径。
// POSIX 接收端(common/FileTransferV2.h)按包里的 name 派生落盘路径,不需要专门指令。
CString clientType = ctx->GetAdditionalData(RES_CLIENT_TYPE);
if (clientType == "LNX" || clientType == "MAC")
return BuildError(id, -32005, "upload_file is only supported on Windows hosts");
const bool posixTarget = (clientType == "LNX" || clientType == "MAC");
const bool dstAcks = ctx->SupportsC2CRecvAck(); // 目前仅 Linux 接收端会回执
if (!ctx->SupportsFileV2())
return BuildError(id, -32006, "Host does not support V2 file transfer");
@@ -4945,17 +4961,33 @@ std::string BuildUploadFile(const Json::Value& id, const Json::Value& args, CMy2
if (attr & FILE_ATTRIBUTE_DIRECTORY)
CollectLocalFiles(localPathAnsi, files);
// 远程保存目录:UTF-8 → 客户端 ANSI(936),去尾斜杠 + 结尾 '\'(作 targetDir 前缀)。
// 远程保存目录:UTF-8 → 客户端 ANSI(936),去尾部分隔符。
std::string remoteDirAnsi = ToAnsi(remoteDirUtf8, 936);
if (remoteDirAnsi.empty())
return BuildError(id, -32602, "Invalid remote_dir encoding");
while (!remoteDirAnsi.empty() && (remoteDirAnsi.back() == '\\' || remoteDirAnsi.back() == '/'))
remoteDirAnsi.pop_back();
if (remoteDirAnsi.empty())
return BuildError(id, -32602, "Invalid remote_dir: " + remoteDirUtf8);
if (HasDotDotSegment(remoteDirAnsi))
return BuildError(id, -32602, "remote_dir must not contain '..' path segments");
std::string targetDir = remoteDirAnsi + "\\";
if (posixTarget) {
// 只接受绝对 POSIX 路径(单独的 "/" 也是合法目标)。
while (remoteDirAnsi.size() > 1 && remoteDirAnsi.back() == '/')
remoteDirAnsi.pop_back();
if (remoteDirAnsi[0] != '/')
return BuildError(id, -32602, "remote_dir must be an absolute POSIX path for Linux/macOS hosts, e.g. /home/user/uploads");
if (remoteDirAnsi.find('\\') != std::string::npos)
return BuildError(id, -32602, "remote_dir must not contain backslashes on Linux/macOS hosts; use '/'");
if (HasDotDotSegment(remoteDirAnsi))
return BuildError(id, -32602, "remote_dir must not contain '..' path segments");
} else {
while (!remoteDirAnsi.empty() && (remoteDirAnsi.back() == '\\' || remoteDirAnsi.back() == '/'))
remoteDirAnsi.pop_back();
if (remoteDirAnsi.empty())
return BuildError(id, -32602, "Invalid remote_dir: " + remoteDirUtf8);
if (HasDotDotSegment(remoteDirAnsi))
return BuildError(id, -32602, "remote_dir must not contain '..' path segments");
}
// 拼接前缀:Windows 用 '\';POSIX 用 '/'(根目录不再重复补分隔符)。
// relPath 里的 '\' 由接收端归一化成 '/',两条路都能落对。
std::string targetDir = posixTarget
? (remoteDirAnsi == "/" ? "/" : remoteDirAnsi + "/")
: (remoteDirAnsi + "\\");
// overwrite=false:预检 remote_dir 一层,跳过已存在的同名顶层项(单文件精确、目录整体跳过)。
int skipped = 0;
@@ -4999,16 +5031,16 @@ std::string BuildUploadFile(const Json::Value& id, const Json::Value& args, CMy2
return BuildResult(id, result);
}
CMcpServer& mcp = CMcpServer::Instance();
if (!mcp.BeginFileUpload(devId))
return BuildError(id, -32003, "Device busy: another request is pending for this host");
TransferOptionsV2 opts;
opts.transferID = GenerateTransferID();
opts.transferID = GenerateTransferID(); // 先定下来:会话要用它匹配 POSIX 目标端回执
opts.srcClientID = 0; // 主控端
opts.dstClientID = devId;
opts.enableResume = false;
CMcpServer& mcp = CMcpServer::Instance();
if (!mcp.BeginFileUpload(devId, opts.transferID))
return BuildError(id, -32003, "Device busy: another request is pending for this host");
std::string hash = GetPwdHash();
std::string hmac = GetHMAC(100);
@@ -5016,16 +5048,57 @@ std::string BuildUploadFile(const Json::Value& id, const Json::Value& args, CMy2
cbData.parent = parent;
cbData.clientID = devId;
cbData.deadline = GetTickCount() + (DWORD)timeoutMs; // DWORD 回绕由回调的 (int) 差值判断吸收
// 空文件:worker 对 0 字节文件不发数据块,只发 COMMAND_FILE_COMPLETE_V2,而完成包没有路径字段,
// POSIX 接收端无从创建文件(会「回执成功但文件不存在」)。这里替 POSIX 目标端补一个长度 0 的
// 收尾块(名字拼法与 worker 一致),接收端据此建空文件;随后 worker 的完成包只会被去重。
// Windows 目标端维持原行为不动。
std::string xferRoot = GetCommonRoot(sendFiles);
if (posixTarget) {
for (size_t i = 0; i < sendFiles.size(); ++i) {
DWORD a = GetFileAttributesA(sendFiles[i].c_str());
if (a == INVALID_FILE_ATTRIBUTES || (a & FILE_ATTRIBUTE_DIRECTORY)) continue;
WIN32_FILE_ATTRIBUTE_DATA fad;
if (!GetFileAttributesExA(sendFiles[i].c_str(), GetFileExInfoStandard, &fad)) continue;
if ((((uint64_t)fad.nFileSizeHigh << 32) | fad.nFileSizeLow) != 0) continue;
std::string name = targetDir + GetRelativePath(xferRoot, sendFiles[i]);
size_t packetSize = sizeof(FileChunkPacketV2) + name.size();
std::vector<uint8_t> packet(packetSize);
auto* pkt = (FileChunkPacketV2*)packet.data();
pkt->cmd = COMMAND_SEND_FILE_V2;
pkt->transferID = opts.transferID;
pkt->srcClientID = 0;
pkt->dstClientID = devId;
pkt->fileIndex = (uint32_t)i;
pkt->totalFiles = (uint32_t)sendFiles.size();
pkt->fileSize = 0;
pkt->offset = 0;
pkt->dataLength = 0;
pkt->nameLength = (uint32_t)name.size();
pkt->flags = FFV2_LAST_CHUNK;
pkt->checksum = 0;
memset(pkt->reserved, 0, sizeof(pkt->reserved));
if (!name.empty()) memcpy(packet.data() + sizeof(FileChunkPacketV2), name.data(), name.size());
UploadSendChunkHeadless(&cbData, pkt, packet.data(), (int)packetSize);
}
}
// 同步驱动发送(走主连接);无外来流,无需 WaitFileTransferDone / 收包计数。
int result = FileBatchTransferWorkerV2(sendFiles, targetDir, &cbData,
UploadSendChunkHeadless, nullptr, hash, hmac, opts);
bool clientOnline = (FindMainContext(parent, devId) != nullptr);
mcp.ClearFileTransfer(devId);
bool ok = (result == 0) && !cbData.failed && clientOnline;
// POSIX 目标端回执:接收端写完最后一包才回(Windows 客户端从不回,dstAcks 为 false)。
// 回执可能在上面的同步发送过程中就已到达并 latch 进会话,这里的等待通常立即返回。
CMcpServer::C2CResult ack;
if (ok && dstAcks) mcp.WaitC2CReceiver(devId, 5000, ack);
mcp.ClearFileTransfer(devId);
// 计算已发送文件(远程完整路径 = targetDir + relPath)与字节数;目录项不计入 files。
std::string outRoot = GetCommonRoot(sendFiles);
std::string outRoot = xferRoot;
std::vector<FileTransferEntry> outFiles;
uint64_t totalBytes = 0;
for (const std::string& f : sendFiles) {
@@ -5034,6 +5107,9 @@ std::string BuildUploadFile(const Json::Value& id, const Json::Value& args, CMy2
std::string rel = GetRelativePath(outRoot, f);
FileTransferEntry e;
e.path = targetDir + rel;
if (posixTarget) {
for (char& c : e.path) if (c == '\\') c = '/'; // relPath 是 Windows 分隔符
}
WIN32_FILE_ATTRIBUTE_DATA fad;
if (GetFileAttributesExA(f.c_str(), GetFileExInfoStandard, &fad))
e.size = ((uint64_t)fad.nFileSizeHigh << 32) | fad.nFileSizeLow;
@@ -5043,6 +5119,25 @@ std::string BuildUploadFile(const Json::Value& id, const Json::Value& args, CMy2
outFiles.push_back(std::move(e));
}
// 目标端回了回执但失败:发送端的「全部发出」不算完成,按目标端写入失败上报。
// 与 copy_files_c2c 的 -32011 同义(那两个 -32009/-32010 是发送端路径语义)。
if (ok && ack.recvAck && ack.recvStatus != (uint32_t)FEV2_OK) {
const char* why = "write failed";
if (ack.recvStatus == (uint32_t)FEV2_ACCESS_DENIED) why = "permission denied";
else if (ack.recvStatus == (uint32_t)FEV2_FILE_NOT_FOUND) why = "no such file or directory";
else if (ack.recvStatus == (uint32_t)FEV2_DISK_FULL) why = "no space left on device";
return BuildError(id, -32011, std::string("Destination host failed to write the files: ") + why
+ " (status=" + std::to_string(ack.recvStatus)
+ ", errno=" + std::to_string(ack.recvErrno)
+ ", files=" + std::to_string(ack.recvFileCount) + ")");
}
// 声明了会回执却不回:目标端可能中途掉线,发送端的「已发出」不能当成落盘成功。
// 例外:整包没有字节(全是空文件)——接收端无从回执。
if (ok && dstAcks && !ack.recvAck && totalBytes != 0)
return BuildError(id, -32001, "Upload finished sending but the destination did not confirm "
"the write (verify with list_files on the target host)");
if (ok) {
if (parent) {
std::string audit = "host " + std::to_string(devId) + " upload_file: "
@@ -5067,13 +5162,17 @@ std::string BuildUploadFile(const Json::Value& id, const Json::Value& args, CMy2
structuredContent["files"] = filesArr;
structuredContent["total_bytes"] = (Json::UInt64)totalBytes;
structuredContent["skipped"] = skipped;
// receiver_ack=true 只可能来自 POSIX 接收端(Linux 客户端写完最后一包回执);
// Windows 目标与老客户端没有回执,false 时请用 list_files 在目标主机上复核。
structuredContent["receiver_ack"] = ack.recvAck;
result["structuredContent"] = structuredContent;
Json::Value content(Json::arrayValue);
Json::Value item(Json::objectValue);
item["type"] = "text";
item["text"] = std::string(u8"上传完成:") + std::to_string(outFiles.size()) + u8" 个文件("
+ std::to_string(totalBytes) + u8" 字节),跳过 " + std::to_string(skipped) + u8" 个。";
+ std::to_string(totalBytes) + u8" 字节),跳过 " + std::to_string(skipped) + u8" 个"
+ (ack.recvAck ? u8";目标端已确认写入。" : u8"。");
content.append(item);
result["content"] = content;
result["isError"] = false;
@@ -8065,7 +8164,7 @@ bool CMcpServer::BeginFileTransferPending(uint64_t device_id, const std::string&
return true;
}
bool CMcpServer::BeginFileUpload(uint64_t device_id) {
bool CMcpServer::BeginFileUpload(uint64_t device_id, uint64_t transfer_id) {
// 与一次性挂起请求互斥(F2):先查对方注册表再查自己的,避免跨锁嵌套死锁。
{
std::lock_guard<std::mutex> lk(m_PendingMutex);
@@ -8074,9 +8173,11 @@ bool CMcpServer::BeginFileUpload(uint64_t device_id) {
std::lock_guard<std::mutex> lk(m_FileXferMutex);
if (m_FileXferSessions.find(device_id) != m_FileXferSessions.end()) return false; // 单设备单传输
FileTransferSession s;
s.tool = "upload_file";
s.startAt = time(nullptr);
s.lastDataAt = std::chrono::steady_clock::now();
s.tool = "upload_file";
s.dstClientID = device_id; // 目标端 = 会话宿主,回执按 dstClientID + transferID 匹配
s.transferID = transfer_id;
s.startAt = time(nullptr);
s.lastDataAt = std::chrono::steady_clock::now();
m_FileXferSessions[device_id] = std::move(s);
return true;
}
@@ -8132,11 +8233,12 @@ void CMcpServer::OnC2CRecvResult(uint64_t dst_id, const BYTE* buf, ULONG len) {
if (res->cmd != TOKEN_C2C_RECV_RESULT || res->dstClientID != dst_id) return; // 身份自洽性
std::lock_guard<std::mutex> lk(m_FileXferMutex);
// 目标端在服务端没有自己的会话条目(BeginC2CTransfer 只登记源端),只能按 dstClientID + transferID 反查。
// copy_files_c2c:目标端在服务端没有会话条目(BeginC2CTransfer 只登记源端),只能反查;
// upload_file:会话键就是目标端本身。两者都用 dstClientID + transferID 定位。
for (auto& kv : m_FileXferSessions) {
FileTransferSession& s = kv.second;
if (s.tool != "copy_files_c2c" || s.dstClientID != dst_id
|| s.transferID != res->transferID) continue;
if (s.tool != "copy_files_c2c" && s.tool != "upload_file") continue;
if (s.dstClientID != dst_id || s.transferID != res->transferID) continue;
if (s.recvAck) return; // 重复回执只认第一包
s.recvAck = true;
s.recvStatus = res->status;
@@ -8271,10 +8373,9 @@ void CMcpServer::OnFileChunkV2(uint64_t device_id, context* streamSubCtx, const
}
s.directoryCount++;
RecvFileChunkV2((char*)buf, len, nullptr, nullptr, hash, hmac, 0);
// 纯目录树(空目录、只有子目录):期望文件数 = totalFiles - directoryCount = 0,
// 而客户端对目录项不发 COMPLETE 包,完成判定永远等不到 → 只能干等到超时。
// 目录项数已覆盖全部条目时直接判完成(totalFiles 含目录项,见 F4)。
if (s.totalFiles > 0 && s.directoryCount >= s.totalFiles) {
// 目录项不发 COMPLETE 包,而条目顺序由客户端决定(目录可能排在最后,如空子目录),
// 完成判定必须在每一类条目定案后都做:文件数 + 目录数覆盖 totalFiles 即完成(F4)。
if (s.totalFiles > 0 && s.filesDone + s.directoryCount >= s.totalFiles) {
s.done = true;
m_FileXferCv.notify_one();
}
+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)。