int doCopy(int argc, char **argv){ struct location *dest, *src; int i; int r; if(opts(argc,argv)<0) return EXIT_FAILURE; dest=parse_location(destination); if(open_location(dest,WRITE)<0) return EXIT_FAILURE; for(i=0;i<nsources;++i){ src=parse_location(sources[i]); if(open_location(src,READ)<0){ return EXIT_FAILURE; } if(do_copy(src,dest,0) < 0){ break; } } if(dest->is_ssh){ r=ssh_scp_close(dest->scp); if(r == SSH_ERROR){ fprintf(stderr,"Error closing scp: %s\n",ssh_get_error(dest->session)); ssh_scp_free(dest->scp); dest->scp=NULL; return -1; } } else { fclose(dest->file); dest->file=NULL; } ssh_disconnect(dest->session); ssh_finalize(); return 0; }
static void close_location(struct location *loc) { int rc; if (loc) { if (loc->is_ssh) { if (loc->scp) { rc = ssh_scp_close(loc->scp); if (rc == SSH_ERROR) { fprintf(stderr, "Error closing scp: %s\n", ssh_get_error(loc->session)); } ssh_scp_free(loc->scp); loc->scp = NULL; } if (loc->session) { ssh_disconnect(loc->session); ssh_free(loc->session); loc->session = NULL; } } else { if (loc->file) { fclose(loc->file); loc->file = NULL; } } } }
static ssh_session scp_close(struct scp_ud *scpud){ if (scpud == NULL || scpud->scp == NULL)return NULL; ssh_session session= scpud->scp->session; ssh_scp_close(scpud->scp); ssh_scp_free(scpud->scp); scpud->scp=NULL; return session; }
void ssh_scp_free(ssh_scp scp){ if(scp->state != SSH_SCP_NEW) ssh_scp_close(scp); if(scp->channel) channel_free(scp->channel); SAFE_FREE(scp->location); SAFE_FREE(scp->request_name); SAFE_FREE(scp->warning); SAFE_FREE(scp); }
int SSH::scp(const std::string& filePath, const byte* data, size_t size) { ssh_scp scp; int rc; size_t found = filePath.find_last_of("/\\"); std::string filename = filePath.substr(found+1); std::string path = filePath.substr(0, found+1); scp = ssh_scp_new(session_, SSH_SCP_WRITE, path.c_str()); if (scp == NULL) { throw Error(1, ssh_get_error(session_)); rc = SSH_ERROR; } else { rc = ssh_scp_init(scp); if (rc != SSH_OK) { throw Error(1, ssh_get_error(session_)); } else { #ifdef _MSC_VER // S_IRUSR & S_IWUSR not in MSVC (0000400 & 0000200 in /usr/include/sys/stat.h on MacOS-X 10.8) #define S_IRUSR S_IREAD #define S_IWUSR S_IWRITE #endif rc = ssh_scp_push_file (scp, filename.c_str(), size, S_IRUSR | S_IWUSR); if (rc != SSH_OK) { throw Error(1, ssh_get_error(session_)); } else { rc = ssh_scp_write(scp, data, size); if (rc != SSH_OK) { throw Error(1, ssh_get_error(session_)); } } ssh_scp_close(scp); } ssh_scp_free(scp); } return rc; }
int scp_write(ssh_session session) { ssh_scp scp; int rc; scp = ssh_scp_new(session, SSH_SCP_WRITE | SSH_SCP_RECURSIVE, "."); if(scp == NULL) { fprintf(stderr, "Error allocation scp session: %s\n", ssh_get_error(session)); return SSH_ERROR; } rc = ssh_scp_init(scp); if(rc != SSH_OK) { fprintf(stderr, "Error initializing scp session: %s\n", ssh_get_error(session)); ssh_scp_free(scp); return rc; } ssh_scp_close(scp); ssh_scp_free(scp); return SSH_OK; }
static int do_scp_write(ssh_scp scp, const char* path, FILE* fp, ftp_transfer_func hookf) { time_t then = time(NULL) - 1; ftp_set_close_handler(); if (hookf) hookf(&ftp->ti); ftp->ti.begin = false; struct stat sb; errno = 0; if (fstat(fileno(fp), &sb) == -1) { ftp_err(_("Couldn't fstat local file: %s\n"), strerror(errno)); ssh_scp_free(scp); return -1; } int rc = ssh_scp_push_file(scp, path, sb.st_size, sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)); if (rc != SSH_OK) { ftp_err(_("Failed to start scp upload: %s\n"), ssh_get_error(ftp->session)); ssh_scp_close(scp); ssh_scp_free(scp); return -1; } /* read file */ char buffer[SSH_BUFSIZ]; ssize_t nbytes = 0; errno = 0; while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), fp)) > 0) { if (ftp_sigints() > 0) { ftp_trace("break due to sigint\n"); break; } rc = ssh_scp_write(scp, buffer, nbytes); if (rc != SSH_OK) { ftp_err(_("Error while writing to file: %s\n"), ssh_get_error(ftp->session)); ssh_scp_close(scp); ssh_scp_free(scp); return -1; } ftp->ti.size += nbytes; if (hookf) { time_t now = time(NULL); if (now > then) { hookf(&ftp->ti); then = now; } } errno = 0; } if (ferror(fp)) { ftp_err(_("Failed to read from file: %s\n"), strerror(errno)); rc = -1; } ssh_scp_close(scp); ssh_scp_free(scp); return rc; }
static int do_scp_read(ssh_scp scp, const char* infile, FILE* fp, getmode_t mode, ftp_transfer_func hookf) { time_t then = time(NULL) - 1; ftp_set_close_handler(); if (hookf) hookf(&ftp->ti); ftp->ti.begin = false; int rc = ssh_scp_pull_request(scp); if (rc != SSH_SCP_REQUEST_NEWFILE) { ftp_err(_("Failed to start scp download: %s\n"), ssh_get_error(ftp->session)); ssh_scp_close(scp); ssh_scp_free(scp); return -1; } size_t size = ssh_scp_request_get_size(scp); ssh_scp_accept_request(scp); /* read file */ char buffer[SSH_BUFSIZ]; int r = 0; while (size && (r = ssh_scp_read(scp, buffer, MIN(SSH_BUFSIZ, size))) != SSH_ERROR) { if (ftp_sigints() > 0) { ftp_trace("break due to sigint\n"); break; } errno = 0; if (fwrite(buffer, r, 1, fp) != 1) { ftp_err(_("Error while writing to file: %s\n"), strerror(errno)); ssh_scp_close(scp); ssh_scp_free(scp); return -1; } ftp->ti.size += r; if (hookf) { time_t now = time(NULL); if (now > then) { hookf(&ftp->ti); then = now; } } size -= r; } if (r == SSH_ERROR) { ftp_err(_("Error while reading from file: %s\n"), ssh_get_error(ftp->session)); r = -1; } else { r = ssh_scp_pull_request(scp); if (r != SSH_SCP_REQUEST_EOF) ftp_err(_("Unexpected request: %s %lu\n"), ssh_get_error(ftp->session), size); else r = 0; } ssh_scp_close(scp); ssh_scp_free(scp); return r; }