fs_sendfile.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /****************************************************************************
  2. * fs/vfs/fs_sendfile.c
  3. *
  4. * Copyright (C) 2007, 2009, 2011, 2013, 2017-2018 Gregory Nutt. All
  5. * rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <sys/sendfile.h>
  41. #include <stdbool.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #include <errno.h>
  45. #include <assert.h>
  46. #include <nuttx/sched.h>
  47. #include <nuttx/net/net.h>
  48. #ifdef CONFIG_NET_SENDFILE
  49. /****************************************************************************
  50. * Public Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: sendfile
  54. *
  55. * Description:
  56. * sendfile() copies data between one file descriptor and another.
  57. * Used with file descriptors it basically just wraps a sequence of
  58. * reads() and writes() to perform a copy.
  59. *
  60. * If the destination descriptor is a socket, it gives a better
  61. * performance than simple reds() and writes(). The data is read directly
  62. * into the net buffer and the whole tcp window is filled if possible.
  63. *
  64. * NOTE: This interface is *not* specified in POSIX.1-2001, or other
  65. * standards. The implementation here is very similar to the Linux
  66. * sendfile interface. Other UNIX systems implement sendfile() with
  67. * different semantics and prototypes. sendfile() should not be used
  68. * in portable programs.
  69. *
  70. * Input Parameters:
  71. * infd - A file (or socket) descriptor opened for reading
  72. * outfd - A descriptor opened for writing.
  73. * offset - If 'offset' is not NULL, then it points to a variable
  74. * holding the file offset from which sendfile() will start
  75. * reading data from 'infd'. When sendfile() returns, this
  76. * variable will be set to the offset of the byte following
  77. * the last byte that was read. If 'offset' is not NULL,
  78. * then sendfile() does not modify the current file offset of
  79. * 'infd'; otherwise the current file offset is adjusted to
  80. * reflect the number of bytes read from 'infd.'
  81. *
  82. * If 'offset' is NULL, then data will be read from 'infd'
  83. * starting at the current file offset, and the file offset
  84. * will be updated by the call.
  85. * count - The number of bytes to copy between the file descriptors.
  86. *
  87. * Returned Value:
  88. * If the transfer was successful, the number of bytes written to outfd is
  89. * returned. On error, -1 is returned, and errno is set appropriately.
  90. * There error values are those returned by read() or write() plus:
  91. *
  92. * EINVAL - Bad input parameters.
  93. * ENOMEM - Could not allocated an I/O buffer
  94. *
  95. ****************************************************************************/
  96. ssize_t sendfile(int outfd, int infd, off_t *offset, size_t count)
  97. {
  98. #ifdef CONFIG_NET_SENDFILE
  99. /* Check the destination file descriptor: Is it a (probable) file
  100. * descriptor? Check the source file: Is it a normal file?
  101. */
  102. if ((unsigned int)outfd >= CONFIG_NFILE_DESCRIPTORS &&
  103. (unsigned int)infd < CONFIG_NFILE_DESCRIPTORS)
  104. {
  105. FAR struct file *filep;
  106. int ret;
  107. /* This appears to be a file-to-socket transfer. Get the file
  108. * structure.
  109. */
  110. ret = fs_getfilep(infd, &filep);
  111. if (ret < 0)
  112. {
  113. set_errno(-ret);
  114. return ERROR;
  115. }
  116. DEBUGASSERT(filep != NULL);
  117. /* Then let net_sendfile do the work. */
  118. ret = net_sendfile(outfd, filep, offset, count);
  119. if (ret >= 0 || get_errno() != ENOSYS)
  120. {
  121. return ret;
  122. }
  123. /* Fall back to the slow path if errno equals ENOSYS,
  124. * because net_sendfile fail to optimize this transfer.
  125. */
  126. }
  127. #endif
  128. /* No... then this is probably a file-to-file transfer. The generic
  129. * lib_sendfile() can handle that case.
  130. */
  131. return lib_sendfile(outfd, infd, offset, count);
  132. }
  133. #endif /* CONFIG_NET_SENDFILE */