diff --git a/include/libssh/sftp.h b/include/libssh/sftp.h index 6620841b..63673ca3 100644 --- a/include/libssh/sftp.h +++ b/include/libssh/sftp.h @@ -788,6 +788,22 @@ LIBSSH_API sftp_statvfs_t sftp_fstatvfs(sftp_file file); */ LIBSSH_API void sftp_statvfs_free(sftp_statvfs_t statvfs_o); +/** + * @brief Synchronize a file's in-core state with storage device + * + * This calls the "fsync@openssh.com" extention. You should check if the + * extensions is supported using: + * + * @code + * int supported = sftp_extension_supported(sftp, "fsync@openssh.com", "1"); + * @endcode + * + * @param file The opened sftp file handle to sync + * + * @return 0 on success, < 0 on error with ssh and sftp error set. + */ +LIBSSH_API int sftp_fsync(sftp_file file); + /** * @brief Canonicalize a sftp path. * diff --git a/src/sftp.c b/src/sftp.c index e34bba0d..210c82b4 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -2774,6 +2774,118 @@ sftp_statvfs_t sftp_statvfs(sftp_session sftp, const char *path) { return NULL; } +int sftp_fsync(sftp_file file) +{ + sftp_session sftp; + sftp_message msg = NULL; + ssh_string ext; + ssh_buffer buffer; + uint32_t id; + int rc; + + if (file == NULL) { + return -1; + } + sftp = file->sftp; + + buffer = ssh_buffer_new(); + if (buffer == NULL) { + ssh_set_error_oom(sftp->session); + return -1; + } + + ext = ssh_string_from_char("fsync@openssh.com"); + if (ext == NULL) { + ssh_set_error_oom(sftp->session); + rc = -1; + goto done; + } + + id = sftp_get_new_id(sftp); + rc = ssh_buffer_add_u32(buffer, htonl(id)); + if (rc < 0) { + ssh_set_error_oom(sftp->session); + goto done; + } + + rc = ssh_buffer_add_ssh_string(buffer, ext); + if (rc < 0) { + ssh_set_error_oom(sftp->session); + goto done; + } + + rc = ssh_buffer_add_ssh_string(buffer, file->handle); + if (rc < 0) { + ssh_set_error_oom(sftp->session); + goto done; + } + + rc = sftp_packet_write(sftp, SSH_FXP_EXTENDED, buffer); + if (rc < 0) { + ssh_set_error_oom(sftp->session); + goto done; + } + + do { + rc = sftp_read_and_dispatch(sftp); + if (rc < 0) { + ssh_set_error_oom(sftp->session); + rc = -1; + goto done; + } + msg = sftp_dequeue(sftp, id); + } while (msg == NULL); + + /* By specification, this command only returns SSH_FXP_STATUS */ + if (msg->packet_type == SSH_FXP_STATUS) { + sftp_status_message status; + + status = parse_status_msg(msg); + sftp_message_free(msg); + if (status == NULL) { + rc = -1; + goto done; + } + + sftp_set_error(sftp, status->status); + switch (status->status) { + case SSH_FX_OK: + /* SUCCESS, LEAVE */ + status_msg_free(status); + rc = 0; + goto done; + default: + break; + } + + /* + * The status should be SSH_FX_OK if the command was successful, if it + * didn't, then there was an error + */ + ssh_set_error(sftp->session, + SSH_REQUEST_DENIED, + "SFTP server: %s", + status->errormsg); + status_msg_free(status); + + rc = -1; + goto done; + } else { + ssh_set_error(sftp->session, + SSH_FATAL, + "Received message %d when attempting to set stats", + msg->packet_type); + sftp_message_free(msg); + } + + rc = -1; +done: + ssh_buffer_free(buffer); + ssh_string_free(ext); + + return rc; +} + sftp_statvfs_t sftp_fstatvfs(sftp_file file) { sftp_status_message status = NULL; sftp_message msg = NULL; @@ -3076,4 +3188,3 @@ sftp_attributes sftp_fstat(sftp_file file) { } #endif /* WITH_SFTP */ -/* vim: set ts=2 sw=2 et cindent: */