Dennis Syrovatsky | 265d3c8 | 2006-04-17 12:36:11 +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 | *
|
| 20 | * TightVNC distribution homepage on the Web: http://www.tightvnc.com/
|
| 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_pSocket(sock), m_reader(&sock->inStream()), m_writer(&sock->outStream()),
|
| 33 | m_bUploadStarted(false), m_bDownloadStarted(false)
|
| 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)) return false;
|
| 87 |
|
| 88 | return m_writer.writeFileListData((unsigned char)flags, &fi);
|
| 89 | }
|
| 90 |
|
| 91 | bool
|
| 92 | SFileTransfer::processFileDownloadRequest()
|
| 93 | {
|
| 94 | char szName[FT_FILENAME_SIZE] = {0};
|
| 95 | unsigned int nameSize = FT_FILENAME_SIZE;
|
| 96 | unsigned int position = 0;
|
| 97 |
|
| 98 | if (!m_reader.readFileDownloadRqst(&nameSize, szName, &position)) return false;
|
| 99 |
|
| 100 | if (!convertPathFromNet(szName)) return false;
|
| 101 |
|
| 102 | if (m_bDownloadStarted) {
|
| 103 | char reason[] = "The download is already started";
|
| 104 | m_writer.writeFileLastRqstFailed(msgTypeFileDownloadRequest, strlen(reason), reason);
|
| 105 | return false;
|
| 106 | }
|
| 107 |
|
| 108 | if (!m_fileReader.create(szName)) return false;
|
| 109 |
|
| 110 | m_bDownloadStarted = true;
|
| 111 |
|
| 112 | sendFileDownloadPortion();
|
| 113 |
|
| 114 | return true;
|
| 115 | }
|
| 116 |
|
| 117 | bool
|
| 118 | SFileTransfer::sendFileDownloadPortion()
|
| 119 | {
|
| 120 | char buffer[FT_MAX_SENDING_SIZE];
|
| 121 | unsigned int bytesRead = 0;
|
| 122 |
|
| 123 | if (m_fileReader.read((void *)buffer, FT_MAX_SENDING_SIZE, &bytesRead)) {
|
| 124 | if (bytesRead == 0) {
|
| 125 | m_writer.writeFileDownloadData(m_fileReader.getTime());
|
| 126 | m_fileReader.close();
|
| 127 | m_bDownloadStarted = false;
|
| 128 | return true;
|
| 129 | } else {
|
| 130 | m_writer.writeFileDownloadData(bytesRead, buffer);
|
| 131 | return initDownloadCallback();
|
| 132 | }
|
| 133 | } else {
|
| 134 | char reason[] = "Error while reading from file";
|
| 135 | m_writer.writeFileDownloadFailed(strlen(reason), reason);
|
| 136 | m_fileReader.close();
|
| 137 | m_bDownloadStarted = false;
|
| 138 | return true;
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | bool
|
| 143 | SFileTransfer::processFileUploadRequest()
|
| 144 | {
|
| 145 | char szName[FT_FILENAME_SIZE] = {0};
|
| 146 | unsigned int nameSize = FT_FILENAME_SIZE;
|
| 147 | unsigned int position = 0;
|
| 148 |
|
| 149 | if (!m_reader.readFileUploadRqst(&nameSize, szName, &position)) return false;
|
| 150 |
|
| 151 | if (!convertPathFromNet(szName)) return false;
|
| 152 |
|
| 153 | if (m_bUploadStarted) {
|
| 154 | char reason[] = "The upload is already started";
|
| 155 | m_writer.writeFileLastRqstFailed(msgTypeFileUploadRequest, strlen(reason), reason);
|
| 156 | return false;
|
| 157 | }
|
| 158 |
|
| 159 | if (!m_fileWriter.create(szName)) {
|
| 160 | char reason[] = "Can't create local file";
|
| 161 | m_writer.writeFileLastRqstFailed(msgTypeFileUploadRequest, strlen(reason), reason);
|
| 162 | return true;
|
| 163 | }
|
| 164 |
|
| 165 | m_bUploadStarted = true;
|
| 166 |
|
| 167 | return true;
|
| 168 | }
|
| 169 |
|
| 170 | bool
|
| 171 | SFileTransfer::processFileUploadData()
|
| 172 | {
|
| 173 | unsigned int dataSize = 0;
|
| 174 | unsigned int modTime = 0;
|
| 175 |
|
| 176 | void *pUploadData = m_reader.readFileUploadData(&dataSize, &modTime);
|
| 177 |
|
| 178 | if (!m_bUploadStarted) {
|
| 179 | char reason[] = "Upload is impossible";
|
| 180 | m_writer.writeFileUploadCancel(strlen(reason), reason);
|
| 181 | } else {
|
| 182 | if (pUploadData == NULL) {
|
| 183 | if (modTime == 0) {
|
| 184 | char reason[] = "Upload failed";
|
| 185 | m_writer.writeFileUploadCancel(strlen(reason), reason);
|
| 186 | } else {
|
| 187 | m_fileWriter.setTime(modTime);
|
| 188 | }
|
| 189 | m_fileWriter.close();
|
| 190 | m_bUploadStarted = false;
|
| 191 | } else {
|
| 192 | unsigned int dataWritten = 0;
|
| 193 | m_fileWriter.write(pUploadData, dataSize, &dataWritten);
|
| 194 | if (dataWritten != dataSize) {
|
| 195 | char reason[] = "Upload failed";
|
| 196 | m_writer.writeFileUploadCancel(strlen(reason), reason);
|
| 197 | m_fileWriter.close();
|
| 198 | m_bUploadStarted = false;
|
| 199 | }
|
| 200 | }
|
| 201 | }
|
| 202 | delete [] pUploadData;
|
| 203 | return true;
|
| 204 | }
|
| 205 |
|
| 206 | bool
|
| 207 | SFileTransfer::processFileDownloadCancel()
|
| 208 | {
|
| 209 | char szReason[FT_FILENAME_SIZE] = {0};
|
| 210 | unsigned int reasonSize = FT_FILENAME_SIZE;
|
| 211 |
|
| 212 | if (!m_reader.readFileDownloadCancel(&reasonSize, szReason)) return false;
|
| 213 |
|
| 214 | m_fileReader.close();
|
| 215 | m_bDownloadStarted = false;
|
| 216 | return true;
|
| 217 | }
|
| 218 |
|
| 219 | bool
|
| 220 | SFileTransfer::processFileUploadFailed()
|
| 221 | {
|
| 222 | char szReason[FT_FILENAME_SIZE] = {0};
|
| 223 | unsigned int reasonSize = FT_FILENAME_SIZE;
|
| 224 |
|
| 225 | if (!m_reader.readFileUploadFailed(&reasonSize, szReason)) return false;
|
| 226 |
|
| 227 | deleteIt(m_fileWriter.getFilename());
|
| 228 | m_fileWriter.close();
|
| 229 | m_bUploadStarted = false;
|
| 230 | return true;
|
| 231 | }
|
| 232 |
|
| 233 | bool
|
| 234 | SFileTransfer::processFileCreateDirRequest()
|
| 235 | {
|
| 236 | char szName[FT_FILENAME_SIZE] = {0};
|
| 237 | unsigned int nameSize = FT_FILENAME_SIZE;
|
| 238 |
|
| 239 | if (!m_reader.readFileCreateDirRqst(&nameSize, szName)) return false;
|
| 240 |
|
| 241 | if (!convertPathFromNet(szName)) return false;
|
| 242 |
|
| 243 | return createDir(szName);
|
| 244 | }
|
| 245 |
|
| 246 | bool
|
| 247 | SFileTransfer::processFileDirSizeRequest()
|
| 248 | {
|
| 249 | char szName[FT_FILENAME_SIZE] = {0};
|
| 250 | unsigned int nameSize = FT_FILENAME_SIZE;
|
| 251 |
|
| 252 | if (!m_reader.readFileDirSizeRqst(&nameSize, szName)) return false;
|
| 253 |
|
| 254 | if (!convertPathFromNet(szName)) return false;
|
| 255 |
|
| 256 | unsigned short highSize16 = 0;
|
| 257 | unsigned int lowSize32 = 0;
|
| 258 |
|
| 259 | if (!getDirSize(szName, &highSize16, &lowSize32)) return false;
|
| 260 |
|
| 261 | return m_writer.writeFileDirSizeData(lowSize32, highSize16);
|
| 262 | }
|
| 263 |
|
| 264 | bool
|
| 265 | SFileTransfer::processFileRenameRequest()
|
| 266 | {
|
| 267 | char szOldName[FT_FILENAME_SIZE] = {0};
|
| 268 | char szNewName[FT_FILENAME_SIZE] = {0};
|
| 269 |
|
| 270 | unsigned int oldNameSize = FT_FILENAME_SIZE;
|
| 271 | unsigned int newNameSize = FT_FILENAME_SIZE;
|
| 272 |
|
| 273 | if (!m_reader.readFileRenameRqst(&oldNameSize, &newNameSize, szOldName, szNewName)) return false;
|
| 274 |
|
| 275 | if ((!convertPathFromNet(szOldName)) || (!convertPathFromNet(szNewName))) return false;
|
| 276 |
|
| 277 | return renameIt(szOldName, szNewName);
|
| 278 | }
|
| 279 |
|
| 280 | bool
|
| 281 | SFileTransfer::processFileDeleteRequest()
|
| 282 | {
|
| 283 | char szName[FT_FILENAME_SIZE] = {0};
|
| 284 | unsigned int nameSize = FT_FILENAME_SIZE;
|
| 285 |
|
| 286 | if (!m_reader.readFileDeleteRqst(&nameSize, szName)) return false;
|
| 287 |
|
| 288 | if (!convertPathFromNet(szName)) return false;
|
| 289 |
|
| 290 | return deleteIt(szName);
|
| 291 | }
|
| 292 |
|
| 293 | bool
|
| 294 | SFileTransfer::convertPathFromNet(char *pszPath)
|
| 295 | {
|
| 296 | return true;
|
| 297 | }
|
| 298 |
|
| 299 | bool
|
| 300 | SFileTransfer::makeFileList(char *pszPath, FileInfo *pFI, bool bDirOnly)
|
| 301 | {
|
| 302 | return false;
|
| 303 | }
|
| 304 |
|
| 305 | bool
|
| 306 | SFileTransfer::deleteIt(char *pszPath)
|
| 307 | {
|
| 308 | return false;
|
| 309 | }
|
| 310 |
|
| 311 | bool
|
| 312 | SFileTransfer::renameIt(char *pszOldPath, char *pszNewPath)
|
| 313 | {
|
| 314 | return false;
|
| 315 | }
|
| 316 |
|
| 317 | bool
|
| 318 | SFileTransfer::createDir(char *pszPath)
|
| 319 | {
|
| 320 | return false;
|
| 321 | }
|
| 322 |
|
| 323 | bool
|
| 324 | SFileTransfer::getDirSize(char *pszName, unsigned short *pHighSize16,
|
| 325 | unsigned int *pLowSize32)
|
| 326 | {
|
| 327 | return false;
|
| 328 | }
|
| 329 |
|
| 330 | bool
|
| 331 | SFileTransfer::initDownloadCallback()
|
| 332 | {
|
| 333 | return false;
|
| 334 | }
|