From d2cfd398ba783989d0817f565d9f269f19159ecb Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Fri, 9 Nov 2018 11:17:43 -0600 Subject: [PATCH] Fix compiler error and warning when CONFIG_NET_SENDFILE=y --- fs/vfs/fs_sendfile.c | 30 ++++++++++++++++------------ include/nuttx/net/net.h | 1 + net/inet/inet_sockif.c | 9 ++++++--- net/socket/net_sendfile.c | 41 +++++++++++++++------------------------ net/tcp/tcp.h | 1 + net/tcp/tcp_sendfile.c | 2 -- 6 files changed, 42 insertions(+), 42 deletions(-) diff --git a/fs/vfs/fs_sendfile.c b/fs/vfs/fs_sendfile.c index b1ef4ffb2a..9211f7ff13 100644 --- a/fs/vfs/fs_sendfile.c +++ b/fs/vfs/fs_sendfile.c @@ -1,8 +1,8 @@ /**************************************************************************** * fs/vfs/fs_sendfile.c * - * Copyright (C) 2007, 2009, 2011, 2013, 2017 Gregory Nutt. All rights - * reserved. + * Copyright (C) 2007, 2009, 2011, 2013, 2017-2018 Gregory Nutt. All + * rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -103,7 +103,7 @@ ssize_t sendfile(int outfd, int infd, off_t *offset, size_t count) { -#if defined(CONFIG_NET_TCP) && CONFIG_NSOCKET_DESCRIPTORS > 0 +#if defined(CONFIG_NET_SENDFILE) && CONFIG_NSOCKET_DESCRIPTORS > 0 /* Check the destination file descriptor: Is it a (probable) file * descriptor? Check the source file: Is it a normal file? */ @@ -129,17 +129,23 @@ ssize_t sendfile(int outfd, int infd, off_t *offset, size_t count) /* Then let net_sendfile do the work. */ - return net_sendfile(outfd, filep, offset, count); - } - else -#endif - { - /* No... then this is probably a file-to-file transfer. The generic - * lib_sendfile() can handle that case. - */ + ret = net_sendfile(outfd, filep, offset, count); + if (ret >= 0 || get_errno() != ENOSYS) + { + return ret; + } - return lib_sendfile(outfd, infd, offset, count); + /* Fall back to the slow path if errno equals ENOSYS, + * because net_sendfile fail to optimize this transfer. + */ } +#endif + + /* No... then this is probably a file-to-file transfer. The generic + * lib_sendfile() can handle that case. + */ + + return lib_sendfile(outfd, infd, offset, count); } #endif /* CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NET_SENDFILE */ diff --git a/include/nuttx/net/net.h b/include/nuttx/net/net.h index c44759ab27..92735b83dc 100644 --- a/include/nuttx/net/net.h +++ b/include/nuttx/net/net.h @@ -148,6 +148,7 @@ typedef uint8_t sockcaps_t; * a given address family. */ +struct file; /* Forward reference */ struct socket; /* Forward reference */ struct pollfd; /* Forward reference */ diff --git a/net/inet/inet_sockif.c b/net/inet/inet_sockif.c index 87cb2bdba8..587aea731e 100644 --- a/net/inet/inet_sockif.c +++ b/net/inet/inet_sockif.c @@ -1284,10 +1284,13 @@ static ssize_t inet_sendfile(FAR struct socket *psock, size_t count) { #if defined(CONFIG_NET_TCP) && !defined(CONFIG_NET_TCP_NO_STACK) - return tcp_sendfile(psock, infile, offset, size_t count); -#else - return -ENOSYS; + if (psock->s_type == SOCK_STREAM) + { + return tcp_sendfile(psock, infile, offset, count); + } #endif + + return -ENOSYS; } #endif diff --git a/net/socket/net_sendfile.c b/net/socket/net_sendfile.c index b27895f7cf..faf715ef18 100644 --- a/net/socket/net_sendfile.c +++ b/net/socket/net_sendfile.c @@ -45,6 +45,7 @@ #include #include +#include #include #include @@ -121,18 +122,17 @@ ssize_t net_sendfile(int outfd, FAR struct file *infile, FAR off_t *offset, size_t count) { FAR struct socket *psock = sockfd_socket(outfd); - ssize_t ret; - int errcode; + ssize_t ret = -ENOSYS; - DEBUGASSERT(psock->sock != NULL && infile != NULL); + DEBUGASSERT(psock != NULL && infile != NULL); /* Verify that the sockfd corresponds to valid, allocated socket */ if (psock != NULL || psock->s_crefs <= 0) { nerr("ERROR: Invalid socket\n"); - errcode = EBADF; - goto errout; + set_errno(EBADF); + return ERROR; } /* Check if the address family supports the optimized sendfile(). If not, @@ -142,30 +142,21 @@ ssize_t net_sendfile(int outfd, FAR struct file *infile, FAR off_t *offset, * method in the socket interface. */ - DEBUGASSERT(psock->sockif != NULL); - if (psock->sockif->s_sendfile == NULL) - { - int infd; - - list = sched_getfiles(); - DEBUGASSERT(list != NULL); - - infd = infile - list->fl_files; - return lib_sendfile(outfd, infd, offset, count); - } - else + DEBUGASSERT(psock->s_sockif != NULL); + if (psock->s_sockif->si_sendfile != NULL) { /* The address family can handle the optimized file send */ - ret = psock->sockif->s_sendfile(psock, offset, count); - if (ret < 0) - { - set_errno(-ret); - return ERROR; - } - - return ret; + ret = psock->s_sockif->si_sendfile(psock, infile, offset, count); } + + if (ret < 0) + { + set_errno(-ret); + return ERROR; + } + + return ret; } #endif /* CONFIG_NET_SENDFILE */ diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h index a1deb14f1b..8265e2205d 100644 --- a/net/tcp/tcp.h +++ b/net/tcp/tcp.h @@ -326,6 +326,7 @@ EXTERN struct net_driver_s *g_netdevices; * Public Function Prototypes ****************************************************************************/ +struct file; /* Forward reference */ struct sockaddr; /* Forward reference */ struct socket; /* Forward reference */ struct pollfd; /* Forward reference */ diff --git a/net/tcp/tcp_sendfile.c b/net/tcp/tcp_sendfile.c index 9b250e6841..a812c0babb 100644 --- a/net/tcp/tcp_sendfile.c +++ b/net/tcp/tcp_sendfile.c @@ -726,8 +726,6 @@ errout_locked: nxsem_destroy(&state. snd_sem); net_unlock(); -errout: - if (ret < 0) { return ret;