Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/dirent.c

    r34832c3 rbebd154  
    11/*
    22 * Copyright (c) 2008 Jakub Jermar
    3  * Copyright (c) 2026 Vít Skalický
    43 * All rights reserved.
    54 *
     
    4847};
    4948
    50 /** Like opendir_handle, but takes ownership of the handle if successful. */
    51 static DIR *opendir_internal(int handle)
     49/** Open directory.
     50 *
     51 * @param dirname Directory pathname
     52 *
     53 * @return Non-NULL pointer on success. On error returns @c NULL and sets errno.
     54 */
     55DIR *opendir(const char *dirname)
    5256{
    5357        DIR *dirp = malloc(sizeof(DIR));
     
    5761        }
    5862
    59         errno_t rc = vfs_open(handle, MODE_READ);
     63        int fd;
     64        errno_t rc = vfs_lookup(dirname, WALK_DIRECTORY, &fd);
    6065        if (rc != EOK) {
    6166                free(dirp);
     
    6469        }
    6570
    66         dirp->fd = handle;
     71        rc = vfs_open(fd, MODE_READ);
     72        if (rc != EOK) {
     73                free(dirp);
     74                vfs_put(fd);
     75                errno = rc;
     76                return NULL;
     77        }
     78
     79        dirp->fd = fd;
    6780        dirp->pos = 0;
    6881        return dirp;
    6982}
    7083
    71 /** Open directory by its handle (a.k.a. file descriptor).
    72  *
    73  * @param handle Directory handle
    74  * @return Non-NULL pointer on success. On error returns @c NULL and sets errno.
    75  */
    76 DIR *opendir_handle(int handle)
    77 {
    78         int my_handle;
    79         // Clone the file handle, otherwise closedir would put the
    80         // handle that was passed to us here by the caller and that we don't own.
    81         errno_t rc = vfs_clone(handle, -1, false, &my_handle);
    82         if (rc != EOK) {
    83                 errno = rc;
    84                 return NULL;
    85         }
    86 
    87         DIR *dirp = opendir_internal(my_handle);
    88         if (!dirp) {
    89                 rc = errno;
    90                 vfs_put(my_handle);
    91                 errno = rc;
    92                 return NULL;
    93         }
    94         return dirp;
    95 }
    96 
    97 /** Open directory by its pathname.
    98  *
    99  * @param dirname Directory pathname
    100  *
    101  * @return Non-NULL pointer on success. On error returns @c NULL and sets errno.
    102  */
    103 DIR *opendir(const char *dirname)
    104 {
    105         int fd;
    106         errno_t rc = vfs_lookup(dirname, WALK_DIRECTORY, &fd);
    107         if (rc != EOK) {
    108                 errno = rc;
    109                 return NULL;
    110         }
    111 
    112         DIR *dirp = opendir_internal(fd);
    113         if (!dirp) {
    114                 rc = errno;
    115                 vfs_put(fd);
    116                 errno = rc;
    117                 return NULL;
    118         }
    119         return dirp;
    120 }
    121 
    122 /** Read current directory entry and advance to the next one.
     84/** Read directory entry.
    12385 *
    12486 * @param dirp Open directory
Note: See TracChangeset for help on using the changeset viewer.