| 600 | } |
| 601 | |
| 602 | bool SFTPClient::rename(const std::string& source_path, const std::string& target_path, bool overwrite) { |
| 603 | int flags = 0; |
| 604 | if (overwrite) { |
| 605 | flags = LIBSSH2_SFTP_RENAME_ATOMIC | LIBSSH2_SFTP_RENAME_NATIVE | LIBSSH2_SFTP_RENAME_OVERWRITE; |
| 606 | } else { |
| 607 | flags = LIBSSH2_SFTP_RENAME_ATOMIC | LIBSSH2_SFTP_RENAME_NATIVE; |
| 608 | } |
| 609 | bool tried_deleting = false; |
| 610 | while (libssh2_sftp_rename_ex(sftp_session_, |
| 611 | source_path.c_str(), |
| 612 | source_path.length(), |
| 613 | target_path.c_str(), |
| 614 | target_path.length(), |
| 615 | flags) != 0) { |
| 616 | auto err = libssh2_sftp_last_error(sftp_session_); |
| 617 | if (overwrite && err == LIBSSH2_FX_FILE_ALREADY_EXISTS && !tried_deleting) { |
| 618 | /* It couldn't overwrite the file, let's delete it and try again */ |
| 619 | logger_->log_debug("Failed to overwrite file \"%s\" with rename, deleting instead", target_path.c_str()); |
| 620 | tried_deleting = true; |
| 621 | if (!this->removeFile(target_path)) { |
| 622 | return false; |
| 623 | } |
| 624 | continue; |
| 625 | } |
| 626 | last_error_.setLibssh2Error(libssh2_sftp_last_error(sftp_session_)); |
| 627 | logger_->log_error("Failed to rename remote file \"%s\" to \"%s\", error: %s", |
| 628 | source_path.c_str(), |
| 629 | target_path.c_str(), |
| 630 | sftp_strerror(last_error_)); |
| 631 | return false; |
| 632 | } |
| 633 | return true; |
| 634 | } |
| 635 | |
| 636 | bool SFTPClient::createDirectoryHierarchy(const std::string& path) { |
| 637 | if (path.empty()) { |
no test coverage detected