11/*
2- * Copyright (c) 2006-2022, RT-Thread Development Team
2+ * Copyright (c) 2006-2024 RT-Thread Development Team
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 *
@@ -107,7 +107,8 @@ int dfs_init(void)
107107INIT_PREV_EXPORT (dfs_init );
108108
109109/**
110- * this function will lock device file system.
110+ * @brief this function will lock device file system.
111+ * this lock (fslock) is used for protecting filesystem_operation_table and filesystem_table.
111112 *
112113 * @note please don't invoke it on ISR.
113114 */
@@ -126,6 +127,12 @@ void dfs_lock(void)
126127 }
127128}
128129
130+ /**
131+ * @brief this function will lock file descriptors.
132+ * this lock (fdlock) is used for protecting fd table (_fdtab).
133+ *
134+ * @note please don't invoke it on ISR.
135+ */
129136void dfs_file_lock (void )
130137{
131138 rt_err_t result = - RT_EBUSY ;
@@ -142,7 +149,7 @@ void dfs_file_lock(void)
142149}
143150
144151/**
145- * this function will lock device file system.
152+ * @brief this function will unlock device file system.
146153 *
147154 * @note please don't invoke it on ISR.
148155 */
@@ -151,33 +158,56 @@ void dfs_unlock(void)
151158 rt_mutex_release (& fslock );
152159}
153160
154- #ifdef DFS_USING_POSIX
155-
161+ /**
162+ * @brief this function will unlock fd table.
163+ */
156164void dfs_file_unlock (void )
157165{
158166 rt_mutex_release (& fdlock );
159167}
160168
169+ #ifdef DFS_USING_POSIX
170+ /**
171+ * @brief Expand the file descriptor table to accommodate a specific file descriptor.
172+ *
173+ * This function ensures that the file descriptor table in the given `dfs_fdtable` structure
174+ * has sufficient capacity to include the specified file descriptor `fd`. If the table
175+ * needs to be expanded, it reallocates memory and initializes new slots to `NULL`.
176+ *
177+ * @param fdt Pointer to the `dfs_fdtable` structure representing the file descriptor table.
178+ * @param fd The file descriptor that the table must accommodate.
179+ * @return int
180+ * - The input file descriptor `fd` if it is within the current or newly expanded table's capacity.
181+ * - `-1` if the requested file descriptor exceeds `DFS_FD_MAX` or memory allocation fails.
182+ */
161183static int fd_slot_expand (struct dfs_fdtable * fdt , int fd )
162184{
163185 int nr ;
164186 int index ;
165187 struct dfs_file * * fds = NULL ;
166188
189+ /* If the file descriptor is already within the current capacity, no expansion is needed.*/
167190 if (fd < fdt -> maxfd )
168191 {
169192 return fd ;
170193 }
194+
195+ /* If the file descriptor exceeds the maximum allowable limit, return an error.*/
171196 if (fd >= DFS_FD_MAX )
172197 {
173198 return -1 ;
174199 }
175200
201+ /* Calculate the new capacity, rounding up to the nearest multiple of 4.*/
176202 nr = ((fd + 4 ) & ~3 );
203+
204+ /* Ensure the new capacity does not exceed the maximum limit.*/
177205 if (nr > DFS_FD_MAX )
178206 {
179207 nr = DFS_FD_MAX ;
180208 }
209+
210+ /* Attempt to reallocate the file descriptor table to the new capacity.*/
181211 fds = (struct dfs_file * * )rt_realloc (fdt -> fds , nr * sizeof (struct dfs_file * ));
182212 if (!fds )
183213 {
@@ -189,12 +219,23 @@ static int fd_slot_expand(struct dfs_fdtable *fdt, int fd)
189219 {
190220 fds [index ] = NULL ;
191221 }
222+
223+ /* Update the file descriptor table and its capacity.*/
192224 fdt -> fds = fds ;
193225 fdt -> maxfd = nr ;
194226
195227 return fd ;
196228}
197229
230+ /**
231+ * @brief Allocate a file descriptor slot starting from a specified index.
232+ *
233+ * @param fdt fdt Pointer to the `dfs_fdtable` structure representing the file descriptor table.
234+ * @param startfd The starting index for the search for an empty slot.
235+ * @return int
236+ * - The index of the first available slot if successful.
237+ * - `-1` if no slot is available or if table expansion fails
238+ */
198239static int fd_slot_alloc (struct dfs_fdtable * fdt , int startfd )
199240{
200241 int idx ;
@@ -219,6 +260,17 @@ static int fd_slot_alloc(struct dfs_fdtable *fdt, int startfd)
219260 }
220261 return idx ;
221262}
263+
264+ /**
265+ * @brief Allocate a new file descriptor and associate it with a newly allocated `struct dfs_file`.
266+ *
267+ * @param fdt Pointer to the `dfs_fdtable` structure representing the file descriptor table.
268+ * @param startfd The starting index for searching an available file descriptor slot.
269+ *
270+ * @return
271+ * - The index of the allocated file descriptor if successful.
272+ * - `-1` if no slot is available or memory allocation fails.
273+ */
222274static int fd_alloc (struct dfs_fdtable * fdt , int startfd )
223275{
224276 int idx ;
@@ -323,7 +375,11 @@ struct dfs_file *fd_get(int fd)
323375/**
324376 * @ingroup Fd
325377 *
326- * This function will put the file descriptor.
378+ * @brief This function will release the file descriptor.
379+ *
380+ * This function releases a file descriptor slot in the file descriptor table, decrements reference
381+ * counts, and cleans up resources associated with the `dfs_file` and `dfs_vnode` structures when applicable.
382+ *
327383 */
328384void fdt_fd_release (struct dfs_fdtable * fdt , int fd )
329385{
@@ -378,6 +434,20 @@ void fd_release(int fd)
378434 fdt_fd_release (fdt , fd );
379435}
380436
437+ /**
438+ * @brief Duplicates a file descriptor.
439+ *
440+ * This function duplicates an existing file descriptor (`oldfd`) and returns
441+ * a new file descriptor that refers to the same underlying file object.
442+ *
443+ * @param oldfd The file descriptor to duplicate. It must be a valid file
444+ * descriptor within the range of allocated descriptors.
445+ *
446+ * @return The new file descriptor if successful, or a negative value
447+ * (e.g., -1) if an error occurs.
448+ *
449+ * @see sys_dup2()
450+ */
381451rt_err_t sys_dup (int oldfd )
382452{
383453 int newfd = -1 ;
@@ -470,6 +540,23 @@ int fd_is_open(const char *pathname)
470540 return -1 ;
471541}
472542
543+ /**
544+ * @brief Duplicates a file descriptor to a specified file descriptor.
545+ *
546+ * This function duplicates an existing file descriptor (`oldfd`) and assigns it
547+ * to the specified file descriptor (`newfd`).
548+ *
549+ * @param oldfd The file descriptor to duplicate. It must be a valid and open file
550+ * descriptor within the range of allocated descriptors.
551+ * @param newfd The target file descriptor. If `newfd` is already in use, it will
552+ * be closed before duplication. If `newfd` exceeds the current file
553+ * descriptor table size, the table will be expanded to accommodate it.
554+ *
555+ * @return The value of `newfd` on success, or a negative value (e.g., -1) if an
556+ * error occurs.
557+ *
558+ * @see sys_dup()
559+ */
473560rt_err_t sys_dup2 (int oldfd , int newfd )
474561{
475562 struct dfs_fdtable * fdt = NULL ;
@@ -550,6 +637,10 @@ static int fd_get_fd_index_form_fdt(struct dfs_fdtable *fdt, struct dfs_file *fi
550637 return fd ;
551638}
552639
640+ /**
641+ * @brief get fd (index) by dfs file object.
642+ *
643+ */
553644int fd_get_fd_index (struct dfs_file * file )
554645{
555646 struct dfs_fdtable * fdt ;
@@ -558,6 +649,21 @@ int fd_get_fd_index(struct dfs_file *file)
558649 return fd_get_fd_index_form_fdt (fdt , file );
559650}
560651
652+ /**
653+ * @brief Associates a file descriptor with a file object.
654+ *
655+ * This function associates a given file descriptor (`fd`) with a specified
656+ * file object (`file`) in the file descriptor table (`fdt`).
657+ *
658+ * @param fdt The file descriptor table to operate on. It must be a valid
659+ * and initialized `dfs_fdtable` structure.
660+ * @param fd The file descriptor to associate. It must be within the range
661+ * of allocated file descriptors and currently unoccupied.
662+ * @param file The file object to associate with the file descriptor. It must
663+ * be a valid and initialized `dfs_file` structure.
664+ *
665+ * @return The value of `fd` on success, or -1 if an error occurs.
666+ */
561667int fd_associate (struct dfs_fdtable * fdt , int fd , struct dfs_file * file )
562668{
563669 int retfd = -1 ;
@@ -591,6 +697,10 @@ int fd_associate(struct dfs_fdtable *fdt, int fd, struct dfs_file *file)
591697 return retfd ;
592698}
593699
700+ /**
701+ * @brief initialize a dfs file object.
702+ *
703+ */
594704void fd_init (struct dfs_file * fd )
595705{
596706 if (fd )
0 commit comments