From f87bd9e860962b7f7dbf5bbc753ebc5c80291d9b Mon Sep 17 00:00:00 2001 From: jerry Date: Wed, 14 May 2014 14:45:15 +0000 Subject: [PATCH] - checked filename's validation before sending drag info - refactored duplicated write to drop dir code - removed legacy member variables - fixed code style - fixed typo --- src/lib/client/Client.cpp | 26 ++------------ src/lib/client/Client.h | 5 --- src/lib/platform/MSWindowsScreen.cpp | 7 +++- src/lib/platform/OSXDragSimulator.m | 2 +- src/lib/platform/OSXScreen.cpp | 6 ---- src/lib/server/Server.cpp | 28 ++------------- src/lib/server/Server.h | 5 --- src/lib/synergy/DragInformation.cpp | 21 ++++++++++-- src/lib/synergy/DragInformation.h | 6 ++-- src/lib/synergy/DropHelper.cpp | 51 ++++++++++++++++++++++++++++ src/lib/synergy/DropHelper.h | 27 +++++++++++++++ 11 files changed, 113 insertions(+), 71 deletions(-) create mode 100644 src/lib/synergy/DropHelper.cpp create mode 100644 src/lib/synergy/DropHelper.h diff --git a/src/lib/client/Client.cpp b/src/lib/client/Client.cpp index 1d2d3d62..2d0442d4 100644 --- a/src/lib/client/Client.cpp +++ b/src/lib/client/Client.cpp @@ -21,6 +21,7 @@ #include "client/ServerProxy.h" #include "synergy/Screen.h" #include "synergy/Clipboard.h" +#include "synergy/DropHelper.h" #include "synergy/PacketStreamFilter.h" #include "synergy/ProtocolUtil.h" #include "synergy/protocol_types.h" @@ -746,29 +747,8 @@ CClient::writeToDropDirThread(void*) ARCH->sleep(.1f); } - m_fileTransferDes = m_screen->getDropTarget(); - LOG((CLOG_DEBUG "dropping file, files=%i target=%s", m_dragFileList.size(), m_fileTransferDes.c_str())); - - if (!m_fileTransferDes.empty() && m_dragFileList.size() > 0) { - std::fstream file; - CString dropTarget = m_fileTransferDes; -#ifdef SYSAPI_WIN32 - dropTarget.append("\\"); -#else - dropTarget.append("/"); -#endif - dropTarget.append(m_dragFileList.at(0).getFilename()); - file.open(dropTarget.c_str(), std::ios::out | std::ios::binary); - if (!file.is_open()) { - // TODO: file open failed - } - - file.write(m_receivedFileData.c_str(), m_receivedFileData.size()); - file.close(); - } - else { - LOG((CLOG_ERR "drop file failed: drop target is empty")); - } + CDropHelper::writeToDir(m_screen->getDropTarget(), m_dragFileList, + m_receivedFileData); } void diff --git a/src/lib/client/Client.h b/src/lib/client/Client.h index 76bea8a6..4995da64 100644 --- a/src/lib/client/Client.h +++ b/src/lib/client/Client.h @@ -109,9 +109,6 @@ public: //! Create a new thread and use it to send file to Server void sendFileToServer(const char* filename); - - //! Set file transder destination - void setFileTransferDes(CString& des) { m_fileTransferDes = des; } //! Send dragging file information back to server void sendDragInfo(UInt32 fileCount, CString& info, size_t size); @@ -231,8 +228,6 @@ private: CCryptoOptions m_crypto; std::size_t m_expectedFileSize; CString m_receivedFileData; - CString m_fileTransferSrc; - CString m_fileTransferDes; CDragFileList m_dragFileList; CString m_dragFileExt; CThread* m_sendFileThread; diff --git a/src/lib/platform/MSWindowsScreen.cpp b/src/lib/platform/MSWindowsScreen.cpp index 5dda3a4e..4ca81657 100644 --- a/src/lib/platform/MSWindowsScreen.cpp +++ b/src/lib/platform/MSWindowsScreen.cpp @@ -1873,7 +1873,12 @@ CMSWindowsScreen::getDraggingFilename() ShowWindow(m_dropWindow, SW_HIDE); if (!filename.empty()) { - m_draggingFilename = filename; + if (CDragInformation::isFileValid(filename)) { + m_draggingFilename = filename; + } + else { + LOG((CLOG_DEBUG "drag file name is invalid: %s", filename.c_str())); + } } if (m_draggingFilename.empty()) { diff --git a/src/lib/platform/OSXDragSimulator.m b/src/lib/platform/OSXDragSimulator.m index 70c788db..d6588d41 100644 --- a/src/lib/platform/OSXDragSimulator.m +++ b/src/lib/platform/OSXDragSimulator.m @@ -82,7 +82,7 @@ fakeDragging(const char* str, int cursorX, int cursorY) [g_dragWindow setFrame:rect display:NO]; [g_dragWindow makeKeyAndOrderFront:nil]; - [NSApp activateIgnoringOtherApps:YES]; + [NSApp activateIgnoringOtherApps:YES]; [g_dragView setFileExt:g_ext]; diff --git a/src/lib/platform/OSXScreen.cpp b/src/lib/platform/OSXScreen.cpp index c4d99eec..cea383f3 100644 --- a/src/lib/platform/OSXScreen.cpp +++ b/src/lib/platform/OSXScreen.cpp @@ -911,12 +911,6 @@ COSXScreen::leave() CString& fileList = getDraggingFilename(); if (!m_isPrimary) { - // TODO: is this duplicated? - // fake esc key down and up - fakeKeyDown(kKeyEscape, 8192, 1); - fakeKeyUp(1); - fakeMouseButton(kButtonLeft, false); - if (fileList.empty() == false) { CClientApp& app = CClientApp::instance(); CClient* client = app.getClientPtr(); diff --git a/src/lib/server/Server.cpp b/src/lib/server/Server.cpp index 0445e380..ef69f152 100644 --- a/src/lib/server/Server.cpp +++ b/src/lib/server/Server.cpp @@ -22,6 +22,7 @@ #include "server/ClientProxyUnknown.h" #include "server/PrimaryClient.h" #include "synergy/IPlatformScreen.h" +#include "synergy/DropHelper.h" #include "synergy/option_types.h" #include "synergy/protocol_types.h" #include "synergy/XScreen.h" @@ -2040,31 +2041,8 @@ CServer::writeToDropDirThread(void*) ARCH->sleep(.1f); } - m_fileTransferDes = m_screen->getDropTarget(); - LOG((CLOG_DEBUG "dropping file, files=%i target=%s", m_dragFileList.size(), m_fileTransferDes.c_str())); - - if (!m_fileTransferDes.empty() && m_dragFileList.size() > 0) { - std::fstream file; - CString dropTarget = m_fileTransferDes; -#ifdef SYSAPI_WIN32 - dropTarget.append("\\"); -#else - dropTarget.append("/"); -#endif - dropTarget.append(m_dragFileList.at(0).getFilename()); - file.open(dropTarget.c_str(), std::ios::out | std::ios::binary); - if (!file.is_open()) { - // TODO: file open failed - } - - file.write(m_receivedFileData.c_str(), m_receivedFileData.size()); - file.close(); - - m_dragFileList.clear(); - } - else { - LOG((CLOG_ERR "drop file failed: drop target is empty")); - } + CDropHelper::writeToDir(m_screen->getDropTarget(), m_dragFileList, + m_receivedFileData); } bool diff --git a/src/lib/server/Server.h b/src/lib/server/Server.h index a3cd1d0f..1d23b4cb 100644 --- a/src/lib/server/Server.h +++ b/src/lib/server/Server.h @@ -144,9 +144,6 @@ public: //! Set the expected size of receiving file void setExpectedFileSize(CString data); - - //! Set file transder destination - void setFileTransferDes(CString& des) { m_fileTransferDes = des; } //! Received a chunk of file data void fileChunkReceived(CString data); @@ -467,8 +464,6 @@ private: // file transfer size_t m_expectedFileSize; CString m_receivedFileData; - CString m_fileTransferSrc; - CString m_fileTransferDes; CDragFileList m_dragFileList; CThread* m_sendFileThread; CThread* m_writeToDropDirThread; diff --git a/src/lib/synergy/DragInformation.cpp b/src/lib/synergy/DragInformation.cpp index 0142e033..fe1d8b56 100644 --- a/src/lib/synergy/DragInformation.cpp +++ b/src/lib/synergy/DragInformation.cpp @@ -86,12 +86,12 @@ CDragInformation::parseDragInfo(CDragFileList& dragFileList, UInt32 fileNum, CSt } CString -CDragInformation::getDragFileExtension(CString fileName) +CDragInformation::getDragFileExtension(CString filename) { size_t findResult = string::npos; - findResult = fileName.find_last_of(".", fileName.size()); + findResult = filename.find_last_of(".", filename.size()); if (findResult != string::npos) { - return fileName.substr(findResult + 1, fileName.size() - findResult - 1); + return filename.substr(findResult + 1, filename.size() - findResult - 1); } else { return ""; @@ -112,6 +112,21 @@ CDragInformation::setupDragInfo(CDragFileList& fileList, CString& output) return size; } +bool +CDragInformation::isFileValid(CString filename) +{ + bool result = false; + std::fstream file(filename.c_str(), ios::in|ios::binary); + + if (file.is_open()) { + result = true; + } + + file. close(); + + return result; +} + size_t CDragInformation::stringToNum(CString& str) { diff --git a/src/lib/synergy/DragInformation.h b/src/lib/synergy/DragInformation.h index b328f732..8b6bcc48 100644 --- a/src/lib/synergy/DragInformation.h +++ b/src/lib/synergy/DragInformation.h @@ -35,12 +35,14 @@ public: void setFilesize(size_t size) { m_filesize = size; } static void parseDragInfo(CDragFileList& dragFileList, UInt32 fileNum, CString data); - static CString getDragFileExtension(CString fileName); - // helper function to setuo drag info + static CString getDragFileExtension(CString filename); + // helper function to setup drag info // example: filename1,filesize1,filename2,filesize2, // return file count static int setupDragInfo(CDragFileList& fileList, CString& output); + static bool isFileValid(CString filename); + private: static size_t stringToNum(CString& str); static CString getFileSize(CString& filename); diff --git a/src/lib/synergy/DropHelper.cpp b/src/lib/synergy/DropHelper.cpp new file mode 100644 index 00000000..19d54963 --- /dev/null +++ b/src/lib/synergy/DropHelper.cpp @@ -0,0 +1,51 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2014 Bolton Software Ltd. + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file COPYING that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "synergy/DropHelper.h" + +#include "base/Log.h" + +#include + +void +CDropHelper::writeToDir(const CString& destination, CDragFileList& fileList, CString& data) +{ + LOG((CLOG_DEBUG "dropping file, files=%i target=%s", fileList.size(), destination.c_str())); + + if (!destination.empty() && fileList.size() > 0) { + std::fstream file; + CString dropTarget = destination; +#ifdef SYSAPI_WIN32 + dropTarget.append("\\"); +#else + dropTarget.append("/"); +#endif + dropTarget.append(fileList.at(0).getFilename()); + file.open(dropTarget.c_str(), std::ios::out | std::ios::binary); + if (!file.is_open()) { + LOG((CLOG_DEBUG "drop file failed: can not open %s", dropTarget.c_str())); + } + + file.write(data.c_str(), data.size()); + file.close(); + + fileList.clear(); + } + else { + LOG((CLOG_ERR "drop file failed: drop target is empty")); + } +} diff --git a/src/lib/synergy/DropHelper.h b/src/lib/synergy/DropHelper.h new file mode 100644 index 00000000..afe65a31 --- /dev/null +++ b/src/lib/synergy/DropHelper.h @@ -0,0 +1,27 @@ +/* + * synergy -- mouse and keyboard sharing utility + * Copyright (C) 2014 Bolton Software Ltd. + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file COPYING that should have accompanied this file. + * + * This package is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "synergy/DragInformation.h" +#include "base/String.h" + +class CDropHelper { +public: + static void writeToDir(const CString& destination, + CDragFileList& fileList, CString& data); +};