mingw-spice-vdagent/0015-file_xfer-Use-destructor-for-FileXferTask.patch

100 lines
2.6 KiB
Diff
Raw Permalink Normal View History

2023-04-04 16:01:10 +00:00
From aefc220c027c98c0877cbb6dc7140e72f119262b Mon Sep 17 00:00:00 2001
From: Frediano Ziglio <fziglio@redhat.com>
Date: Fri, 25 May 2018 20:01:11 +0100
Subject: [PATCH 15/43] file_xfer: Use destructor for FileXferTask
Limit too much manual work.
By default delete the file, unless success() is called.
Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
Acked-by: Christophe Fergeau <cfergeau@redhat.com>
---
vdagent/file_xfer.cpp | 24 +++++++++++++++---------
vdagent/file_xfer.h | 4 +++-
2 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/vdagent/file_xfer.cpp b/vdagent/file_xfer.cpp
index 8203b99..ff4c0b9 100644
--- a/vdagent/file_xfer.cpp
+++ b/vdagent/file_xfer.cpp
@@ -46,7 +46,6 @@ void FileXfer::reset()
for (iter = _tasks.begin(); iter != _tasks.end(); iter++) {
task = iter->second;
- task->cancel();
delete task;
}
_tasks.clear();
@@ -181,14 +180,11 @@ bool FileXfer::handle_data(VDAgentFileXferDataMessage* data,
return false;
}
vd_printf("%u completed", iter->first);
+ task->success();
status->result = VD_AGENT_FILE_XFER_STATUS_SUCCESS;
fin:
if (task) {
- CloseHandle(task->handle);
- if (status->result != VD_AGENT_FILE_XFER_STATUS_SUCCESS) {
- DeleteFile(task->name);
- }
_tasks.erase(iter);
delete task;
}
@@ -196,10 +192,21 @@ fin:
return true;
}
-void FileXferTask::cancel()
+FileXferTask::~FileXferTask()
{
- CloseHandle(handle);
- DeleteFile(name);
+ if (handle != INVALID_HANDLE_VALUE) {
+ CloseHandle(handle);
+ DeleteFile(name);
+ }
+}
+
+void FileXferTask::success()
+{
+ // close the handle so the destructor won't delete the file
+ if (handle != INVALID_HANDLE_VALUE) {
+ CloseHandle(handle);
+ handle = INVALID_HANDLE_VALUE;
+ }
}
void FileXfer::handle_status(VDAgentFileXferStatusMessage* status)
@@ -218,7 +225,6 @@ void FileXfer::handle_status(VDAgentFileXferStatusMessage* status)
return;
}
task = iter->second;
- task->cancel();
_tasks.erase(iter);
delete task;
}
diff --git a/vdagent/file_xfer.h b/vdagent/file_xfer.h
index 747c29c..41f677a 100644
--- a/vdagent/file_xfer.h
+++ b/vdagent/file_xfer.h
@@ -30,12 +30,14 @@ struct FileXferTask {
lstrcpyn(name, _name, ARRAYSIZE(name));
name[ARRAYSIZE(name)-1] = 0;
}
+ ~FileXferTask();
+
HANDLE handle;
uint64_t size;
uint64_t pos;
TCHAR name[MAX_PATH];
- void cancel();
+ void success();
};
typedef std::map<uint32_t, FileXferTask*> FileXferTasks;
--
2.17.1