2022-01-10 22:56:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* libs/libc/stream/lib_blkoutstream.c
|
|
|
|
|
*
|
2024-09-25 14:05:00 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
2022-01-10 22:56:39 +08: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
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Included Files
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <nuttx/streams.h>
|
|
|
|
|
|
|
|
|
|
#include "libc.h"
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Pre-processor Definitions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: blkoutstream_flush
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2024-11-08 20:59:08 +08:00
|
|
|
static int blkoutstream_flush(FAR struct lib_sostream_s *self)
|
2022-01-10 22:56:39 +08:00
|
|
|
{
|
|
|
|
|
FAR struct lib_blkoutstream_s *stream =
|
2023-09-10 12:32:34 -07:00
|
|
|
(FAR struct lib_blkoutstream_s *)self;
|
2022-01-10 22:56:39 +08:00
|
|
|
size_t sectorsize = stream->geo.geo_sectorsize;
|
|
|
|
|
int ret = OK;
|
|
|
|
|
|
2023-09-10 12:32:34 -07:00
|
|
|
if (self->nput % sectorsize > 0)
|
2022-01-10 22:56:39 +08:00
|
|
|
{
|
|
|
|
|
ret = stream->inode->u.i_bops->write(stream->inode, stream->cache,
|
2023-09-10 12:32:34 -07:00
|
|
|
self->nput / sectorsize, 1);
|
2022-01-10 22:56:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 20:59:08 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: blkoutstream_seek
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
static off_t blkoutstream_seek(FAR struct lib_sostream_s *self,
|
|
|
|
|
off_t offset, int whence)
|
|
|
|
|
{
|
|
|
|
|
FAR struct lib_blkoutstream_s *stream =
|
|
|
|
|
(FAR struct lib_blkoutstream_s *)self;
|
|
|
|
|
size_t sectorsize = stream->geo.geo_sectorsize;
|
|
|
|
|
off_t streamsize = sectorsize * stream->geo.geo_nsectors;
|
|
|
|
|
FAR struct inode *inode = stream->inode;
|
|
|
|
|
off_t sector;
|
|
|
|
|
off_t ret;
|
|
|
|
|
|
|
|
|
|
switch (whence)
|
|
|
|
|
{
|
|
|
|
|
case SEEK_SET:
|
|
|
|
|
break;
|
|
|
|
|
case SEEK_END:
|
|
|
|
|
offset += streamsize;
|
|
|
|
|
break;
|
|
|
|
|
case SEEK_CUR:
|
|
|
|
|
offset += self->nput;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Seek to negative value or value larger than maximum size shall fail. */
|
|
|
|
|
|
|
|
|
|
if (offset < 0 || offset > streamsize)
|
|
|
|
|
{
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (self->nput % sectorsize)
|
|
|
|
|
{
|
|
|
|
|
sector = self->nput / sectorsize;
|
|
|
|
|
if (offset >= sector * sectorsize &&
|
|
|
|
|
offset < (sector + 1) * sectorsize)
|
|
|
|
|
{
|
|
|
|
|
/* Inside same sector */
|
|
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = inode->u.i_bops->write(stream->inode, stream->cache,
|
|
|
|
|
sector, 1);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (offset % sectorsize)
|
|
|
|
|
{
|
|
|
|
|
ret = inode->u.i_bops->read(inode, stream->cache,
|
|
|
|
|
offset / sectorsize, 1);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
self->nput = offset;
|
|
|
|
|
return offset;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 22:56:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: blkoutstream_puts
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2024-11-08 20:59:08 +08:00
|
|
|
static ssize_t blkoutstream_puts(FAR struct lib_sostream_s *self,
|
2024-11-09 17:10:17 +08:00
|
|
|
FAR const void *buf, size_t len)
|
2022-01-10 22:56:39 +08:00
|
|
|
{
|
|
|
|
|
FAR struct lib_blkoutstream_s *stream =
|
2023-09-10 12:32:34 -07:00
|
|
|
(FAR struct lib_blkoutstream_s *)self;
|
2022-01-10 22:56:39 +08:00
|
|
|
size_t sectorsize = stream->geo.geo_sectorsize;
|
|
|
|
|
FAR struct inode *inode = stream->inode;
|
|
|
|
|
FAR const unsigned char *ptr = buf;
|
|
|
|
|
size_t remain = len;
|
2024-11-09 17:10:17 +08:00
|
|
|
ssize_t ret;
|
2022-01-10 22:56:39 +08:00
|
|
|
|
|
|
|
|
while (remain > 0)
|
|
|
|
|
{
|
2024-11-09 17:10:17 +08:00
|
|
|
off_t sector = self->nput / sectorsize;
|
|
|
|
|
off_t offset = self->nput % sectorsize;
|
2022-01-10 22:56:39 +08:00
|
|
|
|
|
|
|
|
if (offset > 0)
|
|
|
|
|
{
|
2025-05-02 11:31:19 +02:00
|
|
|
size_t copying = offset + remain > sectorsize ?
|
|
|
|
|
sectorsize - offset : remain;
|
2022-01-10 22:56:39 +08:00
|
|
|
|
2025-05-02 11:31:19 +02:00
|
|
|
memcpy(stream->cache + offset, ptr, copying);
|
2022-01-10 22:56:39 +08:00
|
|
|
|
2025-05-02 11:31:19 +02:00
|
|
|
ptr += copying;
|
|
|
|
|
offset += copying;
|
|
|
|
|
self->nput += copying;
|
|
|
|
|
remain -= copying;
|
2022-01-10 22:56:39 +08:00
|
|
|
|
2024-08-19 01:06:33 +08:00
|
|
|
if (offset == sectorsize)
|
2022-01-10 22:56:39 +08:00
|
|
|
{
|
2024-08-19 01:06:33 +08:00
|
|
|
ret = inode->u.i_bops->write(inode, stream->cache, sector, 1);
|
2022-01-10 22:56:39 +08:00
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-19 01:06:33 +08:00
|
|
|
else if (remain < sectorsize)
|
2022-01-10 22:56:39 +08:00
|
|
|
{
|
2024-11-08 20:51:23 +08:00
|
|
|
/* Read sector back to keep as more as possible old data */
|
|
|
|
|
|
|
|
|
|
ret = inode->u.i_bops->read(inode, stream->cache, sector, 1);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2024-08-19 01:19:29 +08:00
|
|
|
|
2022-01-10 22:56:39 +08:00
|
|
|
memcpy(stream->cache, ptr, remain);
|
2023-09-10 12:32:34 -07:00
|
|
|
self->nput += remain;
|
2022-01-10 22:56:39 +08:00
|
|
|
remain = 0;
|
|
|
|
|
}
|
2024-08-19 01:24:07 +08:00
|
|
|
else
|
2022-01-10 22:56:39 +08:00
|
|
|
{
|
2024-08-19 01:24:07 +08:00
|
|
|
size_t nsector = remain / sectorsize;
|
2025-05-02 11:31:19 +02:00
|
|
|
size_t copying = nsector * sectorsize;
|
2022-01-10 22:56:39 +08:00
|
|
|
|
2024-08-19 01:24:07 +08:00
|
|
|
ret = inode->u.i_bops->write(inode, ptr, sector, nsector);
|
2022-01-10 22:56:39 +08:00
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 11:31:19 +02:00
|
|
|
ptr += copying;
|
|
|
|
|
self->nput += copying;
|
|
|
|
|
remain -= copying;
|
2022-01-10 22:56:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 14:51:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: blkoutstream_putc
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2024-11-08 20:59:08 +08:00
|
|
|
static void blkoutstream_putc(FAR struct lib_sostream_s *self, int ch)
|
2022-11-28 14:51:52 +08:00
|
|
|
{
|
|
|
|
|
char tmp = ch;
|
2023-09-10 12:32:34 -07:00
|
|
|
blkoutstream_puts(self, &tmp, 1);
|
2022-11-28 14:51:52 +08:00
|
|
|
}
|
|
|
|
|
|
2022-01-10 22:56:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Public Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: lib_blkoutstream_close
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* close block driver stream backend
|
|
|
|
|
*
|
|
|
|
|
* Input Parameters:
|
|
|
|
|
* stream - User allocated, uninitialized instance of struct
|
|
|
|
|
* lib_blkoutstream_s to be initialized.
|
|
|
|
|
*
|
|
|
|
|
* Returned Value:
|
|
|
|
|
* None (User allocated instance initialized).
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
void lib_blkoutstream_close(FAR struct lib_blkoutstream_s *stream)
|
|
|
|
|
{
|
2022-04-14 07:23:18 +08:00
|
|
|
if (stream != NULL)
|
2022-01-10 22:56:39 +08:00
|
|
|
{
|
2022-04-14 07:23:18 +08:00
|
|
|
if (stream->cache != NULL)
|
2022-01-10 22:56:39 +08:00
|
|
|
{
|
2024-11-08 12:13:55 +08:00
|
|
|
blkoutstream_flush(&stream->common);
|
2022-01-10 22:56:39 +08:00
|
|
|
lib_free(stream->cache);
|
|
|
|
|
stream->cache = NULL;
|
|
|
|
|
}
|
2025-01-09 22:33:11 +08:00
|
|
|
|
|
|
|
|
if (stream->inode != NULL)
|
|
|
|
|
{
|
|
|
|
|
close_blockdriver(stream->inode);
|
|
|
|
|
stream->inode = NULL;
|
|
|
|
|
}
|
2022-01-10 22:56:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: lib_blkoutstream_open
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* block driver stream backend
|
|
|
|
|
*
|
|
|
|
|
* Input Parameters:
|
|
|
|
|
* stream - User allocated, uninitialized instance of struct
|
|
|
|
|
* lib_blkoutstream_s to be initialized.
|
|
|
|
|
* name - The full path to the block driver to be opened.
|
|
|
|
|
*
|
|
|
|
|
* Returned Value:
|
|
|
|
|
* Returns zero on success or a negated errno on failure
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int lib_blkoutstream_open(FAR struct lib_blkoutstream_s *stream,
|
|
|
|
|
FAR const char *name)
|
|
|
|
|
{
|
|
|
|
|
FAR struct inode *inode = NULL;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if (stream == NULL || name == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = open_blockdriver(name, 0, &inode);
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(stream, 0, sizeof(*stream));
|
|
|
|
|
|
|
|
|
|
if (inode->u.i_bops->geometry == NULL ||
|
|
|
|
|
inode->u.i_bops->write == NULL ||
|
|
|
|
|
inode->u.i_bops->geometry(inode, &stream->geo) < 0 ||
|
|
|
|
|
stream->geo.geo_sectorsize <= 0 ||
|
|
|
|
|
stream->geo.geo_nsectors <= 0)
|
|
|
|
|
{
|
|
|
|
|
close_blockdriver(inode);
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream->cache = lib_malloc(stream->geo.geo_sectorsize);
|
|
|
|
|
if (stream->cache == NULL)
|
|
|
|
|
{
|
|
|
|
|
close_blockdriver(inode);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream->inode = inode;
|
2023-09-10 12:32:34 -07:00
|
|
|
stream->common.putc = blkoutstream_putc;
|
|
|
|
|
stream->common.puts = blkoutstream_puts;
|
|
|
|
|
stream->common.flush = blkoutstream_flush;
|
2024-11-08 20:59:08 +08:00
|
|
|
stream->common.seek = blkoutstream_seek;
|
2022-01-10 22:56:39 +08:00
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
#endif
|