Changeset 2b88074b in mainline for uspace/srv/vfs/vfs_file.c


Ignore:
Timestamp:
2009-10-29T22:06:46Z (14 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2e983c7
Parents:
14ecd6c
Message:

vfs: file descriptors housekeeping changes

  • add support for allocating new file descriptors from the end of the fd range (O_DESC)
  • add support for assigning of an already opened file to a free file descriptor

vfs: VFS_OUT_CLOSE is called only when the file reference count is about to drop to zero
vfs: implement VFS_IN_DUP

libc: optimize current working directory housekeeping

  • using opendir() was an overkill
  • allocate current working directory file descriptor from the end of the fd range using O_DESC (so it doesn't mess with the well-known descriptors 0, 1 and 2)

libc: implement dup2() (via VFS_IN_DUP)

getvc: change stdin/stdout/stderr by a slightly more elegant way (using dup2())

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/vfs/vfs_file.c

    r14ecd6c r2b88074b  
    7676/** Allocate a file descriptor.
    7777 *
    78  * @return              First available file descriptor or a negative error
    79  *                      code.
    80  */
    81 int vfs_fd_alloc(void)
     78 * @param desc If true, look for an available file descriptor
     79 *             in a descending order.
     80 *
     81 * @return First available file descriptor or a negative error
     82 *         code.
     83 */
     84int vfs_fd_alloc(bool desc)
    8285{
    8386        if (!vfs_files_init())
     
    8588       
    8689        unsigned int i;
    87         for (i = 0; i < MAX_OPEN_FILES; i++) {
     90        if (desc)
     91                i = MAX_OPEN_FILES;
     92        else
     93                i = 0;
     94       
     95        while (true) {
    8896                if (!files[i]) {
    8997                        files[i] = (vfs_file_t *) malloc(sizeof(vfs_file_t));
     
    96104                        return (int) i;
    97105                }
     106               
     107                if (desc) {
     108                        if (i == 0)
     109                                break;
     110                       
     111                        i--;
     112                } else {
     113                        if (i == MAX_OPEN_FILES)
     114                                break;
     115                       
     116                        i++;
     117                }
    98118        }
    99119       
     
    118138        vfs_file_delref(files[fd]);
    119139        files[fd] = NULL;
     140       
     141        return EOK;
     142}
     143
     144/** Assign a file to a file descriptor.
     145 *
     146 * @param file File to assign.
     147 * @param fd   File descriptor to assign to.
     148 *
     149 * @return EOK on success or EINVAL if fd is an invalid or already
     150 *         used file descriptor.
     151 *
     152 */
     153int vfs_fd_assign(vfs_file_t *file, int fd)
     154{
     155        if (!vfs_files_init())
     156                return ENOMEM;
     157       
     158        if ((fd < 0) || (fd >= MAX_OPEN_FILES) || (files[fd] != NULL))
     159                return EINVAL;
     160       
     161        files[fd] = file;
     162        vfs_file_addref(files[fd]);
    120163       
    121164        return EOK;
Note: See TracChangeset for help on using the changeset viewer.