xiaoxiang781216 commented on code in PR #6066: URL: https://github.com/apache/incubator-nuttx/pull/6066#discussion_r850354431
########## libs/libc/stream/lib_blkoutstream.c: ########## @@ -0,0 +1,225 @@ +/**************************************************************************** + * libs/libc/stream/lib_blkoutstream.c + * + * 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 + ****************************************************************************/ + +static int blkoutstream_flush(FAR struct lib_outstream_s *this) +{ + FAR struct lib_blkoutstream_s *stream = + (FAR struct lib_blkoutstream_s *)this; + size_t sectorsize = stream->geo.geo_sectorsize; + int ret = OK; + + if (this->nput % sectorsize > 0) + { + ret = stream->inode->u.i_bops->write(stream->inode, stream->cache, + this->nput / sectorsize, 1); + } + + return ret; +} + +/**************************************************************************** + * Name: blkoutstream_puts + ****************************************************************************/ + +static int blkoutstream_puts(FAR struct lib_outstream_s *this, + FAR const void *buf, int len) +{ + FAR struct lib_blkoutstream_s *stream = + (FAR struct lib_blkoutstream_s *)this; + size_t sectorsize = stream->geo.geo_sectorsize; + FAR struct inode *inode = stream->inode; + FAR const unsigned char *ptr = buf; + size_t remain = len; + int ret; + + while (remain > 0) + { + size_t sblock = this->nput / sectorsize; + size_t offset = this->nput % sectorsize; + + if (offset > 0) + { + size_t copyin = offset + remain > sectorsize ? + sectorsize - offset : remain; + + memcpy(stream->cache + offset, ptr, copyin); + + ptr += copyin; + offset += copyin; + this->nput += copyin; + remain -= copyin; + + if (offset == stream->geo.geo_sectorsize) + { + ret = inode->u.i_bops->write(inode, stream->cache, sblock, 1); + if (ret < 0) + { + return ret; + } + } + } + else if (remain < stream->geo.geo_sectorsize) + { + memcpy(stream->cache, ptr, remain); + this->nput += remain; + remain = 0; + } + else if (remain >= stream->geo.geo_sectorsize) + { + size_t copyin = (remain / stream->geo.geo_sectorsize) * + stream->geo.geo_sectorsize; + + ret = inode->u.i_bops->write(inode, ptr, sblock, + remain / stream->geo.geo_sectorsize); + if (ret < 0) + { + return ret; + } + + ptr += copyin; + this->nput += copyin; + remain -= copyin; + } + } + + return len; +} + +/**************************************************************************** + * 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) +{ + if (stream) + { + if (stream->inode) + { + close_blockdriver(stream->inode); + stream->inode = NULL; + } + + if (stream->cache) Review Comment: Done. ########## libs/libc/stream/lib_blkoutstream.c: ########## @@ -0,0 +1,225 @@ +/**************************************************************************** + * libs/libc/stream/lib_blkoutstream.c + * + * 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 + ****************************************************************************/ + +static int blkoutstream_flush(FAR struct lib_outstream_s *this) +{ + FAR struct lib_blkoutstream_s *stream = + (FAR struct lib_blkoutstream_s *)this; + size_t sectorsize = stream->geo.geo_sectorsize; + int ret = OK; + + if (this->nput % sectorsize > 0) + { + ret = stream->inode->u.i_bops->write(stream->inode, stream->cache, + this->nput / sectorsize, 1); + } + + return ret; +} + +/**************************************************************************** + * Name: blkoutstream_puts + ****************************************************************************/ + +static int blkoutstream_puts(FAR struct lib_outstream_s *this, + FAR const void *buf, int len) +{ + FAR struct lib_blkoutstream_s *stream = + (FAR struct lib_blkoutstream_s *)this; + size_t sectorsize = stream->geo.geo_sectorsize; + FAR struct inode *inode = stream->inode; + FAR const unsigned char *ptr = buf; + size_t remain = len; + int ret; + + while (remain > 0) + { + size_t sblock = this->nput / sectorsize; + size_t offset = this->nput % sectorsize; + + if (offset > 0) + { + size_t copyin = offset + remain > sectorsize ? + sectorsize - offset : remain; + + memcpy(stream->cache + offset, ptr, copyin); + + ptr += copyin; + offset += copyin; + this->nput += copyin; + remain -= copyin; + + if (offset == stream->geo.geo_sectorsize) + { + ret = inode->u.i_bops->write(inode, stream->cache, sblock, 1); + if (ret < 0) + { + return ret; + } + } + } + else if (remain < stream->geo.geo_sectorsize) + { + memcpy(stream->cache, ptr, remain); + this->nput += remain; + remain = 0; + } + else if (remain >= stream->geo.geo_sectorsize) + { + size_t copyin = (remain / stream->geo.geo_sectorsize) * + stream->geo.geo_sectorsize; + + ret = inode->u.i_bops->write(inode, ptr, sblock, + remain / stream->geo.geo_sectorsize); + if (ret < 0) + { + return ret; + } + + ptr += copyin; + this->nput += copyin; + remain -= copyin; + } + } + + return len; +} + +/**************************************************************************** + * 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) +{ + if (stream) + { + if (stream->inode) Review Comment: Done. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
