static int _sftp_rename(const char *olduri, const char *newuri) { char *oldpath = NULL; char *newpath = NULL; int rc = -1; if (_sftp_connect(olduri) < 0) { return -1; } if (c_parse_uri(olduri, NULL, NULL, NULL, NULL, NULL, &oldpath) < 0) { rc = -1; goto out; } if (c_parse_uri(newuri, NULL, NULL, NULL, NULL, NULL, &newpath) < 0) { rc = -1; goto out; } /* FIXME: workaround cause, sftp_rename can't overwrite */ sftp_unlink(_sftp_session, newpath); rc = sftp_rename(_sftp_session, oldpath, newpath); if (rc < 0) { errno = _sftp_portable_to_errno(sftp_get_error(_sftp_session)); } out: SAFE_FREE(oldpath); SAFE_FREE(newpath); return rc; }
void clSFTP::Write(const wxMemoryBuffer& fileContent, const wxString& remotePath) throw(clException) { if(!m_sftp) { throw clException("SFTP is not initialized"); } int access_type = O_WRONLY | O_CREAT | O_TRUNC; sftp_file file; wxString tmpRemoteFile = remotePath; tmpRemoteFile << ".codelitesftp"; file = sftp_open(m_sftp, tmpRemoteFile.mb_str(wxConvUTF8).data(), access_type, 0644); if(file == NULL) { throw clException(wxString() << _("Can't open file: ") << tmpRemoteFile << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } char* p = (char*)fileContent.GetData(); const int maxChunkSize = 65536; wxInt64 bytesLeft = fileContent.GetDataLen(); while(bytesLeft > 0) { wxInt64 chunkSize = bytesLeft > maxChunkSize ? maxChunkSize : bytesLeft; wxInt64 bytesWritten = sftp_write(file, p, chunkSize); if(bytesWritten < 0) { sftp_close(file); throw clException(wxString() << _("Can't write data to file: ") << tmpRemoteFile << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } bytesLeft -= bytesWritten; p += bytesWritten; } sftp_close(file); // Unlink the original file if it exists bool needUnlink = false; { // Check if the file exists sftp_attributes attr = sftp_stat(m_sftp, remotePath.mb_str(wxConvISO8859_1).data()); if(attr) { needUnlink = true; sftp_attributes_free(attr); } } if(needUnlink && sftp_unlink(m_sftp, remotePath.mb_str(wxConvUTF8).data()) < 0) { throw clException(wxString() << _("Failed to unlink file: ") << remotePath << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } // Rename the file if(sftp_rename(m_sftp, tmpRemoteFile.mb_str(wxConvUTF8).data(), remotePath.mb_str(wxConvUTF8).data()) < 0) { throw clException(wxString() << _("Failed to rename file: ") << tmpRemoteFile << " -> " << remotePath << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } }
void clSFTP::UnlinkFile(const wxString& path) throw (clException) { if ( !m_sftp ) { throw clException("SFTP is not initialized"); } int rc; rc = sftp_unlink(m_sftp, path.mb_str(wxConvISO8859_1).data()); if ( rc != SSH_OK ) { throw clException(wxString() << _("Failed to unlink path: ") << path << ". " << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } }
int ssh_unlink(const char *path) { int rc = sftp_unlink(ftp->sftp_session, path); if (rc != SSH_OK && sftp_get_error(ftp->sftp_session) != SSH_FX_NO_SUCH_FILE) { ftp_err(_("Couldn't delete file: %s\n"), ssh_get_error(ftp->session)); ftp->code = ctError; ftp->fullcode = 500; return -1; } else ftp_cache_flush_mark_for(path); return 0; }
int FSSftp::Delete ( FSPath& path, int* err, FSCInfo* info ) { MutexLock lock( &mutex ); int ret = CheckSession( err, info ); if ( ret ) { return ret; } if ( sftp_unlink( sftpSession, ( char* )path.GetString( _operParam.charset ) ) ) { if ( err ) { *err = sftp_get_error( sftpSession ); } return -1; } return 0; }
static int _sftp_unlink(const char *uri) { char *path = NULL; int rc = -1; if (_sftp_connect(uri) < 0) { return -1; } if (c_parse_uri(uri, NULL, NULL, NULL, NULL, NULL, &path) < 0) { return -1; } rc = sftp_unlink(_sftp_session, path); if (rc < 0) { errno = _sftp_portable_to_errno(sftp_get_error(_sftp_session)); } SAFE_FREE(path); return rc; }
static void do_sftp(ssh_session session){ sftp_session sftp=sftp_new(session); sftp_dir dir; sftp_attributes file; sftp_statvfs_t sftpstatvfs; struct statvfs sysstatvfs; sftp_file fichier; sftp_file to; int len=1; unsigned int i; char data[DATALEN]={0}; char *lnk; unsigned int count; if(!sftp){ fprintf(stderr, "sftp error initialising channel: %s\n", ssh_get_error(session)); return; } if(sftp_init(sftp)){ fprintf(stderr, "error initialising sftp: %s\n", ssh_get_error(session)); return; } printf("Additional SFTP extensions provided by the server:\n"); count = sftp_extensions_get_count(sftp); for (i = 0; i < count; i++) { printf("\t%s, version: %s\n", sftp_extensions_get_name(sftp, i), sftp_extensions_get_data(sftp, i)); } /* test symlink and readlink */ if (sftp_symlink(sftp, "/tmp/this_is_the_link", "/tmp/sftp_symlink_test") < 0) { fprintf(stderr, "Could not create link (%s)\n", ssh_get_error(session)); return; } lnk = sftp_readlink(sftp, "/tmp/sftp_symlink_test"); if (lnk == NULL) { fprintf(stderr, "Could not read link (%s)\n", ssh_get_error(session)); return; } printf("readlink /tmp/sftp_symlink_test: %s\n", lnk); sftp_unlink(sftp, "/tmp/sftp_symlink_test"); if (sftp_extension_supported(sftp, "*****@*****.**", "2")) { sftpstatvfs = sftp_statvfs(sftp, "/tmp"); if (sftpstatvfs == NULL) { fprintf(stderr, "statvfs failed (%s)\n", ssh_get_error(session)); return; } printf("sftp statvfs:\n" "\tfile system block size: %llu\n" "\tfundamental fs block size: %llu\n" "\tnumber of blocks (unit f_frsize): %llu\n" "\tfree blocks in file system: %llu\n" "\tfree blocks for non-root: %llu\n" "\ttotal file inodes: %llu\n" "\tfree file inodes: %llu\n" "\tfree file inodes for to non-root: %llu\n" "\tfile system id: %llu\n" "\tbit mask of f_flag values: %llu\n" "\tmaximum filename length: %llu\n", (unsigned long long) sftpstatvfs->f_bsize, (unsigned long long) sftpstatvfs->f_frsize, (unsigned long long) sftpstatvfs->f_blocks, (unsigned long long) sftpstatvfs->f_bfree, (unsigned long long) sftpstatvfs->f_bavail, (unsigned long long) sftpstatvfs->f_files, (unsigned long long) sftpstatvfs->f_ffree, (unsigned long long) sftpstatvfs->f_favail, (unsigned long long) sftpstatvfs->f_fsid, (unsigned long long) sftpstatvfs->f_flag, (unsigned long long) sftpstatvfs->f_namemax); sftp_statvfs_free(sftpstatvfs); if (statvfs("/tmp", &sysstatvfs) < 0) { fprintf(stderr, "statvfs failed (%s)\n", strerror(errno)); return; } printf("sys statvfs:\n" "\tfile system block size: %llu\n" "\tfundamental fs block size: %llu\n" "\tnumber of blocks (unit f_frsize): %llu\n" "\tfree blocks in file system: %llu\n" "\tfree blocks for non-root: %llu\n" "\ttotal file inodes: %llu\n" "\tfree file inodes: %llu\n" "\tfree file inodes for to non-root: %llu\n" "\tfile system id: %llu\n" "\tbit mask of f_flag values: %llu\n" "\tmaximum filename length: %llu\n", (unsigned long long) sysstatvfs.f_bsize, (unsigned long long) sysstatvfs.f_frsize, (unsigned long long) sysstatvfs.f_blocks, (unsigned long long) sysstatvfs.f_bfree, (unsigned long long) sysstatvfs.f_bavail, (unsigned long long) sysstatvfs.f_files, (unsigned long long) sysstatvfs.f_ffree, (unsigned long long) sysstatvfs.f_favail, (unsigned long long) sysstatvfs.f_fsid, (unsigned long long) sysstatvfs.f_flag, (unsigned long long) sysstatvfs.f_namemax); } /* the connection is made */ /* opening a directory */ dir=sftp_opendir(sftp,"./"); if(!dir) { fprintf(stderr, "Directory not opened(%s)\n", ssh_get_error(session)); return ; } /* reading the whole directory, file by file */ while((file=sftp_readdir(sftp,dir))){ fprintf(stderr, "%30s(%.8o) : %s(%.5d) %s(%.5d) : %.10llu bytes\n", file->name, file->permissions, file->owner, file->uid, file->group, file->gid, (long long unsigned int) file->size); sftp_attributes_free(file); } /* when file=NULL, an error has occured OR the directory listing is end of file */ if(!sftp_dir_eof(dir)){ fprintf(stderr, "Error: %s\n", ssh_get_error(session)); return; } if(sftp_closedir(dir)){ fprintf(stderr, "Error: %s\n", ssh_get_error(session)); return; } /* this will open a file and copy it into your /home directory */ /* the small buffer size was intended to stress the library. of course, you can use a buffer till 20kbytes without problem */ fichier=sftp_open(sftp,"/usr/bin/ssh",O_RDONLY, 0); if(!fichier){ fprintf(stderr, "Error opening /usr/bin/ssh: %s\n", ssh_get_error(session)); return; } /* open a file for writing... */ to=sftp_open(sftp,"ssh-copy",O_WRONLY | O_CREAT, 0700); if(!to){ fprintf(stderr, "Error opening ssh-copy for writing: %s\n", ssh_get_error(session)); return; } while((len=sftp_read(fichier,data,4096)) > 0){ if(sftp_write(to,data,len)!=len){ fprintf(stderr, "Error writing %d bytes: %s\n", len, ssh_get_error(session)); return; } } printf("finished\n"); if(len<0) fprintf(stderr, "Error reading file: %s\n", ssh_get_error(session)); sftp_close(fichier); sftp_close(to); printf("fichiers ferm\n"); to=sftp_open(sftp,"/tmp/grosfichier",O_WRONLY|O_CREAT, 0644); for(i=0;i<1000;++i){ len=sftp_write(to,data,DATALEN); printf("wrote %d bytes\n",len); if(len != DATALEN){ printf("chunk %d : %d (%s)\n",i,len,ssh_get_error(session)); } } sftp_close(to); /* close the sftp session */ sftp_free(sftp); printf("sftp session terminated\n"); }