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 /****************************************************************************
37 * Included Files
38 ****************************************************************************/
39
40 #include "vfs_config.h"
41
42 #include "sys/types.h"
43 #include "unistd.h"
44 #include "errno.h"
45
46 #include "fs/file.h"
47
48 /****************************************************************************
49 * Public Functions
50 ****************************************************************************/
51
52 /****************************************************************************
53 * Name: file_pwrite
54 *
55 * Description:
56 * Equivalent to the standard pwrite function except that is accepts a
57 * struct file instance instead of a file descriptor. Currently used
58 * only by aio_write();
59 *
60 ****************************************************************************/
61
file_pwrite64(struct file * filep,const void * buf,size_t nbytes,off64_t offset)62 static ssize_t file_pwrite64(struct file *filep, const void *buf,
63 size_t nbytes, off64_t offset)
64 {
65 off64_t savepos;
66 off64_t pos;
67 ssize_t ret;
68 int errcode;
69
70 /* Perform the seek to the current position. This will not move the
71 * file pointer, but will return its current setting
72 */
73
74 savepos = file_seek64(filep, 0, SEEK_CUR);
75 if (savepos == (off64_t)-1)
76 {
77 /* file_seek64 might fail if this if the media is not seekable */
78
79 return VFS_ERROR;
80 }
81
82 /* Then seek to the correct position in the file */
83
84 pos = file_seek64(filep, offset, SEEK_SET);
85 if (pos == (off64_t)-1)
86 {
87 /* This might fail is the offset is beyond the end of file */
88
89 return VFS_ERROR;
90 }
91
92 /* Then perform the write operation */
93
94 ret = file_write(filep, buf, nbytes);
95 errcode = get_errno();
96
97 /* Restore the file position */
98
99 pos = file_seek64(filep, savepos, SEEK_SET);
100 if (pos == (off64_t)-1 && ret >= 0)
101 {
102 /* This really should not fail */
103
104 return VFS_ERROR;
105 }
106
107 if (errcode != 0)
108 {
109 set_errno(errcode);
110 }
111 return ret;
112 }
113
114 /****************************************************************************
115 * Name: pwrite64
116 *
117 * Description:
118 * The pwrite64() function performs the same action as write(), except that
119 * it writes into a given position without changing the file pointer. The
120 * first three arguments to pwrite() are the same as write() with the
121 * addition of a fourth argument offset for the desired position inside
122 * the file.
123 *
124 * NOTE: This function could have been wholly implemented within libc but
125 * it is not. Why? Because if pwrite were implemented in libc, it would
126 * require four system calls. If it is implemented within the kernel,
127 * only three.
128 *
129 * Input Parameters:
130 * fd file descriptor (or socket descriptor) to write to
131 * buf Data to write
132 * nbytes Length of data to write
133 *
134 * Returned Value:
135 * The positive non-zero number of bytes read on success, 0 on if an
136 * end-of-file condition, or -1 on failure with errno set appropriately.
137 * See write() return values
138 *
139 * Assumptions/Limitations:
140 * POSIX requires that opening a file with the O_APPEND flag should have no
141 * effect on the location at which pwrite() writes data. However, on NuttX
142 * like on Linux, if a file is opened with O_APPEND, pwrite() appends data
143 * to the end of the file, regardless of the value of offset.
144 *
145 ****************************************************************************/
146
pwrite64(int fd,const void * buf,size_t nbytes,off64_t offset)147 ssize_t pwrite64(int fd, const void *buf, size_t nbytes, off64_t offset)
148 {
149 struct file *filep;
150
151 /* Get the file structure corresponding to the file descriptor. */
152
153 int ret = fs_getfilep(fd, &filep);
154 if (ret < 0)
155 {
156 /* The errno value has already been set */
157 return (ssize_t)VFS_ERROR;
158 }
159
160 if (filep->f_oflags & O_DIRECTORY)
161 {
162 set_errno(EBADF);
163 return VFS_ERROR;
164 }
165
166 /* Let file_pread do the real work */
167
168 return file_pwrite64(filep, buf, nbytes, offset);
169 }
170