* @brief Dup the specified fd_src from fdt_src to fdt_dst. * * @param[out] fdt_dst is the fd table for destination, if empty, use global (_fdtab). * * @param[in] fdt_src is the fd table for source, if empty, use global (_fdtab). * * @param[in] fd_src is the fd in the designate fdt_src table. * * @return -1 on failed or the allocated file descriptor. */
| 567 | * @return -1 on failed or the allocated file descriptor. |
| 568 | */ |
| 569 | int dfs_fdtable_dup(struct dfs_fdtable *fdt_dst, struct dfs_fdtable *fdt_src, int fd_src) |
| 570 | { |
| 571 | int newfd = -1; |
| 572 | |
| 573 | if (dfs_file_lock() != RT_EOK) |
| 574 | { |
| 575 | return -RT_ENOSYS; |
| 576 | } |
| 577 | |
| 578 | if (fdt_src == NULL) |
| 579 | { |
| 580 | fdt_src = &_fdtab; |
| 581 | } |
| 582 | |
| 583 | if (fdt_dst == NULL) |
| 584 | { |
| 585 | fdt_dst = &_fdtab; |
| 586 | } |
| 587 | |
| 588 | /* check fd */ |
| 589 | if ((fd_src < 0) || (fd_src >= fdt_src->maxfd)) |
| 590 | { |
| 591 | goto _EXIT; |
| 592 | } |
| 593 | if (!fdt_src->fds[fd_src]) |
| 594 | { |
| 595 | goto _EXIT; |
| 596 | } |
| 597 | |
| 598 | /* get a new fd*/ |
| 599 | newfd = fdt_fd_new(fdt_dst); |
| 600 | if (newfd >= 0) |
| 601 | { |
| 602 | fdt_dst->fds[newfd]->mode = fdt_src->fds[fd_src]->mode; |
| 603 | fdt_dst->fds[newfd]->flags = fdt_src->fds[fd_src]->flags; |
| 604 | fdt_dst->fds[newfd]->fops = fdt_src->fds[fd_src]->fops; |
| 605 | fdt_dst->fds[newfd]->dentry = dfs_dentry_ref(fdt_src->fds[fd_src]->dentry); |
| 606 | fdt_dst->fds[newfd]->vnode = fdt_src->fds[fd_src]->vnode; |
| 607 | fdt_dst->fds[newfd]->mmap_context = RT_NULL; |
| 608 | fdt_dst->fds[newfd]->data = fdt_src->fds[fd_src]->data; |
| 609 | |
| 610 | /* |
| 611 | * dma-buf/socket fd is without dentry, so should used the vnode reference. |
| 612 | */ |
| 613 | if (!fdt_dst->fds[newfd]->dentry) |
| 614 | { |
| 615 | rt_atomic_add(&(fdt_dst->fds[newfd]->vnode->ref_count), 1); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | _EXIT: |
| 620 | dfs_file_unlock(); |
| 621 | |
| 622 | return newfd; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * @brief drop fd from the fd table. |
nothing calls this directly
no test coverage detected