| 1 | #include "git-compat-util.h" |
| 2 | #include "dir.h" |
| 3 | #include "iterator.h" |
| 4 | #include "dir-iterator.h" |
| 5 | #include "string-list.h" |
| 6 | |
| 7 | struct dir_iterator_level { |
| 8 | DIR *dir; |
| 9 | |
| 10 | /* |
| 11 | * The directory entries of the current level. This list will only be |
| 12 | * populated when the iterator is ordered. In that case, `dir` will be |
| 13 | * set to `NULL`. |
| 14 | */ |
| 15 | struct string_list entries; |
| 16 | size_t entries_idx; |
| 17 | |
| 18 | /* |
| 19 | * The length of the directory part of path at this level |
| 20 | * (including a trailing '/'): |
| 21 | */ |
| 22 | size_t prefix_len; |
| 23 | }; |
| 24 | |
| 25 | /* |
| 26 | * The full data structure used to manage the internal directory |
| 27 | * iteration state. It includes members that are not part of the |
| 28 | * public interface. |
| 29 | */ |
| 30 | struct dir_iterator_int { |
| 31 | struct dir_iterator base; |
| 32 | |
| 33 | /* |
| 34 | * The number of levels currently on the stack. After the first |
| 35 | * call to dir_iterator_begin(), if it succeeds to open the |
| 36 | * first level's dir, this will always be at least 1. Then, |
| 37 | * when it comes to zero the iteration is ended and this |
| 38 | * struct is freed. |
| 39 | */ |
| 40 | size_t levels_nr; |
| 41 | |
| 42 | /* The number of levels that have been allocated on the stack */ |
| 43 | size_t levels_alloc; |
| 44 | |
| 45 | /* |
| 46 | * A stack of levels. levels[0] is the uppermost directory |
| 47 | * that will be included in this iteration. |
| 48 | */ |
| 49 | struct dir_iterator_level *levels; |
| 50 | |
| 51 | /* Combination of flags for this dir-iterator */ |
| 52 | unsigned int flags; |
| 53 | }; |
| 54 | |
| 55 | static int next_directory_entry(DIR *dir, const char *path, |
| 56 | struct dirent **out) |
| 57 | { |
| 58 | struct dirent *de; |
| 59 | |
| 60 | repeat: |
| 61 | errno = 0; |
| 62 | de = readdir(dir); |
| 63 | if (!de) { |
| 64 | if (errno) { |
| 65 | warning_errno("error reading directory '%s'", |
| 66 | path); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | if (is_dot_or_dotdot(de->d_name)) |
| 74 | goto repeat; |
| 75 | |
| 76 | *out = de; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Push a level in the iter stack and initialize it with information from |
| 82 | * the directory pointed by iter->base->path. It is assumed that this |
| 83 | * strbuf points to a valid directory path. Return 0 on success and -1 |
| 84 | * otherwise, setting errno accordingly and leaving the stack unchanged. |
| 85 | */ |
| 86 | static int push_level(struct dir_iterator_int *iter) |
| 87 | { |
| 88 | struct dir_iterator_level *level; |
| 89 | |
| 90 | ALLOC_GROW(iter->levels, iter->levels_nr + 1, iter->levels_alloc); |
| 91 | level = &iter->levels[iter->levels_nr++]; |
| 92 | |
| 93 | if (!is_dir_sep(iter->base.path.buf[iter->base.path.len - 1])) |
| 94 | strbuf_addch(&iter->base.path, '/'); |
| 95 | level->prefix_len = iter->base.path.len; |
| 96 | |
| 97 | level->dir = opendir(iter->base.path.buf); |
| 98 | if (!level->dir) { |
| 99 | int saved_errno = errno; |
| 100 | if (errno != ENOENT) { |
| 101 | warning_errno("error opening directory '%s'", |
| 102 | iter->base.path.buf); |
| 103 | } |
| 104 | iter->levels_nr--; |
| 105 | errno = saved_errno; |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | string_list_init_dup(&level->entries); |
| 110 | level->entries_idx = 0; |
| 111 | |
| 112 | /* |
| 113 | * When the iterator is sorted we read and sort all directory entries |
| 114 | * directly. |
| 115 | */ |
| 116 | if (iter->flags & DIR_ITERATOR_SORTED) { |
| 117 | struct dirent *de; |
| 118 | |
| 119 | while (1) { |
| 120 | int ret = next_directory_entry(level->dir, iter->base.path.buf, &de); |
| 121 | if (ret < 0) { |
| 122 | if (errno != ENOENT && |
| 123 | iter->flags & DIR_ITERATOR_PEDANTIC) |
| 124 | return -1; |
| 125 | continue; |
| 126 | } else if (ret > 0) { |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | string_list_append(&level->entries, de->d_name); |
| 131 | } |
| 132 | string_list_sort(&level->entries); |
| 133 | |
| 134 | closedir(level->dir); |
| 135 | level->dir = NULL; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Pop the top level on the iter stack, releasing any resources associated |
| 143 | * with it. Return the new value of iter->levels_nr. |
| 144 | */ |
| 145 | static int pop_level(struct dir_iterator_int *iter) |
| 146 | { |
| 147 | struct dir_iterator_level *level = |
| 148 | &iter->levels[iter->levels_nr - 1]; |
| 149 | |
| 150 | if (level->dir && closedir(level->dir)) |
| 151 | warning_errno("error closing directory '%s'", |
| 152 | iter->base.path.buf); |
| 153 | level->dir = NULL; |
| 154 | string_list_clear(&level->entries, 0); |
| 155 | |
| 156 | return --iter->levels_nr; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Populate iter->base with the necessary information on the next iteration |
| 161 | * entry, represented by the given name. Return 0 on success and -1 |
| 162 | * otherwise, setting errno accordingly. |
| 163 | */ |
| 164 | static int prepare_next_entry_data(struct dir_iterator_int *iter, |
| 165 | const char *name) |
| 166 | { |
| 167 | int err, saved_errno; |
| 168 | |
| 169 | strbuf_addstr(&iter->base.path, name); |
| 170 | /* |
| 171 | * We have to reset these because the path strbuf might have |
| 172 | * been realloc()ed at the previous strbuf_addstr(). |
| 173 | */ |
| 174 | iter->base.relative_path = iter->base.path.buf + |
| 175 | iter->levels[0].prefix_len; |
| 176 | iter->base.basename = iter->base.path.buf + |
| 177 | iter->levels[iter->levels_nr - 1].prefix_len; |
| 178 | |
| 179 | err = lstat(iter->base.path.buf, &iter->base.st); |
| 180 | |
| 181 | saved_errno = errno; |
| 182 | if (err && errno != ENOENT) |
| 183 | warning_errno("failed to stat '%s'", iter->base.path.buf); |
| 184 | |
| 185 | errno = saved_errno; |
| 186 | return err; |
| 187 | } |
| 188 | |
| 189 | int dir_iterator_advance(struct dir_iterator *dir_iterator) |
| 190 | { |
| 191 | struct dir_iterator_int *iter = |
| 192 | (struct dir_iterator_int *)dir_iterator; |
| 193 | |
| 194 | if (S_ISDIR(iter->base.st.st_mode) && push_level(iter)) { |
| 195 | if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC) |
| 196 | return ITER_ERROR; |
| 197 | if (iter->levels_nr == 0) |
| 198 | return ITER_ERROR; |
| 199 | } |
| 200 | |
| 201 | /* Loop until we find an entry that we can give back to the caller. */ |
| 202 | while (1) { |
| 203 | struct dirent *de; |
| 204 | struct dir_iterator_level *level = |
| 205 | &iter->levels[iter->levels_nr - 1]; |
| 206 | const char *name; |
| 207 | |
| 208 | strbuf_setlen(&iter->base.path, level->prefix_len); |
| 209 | |
| 210 | if (level->dir) { |
| 211 | int ret = next_directory_entry(level->dir, iter->base.path.buf, &de); |
| 212 | if (ret < 0) { |
| 213 | if (iter->flags & DIR_ITERATOR_PEDANTIC) |
| 214 | return ITER_ERROR; |
| 215 | continue; |
| 216 | } else if (ret > 0) { |
| 217 | if (pop_level(iter) == 0) |
| 218 | return ITER_DONE; |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | name = de->d_name; |
| 223 | } else { |
| 224 | if (level->entries_idx >= level->entries.nr) { |
| 225 | if (pop_level(iter) == 0) |
| 226 | return ITER_DONE; |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | name = level->entries.items[level->entries_idx++].string; |
| 231 | } |
| 232 | |
| 233 | if (prepare_next_entry_data(iter, name)) { |
| 234 | if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC) |
| 235 | return ITER_ERROR; |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | return ITER_OK; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void dir_iterator_free(struct dir_iterator *dir_iterator) |
| 244 | { |
| 245 | struct dir_iterator_int *iter = (struct dir_iterator_int *)dir_iterator; |
| 246 | |
| 247 | if (!iter) |
| 248 | return; |
| 249 | |
| 250 | for (; iter->levels_nr; iter->levels_nr--) { |
| 251 | struct dir_iterator_level *level = |
| 252 | &iter->levels[iter->levels_nr - 1]; |
| 253 | |
| 254 | if (level->dir && closedir(level->dir)) { |
| 255 | int saved_errno = errno; |
| 256 | strbuf_setlen(&iter->base.path, level->prefix_len); |
| 257 | errno = saved_errno; |
| 258 | warning_errno("error closing directory '%s'", |
| 259 | iter->base.path.buf); |
| 260 | } |
| 261 | |
| 262 | string_list_clear(&level->entries, 0); |
| 263 | } |
| 264 | |
| 265 | free(iter->levels); |
| 266 | strbuf_release(&iter->base.path); |
| 267 | free(iter); |
| 268 | } |
| 269 | |
| 270 | struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags) |
| 271 | { |
| 272 | struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter)); |
| 273 | struct dir_iterator *dir_iterator = &iter->base; |
| 274 | int saved_errno, err; |
| 275 | |
| 276 | strbuf_init(&iter->base.path, PATH_MAX); |
| 277 | strbuf_addstr(&iter->base.path, path); |
| 278 | |
| 279 | ALLOC_GROW(iter->levels, 10, iter->levels_alloc); |
| 280 | iter->levels_nr = 0; |
| 281 | iter->flags = flags; |
| 282 | |
| 283 | /* |
| 284 | * Note: lstat already checks for NULL or empty strings and |
| 285 | * nonexistent paths. |
| 286 | */ |
| 287 | err = lstat(iter->base.path.buf, &iter->base.st); |
| 288 | |
| 289 | if (err < 0) { |
| 290 | saved_errno = errno; |
| 291 | goto error_out; |
| 292 | } |
| 293 | |
| 294 | if (!S_ISDIR(iter->base.st.st_mode)) { |
| 295 | saved_errno = ENOTDIR; |
| 296 | goto error_out; |
| 297 | } |
| 298 | |
| 299 | return dir_iterator; |
| 300 | |
| 301 | error_out: |
| 302 | dir_iterator_free(dir_iterator); |
| 303 | errno = saved_errno; |
| 304 | return NULL; |
| 305 | } |