From a8985a8ad8381d9252b1dc534f20d25c9ed6ddc6 Mon Sep 17 00:00:00 2001 From: David Mulder Date: Thu, 17 Mar 2022 11:18:26 -0600 Subject: [PATCH] smbd: Move sendfile_short_send to smb2_reply.c Signed-off-by: David Mulder Reviewed-by: Jeremy Allison --- source3/smbd/proto.h | 10 +++--- source3/smbd/reply.c | 67 --------------------------------------- source3/smbd/smb2_reply.c | 67 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 72 deletions(-) diff --git a/source3/smbd/proto.h b/source3/smbd/proto.h index caa5df3485a..32f0f417765 100644 --- a/source3/smbd/proto.h +++ b/source3/smbd/proto.h @@ -928,11 +928,6 @@ void reply_ulogoffX(struct smb_request *req); void reply_mknew(struct smb_request *req); void reply_ctemp(struct smb_request *req); void reply_unlink(struct smb_request *req); -ssize_t sendfile_short_send(struct smbXsrv_connection *xconn, - files_struct *fsp, - ssize_t nread, - size_t headersize, - size_t smb_maxcnt); void reply_readbraw(struct smb_request *req); void reply_lockread(struct smb_request *req); size_t setup_readX_header(char *outbuf, size_t smb_maxcnt); @@ -1036,6 +1031,11 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_filename *smb_fname); ssize_t fake_sendfile(struct smbXsrv_connection *xconn, files_struct *fsp, off_t startpos, size_t nread); +ssize_t sendfile_short_send(struct smbXsrv_connection *xconn, + files_struct *fsp, + ssize_t nread, + size_t headersize, + size_t smb_maxcnt); /* The following definitions come from smbd/seal.c */ diff --git a/source3/smbd/reply.c b/source3/smbd/reply.c index 1785e95d4dc..20e223b927c 100644 --- a/source3/smbd/reply.c +++ b/source3/smbd/reply.c @@ -2446,73 +2446,6 @@ static void fail_readraw(void) exit_server_cleanly(errstr); } -/**************************************************************************** - Deal with the case of sendfile reading less bytes from the file than - requested. Fill with zeros (all we can do). Returns 0 on success -****************************************************************************/ - -ssize_t sendfile_short_send(struct smbXsrv_connection *xconn, - files_struct *fsp, - ssize_t nread, - size_t headersize, - size_t smb_maxcnt) -{ -#define SHORT_SEND_BUFSIZE 1024 - if (nread < headersize) { - DEBUG(0,("sendfile_short_send: sendfile failed to send " - "header for file %s (%s). Terminating\n", - fsp_str_dbg(fsp), strerror(errno))); - return -1; - } - - nread -= headersize; - - if (nread < smb_maxcnt) { - char buf[SHORT_SEND_BUFSIZE] = { 0 }; - - DEBUG(0,("sendfile_short_send: filling truncated file %s " - "with zeros !\n", fsp_str_dbg(fsp))); - - while (nread < smb_maxcnt) { - /* - * We asked for the real file size and told sendfile - * to not go beyond the end of the file. But it can - * happen that in between our fstat call and the - * sendfile call the file was truncated. This is very - * bad because we have already announced the larger - * number of bytes to the client. - * - * The best we can do now is to send 0-bytes, just as - * a read from a hole in a sparse file would do. - * - * This should happen rarely enough that I don't care - * about efficiency here :-) - */ - size_t to_write; - ssize_t ret; - - to_write = MIN(SHORT_SEND_BUFSIZE, smb_maxcnt - nread); - ret = write_data(xconn->transport.sock, buf, to_write); - if (ret != to_write) { - int saved_errno = errno; - /* - * Try and give an error message saying what - * client failed. - */ - DEBUG(0, ("write_data failed for client %s. " - "Error %s\n", - smbXsrv_connection_dbg(xconn), - strerror(saved_errno))); - errno = saved_errno; - return -1; - } - nread += to_write; - } - } - - return 0; -} - /**************************************************************************** Return a readbraw error (4 bytes of zero). ****************************************************************************/ diff --git a/source3/smbd/smb2_reply.c b/source3/smbd/smb2_reply.c index a74524d43ce..415c60011e9 100644 --- a/source3/smbd/smb2_reply.c +++ b/source3/smbd/smb2_reply.c @@ -950,3 +950,70 @@ ssize_t fake_sendfile(struct smbXsrv_connection *xconn, files_struct *fsp, SAFE_FREE(buf); return (ssize_t)nread; } + +/**************************************************************************** + Deal with the case of sendfile reading less bytes from the file than + requested. Fill with zeros (all we can do). Returns 0 on success +****************************************************************************/ + +ssize_t sendfile_short_send(struct smbXsrv_connection *xconn, + files_struct *fsp, + ssize_t nread, + size_t headersize, + size_t smb_maxcnt) +{ +#define SHORT_SEND_BUFSIZE 1024 + if (nread < headersize) { + DEBUG(0,("sendfile_short_send: sendfile failed to send " + "header for file %s (%s). Terminating\n", + fsp_str_dbg(fsp), strerror(errno))); + return -1; + } + + nread -= headersize; + + if (nread < smb_maxcnt) { + char buf[SHORT_SEND_BUFSIZE] = { 0 }; + + DEBUG(0,("sendfile_short_send: filling truncated file %s " + "with zeros !\n", fsp_str_dbg(fsp))); + + while (nread < smb_maxcnt) { + /* + * We asked for the real file size and told sendfile + * to not go beyond the end of the file. But it can + * happen that in between our fstat call and the + * sendfile call the file was truncated. This is very + * bad because we have already announced the larger + * number of bytes to the client. + * + * The best we can do now is to send 0-bytes, just as + * a read from a hole in a sparse file would do. + * + * This should happen rarely enough that I don't care + * about efficiency here :-) + */ + size_t to_write; + ssize_t ret; + + to_write = MIN(SHORT_SEND_BUFSIZE, smb_maxcnt - nread); + ret = write_data(xconn->transport.sock, buf, to_write); + if (ret != to_write) { + int saved_errno = errno; + /* + * Try and give an error message saying what + * client failed. + */ + DEBUG(0, ("write_data failed for client %s. " + "Error %s\n", + smbXsrv_connection_dbg(xconn), + strerror(saved_errno))); + errno = saved_errno; + return -1; + } + nread += to_write; + } + } + + return 0; +}