xiaoxiang781216 commented on PR #10602: URL: https://github.com/apache/nuttx/pull/10602#issuecomment-1719917277
> I once had a look at that to handle a `fomempen` and alike properly, it would require that LIBC `struct FILE` not to be a direct alias to internal FS struct, as indeed you need to keep a reference to fs operation callbacks and permission flags, so it would require an indirection where `struct FILE` holds a private pointer to internal. Meanwhile, I think LIBC stdio would benefit at term that indirection to be made. > > ```c > struct libc_stdio_file > { > // ... stdio specifics; > libc_stdio_fread_t read; > libc_stdio_fwrite_t write; > libc_stdio_fseek_t seek; > libc_stdio_fclose_t close; > libc_stdio_cookie_t cookie; > > file_struct * internal; > } > > typedef libc_stdio_file FILE; > ``` we don't need add a new struct libc_stdio_file. Here is the current definition: ``` struct file_struct { FAR struct file_struct *fs_next; /* Pointer to next file stream */ rmutex_t fs_lock; /* Recursive lock */ int fs_fd; /* File descriptor associated with stream */ #ifndef CONFIG_STDIO_DISABLE_BUFFERING FAR unsigned char *fs_bufstart; /* Pointer to start of buffer */ FAR unsigned char *fs_bufend; /* Pointer to 1 past end of buffer */ FAR unsigned char *fs_bufpos; /* Current position in buffer */ FAR unsigned char *fs_bufread; /* Pointer to 1 past last buffered read char. */ # if CONFIG_STDIO_BUFFER_SIZE > 0 unsigned char fs_buffer[CONFIG_STDIO_BUFFER_SIZE]; # endif #endif uint16_t fs_oflags; /* Open mode flags */ uint8_t fs_flags; /* Stream flags */ #if CONFIG_NUNGET_CHARS > 0 uint8_t fs_nungotten; /* The number of characters buffered for ungetc */ unsigned char fs_ungotten[CONFIG_NUNGET_CHARS]; #endif }; typedef struct file_struct FILE; ``` Here is the simple change to the struct: ``` struct file_struct { ... - int fs_fd; /* File descriptor associated with stream */ + cookie_io_functions_t fs_iofunc; + FAR void * fs_cookie; ``` }; ``` and replace all read/write/seek/close with stream->iofunc.read/write/seek/close. The remaining work is adding the simple wrapper for fopen: ``` static ssize_t read_fd(void *cookie, char *buf, size_t size) { int fd = (int)(intptr_t)cookie; return read(fd, buf, size); } static ssize_t write(void *cookie, const char *buf, size_t size) { int fd = (int)(intptr_t)cookie; return write(fd, buf, size); } static int seek(void *cookie, off_t *offset, int whence) { int fd = (int)(intptr_t)cookie; off_t ret = seek(fd, *offset, whence); if (ret >= 0) { *offset = ret; ret = 0; } return ret; } ``` -- 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: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org