• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * fs/vfs/fs_pread.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_pread
54  *
55  * Description:
56  *   Equivalent to the standard pread function except that is accepts a
57  *   struct file instance instead of a file descriptor.  Currently used
58  *   only by aio_read();
59  *
60  ****************************************************************************/
61 
file_pread(struct file * filep,void * buf,size_t nbytes,off_t offset)62 ssize_t file_pread(struct file *filep, void *buf, size_t nbytes,
63                    off_t offset)
64 {
65   off_t savepos;
66   off_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_seek(filep, 0, SEEK_CUR);
75   if (savepos == (off_t)-1)
76     {
77       /* file_seek 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_seek(filep, offset, SEEK_SET);
85   if (pos == (off_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 read operation */
93 
94   ret = file_read(filep, buf, nbytes);
95   errcode = get_errno();
96 
97   /* Restore the file position */
98 
99   pos = file_seek(filep, savepos, SEEK_SET);
100   if (pos == (off_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: pread
116  *
117  * Description:
118  *   The pread() function performs the same action as read(), except that it
119  *   reads from a given position in the file without changing the file
120  *   pointer. The first three arguments to pread() are the same as read()
121  *   with the addition of a fourth argument offset for the desired position
122  *   inside the file. An attempt to perform a pread() on a file that is
123  *   incapable of seeking results in an error.
124  *
125  *   NOTE: This function could have been wholly implemented within libc but
126  *   it is not.  Why?  Because if pread were implemented in libc, it would
127  *   require four system calls.  If it is implemented within the kernel,
128  *   only three.
129  *
130  * Input Parameters:
131  *   file     File structure instance
132  *   buf      User-provided to save the data
133  *   nbytes   The maximum size of the user-provided buffer
134  *   offset   The file offset
135  *
136  * Returned Value:
137  *   The positive non-zero number of bytes read on success, 0 on if an
138  *   end-of-file condition, or -1 on failure with errno set appropriately.
139  *   See read() return values
140  *
141  ****************************************************************************/
142 
pread(int fd,void * buf,size_t nbytes,off_t offset)143 ssize_t pread(int fd, void *buf, size_t nbytes, off_t offset)
144 {
145   struct file *filep;
146 
147   /* Get the file structure corresponding to the file descriptor. */
148 
149   int ret = fs_getfilep(fd, &filep);
150   if (ret < 0)
151     {
152       /* The errno value has already been set */
153       set_errno(-ret);
154       return (ssize_t)VFS_ERROR;
155     }
156 
157   if (filep->f_oflags & O_DIRECTORY)
158     {
159       set_errno(EBADF);
160       return (ssize_t)VFS_ERROR;
161     }
162 
163   /* Let file_pread do the real work */
164 
165   return file_pread(filep, buf, nbytes, offset);
166 }
167