libc: add support for custom streams with fopencookie()

This commit adds support for custom stream via fopencookie function.
The function allows the programmer the create his own custom stream
for IO operations and hook his custom functions to it.

This is a non POSIX interface defined in Standard C library and implemented
according to it. The only difference is in usage of off_t instead of
off64_t. Programmer can use 64 bits offset if CONFIG_FS_LARGEFILE is
enabled. In that case off_t is defined as int64_t (int32_t otherwise).

Field fs_fd is removed from file_struct and fs_cookie is used instead
as a shared variable for file descriptor or user defined cookie.

The interface will be useful for future fmemopen implementation.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
This commit is contained in:
Michal Lenc
2023-08-27 21:33:44 +02:00
committed by Xiang Xiao
parent 083c87b39a
commit 0a107ca6d9
18 changed files with 347 additions and 49 deletions
+17 -16
View File
@@ -115,32 +115,33 @@ int fclose(FAR FILE *stream)
nxmutex_unlock(&slist->sl_lock);
/* Check that the underlying file descriptor corresponds to an an open
* file.
*/
/* Call user custom callback if it is not NULL. */
if (stream->fs_fd >= 0)
if (stream->fs_iofunc.close != NULL)
{
/* Close the file descriptor and save the return status */
status = stream->fs_iofunc.close(stream->fs_cookie);
}
else
{
int fd = (int)(intptr_t)stream->fs_cookie;
#ifdef CONFIG_FDSAN
uint64_t tag;
tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_FILE,
(uintptr_t)stream);
status = android_fdsan_close_with_tag(stream->fs_fd, tag);
status = android_fdsan_close_with_tag(fd, tag);
#else
status = close(stream->fs_fd);
status = close(fd);
#endif
}
/* If close() returns an error but flush() did not then make sure
* that we return the close() error condition.
*/
/* If close() returns an error but flush() did not then make sure
* that we return the close() error condition.
*/
if (ret == OK)
{
ret = status;
errcode = get_errno();
}
if (ret == OK)
{
ret = status;
errcode = get_errno();
}
#ifndef CONFIG_STDIO_DISABLE_BUFFERING