Fix: handle preview download errors and Unix metadata keys

Consume TOKEN_DOWNLOAD_ERROR in the file manager and fail the matching
preview request with the client's win32 code instead of sending
COMMAND_EXCEPTION and leaving the request to the 15 s watchdog.

Key the exact size/mtime metadata by the list's displayed ANSI/GBK text,
not the raw protocol bytes: Linux/macOS send UTF-8 names while GetItemText
returns the process ACP, so non-ASCII files previously missed the metadata
lookup and could not start a preview.

Bind the Unix progress dialog to the preview request so a stalled transfer
closes it on timeout; save the index after LRU eviction; make the local
right-click Open honour search-result paths; reject over-long received
names; and defer abandoned temp-dir deletion to the watchdog.

The new download-error and hash-error strings are added to en_US, zh_TW
and de_DE.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-12-05 15:44:36 +01:00
1 parent 6cb779ef70
commit bcd7ee692a
6 files changed
+70 -9

No files matched your search

+35 -6
View File
@@ -1088,6 +1088,16 @@ void CFileManagerDlg::OnReceiveComplete()
delete r;
break;
}
case TOKEN_DOWNLOAD_ERROR: { // 预览下载无可用文件(客户端在文件管理器子连接上回报)
LPBYTE buf = m_ContextObject->m_DeCompressionBuffer.GetBuffer(0);
ULONG len = m_ContextObject->m_DeCompressionBuffer.GetBufferLen();
DWORD err = 0;
if (len >= 5) memcpy(&err, buf + 1, sizeof(err));
CString msg;
msg.FormatL("预览下载失败(错误码 %u)", err);
CFilePreviewMgr::Instance().FailForHwnd(m_hWnd, std::string((LPCSTR)msg));
break;
}
case TOKEN_FILE_LIST: // 文件列表
try {
FixedRemoteFileList
@@ -1591,7 +1601,9 @@ void CFileManagerDlg::FixedRemoteFileList(BYTE *pbBuffer, DWORD dwBufferLen)
RemoteMeta meta;
meta.size = (uint64_t)dwFileSizeHigh * (MAXDWORD + 1ULL) + dwFileSizeLow;
meta.mtimeMs = FileTimeToUnixMs(ftm_strReceiveLocalFileTime);
CString full = m_Remote_Path + pszFileName;
// key 必须与双击时一致:用列表里实际显示的 ANSI/GBK 文本,
// 不能用协议原始字节(LNX/MAC 是 UTF-8,双击取回是 GBK)。
CString full = m_Remote_Path + m_list_remote.GetItemText(nItem, 0);
m_RemoteMeta[std::string((LPCSTR)full)] = meta;
}
}
@@ -1698,7 +1710,13 @@ void CFileManagerDlg::FixedRemoteSearchFileList(BYTE *pbBuffer, DWORD dwBufferLe
RemoteMeta meta;
meta.size = (uint64_t)dwFileSizeHigh * (MAXDWORD + 1ULL) + dwFileSizeLow;
meta.mtimeMs = FileTimeToUnixMs(ftLastWrite);
m_RemoteMeta[std::string((LPCSTR)csFullPath)] = meta;
// 与双击路径拼接保持一致(显示文本,不是协议原始字节)。
CString dispName = m_list_remote.GetItemText(nItem, 0);
CString dispDir = m_list_remote.GetItemText(nItem, 3);
CString full = dispDir;
if (!full.IsEmpty() && full.Right(1) != "\\") full += "\\";
full += dispName;
m_RemoteMeta[std::string((LPCSTR)full)] = meta;
}
pList += 16;
@@ -3201,6 +3219,9 @@ LRESULT CFileManagerDlg::OnRecvFileV2Chunk(WPARAM wParam, LPARAM /*lParam*/)
dlg->m_bKeepConnection = TRUE; // FileManager 子连接复用,对话框关闭时不断开
entry = { dlg->GetSafeHwnd(), dlg };
}
// Unix 同连接路径没有 MessageHandle 钩子,在这里把进度框绑到预览请求,
// 供看门狗超时关闭它。
CFilePreviewMgr::Instance().BindProgressWindow(m_ContextObject, entry.first);
// 落盘
std::string hash = GetPwdHash(), hmac = GetHMAC(100);
@@ -3523,10 +3544,18 @@ void CFileManagerDlg::OnRefresh()
void CFileManagerDlg::OnLocalOpen()
{
// TODO: Add your command handler code here
CString str;
str = m_Local_Path + m_list_local.GetItemText(m_list_local.GetSelectionMark(), 0);
ShellExecute(NULL, "open", str, NULL, NULL, SW_SHOW);
int nItem = m_list_local.GetSelectionMark();
if (nItem < 0) return;
CString name = m_list_local.GetItemText(nItem, 0);
CString full;
if (m_bLocalSearchResults) {
full = m_list_local.GetItemText(nItem, 3);
if (!full.IsEmpty() && full.Right(1) != "\\") full += "\\";
full += name;
} else {
full = m_Local_Path + name;
}
OpenLocalPath(full);
}
void CFileManagerDlg::OnRemoteOpenShow()
+29 -3
View File
@@ -274,12 +274,23 @@ void CFilePreviewMgr::CancelForHwnd(HWND hwnd)
if (req.progressHwnd && ::IsWindow(req.progressHwnd))
::PostMessage(req.progressHwnd, WM_CLOSE, 0, 0);
TombstoneLocked(req.tempDir);
RemoveTempDir(req.tempDir);
req.ctx = nullptr;
req.recvName.clear();
}
}
void CFilePreviewMgr::FailForHwnd(HWND hwnd, const std::string& message)
{
std::lock_guard<std::mutex> lk(m_mtx);
for (auto& req : m_reqs) {
if (req.notifyHwnd != hwnd) continue;
if (req.state == Request::WaitFirst || req.state == Request::Transferring) {
FailLocked(req, message);
return;
}
}
}
bool CFilePreviewMgr::ObserveChunk(CONTEXT_OBJECT* ctx, const FileChunkPacketV2* pkt,
const char* name, ULONG len, uint64_t* transferID)
{
@@ -302,6 +313,10 @@ bool CFilePreviewMgr::ObserveChunk(CONTEXT_OBJECT* ctx, const FileChunkPacketV2*
if (req->state == Request::Failed || req->state == Request::Abandoned ||
req->state == Request::Done)
return false; // 迟到/已失败:调用方取消连接,不能变回普通下载
if (safeName.size() > MAX_PATH - 64) {
FailLocked(*req, AsStd(_TR("预览文件名越界")));
return false;
}
if (!IsSingleFileUnder(req->tempDir, safeName)) {
FailLocked(*req, AsStd(_TR("预览文件名越界")));
return false;
@@ -320,6 +335,10 @@ bool CFilePreviewMgr::ObserveChunk(CONTEXT_OBJECT* ctx, const FileChunkPacketV2*
(int)req->state, safeName.c_str());
return false;
}
if (safeName.size() > MAX_PATH - 64) {
FailLocked(*req, AsStd(_TR("预览文件名越界")));
return false;
}
if (!IsSingleFileUnder(req->tempDir, safeName)) {
Mprintf("[Preview] observe path reject temp=%s name=%s\n",
@@ -504,10 +523,13 @@ void CFilePreviewMgr::WatchdogLoop()
for (auto it = m_reqs.begin(); it != m_reqs.end(); ) {
bool finished = (it->state == Request::Done || it->state == Request::Failed ||
it->state == Request::Abandoned);
if (finished && now - it->lastDataAt > std::chrono::seconds(kFinishedKeepSec))
if (finished && now - it->lastDataAt > std::chrono::seconds(kFinishedKeepSec)) {
// 延迟到请求结束后再删临时目录:CancelForHwnd 时网络侧可能还在写。
RemoveTempDir(it->tempDir);
it = m_reqs.erase(it);
else
} else {
++it;
}
}
}
}
@@ -858,16 +880,20 @@ void CFilePreviewMgr::EnforceCacheLimits(const std::string& clientDir, std::vect
std::sort(index.begin(), index.end(), [](const Meta& a, const Meta& b) {
return a.lastUsed < b.lastUsed;
});
bool changed = false;
while ((int)index.size() > maxFiles || uniqueBytes() > limitBytes) {
if (index.empty()) break;
std::string name = index.front().localName;
index.erase(index.begin());
changed = true;
bool referenced = false;
for (const auto& m : index)
if (m.localName == name) { referenced = true; break; }
if (!referenced)
DeleteFileA((clientDir + "\\" + name).c_str());
}
if (changed)
SaveIndex(clientDir, index); // 淘汰后必须落盘,否则 index.txt 会一直持有已删条目
}
bool CFilePreviewMgr::MoveOrCopy(const std::string& from, const std::string& to)
+3
View File
@@ -87,6 +87,9 @@ public:
// UI 线程:对话框关闭时放弃本窗口的所有预览请求。
void CancelForHwnd(HWND hwnd);
// UI 线程:用明确的失败原因结束本窗口当前请求(如客户端 TOKEN_DOWNLOAD_ERROR)。
void FailForHwnd(HWND hwnd, const std::string& message);
// 接收钩子(任意线程)。返回 false 表示该 chunk 属于已失败/越界的预览,调用方必须
// 取消连接并跳过写盘;返回 true 时原流程不变(非预览包也返回 true)。
bool ObserveChunk(CONTEXT_OBJECT* ctx, const FileChunkPacketV2* pkt,
+1
View File
@@ -2371,3 +2371,4 @@ MCP
文件较大,不支持预览:%s(%llu MB,上限 %u MB)=Datei zu gross fuer die Vorschau: %s (%llu MB, Grenze %u MB)
正在预览 %s ...=Vorschau von %s ...
获取远程文件信息失败(错误码 %u)=Dateiinformationen konnten nicht abgerufen werden (Fehler %u)
预览下载失败(错误码 %u)=Vorschau-Download fehlgeschlagen (Fehler %u)
+1
View File
@@ -2382,3 +2382,4 @@ MCP
文件较大,不支持预览:%s(%llu MB,上限 %u MB)=File too large to preview: %s (%llu MB, limit %u MB)
正在预览 %s ...=Previewing %s ...
获取远程文件信息失败(错误码 %u)=Failed to get remote file information (error %u)
预览下载失败(错误码 %u)=Preview download failed (error %u)
+1
View File
@@ -2375,3 +2375,4 @@ MCP
文件较大,不支持预览:%s(%llu MB,上限 %u MB)=檔案過大,不支援預覽:%s(%llu MB,上限 %u MB)
正在预览 %s ...=正在預覽 %s ...
获取远程文件信息失败(错误码 %u)=取得遠端檔案資訊失敗(錯誤碼 %u)
预览下载失败(错误码 %u)=預覽下載失敗(錯誤碼 %u)