mirror of
https://github.com/apache/nuttx.git
synced 2026-05-10 15:30:25 +08:00
8c95e9e05c
[<0x60ddce>] __assert+0x1d/0x5c [<0x60f2be>] android_fdsan_exchange_owner_tag+0x71/0xc0 [<0x60a14e>] close+0x19/0x64 [<0xb77b1c>] freopen+0x47/0xd8 [<0x86fe54>] test_nuttx_fs_stream01+0x4f/0x1a4 [<0xcef54e>] cmocka_run_one_test_or_fixture+0xe5/0x3cc [<0xcefd26>] _cmocka_run_group_tests+0x41d/0xbd8 [<0x86cff2>] cmocka_fs_test_main+0x35/0x58 [<0x6a9d1a>] nxtask_startup+0x15/0x30 [<0x641e92>] nxtask_start+0x75/0x94 Signed-off-by: ligd <liguiding1@xiaomi.com>
186 lines
5.3 KiB
C
186 lines
5.3 KiB
C
/****************************************************************************
|
|
* libs/libc/stdio/lib_freopen.c
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* 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 <stdio.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
|
|
#ifdef CONFIG_FDSAN
|
|
# include <android/fdsan.h>
|
|
#endif
|
|
|
|
#include "libc.h"
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: freopen
|
|
*
|
|
* Description:
|
|
* Reuses stream to either open the file specified by path or to change
|
|
* its access mode.
|
|
*
|
|
* If a new path is specified, the function first attempts to close
|
|
* any file already associated with stream (third parameter) and
|
|
* disassociates it. Then, independently of whether that stream was
|
|
* successfully closed or not, freopen opens the file specified by path
|
|
* and associates it with the stream just as fopen would do using the
|
|
* specified mode.
|
|
*
|
|
* If path is a null pointer, the function attempts to change the mode
|
|
* of the stream. Although a particular library implementation is allowed
|
|
* to restrict the changes permitted, and under which circumstances.
|
|
*
|
|
* The error indicator and eof indicator are automatically cleared (as if
|
|
* clearerr was called).
|
|
*
|
|
* Input Parameters:
|
|
* path - If non-NULL, refers to the name of the file to be opened.
|
|
* mode - String describing the new file access mode
|
|
* stream - Pointer to the type FILE to be reopened.
|
|
*
|
|
* Returned Value:
|
|
* If the file is successfully reopened, the function returns the pointer
|
|
* passed as parameter stream, which can be used to identify the reopened
|
|
* stream. Otherwise, a null pointer is returned and the errno variable
|
|
* is also set to a system-specific error code on failure.
|
|
*
|
|
****************************************************************************/
|
|
|
|
FAR FILE *freopen(FAR const char *path, FAR const char *mode,
|
|
FAR FILE *stream)
|
|
{
|
|
int oflags;
|
|
int ret;
|
|
int fd;
|
|
|
|
/* Was a file name provided? */
|
|
|
|
if (path != NULL)
|
|
{
|
|
/* Yes, open the file directly if no stream is opened yet */
|
|
|
|
if (stream == NULL)
|
|
{
|
|
return fopen(path, mode);
|
|
}
|
|
|
|
/* Otherwise, open the file */
|
|
|
|
oflags = lib_mode2oflags(mode);
|
|
if (oflags < 0)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
/* Make sure that we have exclusive access to the stream */
|
|
|
|
flockfile(stream);
|
|
|
|
/* Flush the stream and invalidate the read buffer. */
|
|
|
|
lib_fflush_unlocked(stream);
|
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
|
lib_rdflush_unlocked(stream);
|
|
#endif
|
|
|
|
funlockfile(stream);
|
|
|
|
/* close the old fd */
|
|
|
|
#ifdef CONFIG_FDSAN
|
|
android_fdsan_close_with_tag(fileno(stream),
|
|
android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_FILE,
|
|
(uintptr_t)stream));
|
|
#else
|
|
close(fileno(stream));
|
|
#endif
|
|
|
|
/* Open the new file and reused the fd */
|
|
|
|
fd = open(path, oflags, 0666);
|
|
#ifdef CONFIG_FDSAN
|
|
android_fdsan_exchange_owner_tag(fd, 0,
|
|
android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_FILE,
|
|
(uintptr_t)stream));
|
|
#endif
|
|
flockfile(stream);
|
|
stream->fs_cookie = (FAR void *)(intptr_t)fd;
|
|
funlockfile(stream);
|
|
|
|
/* To clear the stale fd */
|
|
|
|
if (fd < 0)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
clearerr(stream);
|
|
return stream;
|
|
}
|
|
|
|
/* Otherwise, we are just changing the mode of the current file. */
|
|
|
|
if (stream)
|
|
{
|
|
/* Convert the mode string into standard file open mode flags. */
|
|
|
|
oflags = lib_mode2oflags(mode);
|
|
if (oflags < 0)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
/* Get the underlying file descriptor from the stream */
|
|
|
|
fd = fileno(stream);
|
|
if (fd < 0)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
/* Set the new file mode for the file descriptor */
|
|
|
|
ret = fcntl(fd, F_SETFL, oflags);
|
|
if (ret < 0)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
clearerr(stream);
|
|
return stream;
|
|
}
|
|
|
|
set_errno(EINVAL);
|
|
return NULL;
|
|
}
|