Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2006 TightVNC Team. All Rights Reserved. |
| 2 | * |
| 3 | * Developed by Dennis Syrovatsky |
| 4 | * |
| 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 18 | * USA. |
| 19 | * |
Peter Åstrand | 7877cd6 | 2009-02-25 16:15:48 +0000 | [diff] [blame] | 20 | * |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | // -=- SFileTransfer.cxx |
| 25 | |
| 26 | #include <rfb/msgTypes.h> |
| 27 | #include <rfb/SFileTransfer.h> |
| 28 | |
| 29 | using namespace rfb; |
| 30 | |
| 31 | SFileTransfer::SFileTransfer(network::Socket *sock) : |
| 32 | m_bUploadStarted(false), m_bDownloadStarted(false), |
| 33 | m_reader(&sock->inStream()), m_writer(&sock->outStream()), m_pSocket(sock) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | SFileTransfer::~SFileTransfer() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | bool |
| 42 | SFileTransfer::processMessages(int type) |
| 43 | { |
| 44 | switch(type) |
| 45 | { |
| 46 | case msgTypeFileListRequest: |
| 47 | return processFileListRequest(); |
| 48 | case msgTypeFileDownloadRequest: |
| 49 | return processFileDownloadRequest(); |
| 50 | case msgTypeFileUploadRequest: |
| 51 | return processFileUploadRequest(); |
| 52 | case msgTypeFileUploadData: |
| 53 | return processFileUploadData(); |
| 54 | case msgTypeFileDownloadCancel: |
| 55 | return processFileDownloadCancel(); |
| 56 | case msgTypeFileUploadFailed: |
| 57 | return processFileUploadFailed(); |
| 58 | case msgTypeFileCreateDirRequest: |
| 59 | return processFileCreateDirRequest(); |
| 60 | case msgTypeFileDirSizeRequest: |
| 61 | return processFileDirSizeRequest(); |
| 62 | case msgTypeFileRenameRequest: |
| 63 | return processFileRenameRequest(); |
| 64 | case msgTypeFileDeleteRequest: |
| 65 | return processFileDeleteRequest(); |
| 66 | default: |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | bool |
| 72 | SFileTransfer::processFileListRequest() |
| 73 | { |
| 74 | char szDirName[FT_FILENAME_SIZE] = {0}; |
| 75 | unsigned int dirNameSize = FT_FILENAME_SIZE; |
| 76 | unsigned int flags = 0; |
| 77 | |
| 78 | if (!m_reader.readFileListRqst(&dirNameSize, szDirName, &flags)) return false; |
| 79 | |
| 80 | if (!convertPathFromNet(szDirName)) return false; |
| 81 | |
| 82 | bool bDirOnly = false; |
| 83 | if (flags & 0x10) bDirOnly = true; |
| 84 | |
| 85 | FileInfo fi; |
| 86 | if (!makeFileList(szDirName, &fi, bDirOnly)) { |
| 87 | flags = (flags | 0x80); |
| 88 | } |
| 89 | return m_writer.writeFileListData((unsigned char)flags, &fi); |
| 90 | } |
| 91 | |
| 92 | bool |
| 93 | SFileTransfer::processFileDownloadRequest() |
| 94 | { |
| 95 | char szName[FT_FILENAME_SIZE] = {0}; |
| 96 | unsigned int nameSize = FT_FILENAME_SIZE; |
| 97 | unsigned int position = 0; |
| 98 | |
| 99 | if (!m_reader.readFileDownloadRqst(&nameSize, szName, &position)) return false; |
| 100 | |
| 101 | if (!convertPathFromNet(szName)) return false; |
| 102 | |
| 103 | if (m_bDownloadStarted) { |
| 104 | char reason[] = "The download is already started"; |
| 105 | m_writer.writeFileLastRqstFailed(msgTypeFileDownloadRequest, strlen(reason), reason); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | if (!m_fileReader.create(szName)) return false; |
| 110 | |
| 111 | m_bDownloadStarted = true; |
| 112 | |
| 113 | sendFileDownloadPortion(); |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool |
| 119 | SFileTransfer::sendFileDownloadPortion() |
| 120 | { |
| 121 | char buffer[FT_MAX_SENDING_SIZE]; |
| 122 | unsigned int bytesRead = 0; |
| 123 | |
| 124 | if (m_fileReader.read((void *)buffer, FT_MAX_SENDING_SIZE, &bytesRead)) { |
| 125 | if (bytesRead == 0) { |
| 126 | m_writer.writeFileDownloadData(m_fileReader.getTime()); |
| 127 | m_fileReader.close(); |
| 128 | m_bDownloadStarted = false; |
| 129 | return true; |
| 130 | } else { |
| 131 | m_writer.writeFileDownloadData(bytesRead, buffer); |
| 132 | return initDownloadCallback(); |
| 133 | } |
| 134 | } else { |
| 135 | char reason[] = "Error while reading from file"; |
| 136 | m_writer.writeFileDownloadFailed(strlen(reason), reason); |
| 137 | m_fileReader.close(); |
| 138 | m_bDownloadStarted = false; |
| 139 | return true; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | bool |
| 144 | SFileTransfer::processFileUploadRequest() |
| 145 | { |
| 146 | char szName[FT_FILENAME_SIZE] = {0}; |
| 147 | unsigned int nameSize = FT_FILENAME_SIZE; |
| 148 | unsigned int position = 0; |
| 149 | |
| 150 | if (!m_reader.readFileUploadRqst(&nameSize, szName, &position)) return false; |
| 151 | |
| 152 | if (!convertPathFromNet(szName)) return false; |
| 153 | |
| 154 | if (m_bUploadStarted) { |
| 155 | char reason[] = "The upload is already started"; |
| 156 | m_writer.writeFileLastRqstFailed(msgTypeFileUploadRequest, strlen(reason), reason); |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | if (!m_fileWriter.create(szName)) { |
| 161 | char reason[] = "Can't create local file"; |
| 162 | m_writer.writeFileLastRqstFailed(msgTypeFileUploadRequest, strlen(reason), reason); |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | m_bUploadStarted = true; |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | bool |
| 172 | SFileTransfer::processFileUploadData() |
| 173 | { |
| 174 | unsigned int dataSize = 0; |
| 175 | unsigned int modTime = 0; |
| 176 | |
| 177 | char *pUploadData = m_reader.readFileUploadData(&dataSize, &modTime); |
| 178 | |
| 179 | if (!m_bUploadStarted) { |
| 180 | char reason[] = "Upload is impossible"; |
| 181 | m_writer.writeFileUploadCancel(strlen(reason), reason); |
| 182 | } else { |
| 183 | if (pUploadData == NULL) { |
| 184 | if (modTime == 0) { |
| 185 | char reason[] = "Upload failed"; |
| 186 | m_writer.writeFileUploadCancel(strlen(reason), reason); |
| 187 | } else { |
| 188 | m_fileWriter.setTime(modTime); |
| 189 | } |
| 190 | m_fileWriter.close(); |
| 191 | m_bUploadStarted = false; |
| 192 | } else { |
| 193 | unsigned int dataWritten = 0; |
| 194 | m_fileWriter.write(pUploadData, dataSize, &dataWritten); |
| 195 | if (dataWritten != dataSize) { |
| 196 | char reason[] = "Upload failed"; |
| 197 | m_writer.writeFileUploadCancel(strlen(reason), reason); |
| 198 | m_fileWriter.close(); |
| 199 | m_bUploadStarted = false; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | delete [] pUploadData; |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | bool |
| 208 | SFileTransfer::processFileDownloadCancel() |
| 209 | { |
| 210 | char szReason[FT_FILENAME_SIZE] = {0}; |
| 211 | unsigned int reasonSize = FT_FILENAME_SIZE; |
| 212 | |
| 213 | if (!m_reader.readFileDownloadCancel(&reasonSize, szReason)) return false; |
| 214 | |
| 215 | m_fileReader.close(); |
| 216 | m_bDownloadStarted = false; |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | bool |
| 221 | SFileTransfer::processFileUploadFailed() |
| 222 | { |
| 223 | char szReason[FT_FILENAME_SIZE] = {0}; |
| 224 | unsigned int reasonSize = FT_FILENAME_SIZE; |
| 225 | |
| 226 | if (!m_reader.readFileUploadFailed(&reasonSize, szReason)) return false; |
| 227 | |
| 228 | deleteIt(m_fileWriter.getFilename()); |
| 229 | m_fileWriter.close(); |
| 230 | m_bUploadStarted = false; |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | bool |
| 235 | SFileTransfer::processFileCreateDirRequest() |
| 236 | { |
| 237 | char szName[FT_FILENAME_SIZE] = {0}; |
| 238 | unsigned int nameSize = FT_FILENAME_SIZE; |
| 239 | |
| 240 | if (!m_reader.readFileCreateDirRqst(&nameSize, szName)) return false; |
| 241 | |
| 242 | if (!convertPathFromNet(szName)) return false; |
| 243 | |
| 244 | return createDir(szName); |
| 245 | } |
| 246 | |
| 247 | bool |
| 248 | SFileTransfer::processFileDirSizeRequest() |
| 249 | { |
| 250 | char szName[FT_FILENAME_SIZE] = {0}; |
| 251 | unsigned int nameSize = FT_FILENAME_SIZE; |
| 252 | |
| 253 | if (!m_reader.readFileDirSizeRqst(&nameSize, szName)) return false; |
| 254 | |
| 255 | if (!convertPathFromNet(szName)) return false; |
| 256 | |
| 257 | unsigned short highSize16 = 0; |
| 258 | unsigned int lowSize32 = 0; |
| 259 | |
| 260 | if (!getDirSize(szName, &highSize16, &lowSize32)) return false; |
| 261 | |
| 262 | return m_writer.writeFileDirSizeData(lowSize32, highSize16); |
| 263 | } |
| 264 | |
| 265 | bool |
| 266 | SFileTransfer::processFileRenameRequest() |
| 267 | { |
| 268 | char szOldName[FT_FILENAME_SIZE] = {0}; |
| 269 | char szNewName[FT_FILENAME_SIZE] = {0}; |
| 270 | |
| 271 | unsigned int oldNameSize = FT_FILENAME_SIZE; |
| 272 | unsigned int newNameSize = FT_FILENAME_SIZE; |
| 273 | |
| 274 | if (!m_reader.readFileRenameRqst(&oldNameSize, &newNameSize, szOldName, szNewName)) return false; |
| 275 | |
| 276 | if ((!convertPathFromNet(szOldName)) || (!convertPathFromNet(szNewName))) return false; |
| 277 | |
| 278 | return renameIt(szOldName, szNewName); |
| 279 | } |
| 280 | |
| 281 | bool |
| 282 | SFileTransfer::processFileDeleteRequest() |
| 283 | { |
| 284 | char szName[FT_FILENAME_SIZE] = {0}; |
| 285 | unsigned int nameSize = FT_FILENAME_SIZE; |
| 286 | |
| 287 | if (!m_reader.readFileDeleteRqst(&nameSize, szName)) return false; |
| 288 | |
| 289 | if (!convertPathFromNet(szName)) return false; |
| 290 | |
| 291 | return deleteIt(szName); |
| 292 | } |
| 293 | |
| 294 | bool |
| 295 | SFileTransfer::convertPathFromNet(char *pszPath) |
| 296 | { |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | bool |
| 301 | SFileTransfer::makeFileList(char *pszPath, FileInfo *pFI, bool bDirOnly) |
| 302 | { |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | bool |
| 307 | SFileTransfer::deleteIt(char *pszPath) |
| 308 | { |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | bool |
| 313 | SFileTransfer::renameIt(char *pszOldPath, char *pszNewPath) |
| 314 | { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | bool |
| 319 | SFileTransfer::createDir(char *pszPath) |
| 320 | { |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | bool |
| 325 | SFileTransfer::getDirSize(char *pszName, unsigned short *pHighSize16, |
| 326 | unsigned int *pLowSize32) |
| 327 | { |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | bool |
| 332 | SFileTransfer::initDownloadCallback() |
| 333 | { |
| 334 | return false; |
| 335 | } |