@@ -50,7 +50,7 @@ static struct dfs_fdtable _fdtab = {0};
5050 * @param[in] fd The file descriptor that needs to be accommodated
5151 *
5252 * @return int The input file descriptor if successful (fd >= 0),
53- * -1 if expansion failed or fd exceeds maximum allowed value (DFS_FD_MAX)
53+ * -EMFILE if the descriptor limit is reached, or -ENOMEM if allocation fails
5454 *
5555 * @note Expand table size to next multiple of 4 (but not exceeding DFS_FD_MAX)
5656 */
@@ -66,7 +66,7 @@ static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd)
6666 }
6767 if (fd >= DFS_FD_MAX )
6868 {
69- return -1 ;
69+ return - EMFILE ;
7070 }
7171
7272 nr = ((fd + 4 ) & ~3 );
@@ -77,7 +77,7 @@ static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd)
7777 fds = (struct dfs_file * * )rt_realloc (fdt -> fds , nr * sizeof (struct dfs_file * ));
7878 if (!fds )
7979 {
80- return -1 ;
80+ return - ENOMEM ;
8181 }
8282
8383 /* clean the new allocated fds */
@@ -98,7 +98,7 @@ static int _fdt_slot_expand(struct dfs_fdtable *fdt, int fd)
9898 * @param[in] startfd The starting file descriptor index to begin searching
9999 *
100100 * @return int The allocated file descriptor index if successful (>= 0),
101- * -1 if allocation failed (table expansion failed)
101+ * -EMFILE if the descriptor limit is reached, or -ENOMEM if allocation fails
102102 *
103103 * @note If no empty slot found, expand the table and return the new index.
104104 */
@@ -121,12 +121,7 @@ static int _fdt_slot_alloc(struct dfs_fdtable *fdt, int startfd)
121121 idx = startfd ;
122122 }
123123
124- if (_fdt_slot_expand (fdt , idx ) < 0 )
125- {
126- return -1 ;
127- }
128-
129- return idx ;
124+ return _fdt_slot_expand (fdt , idx );
130125}
131126
132127/**
@@ -136,7 +131,7 @@ static int _fdt_slot_alloc(struct dfs_fdtable *fdt, int startfd)
136131 * @param[in] startfd The starting file descriptor index to begin searching
137132 *
138133 * @return int The allocated file descriptor index if successful (>= 0),
139- * -1 if allocation failed
134+ * -EMFILE if the descriptor limit is reached, or -ENOMEM if allocation fails
140135 *
141136 * @note This is a wrapper function that calls _fdt_slot_alloc() to perform
142137 * the actual allocation. It maintains the same behavior as _fdt_slot_alloc().
@@ -294,51 +289,43 @@ void dfs_file_destroy(struct dfs_file *file)
294289 *
295290 * @return int The allocated file descriptor index if successful (>= 0),
296291 * -RT_ENOSYS if filesystem lock failed,
297- * -1 if allocation failed (no empty slots or memory allocation failed)
292+ * -1 if allocation failed. On failure, errno is set to the cause.
298293 */
299294int fdt_fd_new (struct dfs_fdtable * fdt )
300295{
301- int idx = -1 ;
296+ int idx ;
302297
303298 /* lock filesystem */
304299 if (dfs_file_lock () != RT_EOK )
305300 {
301+ rt_set_errno (ENOSYS );
306302 return - RT_ENOSYS ;
307303 }
308304
309305 /* find an empty fd entry */
310306 idx = _fdt_fd_alloc (fdt , (fdt == & _fdtab ) ? DFS_STDIO_OFFSET : 0 );
311- /* can't find an empty fd entry */
312- if (idx < 0 )
307+ if (idx >= 0 )
313308 {
314- LOG_E ("DFS fd new is failed! Could not found an empty fd entry." );
315- }
316- else if (!fdt -> fds [idx ])
317- {
318- struct dfs_file * file ;
319-
320- file = dfs_file_create ();
309+ struct dfs_file * file = dfs_file_create ();
321310
322311 if (file )
323312 {
324313 fdt -> fds [idx ] = file ;
325-
326314 LOG_D ("allocate a new fd @ %d" , idx );
327315 }
328316 else
329317 {
330- fdt -> fds [idx ] = RT_NULL ;
331- idx = -1 ;
318+ idx = - ENOMEM ;
332319 }
333320 }
334- else
335- {
336- LOG_E ("DFS not found an empty fds entry." );
337- idx = -1 ;
338- }
339321
340322 dfs_file_unlock ();
341-
323+ if (idx < 0 )
324+ {
325+ LOG_E ("Failed to allocate a file descriptor." );
326+ rt_set_errno (- idx );
327+ return -1 ;
328+ }
342329 return idx ;
343330}
344331
@@ -459,7 +446,7 @@ int fdt_fd_associate_file(struct dfs_fdtable *fdt, int fd, struct dfs_file *file
459446 * @brief Allocate a new file descriptor in current process's file descriptor table
460447 *
461448 * @return int The allocated file descriptor index if successful (>= 0),
462- * -1 if allocation failed
449+ * a negative value with errno set on failure
463450 */
464451int fd_new (void )
465452{
@@ -814,7 +801,7 @@ int dfs_dup(int oldfd, int startfd)
814801 }
815802exit :
816803 dfs_file_unlock ();
817- return newfd ;
804+ return newfd < 0 ? -1 : newfd ;
818805}
819806
820807/**
@@ -862,7 +849,7 @@ int dfs_dup_to(int oldfd, struct dfs_fdtable *fdtab)
862849exit :
863850 dfs_file_unlock ();
864851
865- return newfd ;
852+ return newfd < 0 ? -1 : newfd ;
866853}
867854
868855/**
0 commit comments