Changes in / [9b534a4:4a0bfcf0] in mainline


Ignore:
Location:
uspace
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/meson.build

    r9b534a4 r4a0bfcf0  
    3535        'blkdump',
    3636        'calculator',
     37        'cmpdirs',
    3738        'copy',
    3839        'corecfg',
  • uspace/lib/c/generic/dirent.c

    r9b534a4 r4a0bfcf0  
    4747};
    4848
    49 /** Open directory.
     49/** Like opendir_handle, but takes ownership of the handle if successful. */
     50static DIR *opendir_internal(int handle)
     51{
     52        DIR *dirp = malloc(sizeof(DIR));
     53        if (!dirp) {
     54                errno = ENOMEM;
     55                return NULL;
     56        }
     57
     58        errno_t rc = vfs_open(handle, MODE_READ);
     59        if (rc != EOK) {
     60                free(dirp);
     61                errno = rc;
     62                return NULL;
     63        }
     64
     65        dirp->fd = handle;
     66        dirp->pos = 0;
     67        return dirp;
     68}
     69
     70/** Open directory by its handle (a.k.a. file descriptor).
     71 *
     72 * @param handle Directory handle
     73 * @return Non-NULL pointer on success. On error returns @c NULL and sets errno.
     74 */
     75DIR *opendir_handle(int handle)
     76{
     77        int my_handle;
     78        // Clone the file handle, otherwise closedir would put the
     79        // handle that was passed to us here by the caller and that we don't own.
     80        errno_t rc = vfs_clone(handle, -1, false, &my_handle);
     81        if (rc != EOK) {
     82                errno = rc;
     83                return NULL;
     84        }
     85
     86        DIR *dirp = opendir_internal(my_handle);
     87        if (!dirp) {
     88                rc = errno;
     89                vfs_put(my_handle);
     90                errno = rc;
     91                return NULL;
     92        }
     93        return dirp;
     94}
     95
     96/** Open directory by its pathname.
    5097 *
    5198 * @param dirname Directory pathname
     
    55102DIR *opendir(const char *dirname)
    56103{
    57         DIR *dirp = malloc(sizeof(DIR));
    58         if (!dirp) {
    59                 errno = ENOMEM;
    60                 return NULL;
    61         }
    62 
    63104        int fd;
    64105        errno_t rc = vfs_lookup(dirname, WALK_DIRECTORY, &fd);
    65106        if (rc != EOK) {
    66                 free(dirp);
    67107                errno = rc;
    68108                return NULL;
    69109        }
    70110
    71         rc = vfs_open(fd, MODE_READ);
    72         if (rc != EOK) {
    73                 free(dirp);
     111        DIR *dirp = opendir_internal(fd);
     112        if (!dirp) {
     113                rc = errno;
    74114                vfs_put(fd);
    75115                errno = rc;
    76116                return NULL;
    77117        }
    78 
    79         dirp->fd = fd;
    80         dirp->pos = 0;
    81118        return dirp;
    82119}
    83120
    84 /** Read directory entry.
     121/** Read current directory entry and advance to the next one.
    85122 *
    86123 * @param dirp Open directory
  • uspace/lib/c/include/dirent.h

    r9b534a4 r4a0bfcf0  
    4646typedef struct __dirstream DIR;
    4747
     48extern DIR *opendir_handle(int handle);
    4849extern DIR *opendir(const char *);
    4950extern struct dirent *readdir(DIR *);
Note: See TracChangeset for help on using the changeset viewer.