Changes in / [9b534a4:4a0bfcf0] in mainline
- Location:
- uspace
- Files:
-
- 2 added
- 3 edited
-
app/cmpdirs/cmpdirs.c (added)
-
app/cmpdirs/meson.build (added)
-
app/meson.build (modified) (1 diff)
-
lib/c/generic/dirent.c (modified) (2 diffs)
-
lib/c/include/dirent.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/meson.build
r9b534a4 r4a0bfcf0 35 35 'blkdump', 36 36 'calculator', 37 'cmpdirs', 37 38 'copy', 38 39 'corecfg', -
uspace/lib/c/generic/dirent.c
r9b534a4 r4a0bfcf0 47 47 }; 48 48 49 /** Open directory. 49 /** Like opendir_handle, but takes ownership of the handle if successful. */ 50 static 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 */ 75 DIR *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. 50 97 * 51 98 * @param dirname Directory pathname … … 55 102 DIR *opendir(const char *dirname) 56 103 { 57 DIR *dirp = malloc(sizeof(DIR));58 if (!dirp) {59 errno = ENOMEM;60 return NULL;61 }62 63 104 int fd; 64 105 errno_t rc = vfs_lookup(dirname, WALK_DIRECTORY, &fd); 65 106 if (rc != EOK) { 66 free(dirp);67 107 errno = rc; 68 108 return NULL; 69 109 } 70 110 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; 74 114 vfs_put(fd); 75 115 errno = rc; 76 116 return NULL; 77 117 } 78 79 dirp->fd = fd;80 dirp->pos = 0;81 118 return dirp; 82 119 } 83 120 84 /** Read directory entry.121 /** Read current directory entry and advance to the next one. 85 122 * 86 123 * @param dirp Open directory -
uspace/lib/c/include/dirent.h
r9b534a4 r4a0bfcf0 46 46 typedef struct __dirstream DIR; 47 47 48 extern DIR *opendir_handle(int handle); 48 49 extern DIR *opendir(const char *); 49 50 extern struct dirent *readdir(DIR *);
Note:
See TracChangeset
for help on using the changeset viewer.
