Feature: Make Linux a copy_files_c2c destination
The C2C file transfer tool required both ends to be Windows clients. A Linux client can already land the files: the Windows source worker builds every entry name by plain string concatenation of dst_dir and the relative path, so a POSIX dst_dir such as /tmp/recv arrives as "/tmp/recv\sub\file", which the POSIX receiver normalizes to "/" and writes below an absolute path. What was missing was the server gate and any way to tell whether the writes succeeded. The server now accepts LNX/MAC destinations instead of rejecting them, and validates dst_dir by destination type: an absolute Windows path for Windows targets, an absolute POSIX path without backslashes for POSIX targets. The paths and dst_dir arguments also reject embedded NULs, and a POSIX dst_dir is converted to CP936 exactly so unencodable characters fail loudly instead of silently becoming '?'. To make receiver_ack meaningful, a Linux client that receives a C2C transfer now reports what it wrote. It tracks the outcome per transferID, counting a directory entry, the last chunk of a file and the completion packet of an empty file as the three ways an item is settled, with done sets so that source retries and the trailing completion packet never double count. When every item is settled it sends one C2CSendResultPacket back on its main connection under the new TOKEN_C2C_RECV_RESULT (176), with the first errno in the win32err field and the mapped FEV2_* status. A dedicated token is used instead of reusing 175 because a host can be the destination of one transfer while being the source of another, and a destination result must never latch the source side's done flag. The server waits up to five seconds for that acknowledgement after the source reports success, and only for destinations advertising the new CLIENT_CAP_C2C_RECV_ACK capability bit (Linux only, macOS reports nothing). A failed acknowledgement becomes -32011 with the status, errno and received file count; a missing acknowledgement stays an error only for transfers that actually moved bytes, since a transfer of empty files alone gives the receiver nothing to learn totalFiles from. Windows destinations are unchanged and skip the wait entirely. Verified on the debug instance: Windows to Linux (WSL Ubuntu 24.04) with subdirectories, an empty directory, a Chinese file name and a 3 MB binary lands with matching SHA-256 and receiver_ack true; a Windows-style dst_dir returns -32602; an unwritable POSIX destination returns -32011; the existing Windows to Windows cases still pass with receiver_ack false and the same latency. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
4c0e86dcc5
commit
4146c73657
11 files changed
+384
-48
No files matched your search
+154
-12
@@ -25,9 +25,11 @@
|
||||
#include <dirent.h>
|
||||
#include <iconv.h>
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <mutex>
|
||||
#include <fstream>
|
||||
#include <thread>
|
||||
@@ -164,11 +166,15 @@ public:
|
||||
|
||||
// 处理 V2 文件数据包
|
||||
// 返回: 0=成功, >0=错误码
|
||||
static int RecvFileChunkV2(const uint8_t* buf, size_t len, uint64_t myClientID)
|
||||
// reply:主连接(Linux 传 g_pMainClient)。非空时,C2C 传输的最后一个条目处理完会回一包
|
||||
// 落盘结果(TOKEN_C2C_SEND_RESULT),服务端据此把 receiver_ack 变成真实值;
|
||||
// 传 nullptr(macOS 目前没有全局主连接)则保持旧行为,不发回执。
|
||||
static int RecvFileChunkV2(const uint8_t* buf, size_t len, uint64_t myClientID,
|
||||
IOCPClient* reply = nullptr)
|
||||
{
|
||||
// 检查是否是 FILE_COMPLETE_V2 包
|
||||
if (len >= 1 && buf[0] == COMMAND_FILE_COMPLETE_V2) {
|
||||
return HandleFileComplete(buf, len, myClientID);
|
||||
return HandleFileComplete(buf, len, myClientID, reply);
|
||||
}
|
||||
|
||||
if (len < sizeof(FileChunkPacketV2)) {
|
||||
@@ -210,10 +216,10 @@ public:
|
||||
for (char& c : relPath) {
|
||||
if (c == '\\') c = '/';
|
||||
}
|
||||
// 移除盘符前缀 (如 C:/)
|
||||
if (relPath.size() >= 3 && relPath[1] == ':' && relPath[2] == '/') {
|
||||
// 移除盘符前缀 (如 C:/)——只认「字母 + 冒号」,否则 /:data 这类合法路径会被改写
|
||||
if (relPath.size() >= 3 && isalpha((unsigned char)relPath[0]) && relPath[1] == ':' && relPath[2] == '/') {
|
||||
relPath = relPath.substr(3);
|
||||
} else if (relPath.size() >= 2 && relPath[1] == ':') {
|
||||
} else if (relPath.size() >= 2 && isalpha((unsigned char)relPath[0]) && relPath[1] == ':') {
|
||||
relPath = relPath.substr(2);
|
||||
}
|
||||
// 移除开头的 /
|
||||
@@ -228,8 +234,8 @@ public:
|
||||
for (char& c : savePath) {
|
||||
if (c == '\\') c = '/';
|
||||
}
|
||||
// 移除盘符前缀 (如 C:/)
|
||||
if (savePath.size() >= 3 && savePath[1] == ':' && savePath[2] == '/') {
|
||||
// 移除盘符前缀 (如 C:/)——只认「字母 + 冒号」,否则 /:data 这类合法路径会被改写
|
||||
if (savePath.size() >= 3 && isalpha((unsigned char)savePath[0]) && savePath[1] == ':' && savePath[2] == '/') {
|
||||
savePath = savePath.substr(2);
|
||||
}
|
||||
// 确保以 / 开头
|
||||
@@ -241,7 +247,14 @@ public:
|
||||
// 目录处理
|
||||
if (pkt->flags & FFV2_DIRECTORY) {
|
||||
MakeDirs(savePath + "/dummy");
|
||||
// MakeDirs 不回报 mkdir 的失败(它还要给服务端下载复用),这里单独确认一次:
|
||||
// 目录没建成也要让回执带上 errno,否则会向服务端谎报成功。
|
||||
struct stat st;
|
||||
int mkErr = 0;
|
||||
if (stat(savePath.c_str(), &st) != 0) mkErr = errno ? errno : EIO;
|
||||
else if (!S_ISDIR(st.st_mode)) mkErr = EIO;
|
||||
Mprintf("[V2 Recv] Created directory: %s\n", savePath.c_str());
|
||||
FinishC2CItem(pkt, reply, myClientID, false, true, 0, mkErr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -250,9 +263,11 @@ public:
|
||||
|
||||
// 获取或创建接收状态
|
||||
uint64_t stateKey = pkt->transferID ^ ((uint64_t)pkt->fileIndex << 48);
|
||||
RecvState* state = GetOrCreateState(stateKey, pkt, savePath);
|
||||
int openErr = 0;
|
||||
RecvState* state = GetOrCreateState(stateKey, pkt, savePath, &openErr);
|
||||
if (!state) {
|
||||
Mprintf("[V2 Recv] Failed to create state\n");
|
||||
FinishC2CItem(pkt, reply, myClientID, true, IsFinalChunk(pkt), 0, openErr ? openErr : EIO);
|
||||
return 5;
|
||||
}
|
||||
|
||||
@@ -260,7 +275,9 @@ public:
|
||||
const char* data = (const char*)buf + sizeof(FileChunkPacketV2) + pkt->nameLength;
|
||||
|
||||
if (lseek(state->fd, pkt->offset, SEEK_SET) == -1) {
|
||||
Mprintf("[V2 Recv] lseek failed: errno=%d\n", errno);
|
||||
int e = errno;
|
||||
Mprintf("[V2 Recv] lseek failed: errno=%d\n", e);
|
||||
FinishC2CItem(pkt, reply, myClientID, true, IsFinalChunk(pkt), 0, e);
|
||||
return 6;
|
||||
}
|
||||
|
||||
@@ -270,7 +287,9 @@ public:
|
||||
ssize_t n = write(state->fd, data + written, toWrite - written);
|
||||
if (n <= 0) {
|
||||
if (errno == EINTR) continue;
|
||||
Mprintf("[V2 Recv] write failed: errno=%d\n", errno);
|
||||
int e = errno;
|
||||
Mprintf("[V2 Recv] write failed: errno=%d\n", e);
|
||||
FinishC2CItem(pkt, reply, myClientID, true, IsFinalChunk(pkt), 0, e);
|
||||
return 7;
|
||||
}
|
||||
written += n;
|
||||
@@ -292,6 +311,15 @@ public:
|
||||
|
||||
// 文件完成
|
||||
if (isLast) {
|
||||
// 落盘长度自检:写到一半失败过、或偏移被改写,都会在这里落成「长度不符」
|
||||
struct stat st;
|
||||
int szErr = 0;
|
||||
if (fstat(state->fd, &st) != 0) szErr = errno ? errno : EIO;
|
||||
else if ((uint64_t)st.st_size != pkt->fileSize) szErr = EIO;
|
||||
|
||||
// 记账 + 最后一个条目时回执(要在拿 s_statesMtx 之前做:里面会 Send2Server)
|
||||
FinishC2CItem(pkt, reply, myClientID, true, true, pkt->fileSize, szErr);
|
||||
|
||||
close(state->fd);
|
||||
state->fd = -1;
|
||||
Mprintf("[V2 Recv] File complete: %s\n", savePath.c_str());
|
||||
@@ -306,7 +334,8 @@ public:
|
||||
}
|
||||
|
||||
// 处理文件完成校验包
|
||||
static int HandleFileComplete(const uint8_t* buf, size_t len, uint64_t myClientID)
|
||||
static int HandleFileComplete(const uint8_t* buf, size_t len, uint64_t myClientID,
|
||||
IOCPClient* reply = nullptr)
|
||||
{
|
||||
if (len < sizeof(FileCompletePacketV2)) {
|
||||
return 1;
|
||||
@@ -322,6 +351,9 @@ public:
|
||||
Mprintf("[V2] File complete verify: transferID=%llu, fileIndex=%u, size=%llu\n",
|
||||
pkt->transferID, pkt->fileIndex, pkt->fileSize);
|
||||
|
||||
// 空文件不发任何数据块(worker 只发这个完成包),C2C 回执只能靠它把该条目算作已处理
|
||||
NoteC2CComplete(pkt, reply, myClientID);
|
||||
|
||||
// TODO: 实现 SHA-256 校验
|
||||
// 目前简单返回成功
|
||||
return 0;
|
||||
@@ -663,7 +695,7 @@ public:
|
||||
|
||||
private:
|
||||
static RecvState* GetOrCreateState(uint64_t key, const FileChunkPacketV2* pkt,
|
||||
const std::string& savePath)
|
||||
const std::string& savePath, int* openErrno = nullptr)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(s_statesMtx);
|
||||
|
||||
@@ -682,6 +714,7 @@ private:
|
||||
// 打开文件
|
||||
state->fd = open(savePath.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (state->fd < 0) {
|
||||
if (openErrno) *openErrno = errno; // errno 会被下面的 Mprintf 冲掉,先存
|
||||
Mprintf("[V2 Recv] open failed: %s, errno=%d\n", savePath.c_str(), errno);
|
||||
return nullptr;
|
||||
}
|
||||
@@ -691,6 +724,111 @@ private:
|
||||
return ptr;
|
||||
}
|
||||
|
||||
// ===== C2C 接收回执 =====
|
||||
// C2C 的目标端不再只是「收包落盘」:本次传输的每个条目都定案后回一包
|
||||
// C2CSendResultPacket(TOKEN_C2C_RECV_RESULT,与源端的 175 分开,免得两种语义混在一个 token 里),
|
||||
// 把「是否真的写进去了」告诉服务端。Windows 目标端没有这个能力,服务端对它们保持旧语义。
|
||||
//
|
||||
// 条目定案的三个来源(缺一不可):
|
||||
// 1) 目录项:worker 发的 FFV2_DIRECTORY 包(不带 LAST_CHUNK);
|
||||
// 2) 非空文件:最后一块写盘成功(再 fstat 核对长度);
|
||||
// 3) 空文件:worker 不发任何数据块,只发 COMMAND_FILE_COMPLETE_V2。
|
||||
// 源端的重发(最多 3 次)和「最后一块 + 完成包」都会重复命中同一 fileIndex,用 done 集合去重。
|
||||
struct RecvProgress {
|
||||
uint32_t totalFiles = 0; // 从任意数据块/目录包学到(完成包里没有这个字段)
|
||||
std::set<uint32_t> doneFiles; // 已定案的文件 fileIndex
|
||||
std::set<uint32_t> doneDirs; // 已定案的目录项 fileIndex
|
||||
uint64_t bytes = 0; // 实收文件字节数
|
||||
bool failed = false;
|
||||
int firstErrno = 0;
|
||||
};
|
||||
|
||||
static bool IsFinalChunk(const FileChunkPacketV2* pkt)
|
||||
{
|
||||
return (pkt->flags & FFV2_LAST_CHUNK) != 0;
|
||||
}
|
||||
|
||||
static uint8_t MapErrnoToFileError(int e)
|
||||
{
|
||||
switch (e) {
|
||||
case EACCES:
|
||||
case EPERM:
|
||||
case EROFS: return FEV2_ACCESS_DENIED;
|
||||
case ENOENT:
|
||||
case ENOTDIR: return FEV2_FILE_NOT_FOUND;
|
||||
case ENOSPC: return FEV2_DISK_FULL;
|
||||
default: return FEV2_WRITE_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
// 记录一个条目的处理结果;finalChunk 表示这一包关掉了该条目(文件的最后一块 / 目录项 / 完成包)。
|
||||
// 所有条目都定案后就发回执。err != 0 表示这一条目失败(errno 原样带给服务端)。
|
||||
static void FinishC2CItem(const FileChunkPacketV2* pkt, IOCPClient* reply, uint64_t myClientID,
|
||||
bool isFile, bool finalChunk, uint64_t bytes, int err)
|
||||
{
|
||||
if (pkt->dstClientID == 0 || reply == nullptr) return; // 服务端上传/下载、或没有回执通道
|
||||
|
||||
C2CSendResultPacket res = {};
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(s_recvProgressMtx);
|
||||
// 传输中途断掉时条目会残留:攒多了整体清一次(残留的都是已废弃的 transferID)
|
||||
if (s_recvProgress.size() > 32) s_recvProgress.clear();
|
||||
RecvProgress& e = s_recvProgress[pkt->transferID];
|
||||
if (pkt->totalFiles != 0) e.totalFiles = pkt->totalFiles; // 完成包里没有,沿用学到的值
|
||||
|
||||
if (err != 0) {
|
||||
e.failed = true;
|
||||
if (e.firstErrno == 0) e.firstErrno = err;
|
||||
}
|
||||
if (finalChunk) {
|
||||
if (isFile) {
|
||||
if (e.doneFiles.insert(pkt->fileIndex).second && err == 0) e.bytes += bytes;
|
||||
} else {
|
||||
e.doneDirs.insert(pkt->fileIndex);
|
||||
}
|
||||
}
|
||||
|
||||
const bool finished = e.totalFiles != 0
|
||||
&& e.doneFiles.size() + e.doneDirs.size() >= e.totalFiles;
|
||||
if (!finished) return;
|
||||
|
||||
res.cmd = (uint8_t)TOKEN_C2C_RECV_RESULT;
|
||||
res.transferID = pkt->transferID;
|
||||
res.dstClientID = myClientID; // 服务端按连接身份 + dstClientID 双重核对
|
||||
res.status = e.failed ? (uint32_t)MapErrnoToFileError(e.firstErrno)
|
||||
: (uint32_t)FEV2_OK;
|
||||
res.win32err = (uint32_t)e.firstErrno; // 176 包里放的是 errno,不是 win32 码
|
||||
res.workerRc = 0; // 恒 0:不是源端 worker 返回码
|
||||
res.fileCount = (uint32_t)e.doneFiles.size();
|
||||
res.totalBytes = e.bytes;
|
||||
s_recvProgress.erase(pkt->transferID);
|
||||
}
|
||||
|
||||
// 锁外发送:Send2Server 可能阻塞,不应占着进度表
|
||||
if (reply->IsConnected() && reply->Send2Server((char*)&res, sizeof(res))) {
|
||||
Mprintf("[V2 Recv] C2C ack: transferID=%llu, files=%u, bytes=%llu, status=%u, errno=%u\n",
|
||||
(unsigned long long)res.transferID, res.fileCount,
|
||||
(unsigned long long)res.totalBytes, res.status, res.win32err);
|
||||
} else {
|
||||
Mprintf("[V2 Recv] C2C ack send failed: transferID=%llu\n",
|
||||
(unsigned long long)res.transferID);
|
||||
}
|
||||
}
|
||||
|
||||
// COMMAND_FILE_COMPLETE_V2:空文件的唯一「定案」来源(也用来给已定案的非空文件去重)
|
||||
static void NoteC2CComplete(const FileCompletePacketV2* cp, IOCPClient* reply, uint64_t myClientID)
|
||||
{
|
||||
if (cp->dstClientID == 0 || reply == nullptr) return;
|
||||
FileChunkPacketV2 probe = {};
|
||||
probe.cmd = COMMAND_SEND_FILE_V2;
|
||||
probe.transferID = cp->transferID;
|
||||
probe.dstClientID = cp->dstClientID;
|
||||
probe.fileIndex = cp->fileIndex;
|
||||
probe.totalFiles = 0; // 完成包没有 totalFiles,沿用进度表里学到的
|
||||
probe.fileSize = cp->fileSize;
|
||||
FinishC2CItem(&probe, reply, myClientID, true, true, 0, 0); // 空文件 bytes=0
|
||||
}
|
||||
|
||||
// 接收状态存储
|
||||
static inline std::map<uint64_t, std::unique_ptr<RecvState>> s_states;
|
||||
static inline std::mutex s_statesMtx;
|
||||
@@ -698,4 +836,8 @@ private:
|
||||
// C2C 目标目录存储
|
||||
static inline std::map<uint64_t, std::string> s_targetDirs;
|
||||
static inline std::mutex s_targetDirMtx;
|
||||
|
||||
// C2C 接收进度(按 transferID;定案发完回执即删)
|
||||
static inline std::map<uint64_t, RecvProgress> s_recvProgress;
|
||||
static inline std::mutex s_recvProgressMtx;
|
||||
};
|
||||
+8
-1
@@ -137,6 +137,7 @@ inline int isValid_10s()
|
||||
// 无此位 = 老客户端,按系统 ANSI(默认 CP936)解读
|
||||
#define CLIENT_CAP_SCREEN_PREVIEW 0x0004 // 支持屏幕预览(双击在线主机时弹缩略图)
|
||||
#define CLIENT_CAP_C2C_PATHS 0x0008 // 支持按路径清单向另一客户端发送文件(COMMAND_C2C_SEND_FILES,仅 Windows)
|
||||
#define CLIENT_CAP_C2C_RECV_ACK 0x0010 // 作为 C2C 目标端时,落盘完成后回执(TOKEN_C2C_RECV_RESULT,目前仅 Linux)
|
||||
|
||||
#define TALK_DLG_MAXLEN 1024 // 最大输入字符长度
|
||||
|
||||
@@ -339,9 +340,11 @@ enum {
|
||||
TOKEN_DOWNLOAD_ERROR = 173, // [token:1][win32err:4 LE] 下载请求无可用文件(路径全部不存在)
|
||||
|
||||
// MCP C2C 文件传输(服务端 → 源客户端 → 目标客户端;数据由服务端原样中继)。
|
||||
// 只有声明 CLIENT_CAP_C2C_PATHS 的新客户端认识这两个值,老客户端走 default 丢弃。
|
||||
// COMMAND_C2C_SEND_FILES / TOKEN_C2C_SEND_RESULT 只有声明 CLIENT_CAP_C2C_PATHS 的新客户端认识;
|
||||
// TOKEN_C2C_RECV_RESULT 由声明 CLIENT_CAP_C2C_RECV_ACK 的目标端回发,老服务端走 default 丢弃。
|
||||
COMMAND_C2C_SEND_FILES = 174, // [C2CSendFilesPacket][targetDir\0][path1\0]...[\0]
|
||||
TOKEN_C2C_SEND_RESULT = 175, // [C2CSendResultPacket] 源客户端回传的发送结果
|
||||
TOKEN_C2C_RECV_RESULT = 176, // [C2CSendResultPacket] 目标客户端回传的落盘回执(win32err 放 errno)
|
||||
|
||||
TOKEN_DECRYPT = 199,
|
||||
TOKEN_REGEDIT = 200, // 注册表
|
||||
@@ -1111,6 +1114,10 @@ typedef struct LOGIN_INFOR {
|
||||
#ifdef _WIN32
|
||||
caps |= CLIENT_CAP_SCREEN_PREVIEW;
|
||||
caps |= CLIENT_CAP_C2C_PATHS;
|
||||
#elif defined(__linux__) && !defined(__ANDROID__)
|
||||
// Linux 客户端作为 C2C 目标端时会回执落盘结果(common/FileTransferV2.h)。
|
||||
// macOS 暂未接线(无全局主连接句柄),不声明——服务端就不会为它空等回执。
|
||||
caps |= CLIENT_CAP_C2C_RECV_ACK;
|
||||
#endif
|
||||
sprintf_s(moduleVersion, "%s-%04X", DLL_VERSION, caps);
|
||||
}
|
||||
|
||||
@@ -114,17 +114,18 @@ struct C2CSendFilesPacket {
|
||||
// char path1[...]; '\0'; ...; // 源端绝对路径(文件或目录),列表以空串结束:额外一个 '\0'
|
||||
}; // 106 bytes + 字符串
|
||||
|
||||
// C2C 路径清单发送结果(源客户端 → 服务端)
|
||||
// C2C 结果包:源客户端 → 服务端(TOKEN_C2C_SEND_RESULT,发送结果)
|
||||
// 或 目标客户端 → 服务端(TOKEN_C2C_RECV_RESULT,落盘回执;win32err 位置放 errno,workerRc 恒 0)
|
||||
struct C2CSendResultPacket {
|
||||
uint8_t cmd; // TOKEN_C2C_SEND_RESULT = 175
|
||||
uint8_t cmd; // TOKEN_C2C_SEND_RESULT = 175 / TOKEN_C2C_RECV_RESULT = 176
|
||||
uint64_t transferID; // 对应请求的 transferID
|
||||
uint64_t dstClientID; // 目标客户端
|
||||
uint32_t status; // 0=已全部发送;FEV2_* 失败码;1001=发送线程失败(见 workerRc)
|
||||
uint32_t win32err; // status=FEV2_FILE_NOT_FOUND/ACCESS_DENIED 时的首个 win32 码
|
||||
int32_t workerRc; // FileBatchTransferWorkerV2 返回值(0=成功)
|
||||
uint32_t fileCount; // 实际发出的文件数(不含目录项)
|
||||
uint64_t totalBytes; // 实际发出的文件字节数
|
||||
}; // 45 bytes
|
||||
uint64_t totalBytes; // 实际发出的(回执里是实际落盘的)文件字节数
|
||||
}; // 41 bytes(#pragma pack(1))
|
||||
|
||||
// V2 文件完成校验包
|
||||
struct FileCompletePacketV2 {
|
||||
@@ -157,6 +158,7 @@ enum FileErrorV2 : uint8_t {
|
||||
FEV2_TRANSFER_CANCEL = 6, // 传输被取消
|
||||
FEV2_CHECKSUM_ERROR = 7, // 校验和错误(块级CRC)
|
||||
FEV2_HASH_MISMATCH = 8, // 哈希不匹配(文件级SHA-256)
|
||||
FEV2_WRITE_FAILED = 9, // 接收端写盘失败(POSIX 端 errno 无法归类时用它,明细在 win32err)
|
||||
};
|
||||
|
||||
// V2 续传查询包(发送方在传输前发送)
|
||||
|
||||
Reference in new issue
Block a user