[PATCH 2/2] fs: add pread and pwrite functions

Sascha Hauer s.hauer at pengutronix.de
Mon Feb 18 05:17:33 EST 2013


On Sun, Feb 17, 2013 at 10:05:01PM +0100, Alexander Aring wrote:
> Add pread and pwrite functions.
> 
> Split read and write functions to save some space.
> The functions pread and pwrite saves and sets the file
> position to a given offset and restore them afterwards.
> 
> Signed-off-by: Alexander Aring <alex.aring at gmail.com>
> ---
>  fs/fs.c      | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++----------
>  include/fs.h |  2 ++
>  2 files changed, 77 insertions(+), 14 deletions(-)
> 
> diff --git a/fs/fs.c b/fs/fs.c
> index 48d1c89..497a2ea 100644
> --- a/fs/fs.c
> +++ b/fs/fs.c
> @@ -754,17 +754,12 @@ int ioctl(int fd, int request, void *buf)
>  	return ret;
>  }
>  
> -ssize_t read(int fd, void *buf, size_t count)
> +static ssize_t __read(FILE *f, void *buf, size_t count)
>  {
>  	struct device_d *dev;
>  	struct fs_driver_d *fsdrv;
> -	FILE *f;
>  	int ret;
>  
> -	if (check_fd(fd))
> -		return -errno;
> -
> -	f = &files[fd];
>  	dev = f->dev;
>  
>  	fsdrv = dev_to_fs_driver(dev);
> @@ -777,18 +772,34 @@ ssize_t read(int fd, void *buf, size_t count)
>  
>  	ret = fsdrv->read(dev, f, buf, count);
>  
> -	if (ret > 0)
> -		f->pos += ret;
>  	if (ret < 0)
>  		errno = -ret;
>  	return ret;
>  }
> -EXPORT_SYMBOL(read);
> +EXPORT_SYMBOL(pread);
>  
> -ssize_t write(int fd, const void *buf, size_t count)
> +ssize_t pread(int fd, void *buf, size_t count, loff_t offset)
> +{
> +	loff_t pos;
> +	FILE *f;
> +	int ret;
> +
> +	if (check_fd(fd))
> +		return -errno;
> +
> +	f = &files[fd];
> +
> +	pos = f->pos;
> +	f->pos = offset;
> +	ret = __read(f, buf, count);
> +	f->pos = pos;
> +
> +	return ret;
> +}
> +EXPORT_SYMBOL(pread);

pread is exported twice with this patch. I fixed this while applying.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list