| 1 |
/* |
| 2 |
* File System Helpers |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <c-stdaux.h> |
| 6 |
#include <dirent.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <sys/types.h> |
| 9 |
#include <unistd.h> |
| 10 |
#include "util/error.h" |
| 11 |
#include "util/fs.h" |
| 12 |
#include "util/string.h" |
| 13 |
|
| 14 |
/** |
| 15 |
* fs_dirlist_new() - create new directory listing |
| 16 |
* @list: output variable for new object |
| 17 |
* @n_allocated: minimum number of slots to allocate initially |
| 18 |
* |
| 19 |
* Create a new directory listing with space for at least @n_allocated entries. |
| 20 |
* The new object is returned in @list on success. |
| 21 |
* |
| 22 |
* This might reserve space for more than @n_allocated entries, if deemed |
| 23 |
* necessary by the implementation. |
| 24 |
* |
| 25 |
* Return: 0 on success, negative error code on failure. |
| 26 |
*/ |
| 27 |
int fs_dirlist_new(FsDirlist **listp, size_t n_allocated) { |
| 28 |
_c_cleanup_(fs_dirlist_freep) FsDirlist *list = NULL; |
| 29 |
|
| 30 |
if (n_allocated < 8) |
| 31 |
n_allocated = 8; |
| 32 |
|
| 33 |
list = calloc(1, sizeof(*list)); |
| 34 |
if (!list) |
| 35 |
return error_origin(-ENOMEM); |
| 36 |
|
| 37 |
list->entries = calloc(n_allocated, sizeof(*list->entries)); |
| 38 |
if (!list->entries) |
| 39 |
return error_origin(-ENOMEM); |
| 40 |
|
| 41 |
list->n_allocated = n_allocated; |
| 42 |
|
| 43 |
*listp = list; |
| 44 |
list = NULL; |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* fs_dirlist_free() - release directory listing |
| 50 |
* @list: list to operate on, or NULL |
| 51 |
* |
| 52 |
* Free the directory listing and all associated resources. If @list is NULL, |
| 53 |
* this is a no-op. |
| 54 |
* |
| 55 |
* Return: NULL is returned. |
| 56 |
*/ |
| 57 |
FsDirlist *fs_dirlist_free(FsDirlist *list) { |
| 58 |
size_t i; |
| 59 |
|
| 60 |
if (list) { |
| 61 |
for (i = 0; i < list->n_entries; ++i) |
| 62 |
c_free(list->entries[i]); |
| 63 |
c_free(list->entries); |
| 64 |
c_free(list); |
| 65 |
} |
| 66 |
|
| 67 |
return NULL; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* fs_dirlist_push() - push new entry to the end of the listing |
| 72 |
* @list: list to operate on |
| 73 |
* @de: directory entry to push |
| 74 |
* |
| 75 |
* Push a copy of the directory entry given as @de to the end of the directory |
| 76 |
* listing @list. The entire directory entry is copied, no reference to @de is |
| 77 |
* retained. |
| 78 |
* |
| 79 |
* Return: 0 on success, negative error code on failure. |
| 80 |
*/ |
| 81 |
int fs_dirlist_push(FsDirlist *list, const struct dirent *de) { |
| 82 |
struct dirent *next, **entries; |
| 83 |
size_t n, sz, n_name; |
| 84 |
|
| 85 |
/* Ensure the size is properly set with at least an empty name. */ |
| 86 |
c_assert(de->d_reclen > offsetof(struct dirent, d_name)); |
| 87 |
|
| 88 |
/* Ensure the record includes a terminating NULL in the name. */ |
| 89 |
sz = de->d_reclen - offsetof(struct dirent, d_name); |
| 90 |
n_name = strnlen(de->d_name, sz); |
| 91 |
c_assert(n_name < sz); |
| 92 |
|
| 93 |
/* Increase array-size if minimum was exceeded. */ |
| 94 |
if (list->n_entries >= list->n_allocated) { |
| 95 |
c_assert(list->n_allocated > 0); |
| 96 |
|
| 97 |
if (__builtin_mul_overflow(list->n_allocated, 2, &n)) |
| 98 |
return error_origin(-ENOMEM); |
| 99 |
if (__builtin_mul_overflow(n, sizeof(*list->entries), &sz)) |
| 100 |
return error_origin(-ENOMEM); |
| 101 |
|
| 102 |
entries = realloc(list->entries, sz); |
| 103 |
if (!entries) |
| 104 |
return error_origin(-ENOMEM); |
| 105 |
|
| 106 |
list->entries = entries; |
| 107 |
list->n_allocated = n; |
| 108 |
} |
| 109 |
|
| 110 |
/* Copy the directory entry and store it. */ |
| 111 |
|
| 112 |
sz = offsetof(struct dirent, d_name) + n_name + 1; |
| 113 |
next = malloc(sz); |
| 114 |
if (!next) |
| 115 |
return error_origin(-ENOMEM); |
| 116 |
|
| 117 |
memcpy(next, de, sz); |
| 118 |
list->entries[list->n_entries++] = next; |
| 119 |
|
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
static int fs_dirlist_cmp_p(const void *va, const void *vb) { |
| 124 |
const struct dirent * const *pa = va, * const *pb = vb; |
| 125 |
return strcmp((*pa)->d_name, (*pb)->d_name); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* fs_dirlist_sort() - sort all entries alphabetically |
| 130 |
* @list: list to operate on |
| 131 |
* |
| 132 |
* Sort all entries of the given directory listing alphabetically, based on |
| 133 |
* their entry name. |
| 134 |
* |
| 135 |
* Note that a listing of a directory can never contain two entries with equal |
| 136 |
* names. However, if such a listing was manually crafted, their order is |
| 137 |
* preserved. |
| 138 |
*/ |
| 139 |
void fs_dirlist_sort(FsDirlist *list) { |
| 140 |
qsort( |
| 141 |
list->entries, |
| 142 |
list->n_entries, |
| 143 |
sizeof(*list->entries), |
| 144 |
fs_dirlist_cmp_p |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* fs_dir_list() - enumerate all entries of a directory |
| 150 |
* @dir: directory to operate on |
| 151 |
* @listp: output variable for the directory listing |
| 152 |
* @flags: flags for the operation |
| 153 |
* |
| 154 |
* Read all entries of an open directory descriptor and collect them in a |
| 155 |
* directory listing. The directory listing is returned in @listp on success |
| 156 |
* and it is the responsibility of the caller to release it via |
| 157 |
* `fs_dirlist_free()` when done. |
| 158 |
* |
| 159 |
* The directory entries are always sorted via `fs_dirlist_sort()` before they |
| 160 |
* are returned. Hence, the semi-random on-disk order of directory entries is |
| 161 |
* not exposed to the caller. |
| 162 |
* |
| 163 |
* Return: 0 on success, negative error code on failure. |
| 164 |
*/ |
| 165 |
int fs_dir_list(DIR *dir, FsDirlist **listp, unsigned int flags) { |
| 166 |
_c_cleanup_(fs_dirlist_freep) FsDirlist *list = NULL; |
| 167 |
struct dirent *de; |
| 168 |
int r; |
| 169 |
|
| 170 |
r = fs_dirlist_new(&list, 0); |
| 171 |
if (r) |
| 172 |
return error_fold(r); |
| 173 |
|
| 174 |
for ( |
| 175 |
errno = 0, de = readdir(dir); |
| 176 |
de; |
| 177 |
errno = 0, de = readdir(dir) |
| 178 |
) { |
| 179 |
if (string_equal(de->d_name, ".")) |
| 180 |
continue; |
| 181 |
if (string_equal(de->d_name, "..")) |
| 182 |
continue; |
| 183 |
if ((flags & FS_DIR_FLAG_NO_HIDDEN) && de->d_name[0] == '.') |
| 184 |
continue; |
| 185 |
|
| 186 |
r = fs_dirlist_push(list, de); |
| 187 |
if (r) |
| 188 |
return error_fold(r); |
| 189 |
} |
| 190 |
if (errno > 0) |
| 191 |
return error_origin(-errno); |
| 192 |
|
| 193 |
/* |
| 194 |
* The order returned by the kernel is based on the order on disk, |
| 195 |
* which is effectively random. Preserving the on-disk order is useful |
| 196 |
* when operating on streamed data, since it avoids collecting the |
| 197 |
* entire listing. However, here we explicitly want to collect the |
| 198 |
* entire listing, hence we always want to sort the entries to prevent |
| 199 |
* accidentally relying on the semi-random on-disk order. |
| 200 |
*/ |
| 201 |
fs_dirlist_sort(list); |
| 202 |
|
| 203 |
*listp = list; |
| 204 |
list = NULL; |
| 205 |
return 0; |
| 206 |
} |