fs_read.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /****************************************************************************
  2. * fs/vfs/fs_read.c
  3. *
  4. * Copyright (C) 2007-2009, 2012-2014, 2016-2017 Gregory Nutt. All rights
  5. * 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/types.h>
  41. #include <sys/socket.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <sched.h>
  45. #include <errno.h>
  46. #include <nuttx/cancelpt.h>
  47. #include <nuttx/net/net.h>
  48. #include "inode/inode.h"
  49. /****************************************************************************
  50. * Public Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: file_read
  54. *
  55. * Description:
  56. * file_read() is an interanl OS interface. It is functionally similar to
  57. * the standard read() interface except:
  58. *
  59. * - It does not modify the errno variable,
  60. * - It is not a cancellation point,
  61. * - It does not handle socket descriptors, and
  62. * - It accepts a file structure instance instead of file descriptor.
  63. *
  64. * Input Parameters:
  65. * filep - File structure instance
  66. * buf - User-provided to save the data
  67. * nbytes - The maximum size of the user-provided buffer
  68. *
  69. * Returned Value:
  70. * The positive non-zero number of bytes read on success, 0 on if an
  71. * end-of-file condition, or a negated errno value on any failure.
  72. *
  73. ****************************************************************************/
  74. ssize_t file_read(FAR struct file *filep, FAR void *buf, size_t nbytes)
  75. {
  76. FAR struct inode *inode;
  77. int ret = -EBADF;
  78. DEBUGASSERT(filep);
  79. inode = filep->f_inode;
  80. /* Was this file opened for read access? */
  81. if ((filep->f_oflags & O_RDOK) == 0)
  82. {
  83. /* No.. File is not read-able */
  84. ret = -EACCES;
  85. }
  86. /* Is a driver or mountpoint registered? If so, does it support the read
  87. * method?
  88. */
  89. else if (inode != NULL && inode->u.i_ops && inode->u.i_ops->read)
  90. {
  91. /* Yes.. then let it perform the read. NOTE that for the case of the
  92. * mountpoint, we depend on the read methods being identical in
  93. * signature and position in the operations vtable.
  94. */
  95. ret = (int)inode->u.i_ops->read(filep, (FAR char *)buf, (size_t)nbytes);
  96. }
  97. /* Return the number of bytes read (or possibly an error code) */
  98. return ret;
  99. }
  100. /****************************************************************************
  101. * Name: nx_read
  102. *
  103. * Description:
  104. * nx_read() is an internal OS interface. It is functionally similar to
  105. * the standard read() interface except:
  106. *
  107. * - It does not modify the errno variable, and
  108. * - It is not a cancellation point.
  109. *
  110. * Input Parameters:
  111. * fd - File descriptor to read from
  112. * buf - User-provided to save the data
  113. * nbytes - The maximum size of the user-provided buffer
  114. *
  115. * Returned Value:
  116. * The positive non-zero number of bytes read on success, 0 on if an
  117. * end-of-file condition, or a negated errno value on any failure.
  118. *
  119. ****************************************************************************/
  120. ssize_t nx_read(int fd, FAR void *buf, size_t nbytes)
  121. {
  122. /* Did we get a valid file descriptor? */
  123. if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
  124. {
  125. #ifdef CONFIG_NET
  126. /* No.. If networking is enabled, read() is the same as recv() with
  127. * the flags parameter set to zero.
  128. */
  129. return nx_recv(fd, buf, nbytes, 0);
  130. #else
  131. /* No networking... it is a bad descriptor in any event */
  132. return -EBADF;
  133. #endif
  134. }
  135. else
  136. {
  137. FAR struct file *filep;
  138. ssize_t ret;
  139. /* The descriptor is in a valid range to file descriptor... do the
  140. * read. First, get the file structure. Note that on failure,
  141. * fs_getfilep() will set the errno variable.
  142. */
  143. ret = (ssize_t)fs_getfilep(fd, &filep);
  144. if (ret < 0)
  145. {
  146. return ret;
  147. }
  148. /* Then let file_read do all of the work. */
  149. return file_read(filep, buf, nbytes);
  150. }
  151. }
  152. /****************************************************************************
  153. * Name: read
  154. *
  155. * Description:
  156. * The standard, POSIX read interface.
  157. *
  158. * Input Parameters:
  159. * fd - File descriptor to read from
  160. * buf - User-provided to save the data
  161. * nbytes - The maximum size of the user-provided buffer
  162. *
  163. * Returned Value:
  164. * The positive non-zero number of bytes read on success, 0 on if an
  165. * end-of-file condition, or -1 on failure with errno set appropriately.
  166. *
  167. ****************************************************************************/
  168. ssize_t read(int fd, FAR void *buf, size_t nbytes)
  169. {
  170. ssize_t ret;
  171. /* read() is a cancellation point */
  172. (void)enter_cancellation_point();
  173. /* Let nx_read() do the real work */
  174. ret = nx_read(fd, buf, nbytes);
  175. if (ret < 0)
  176. {
  177. set_errno(-ret);
  178. ret = ERROR;
  179. }
  180. leave_cancellation_point();
  181. return ret;
  182. }