[Spice-commits] 3 commits - vdagent/file_xfer.cpp vdagent/file_xfer.h

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon May 28 15:27:26 UTC 2018


 vdagent/file_xfer.cpp |   49 +++++++++++++++++++------------------------------
 vdagent/file_xfer.h   |   11 +++++++----
 2 files changed, 26 insertions(+), 34 deletions(-)

New commits:
commit 7b0c48b15bfe3c02c4158c7cb213403739d078b5
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Fri May 25 23:03:18 2018 +0100

    file_xfer: Use shared_ptr to simplify memory management
    
    Clear automatically tasks items.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Christophe Fergeau <cfergeau at redhat.com>

diff --git a/vdagent/file_xfer.cpp b/vdagent/file_xfer.cpp
index ff4c0b9..ada3b47 100644
--- a/vdagent/file_xfer.cpp
+++ b/vdagent/file_xfer.cpp
@@ -41,19 +41,11 @@
 
 void FileXfer::reset()
 {
-    FileXferTasks::iterator iter;
-    FileXferTask* task;
-
-    for (iter = _tasks.begin(); iter != _tasks.end(); iter++) {
-        task = iter->second;
-        delete task;
-    }
     _tasks.clear();
 }
 
 FileXfer::~FileXfer()
 {
-    reset();
 }
 
 void FileXfer::handle_start(VDAgentFileXferStartMessage* start,
@@ -63,7 +55,6 @@ void FileXfer::handle_start(VDAgentFileXferStartMessage* start,
     TCHAR file_path[MAX_PATH];
     char file_name[MAX_PATH];
     ULARGE_INTEGER free_bytes;
-    FileXferTask* task;
     uint64_t file_size;
     HANDLE handle;
     AsUser as_user;
@@ -146,7 +137,7 @@ void FileXfer::handle_start(VDAgentFileXferStartMessage* start,
         vd_printf("Failed creating %ls. More than 63 copies exist?", file_path);
         return;
     }
-    task = new FileXferTask(handle, file_size, file_path);
+    auto task = std::make_shared<FileXferTask>(handle, file_size, file_path);
     _tasks[start->id] = task;
     status->result = VD_AGENT_FILE_XFER_STATUS_CAN_SEND_DATA;
 }
@@ -155,7 +146,6 @@ bool FileXfer::handle_data(VDAgentFileXferDataMessage* data,
                            VDAgentFileXferStatusMessage* status)
 {
     FileXferTasks::iterator iter;
-    FileXferTask* task = NULL;
     DWORD written;
 
     status->id = data->id;
@@ -163,9 +153,9 @@ bool FileXfer::handle_data(VDAgentFileXferDataMessage* data,
     iter = _tasks.find(data->id);
     if (iter == _tasks.end()) {
         vd_printf("file id %u not found", data->id);
-        goto fin;
+        return true;
     }
-    task = iter->second;
+    auto task = iter->second;
     task->pos += data->size;
     if (task->pos > task->size) {
         vd_printf("file xfer is longer than expected");
@@ -184,11 +174,7 @@ bool FileXfer::handle_data(VDAgentFileXferDataMessage* data,
     status->result = VD_AGENT_FILE_XFER_STATUS_SUCCESS;
 
 fin:
-    if (task) {
-        _tasks.erase(iter);
-        delete task;
-    }
-
+    _tasks.erase(iter);
     return true;
 }
 
@@ -212,7 +198,6 @@ void FileXferTask::success()
 void FileXfer::handle_status(VDAgentFileXferStatusMessage* status)
 {
     FileXferTasks::iterator iter;
-    FileXferTask* task;
 
     vd_printf("id %u result %u", status->id, status->result);
     if (status->result != VD_AGENT_FILE_XFER_STATUS_CANCELLED) {
@@ -224,9 +209,7 @@ void FileXfer::handle_status(VDAgentFileXferStatusMessage* status)
         vd_printf("file id %u not found", status->id);
         return;
     }
-    task = iter->second;
     _tasks.erase(iter);
-    delete task;
 }
 
 bool FileXfer::dispatch(VDAgentMessage* msg, VDAgentFileXferStatusMessage* status)
diff --git a/vdagent/file_xfer.h b/vdagent/file_xfer.h
index 41f677a..b138019 100644
--- a/vdagent/file_xfer.h
+++ b/vdagent/file_xfer.h
@@ -19,6 +19,7 @@
 #define _H_FILE_XFER
 
 #include <map>
+#include <memory>
 #include "vdcommon.h"
 
 struct FileXferTask {
@@ -40,7 +41,7 @@ struct FileXferTask {
     void success();
 };
 
-typedef std::map<uint32_t, FileXferTask*> FileXferTasks;
+typedef std::map<uint32_t, std::shared_ptr<FileXferTask> > FileXferTasks;
 
 class FileXfer {
 public:
commit aefc220c027c98c0877cbb6dc7140e72f119262b
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Fri May 25 20:01:11 2018 +0100

    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 at redhat.com>
    Acked-by: Christophe Fergeau <cfergeau at redhat.com>

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()
+{
+    if (handle != INVALID_HANDLE_VALUE) {
+        CloseHandle(handle);
+        DeleteFile(name);
+    }
+}
+
+void FileXferTask::success()
 {
-    CloseHandle(handle);
-    DeleteFile(name);
+    // 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;
commit e8ab5856a116f6b7b9bd28781fcf2f685cc6645f
Author: Frediano Ziglio <fziglio at redhat.com>
Date:   Fri May 25 19:46:34 2018 +0100

    file_xfer: Remove too C syntax for C++
    
    In C++ simply declaring the struct add the structure name to the global
    namespace, no needs for additional typedef.
    
    Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
    Acked-by: Christophe Fergeau <cfergeau at redhat.com>

diff --git a/vdagent/file_xfer.h b/vdagent/file_xfer.h
index 029d0e7..747c29c 100644
--- a/vdagent/file_xfer.h
+++ b/vdagent/file_xfer.h
@@ -21,7 +21,7 @@
 #include <map>
 #include "vdcommon.h"
 
-typedef struct FileXferTask {
+struct FileXferTask {
     FileXferTask(HANDLE _handle, uint64_t _size, const TCHAR* _name):
     handle(_handle), size(_size), pos(0) {
         // FIXME: should raise an error if name is too long..
@@ -36,7 +36,7 @@ typedef struct FileXferTask {
     TCHAR name[MAX_PATH];
 
     void cancel();
-} FileXferTask;
+};
 
 typedef std::map<uint32_t, FileXferTask*> FileXferTasks;
 


More information about the Spice-commits mailing list