2012-11-11 00:06:01 +08:00
|
|
|
/****************************************************************************
|
2018-05-30 03:21:26 +08:00
|
|
|
* libs/libc/stdio/lib_libflushall.c
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
2021-03-02 22:54:21 +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
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
2021-03-02 22:54:21 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
2021-03-02 22:54:21 +08: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.
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2024-03-15 15:34:30 +08:00
|
|
|
#include <nuttx/nuttx.h>
|
2012-11-11 00:06:01 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
2015-12-30 07:31:17 +08:00
|
|
|
#include "libc.h"
|
2012-11-11 00:06:01 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 06:30:35 +08:00
|
|
|
* Public Functions
|
2012-11-11 00:06:01 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_flushall
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called either (1) by the OS when a task exits, or (2) from fflush()
|
|
|
|
* when a NULL stream argument is provided.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-10-22 03:59:06 +08:00
|
|
|
int lib_flushall_unlocked(FAR struct streamlist *list)
|
|
|
|
{
|
2024-03-15 15:34:30 +08:00
|
|
|
FAR sq_entry_t *entry;
|
2023-10-22 03:59:06 +08:00
|
|
|
int lasterrno = OK;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Make sure that there are streams associated with this thread */
|
|
|
|
|
|
|
|
if (list != NULL)
|
|
|
|
{
|
|
|
|
FAR FILE *stream;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Process each stream in the thread's stream list */
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
lib_fflush_unlocked(&list->sl_std[i]);
|
|
|
|
}
|
|
|
|
|
2024-03-15 15:34:30 +08:00
|
|
|
sq_for_every(&list->sl_queue, entry)
|
2023-10-22 03:59:06 +08:00
|
|
|
{
|
2024-03-15 15:34:30 +08:00
|
|
|
stream = container_of(entry, struct file_struct, fs_entry);
|
|
|
|
|
2023-10-22 03:59:06 +08:00
|
|
|
/* If the stream is opened for writing, then flush all of
|
|
|
|
* the pending write data in the stream.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((stream->fs_oflags & O_WROK) != 0)
|
|
|
|
{
|
|
|
|
/* Flush the writable FILE */
|
|
|
|
|
|
|
|
ret = lib_fflush_unlocked(stream);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* An error occurred during the flush AND/OR we were unable
|
|
|
|
* to flush all of the buffered write data. Remember the
|
|
|
|
* last errcode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lasterrno = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If any flush failed, return the errorcode of the last failed flush */
|
|
|
|
|
|
|
|
return lasterrno;
|
|
|
|
}
|
|
|
|
|
2012-11-11 00:06:01 +08:00
|
|
|
int lib_flushall(FAR struct streamlist *list)
|
|
|
|
{
|
2024-03-15 15:34:30 +08:00
|
|
|
FAR sq_entry_t *entry;
|
2012-11-11 00:06:01 +08:00
|
|
|
int lasterrno = OK;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Make sure that there are streams associated with this thread */
|
|
|
|
|
2023-10-22 03:59:06 +08:00
|
|
|
if (list != NULL)
|
2012-11-11 00:06:01 +08:00
|
|
|
{
|
2020-08-13 18:17:29 +08:00
|
|
|
FAR FILE *stream;
|
2022-05-09 22:19:31 +08:00
|
|
|
int i;
|
2015-10-12 21:45:02 +08:00
|
|
|
|
|
|
|
/* Process each stream in the thread's stream list */
|
|
|
|
|
2022-11-10 01:20:42 +08:00
|
|
|
nxmutex_lock(&list->sl_lock);
|
2022-05-09 22:19:31 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
2023-10-22 04:47:22 +08:00
|
|
|
lib_fflush(&list->sl_std[i]);
|
2022-05-09 22:19:31 +08:00
|
|
|
}
|
|
|
|
|
2024-03-15 15:34:30 +08:00
|
|
|
sq_for_every(&list->sl_queue, entry)
|
2015-10-12 21:45:02 +08:00
|
|
|
{
|
2024-03-15 15:34:30 +08:00
|
|
|
stream = container_of(entry, struct file_struct, fs_entry);
|
|
|
|
|
2020-08-13 18:17:29 +08:00
|
|
|
/* If the stream is opened for writing, then flush all of
|
|
|
|
* the pending write data in the stream.
|
2015-10-12 21:45:02 +08:00
|
|
|
*/
|
|
|
|
|
2020-08-13 18:17:29 +08:00
|
|
|
if ((stream->fs_oflags & O_WROK) != 0)
|
2015-10-12 21:45:02 +08:00
|
|
|
{
|
|
|
|
/* Flush the writable FILE */
|
|
|
|
|
2023-10-22 04:47:22 +08:00
|
|
|
ret = lib_fflush(stream);
|
2015-10-12 21:45:02 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* An error occurred during the flush AND/OR we were unable
|
|
|
|
* to flush all of the buffered write data. Remember the
|
|
|
|
* last errcode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lasterrno = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 01:20:42 +08:00
|
|
|
nxmutex_unlock(&list->sl_lock);
|
2012-11-11 00:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If any flush failed, return the errorcode of the last failed flush */
|
|
|
|
|
|
|
|
return lasterrno;
|
|
|
|
}
|