fs_pwrite.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /****************************************************************************
  2. * fs/vfs/fs_pwrite.c
  3. *
  4. * Copyright (C) 2014, 2016-2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <unistd.h>
  41. #include <errno.h>
  42. #include <nuttx/cancelpt.h>
  43. #include <nuttx/fs/fs.h>
  44. /****************************************************************************
  45. * Public Functions
  46. ****************************************************************************/
  47. /****************************************************************************
  48. * Name: file_pwrite
  49. *
  50. * Description:
  51. * Equivalent to the standard pwrite function except that is accepts a
  52. * struct file instance instead of a file descriptor. Currently used
  53. * only by aio_write();
  54. *
  55. ****************************************************************************/
  56. ssize_t file_pwrite(FAR struct file *filep, FAR const void *buf,
  57. size_t nbytes, off_t offset)
  58. {
  59. off_t savepos;
  60. off_t pos;
  61. ssize_t ret;
  62. /* Perform the seek to the current position. This will not move the
  63. * file pointer, but will return its current setting
  64. */
  65. savepos = file_seek(filep, 0, SEEK_CUR);
  66. if (savepos < 0)
  67. {
  68. /* file_seek might fail if this if the media is not seekable */
  69. return (ssize_t)savepos;
  70. }
  71. /* Then seek to the correct position in the file */
  72. pos = file_seek(filep, offset, SEEK_SET);
  73. if (pos < 0)
  74. {
  75. /* This might fail is the offset is beyond the end of file */
  76. return (ssize_t)pos;
  77. }
  78. /* Then perform the write operation */
  79. ret = file_write(filep, buf, nbytes);
  80. /* Restore the file position */
  81. pos = file_seek(filep, savepos, SEEK_SET);
  82. if (pos < 0 && ret >= 0)
  83. {
  84. /* This really should not fail */
  85. ret = (ssize_t)pos;
  86. }
  87. return ret;
  88. }
  89. /****************************************************************************
  90. * Name: pwrite
  91. *
  92. * Description:
  93. * The pwrite() function performs the same action as write(), except that
  94. * it writes into a given position without changing the file pointer. The
  95. * first three arguments to pwrite() are the same as write() with the
  96. * addition of a fourth argument offset for the desired position inside
  97. * the file.
  98. *
  99. * NOTE: This function could have been wholly implemented within libc but
  100. * it is not. Why? Because if pwrite were implemented in libc, it would
  101. * require four system calls. If it is implemented within the kernel,
  102. * only three.
  103. *
  104. * Input Parameters:
  105. * fd file descriptor (or socket descriptor) to write to
  106. * buf Data to write
  107. * nbytes Length of data to write
  108. *
  109. * Returned Value:
  110. * The positive non-zero number of bytes read on success, 0 on if an
  111. * end-of-file condition, or -1 on failure with errno set appropriately.
  112. * See write() return values
  113. *
  114. * Assumptions/Limitations:
  115. * POSIX requires that opening a file with the O_APPEND flag should have no
  116. * effect on the location at which pwrite() writes data. However, on NuttX
  117. * like on Linux, if a file is opened with O_APPEND, pwrite() appends data
  118. * to the end of the file, regardless of the value of offset.
  119. *
  120. ****************************************************************************/
  121. ssize_t pwrite(int fd, FAR const void *buf, size_t nbytes, off_t offset)
  122. {
  123. FAR struct file *filep;
  124. ssize_t ret;
  125. /* pread() is a cancellation point */
  126. enter_cancellation_point();
  127. /* Get the file structure corresponding to the file descriptor. */
  128. ret = (ssize_t)fs_getfilep(fd, &filep);
  129. if (ret < 0)
  130. {
  131. goto errout;
  132. }
  133. /* Let file_pwrite do the real work */
  134. ret = file_pwrite(filep, buf, nbytes, offset);
  135. if (ret < 0)
  136. {
  137. goto errout;
  138. }
  139. leave_cancellation_point();
  140. return ret;
  141. errout:
  142. set_errno((int)-ret);
  143. leave_cancellation_point();
  144. return (ssize_t)ERROR;
  145. }