2007-05-20 18:54:09 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_read.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2007-05-20 18:54:09 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-05-20 18:54:09 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2007-05-20 18:54:09 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2016-12-09 20:49:36 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <sys/types.h>
|
2007-03-20 17:51:12 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <sched.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
2009-08-15 17:57:15 +02:00
|
|
|
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2023-08-18 14:14:38 +02:00
|
|
|
#include "notify/notify.h"
|
2016-12-09 20:49:36 +01:00
|
|
|
#include "inode/inode.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_read
|
|
|
|
*
|
|
|
|
* Description:
|
2020-01-07 11:07:39 +01:00
|
|
|
* file_read() is an internal OS interface. It is functionally similar to
|
2017-10-11 20:13:41 +02:00
|
|
|
* the standard read() interface except:
|
2013-09-28 22:47:49 +02:00
|
|
|
*
|
2017-10-11 20:13:41 +02:00
|
|
|
* - It does not modify the errno variable,
|
|
|
|
* - It is not a cancellation point,
|
|
|
|
* - It accepts a file structure instance instead of file descriptor.
|
2013-09-28 22:47:49 +02:00
|
|
|
*
|
2017-10-11 20:13:41 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* filep - File structure instance
|
|
|
|
* buf - User-provided to save the data
|
|
|
|
* nbytes - The maximum size of the user-provided buffer
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2013-09-28 22:47:49 +02:00
|
|
|
* The positive non-zero number of bytes read on success, 0 on if an
|
2017-10-11 20:13:41 +02:00
|
|
|
* end-of-file condition, or a negated errno value on any failure.
|
2013-09-28 22:47:49 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t file_read(FAR struct file *filep, FAR void *buf, size_t nbytes)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-09-28 22:47:49 +02:00
|
|
|
FAR struct inode *inode;
|
2024-09-17 10:43:56 +02:00
|
|
|
ssize_t ret = -EBADF;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
|
|
|
|
/* Was this file opened for read access? */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
if ((filep->f_oflags & O_RDOK) == 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-09-28 22:47:49 +02:00
|
|
|
/* No.. File is not read-able */
|
2012-03-31 17:13:12 +02:00
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
ret = -EACCES;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2013-09-28 22:47:49 +02:00
|
|
|
/* Is a driver or mountpoint registered? If so, does it support the read
|
|
|
|
* method?
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-02-22 21:17:14 +01:00
|
|
|
else if (inode != NULL && inode->u.i_ops && inode->u.i_ops->read)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-09-28 22:47:49 +02:00
|
|
|
/* Yes.. then let it perform the read. NOTE that for the case of the
|
2014-10-04 15:31:13 +02:00
|
|
|
* mountpoint, we depend on the read methods being identical in
|
2013-09-28 22:47:49 +02:00
|
|
|
* signature and position in the operations vtable.
|
2012-03-31 17:13:12 +02:00
|
|
|
*/
|
|
|
|
|
2024-09-17 10:43:56 +02:00
|
|
|
ret = inode->u.i_ops->read(filep,
|
|
|
|
(FAR char *)buf,
|
|
|
|
(size_t)nbytes);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2008-10-09 22:22:21 +02:00
|
|
|
|
2017-09-28 22:14:32 +02:00
|
|
|
/* Return the number of bytes read (or possibly an error code) */
|
2008-10-09 22:22:21 +02:00
|
|
|
|
2023-08-18 14:14:38 +02:00
|
|
|
#ifdef CONFIG_FS_NOTIFY
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
notify_read(filep);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2009-08-15 17:57:15 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2017-10-11 20:13:41 +02:00
|
|
|
* Name: nx_read
|
2013-09-28 22:47:49 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-10-12 22:58:21 +02:00
|
|
|
* nx_read() is an internal OS interface. It is functionally similar to
|
2017-10-11 20:13:41 +02:00
|
|
|
* the standard read() interface except:
|
|
|
|
*
|
|
|
|
* - It does not modify the errno variable, and
|
|
|
|
* - It is not a cancellation point.
|
2013-09-28 22:47:49 +02:00
|
|
|
*
|
2017-10-11 20:13:41 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* fd - File descriptor to read from
|
|
|
|
* buf - User-provided to save the data
|
|
|
|
* nbytes - The maximum size of the user-provided buffer
|
2013-09-28 22:47:49 +02:00
|
|
|
*
|
2017-10-11 20:13:41 +02:00
|
|
|
* Returned Value:
|
2013-09-28 22:47:49 +02:00
|
|
|
* The positive non-zero number of bytes read on success, 0 on if an
|
2017-10-11 20:13:41 +02:00
|
|
|
* end-of-file condition, or a negated errno value on any failure.
|
2013-09-28 22:47:49 +02:00
|
|
|
*
|
2009-08-15 17:57:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
ssize_t nx_read(int fd, FAR void *buf, size_t nbytes)
|
2009-08-15 17:57:15 +02:00
|
|
|
{
|
2021-02-23 11:04:13 +01:00
|
|
|
FAR struct file *filep;
|
|
|
|
ssize_t ret;
|
2009-08-15 17:57:15 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
/* First, get the file structure. Note that on failure,
|
|
|
|
* fs_getfilep() will return the errno.
|
|
|
|
*/
|
2009-08-15 17:57:15 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
ret = (ssize_t)fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
2013-09-28 22:47:49 +02:00
|
|
|
{
|
2021-02-23 11:04:13 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2017-10-11 16:39:19 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
/* Then let file_read do all of the work. */
|
2017-10-11 16:39:19 +02:00
|
|
|
|
2023-12-25 08:04:01 +01:00
|
|
|
ret = file_read(filep, buf, nbytes);
|
|
|
|
fs_putfilep(filep);
|
|
|
|
return ret;
|
2017-10-11 20:13:41 +02:00
|
|
|
}
|
2016-12-09 20:49:36 +01:00
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard, POSIX read interface.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd - File descriptor to read from
|
|
|
|
* buf - User-provided to save the data
|
|
|
|
* nbytes - The maximum size of the user-provided buffer
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The positive non-zero number of bytes read on success, 0 on if an
|
|
|
|
* end-of-file condition, or -1 on failure with errno set appropriately.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t read(int fd, FAR void *buf, size_t nbytes)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* read() is a cancellation point */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
enter_cancellation_point();
|
2017-10-11 20:13:41 +02:00
|
|
|
|
|
|
|
/* Let nx_read() do the real work */
|
|
|
|
|
|
|
|
ret = nx_read(fd, buf, nbytes);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
2017-10-11 16:39:19 +02:00
|
|
|
|
|
|
|
leave_cancellation_point();
|
2017-10-11 20:13:41 +02:00
|
|
|
return ret;
|
2009-08-15 17:57:15 +02:00
|
|
|
}
|