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:
1 parent
4146c73657
commit
3a158a241c
7 files changed
+451
-50
No files matched your search
+62
-3
@@ -26,6 +26,8 @@
|
||||
#include <iconv.h>
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
#include <cerrno>
|
||||
#include "sha256_posix.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
@@ -457,6 +459,32 @@ public:
|
||||
return p;
|
||||
}
|
||||
|
||||
// errno -> Win32 错误码:服务端 TOKEN_DOWNLOAD_ERROR 按 Win32 码分类
|
||||
// (McpServer::OnDownloadError: 2/3 -> 路径不存在,5 -> 拒绝访问,其余 -> 一般失败)。
|
||||
static uint32_t PosixErrnoToWin32(int e)
|
||||
{
|
||||
switch (e) {
|
||||
case ENOENT: return 2; // ERROR_FILE_NOT_FOUND
|
||||
case ENOTDIR: return 3; // ERROR_PATH_NOT_FOUND
|
||||
case EACCES:
|
||||
case EPERM: return 5; // ERROR_ACCESS_DENIED
|
||||
default: return 2;
|
||||
}
|
||||
}
|
||||
|
||||
// 下载请求无可用文件的结构化回报:[TOKEN_DOWNLOAD_ERROR:1][win32err:4 LE]。
|
||||
// 走文件管理器子连接(服务端此时还在等第一批 chunk),让它立即失败而不是等停滞看门狗。
|
||||
// 与 Windows 客户端 CFileManager::SendDownloadError 等价。
|
||||
static void SendDownloadError(IOCPClient* client, uint32_t win32err)
|
||||
{
|
||||
if (client == nullptr) return;
|
||||
if (win32err == 0) win32err = 2; // ERROR_FILE_NOT_FOUND
|
||||
uint8_t pkt[1 + sizeof(uint32_t)] = { 0 };
|
||||
pkt[0] = (uint8_t)TOKEN_DOWNLOAD_ERROR;
|
||||
memcpy(pkt + 1, &win32err, sizeof(uint32_t));
|
||||
client->Send2Server((char*)pkt, (ULONG)sizeof(pkt));
|
||||
}
|
||||
|
||||
// 处理 CMD_DOWN_FILES_V2 请求 - 上传文件到服务端
|
||||
// 格式: [cmd:1][targetDir\0][file1\0][file2\0]...[\0]
|
||||
static void HandleDownFilesV2(const uint8_t* data, size_t len, IOCPClient* client, uint64_t myClientID)
|
||||
@@ -480,19 +508,22 @@ public:
|
||||
|
||||
if (remotePaths.empty()) {
|
||||
Mprintf("[V2 Send] No files to send\n");
|
||||
SendDownloadError(client, 87); // ERROR_INVALID_PARAMETER(与 Windows 客户端一致)
|
||||
return;
|
||||
}
|
||||
|
||||
// 收集所有文件 (展开目录)
|
||||
std::vector<std::string> allFiles;
|
||||
std::vector<std::string> rootCandidates;
|
||||
int firstErrno = 0; // 首个失败的 errno(全部不存在时回报给服务端)
|
||||
|
||||
for (const auto& remotePath : remotePaths) {
|
||||
std::string localPath = WinPathToLinux(remotePath);
|
||||
|
||||
struct stat st;
|
||||
if (stat(localPath.c_str(), &st) != 0) {
|
||||
Mprintf("[V2 Send] File not found: %s\n", localPath.c_str());
|
||||
if (firstErrno == 0) firstErrno = errno ? errno : ENOENT;
|
||||
Mprintf("[V2 Send] File not found: %s (errno=%d)\n", localPath.c_str(), errno);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -513,7 +544,8 @@ public:
|
||||
}
|
||||
|
||||
if (allFiles.empty()) {
|
||||
Mprintf("[V2 Send] No files found\n");
|
||||
Mprintf("[V2 Send] No files found (errno=%d)\n", firstErrno);
|
||||
SendDownloadError(client, PosixErrnoToWin32(firstErrno));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -628,6 +660,9 @@ public:
|
||||
uint64_t offset = 0;
|
||||
std::vector<uint8_t> buffer;
|
||||
buffer.reserve(sizeof(FileChunkPacketV2) + targetNameGbk.length() + CHUNK_SIZE);
|
||||
Sha256 hasher; // 流式 SHA-256:服务端落盘后会用它校验
|
||||
bool truncated = false; // 读失败/文件变短 → 不发完成包,避免谎报校验通过
|
||||
bool sendOk = true;
|
||||
|
||||
while (offset < fileSize || (offset == 0 && fileSize == 0)) {
|
||||
size_t toRead = std::min(CHUNK_SIZE, (size_t)(fileSize - offset));
|
||||
@@ -660,21 +695,26 @@ public:
|
||||
ssize_t n = read(fd, dataPtr, toRead);
|
||||
if (n < 0) {
|
||||
Mprintf("[V2 Send] read failed: errno=%d\n", errno);
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
hasher.update(dataPtr, (size_t)n);
|
||||
if ((size_t)n < toRead) {
|
||||
// 文件读取不完整,调整包大小
|
||||
// 文件读取不完整(传输中被截断):调整包大小后作为末块发出,
|
||||
// 但不发完成包 —— 服务端按原 fileSize 收不全,必须让它失败而不是校验通过。
|
||||
toRead = n;
|
||||
pkt->dataLength = toRead;
|
||||
buffer.resize(sizeof(FileChunkPacketV2) + targetNameGbk.length() + toRead);
|
||||
isLast = true;
|
||||
pkt->flags |= FFV2_LAST_CHUNK;
|
||||
truncated = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 发送
|
||||
if (!client->Send2Server((char*)buffer.data(), (ULONG)buffer.size())) {
|
||||
Mprintf("[V2 Send] Send failed\n");
|
||||
sendOk = false;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -682,12 +722,31 @@ public:
|
||||
|
||||
// 空文件处理
|
||||
if (fileSize == 0) break;
|
||||
if (truncated) break; // 已按末块发出,不再续读
|
||||
|
||||
// 发送间隔 (避免发送过快)
|
||||
usleep(10000); // 10ms
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
// 完成校验包:服务端凭它判定该文件结束并比对 SHA-256(目录项不发)。
|
||||
// 与 Windows 客户端 FileBatchTransferWorkerV2 的发送点一致;没有它 MCP
|
||||
// download_file 的会话永远等不到 filesDone 达标。
|
||||
if (sendOk && !truncated) {
|
||||
FileCompletePacketV2 cpkt;
|
||||
memset(&cpkt, 0, sizeof(cpkt));
|
||||
cpkt.cmd = COMMAND_FILE_COMPLETE_V2;
|
||||
cpkt.transferID = transferID;
|
||||
cpkt.srcClientID = myClientID;
|
||||
cpkt.dstClientID = 0; // 目标是主控端
|
||||
cpkt.fileIndex = fileIndex;
|
||||
cpkt.fileSize = fileSize;
|
||||
hasher.final(cpkt.sha256);
|
||||
if (!client->Send2Server((char*)&cpkt, (ULONG)sizeof(cpkt))) {
|
||||
Mprintf("[V2 Send] Send complete packet failed\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Mprintf("[V2 Send] Transfer complete: transferID=%llu\n", transferID);
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* sha256_posix.h - 最小实现 SHA-256(POSIX 侧)
|
||||
*
|
||||
* 为什么不用现成的:Windows 侧的 SHA-256 走 BCrypt(common/VerifyV2.h),
|
||||
* Linux/macOS 没有 BCrypt,也没有 OpenSSL 依赖,而 V2 文件发送要向服务端
|
||||
* 回报 FileCompletePacketV2.sha256,必须自己算。
|
||||
*
|
||||
* 只实现 FileTransferV2 需要的流式接口(update/final),不做别的事。
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#error "sha256_posix.h is POSIX-only; Windows uses BCrypt via VerifyV2.h."
|
||||
#endif
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
class Sha256
|
||||
{
|
||||
public:
|
||||
Sha256() { reset(); }
|
||||
|
||||
void reset()
|
||||
{
|
||||
static const uint32_t init[8] = {
|
||||
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
||||
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
||||
};
|
||||
memcpy(m_h, init, sizeof(m_h));
|
||||
m_bufLen = 0;
|
||||
m_totalLen = 0;
|
||||
}
|
||||
|
||||
void update(const void* data, size_t len)
|
||||
{
|
||||
const uint8_t* p = (const uint8_t*)data;
|
||||
m_totalLen += len;
|
||||
|
||||
if (m_bufLen > 0) {
|
||||
size_t take = 64 - m_bufLen;
|
||||
if (take > len) take = len;
|
||||
memcpy(m_buf + m_bufLen, p, take);
|
||||
m_bufLen += take;
|
||||
p += take;
|
||||
len -= take;
|
||||
if (m_bufLen == 64) {
|
||||
transform(m_buf);
|
||||
m_bufLen = 0;
|
||||
}
|
||||
}
|
||||
while (len >= 64) {
|
||||
transform(p);
|
||||
p += 64;
|
||||
len -= 64;
|
||||
}
|
||||
if (len > 0) {
|
||||
memcpy(m_buf, p, len);
|
||||
m_bufLen = len;
|
||||
}
|
||||
}
|
||||
|
||||
// 输出 32 字节摘要;调用后对象可 reset() 复用(不自动 reset,避免隐式行为)
|
||||
void final(uint8_t out[32])
|
||||
{
|
||||
uint64_t bitLen = m_totalLen * 8;
|
||||
|
||||
// 0x80 + 零填充 + 64 位长度,凑满整块
|
||||
uint8_t pad[72];
|
||||
memset(pad, 0, sizeof(pad));
|
||||
pad[0] = 0x80;
|
||||
size_t padLen = (m_bufLen < 56) ? (56 - m_bufLen) : (120 - m_bufLen);
|
||||
update(pad, padLen);
|
||||
|
||||
uint8_t lenBytes[8];
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
lenBytes[i] = (uint8_t)(bitLen >> (56 - 8 * i));
|
||||
}
|
||||
update(lenBytes, sizeof(lenBytes));
|
||||
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
out[4 * i] = (uint8_t)(m_h[i] >> 24);
|
||||
out[4 * i + 1] = (uint8_t)(m_h[i] >> 16);
|
||||
out[4 * i + 2] = (uint8_t)(m_h[i] >> 8);
|
||||
out[4 * i + 3] = (uint8_t)(m_h[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void hash(const void* data, size_t len, uint8_t out[32])
|
||||
{
|
||||
Sha256 s;
|
||||
s.update(data, len);
|
||||
s.final(out);
|
||||
}
|
||||
|
||||
private:
|
||||
static uint32_t rotr(uint32_t x, int n) { return (x >> n) | (x << (32 - n)); }
|
||||
|
||||
void transform(const uint8_t block[64])
|
||||
{
|
||||
static const uint32_t K[64] = {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
||||
};
|
||||
|
||||
uint32_t w[64];
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
w[i] = ((uint32_t)block[4 * i] << 24) | ((uint32_t)block[4 * i + 1] << 16) |
|
||||
((uint32_t)block[4 * i + 2] << 8) | (uint32_t)block[4 * i + 3];
|
||||
}
|
||||
for (int i = 16; i < 64; ++i) {
|
||||
uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
||||
uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
||||
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
||||
}
|
||||
|
||||
uint32_t a = m_h[0], b = m_h[1], c = m_h[2], d = m_h[3];
|
||||
uint32_t e = m_h[4], f = m_h[5], g = m_h[6], h = m_h[7];
|
||||
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
uint32_t S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
||||
uint32_t ch = (e & f) ^ (~e & g);
|
||||
uint32_t t1 = h + S1 + ch + K[i] + w[i];
|
||||
uint32_t S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
||||
uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
|
||||
uint32_t t2 = S0 + maj;
|
||||
|
||||
h = g; g = f; f = e; e = d + t1;
|
||||
d = c; c = b; b = a; a = t1 + t2;
|
||||
}
|
||||
|
||||
m_h[0] += a; m_h[1] += b; m_h[2] += c; m_h[3] += d;
|
||||
m_h[4] += e; m_h[5] += f; m_h[6] += g; m_h[7] += h;
|
||||
}
|
||||
|
||||
uint32_t m_h[8];
|
||||
uint8_t m_buf[64];
|
||||
size_t m_bufLen;
|
||||
uint64_t m_totalLen;
|
||||
};
|
||||
Reference in new issue
Block a user