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)); } }
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; }
/** *\fn BOOL renameDir(const char *name) *\brief 重命名 *\param[in] const char *name 新名字 *\return BOOL TRUE成功,其它失败 */ BOOL CBrowseWnd::renameDir(const char *name) { if (NULL == name) return FALSE; char oldPath[1024] = ""; path_.GetWindowText(oldPath, sizeof(oldPath)-1); char newPath[1024] = ""; for (int i = strlen(oldPath) - 2; i >=0; i--) { if ('/' == oldPath[i]) { strncpy_s(newPath, oldPath, i+1); break; } } strcat_s(newPath, name); if (0 == strcmp(oldPath, newPath)) { return TRUE; } BOOL ret = (0 == sftp_rename(&ssh_param_, oldPath, newPath)); if (ret) { path_.SetWindowText(newPath); } return ret; }
void clSFTP::Rename(const wxString& oldpath, const wxString& newpath) throw(clException) { if(!m_sftp) { throw clException("SFTP is not initialized"); } int rc; rc = sftp_rename(m_sftp, oldpath.mb_str(wxConvISO8859_1).data(), newpath.mb_str(wxConvISO8859_1).data()); if(rc != SSH_OK) { throw clException(wxString() << _("Failed to rename path. ") << ssh_get_error(m_ssh->GetSession()), sftp_get_error(m_sftp)); } }
int FSSftp::Rename ( FSPath& oldpath, FSPath& newpath, int* err, FSCInfo* info ) { MutexLock lock( &mutex ); int ret = CheckSession( err, info ); if ( ret ) { return ret; } if ( sftp_rename( sftpSession, ( char* ) oldpath.GetString( _operParam.charset, '/' ), ( char* ) newpath.GetString( _operParam.charset, '/' ) ) ) { if ( err ) { *err = sftp_get_error( sftpSession ); } return -1; } return 0; }
/** *\fn BOOL renameFile(int item, int subItem, const char *name) *\brief 重命名 *\param[in] int item 列表节点ID *\param[in] int subItem 列表子节点ID *\param[in] const char * name 新名字 *\return BOOL 0成功,其它失败 */ BOOL CBrowseWnd::renameFile(int item, int subItem, const char *name) { if (item < 0 || 0 != subItem || NULL == name) return FALSE; char oldPath[1024] = ""; path_.GetWindowText(oldPath, sizeof(oldPath)-1); char newPath[1024] = ""; sprintf_s(newPath, "%s%s", oldPath, name); int len = strlen(oldPath); ListView_GetItemText(list_.m_hWnd, item, subItem, &oldPath[len], sizeof(oldPath)-len-1); if (0 == strcmp(oldPath, newPath)) { return TRUE; } return (0 == sftp_rename(&ssh_param_, oldPath, newPath)); }
int ssh_rename(const char *oldname, const char *newname) { char* on = ftp_path_absolute(oldname); char* nn = ftp_path_absolute(newname); stripslash(on); stripslash(nn); int rc = sftp_rename(ftp->sftp_session, on, nn); if (rc != SSH_OK) { ftp_err(_("Couldn't rename file '%s' to '%s': %s\n"), on, nn, ssh_get_error(ftp->session)); free(on); free(nn); return -1; } ftp_cache_flush_mark_for(on); ftp_cache_flush_mark_for(nn); free(on); free(nn); return 0; }