* @ingroup group_fs_file_descriptor * * @brief Allocate a new file descriptor in the file descriptor table * * @param[in,out] fdt Pointer to the file descriptor table to allocate from * * @return int The allocated file descriptor index if successful (>= 0), * -RT_ENOSYS if filesystem lock failed, * -1 if allocation failed (no empty slots or memory allocation failed) */
| 296 | * -1 if allocation failed (no empty slots or memory allocation failed) |
| 297 | */ |
| 298 | int fdt_fd_new(struct dfs_fdtable *fdt) |
| 299 | { |
| 300 | int idx = -1; |
| 301 | |
| 302 | /* lock filesystem */ |
| 303 | if (dfs_file_lock() != RT_EOK) |
| 304 | { |
| 305 | return -RT_ENOSYS; |
| 306 | } |
| 307 | |
| 308 | /* find an empty fd entry */ |
| 309 | idx = _fdt_fd_alloc(fdt, (fdt == &_fdtab) ? DFS_STDIO_OFFSET : 0); |
| 310 | /* can't find an empty fd entry */ |
| 311 | if (idx < 0) |
| 312 | { |
| 313 | LOG_E("DFS fd new is failed! Could not found an empty fd entry."); |
| 314 | } |
| 315 | else if (!fdt->fds[idx]) |
| 316 | { |
| 317 | struct dfs_file *file; |
| 318 | |
| 319 | file = dfs_file_create(); |
| 320 | |
| 321 | if (file) |
| 322 | { |
| 323 | fdt->fds[idx] = file; |
| 324 | |
| 325 | LOG_D("allocate a new fd @ %d", idx); |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | fdt->fds[idx] = RT_NULL; |
| 330 | idx = -1; |
| 331 | } |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | LOG_E("DFS not found an empty fds entry."); |
| 336 | idx = -1; |
| 337 | } |
| 338 | |
| 339 | dfs_file_unlock(); |
| 340 | |
| 341 | return idx; |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * @brief Release a file descriptor from the file descriptor table |
no test coverage detected