void CodaClientApplication::handleFileSystemWrite(const Coda::CodaCommandResult &result) { // Close remote file even if copy fails m_putWriteOk = result; if (!m_putWriteOk) std::fprintf(stderr, "Writing data failed: %s\n", qPrintable(result.toString())); if (!m_putWriteOk || m_putLastChunkSize < m_putChunkSize) { closeRemoteFile(); } else { putSendNextChunk(); } }
void CodaClientApplication::handleFileSystemFStat(const Coda::CodaCommandResult &result) { m_statFstatOk = result.type == Coda::CodaCommandResult::SuccessReply; // Close remote file even if copy fails if (m_statFstatOk) { const Coda::CodaStatResponse statr = Coda::CodaDevice::parseStat(result); printTimeStamp(); std::printf("File: %s\nSize: %llu bytes\nAccessed: %s\nModified: %s\n", qPrintable(m_statRemoteFile), statr.size, qPrintable(statr.accessTime.toString(Qt::LocalDate)), qPrintable(statr.modTime.toString(Qt::LocalDate))); } else { std::fprintf(stderr, "FStat failed: %s\n", qPrintable(result.toString())); } closeRemoteFile(); }
void CodaClientApplication::putSendNextChunk() { // Read and send off next chunk const quint64 pos = m_putFile->pos(); const QByteArray data = m_putFile->read(m_putChunkSize); if (data.isEmpty()) { m_putWriteOk = true; closeRemoteFile(); } else { m_putLastChunkSize = data.size(); printTimeStamp(); std::printf("Writing %llu bytes to remote file '%s' at %llu\n", m_putLastChunkSize, m_remoteFileHandle.constData(), pos); m_trkDevice->sendFileSystemWriteCommand(Coda::CodaCallback(this, &CodaClientApplication::handleFileSystemWrite), m_remoteFileHandle, data, unsigned(pos)); } }
int8_t restore_file(FILE * fp, char * remote_name, uint8_t * in_buffer, uint16_t payload_size) { nwFileHandle nwfp = NULL; nwBackupCodes open_rc; int8_t remote_rc = 0; /* Assume success... */ open_rc = openRemoteFile(nwfp, remote_name, 1); if(!open_rc) { /* If we opened the file ok, read it FROM the remote */ //nwBackupCodes rcv_rc = 0; nwBackupCodes rcv_rc = TARGET_STILL_HAS_DATA; int8_t local_file_error = 0; unsigned long total_size = 0, elapsed_time = 0, prev_elapsed = 0; startTimer(); while((rcv_rc == TARGET_STILL_HAS_DATA) && !local_file_error) { uint16_t chars_written, actual_chars_rcvd; rcv_rc = rcvDataRemote(nwfp, in_buffer, payload_size, &actual_chars_rcvd); chars_written = fwrite(in_buffer, 1, actual_chars_rcvd, fp); if(chars_written < actual_chars_rcvd) { local_file_error = 1; } else { total_size += actual_chars_rcvd; elapsed_time = getElapsedTime(); if(elapsed_time - prev_elapsed > 1000) { prev_elapsed = elapsed_time; fprintf(stderr, "%lu total bytes sent... %lu B/sec\r", total_size, \ computeRate(total_size, elapsed_time)); } } } //fprintf(stderr, "We got here...\n"); /* Cleanup logic scope block */ { nwBackupCodes close_rc; if(!local_file_error) { /* We should close the file even if send_rc errored out! */ close_rc = closeRemoteFile(nwfp); if(!close_rc && !rcv_rc) { fprintf(stderr, "File receive okay... %lu total bytes received... %lu B/sec\n", total_size, \ computeRate(total_size, elapsed_time)); remote_rc = 0; /* If we successfully sent the file, we are done. */ } else { fprintf(stderr, "Open error code: %d, Rcv error code: %d, Close error code: %d\n", open_rc, rcv_rc, close_rc); remote_rc = -1; /* Worth retrying transfer. */ } } else { fprintf(stderr, "There was a local file error...\n"); close_rc = closeRemoteFile(nwfp); remote_rc = -2; /* Do not retry transfer. */ } } } else { remote_rc = -1; } return remote_rc; }