Example #1
0
void DownloadOperation::processFile()
{
  m_fileOffset = 0;

  File targetFile(m_pathToTargetFile.getString());

  if (targetFile.exists()) {
    FileInfo *sourceFileInfo = m_toCopy->getFileInfo();
    FileInfo targetFileInfo(&targetFile);

    int action = m_copyListener->targetFileExists(sourceFileInfo,
                                                  &targetFileInfo,
                                                  m_pathToTargetFile.getString());
    switch (action) {
    case CopyFileEventListener::TFE_OVERWRITE:
      break;
    case CopyFileEventListener::TFE_SKIP:
      m_totalBytesCopied += sourceFileInfo->getSize();
      gotoNext();
      return ;
    case CopyFileEventListener::TFE_APPEND:
      m_fileOffset = targetFileInfo.getSize();
      break;
    case CopyFileEventListener::TFE_CANCEL:
      if (!isTerminating()) {
        terminate();
      } 
      return ;
    default:
      _ASSERT(FALSE);
    } 
  } 

  m_sender->sendDownloadRequest(m_pathToSourceFile.getString(), m_fileOffset);
}
void DownloadOperation::processFile()
{
  m_fileOffset = 0;

  File targetFile(m_pathToTargetFile.getString());

  if (targetFile.exists()) {
    FileInfo *sourceFileInfo = m_toCopy->getFileInfo();
    FileInfo targetFileInfo(&targetFile);

    //
    // Copy listener must decide what to do with this situation
    //

    int action = m_copyListener->targetFileExists(sourceFileInfo,
                                                  &targetFileInfo,
                                                  m_pathToTargetFile.getString());
    switch (action) {
    case CopyFileEventListener::TFE_OVERWRITE:
      break;
    case CopyFileEventListener::TFE_SKIP:
      m_totalBytesCopied += sourceFileInfo->getSize();
      gotoNext();
      return ;
    case CopyFileEventListener::TFE_APPEND:
      m_fileOffset = targetFileInfo.getSize();
      break;
    case CopyFileEventListener::TFE_CANCEL:
      if (!isTerminating()) {
        terminate();
      } // if not terminating
      return ;
    default:
      _ASSERT(FALSE);
    } // switch
  } // if target file exists

  // Send request that we want to download file
  m_sender->sendDownloadRequest(m_pathToSourceFile.getString(), m_fileOffset);
}
Example #3
0
/*! PUT - Upload a file to the server
*/
void QwsClientSocket::handleMessagePUT(const QwMessage &message)
{
    QString targetFileName = message.stringArg(0);
    qint64 targetFileSize = message.stringArg(1).toLongLong();
    QByteArray targetFileChecksum = message.stringArg(2).toAscii();
    qDebug() << this << "Received PUT request:" << targetFileName;

    QwsFile localFile;
    localFile.localFilesRoot = filesRootPath;
    localFile.setRemotePath(targetFileName);

    // Check for jail escape
    if (!localFile.isWithinLocalRoot()) {
        sendError(Qw::ErrorFileOrDirectoryNotFound);
        return;
    }

    QFileInfo targetFileInfo(localFile.localPath());
    if (targetFileInfo.exists()) {
        // The file exists - abort here.
        sendError(Qw::ErrorFileOrDirectoryExists);

    } else {
        // File does not exist yet - check for a file with .WiredTransfer suffix
        localFile.setRemotePath(localFile.remotePath().append(".WiredTransfer"));
        qDebug() << this << "Checking for local partial file:" << localFile.remotePath();

        // Check for jail escape
        if (!localFile.isWithinLocalRoot()) {
            sendError(Qw::ErrorFileOrDirectoryNotFound);
            return;
        }

        targetFileInfo = QFileInfo(localFile.localPath());
        if (targetFileInfo.exists()) {
            // Partial file exists - check if the checksum is the same.
            localFile.calculateLocalChecksum();

            if (localFile.checksum() != targetFileChecksum) {
                qDebug() << "Checksum mismatch - local =" << localFile.checksum() << "expected = " << targetFileChecksum;
                sendError(Qw::ErrorChecksumMismatch);
                return;
            }

            // Checksums are the same - compare the file size to see what needs to be done.
            if (targetFileInfo.size() == targetFileSize) {
                qDebug() << "File exists!";
                sendError(Qw::ErrorFileOrDirectoryExists);
                return;
            }

            // At this point we have a partial file with the same checksum and a file size
            // smaller than the target file. We can resume.
            qDebug() << this << "Resuming file:" << targetFileInfo.absoluteFilePath()
                    << "already got" << targetFileInfo.size() << "of" << targetFileSize;

            localFile.offset = targetFileInfo.size();
            localFile.setSize(targetFileSize);
            localFile.setChecksum(targetFileChecksum);
            emit receivedMessagePUT(localFile);

        } else {
            // No partial file exists. Check if the parent directory exists and transfer the file.
            if (!targetFileInfo.absoluteDir().exists()) {
                qDebug() << "Parent does not exist!";
                sendError(Qw::ErrorFileOrDirectoryNotFound);
                return;
            }
            qDebug() << this << "Receiving new file:" << targetFileInfo.absoluteFilePath();
            localFile.offset = 0;
            localFile.setChecksum(targetFileChecksum);
            localFile.setSize(targetFileSize);
            emit receivedMessagePUT(localFile);
        }
    }
}