Feature: add a remote file hash query for the file manager
Add COMMAND_FILE_HASH/TOKEN_FILE_HASH (182/183) and the CLIENT_CAP_FILE_HASH capability so the server can ask a client for a single file's exact size, mtime and SHA-256 without transferring it. This is the P2 half of the double-click preview design; old clients do not advertise the capability and are skipped by the caller. Windows hashes with BCrypt through the existing VerifyV2 helper; Unix and Android stream Sha256 while reading. Both clamp the peer-supplied maxBytes to FILE_PREVIEW_MAX_BYTES, echo the original request path bytes for correlation, and return TOO_LARGE without reading the file. The Unix loop also verifies the bytes hashed match stat() so a file changed mid-read reports an error instead of a mismatched hash. No existing command changes; the new values are explicit and the four DefaultCaps branches advertise the capability. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
b44935270a
commit
e39b9d1ed5
5 files changed
+263
No files matched your search
@@ -105,6 +105,11 @@ public:
|
||||
FileTransferV2::HandleDownFilesV2(szBuffer, ulLength, m_client, g_myClientID);
|
||||
break;
|
||||
|
||||
case COMMAND_FILE_HASH:
|
||||
// 远程文件信息查询(双击预览):[maxBytes:8][path\0]
|
||||
HandleFileHash(szBuffer + 1, ulLength - 1);
|
||||
break;
|
||||
|
||||
case COMMAND_CONTINUE:
|
||||
// Server requests next chunk of file data
|
||||
SendFileData(szBuffer + 1);
|
||||
@@ -162,6 +167,97 @@ private:
|
||||
m_client->Send2Server((char*)&token, 1);
|
||||
}
|
||||
|
||||
// ---- COMMAND_FILE_HASH:返回文件大小 / mtime / SHA-256 ----
|
||||
void SendFileHashResponse(uint32_t status, uint32_t err, uint64_t size,
|
||||
uint64_t mtimeMs, const uint8_t* sha,
|
||||
const std::string& pathOrig)
|
||||
{
|
||||
if (!m_client) return;
|
||||
std::vector<uint8_t> pkt(sizeof(FileHashResponseHeader) + pathOrig.size() + 1, 0);
|
||||
FileHashResponseHeader* h = (FileHashResponseHeader*)pkt.data();
|
||||
h->token = TOKEN_FILE_HASH;
|
||||
h->status = status;
|
||||
h->err = err;
|
||||
h->size = size;
|
||||
h->mtimeMs = mtimeMs;
|
||||
if (sha) memcpy(h->sha256, sha, sizeof(h->sha256));
|
||||
memcpy(pkt.data() + sizeof(FileHashResponseHeader), pathOrig.data(), pathOrig.size());
|
||||
m_client->Send2Server((char*)pkt.data(), (ULONG)pkt.size());
|
||||
}
|
||||
|
||||
void HandleFileHash(const uint8_t* p, size_t len)
|
||||
{
|
||||
if (!p || len < 9) return;
|
||||
|
||||
uint64_t maxBytes = 0;
|
||||
memcpy(&maxBytes, p, sizeof(maxBytes));
|
||||
if (maxBytes > FILE_PREVIEW_MAX_BYTES) maxBytes = FILE_PREVIEW_MAX_BYTES;
|
||||
|
||||
const char* pathGbk = (const char*)(p + 8);
|
||||
size_t pathLen = strnlen(pathGbk, len - 8);
|
||||
std::string pathOrig(pathGbk, pathLen);
|
||||
if (pathOrig.empty()) {
|
||||
SendFileHashResponse(FILE_HASH_ERROR, EINVAL, 0, 0, nullptr, pathOrig);
|
||||
return;
|
||||
}
|
||||
|
||||
std::string path = winPathToLinux(FileTransferV2::gbkToUtf8(pathOrig).c_str());
|
||||
uint32_t status = FILE_HASH_OK;
|
||||
uint32_t err = 0;
|
||||
uint64_t size = 0;
|
||||
uint64_t mtimeMs = 0;
|
||||
uint8_t sha[32] = {};
|
||||
|
||||
struct stat st = {};
|
||||
if (stat(path.c_str(), &st) != 0) {
|
||||
status = FILE_HASH_ERROR;
|
||||
err = (uint32_t)(errno ? errno : ENOENT);
|
||||
} else if (!S_ISREG(st.st_mode)) {
|
||||
status = FILE_HASH_ERROR;
|
||||
err = (uint32_t)(S_ISDIR(st.st_mode) ? EISDIR : EINVAL);
|
||||
} else {
|
||||
size = (uint64_t)st.st_size;
|
||||
mtimeMs = (uint64_t)(int64_t)st.st_mtime * 1000ULL;
|
||||
if (size > maxBytes) {
|
||||
status = FILE_HASH_TOO_LARGE; // 不读取文件内容
|
||||
} else {
|
||||
std::ifstream f(path.c_str(), std::ios::binary);
|
||||
if (!f) {
|
||||
status = FILE_HASH_ERROR;
|
||||
err = (uint32_t)(errno ? errno : EIO);
|
||||
} else {
|
||||
Sha256 hasher;
|
||||
std::vector<char> buf(64 * 1024);
|
||||
uint64_t total = 0;
|
||||
bool ok = true;
|
||||
bool tooLarge = false;
|
||||
for (;;) {
|
||||
f.read(buf.data(), (std::streamsize)buf.size());
|
||||
std::streamsize n = f.gcount();
|
||||
if (n > 0) {
|
||||
total += (uint64_t)n;
|
||||
if (total > maxBytes) { tooLarge = true; break; }
|
||||
hasher.update(buf.data(), (size_t)n);
|
||||
}
|
||||
if (f.bad()) { ok = false; break; }
|
||||
if (n < (std::streamsize)buf.size()) break;
|
||||
}
|
||||
if (tooLarge) {
|
||||
status = FILE_HASH_TOO_LARGE; // 文件在 stat 后变大
|
||||
size = total;
|
||||
} else if (!ok || total != size) {
|
||||
status = FILE_HASH_ERROR; // 读取失败或 stat 后文件被截断/改写
|
||||
err = EIO;
|
||||
} else {
|
||||
hasher.final(sha);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SendFileHashResponse(status, err, size, mtimeMs, sha, pathOrig);
|
||||
}
|
||||
|
||||
#ifndef __ANDROID__
|
||||
// ---- iconv encoding conversion ----
|
||||
static std::string iconvConvert(const std::string& input, const char* from, const char* to)
|
||||
|
||||
Reference in new issue
Block a user