* Push a level in the iter stack and initialize it with information from * the directory pointed by iter->base->path. It is assumed that this * strbuf points to a valid directory path. Return 0 on success and -1 * otherwise, setting errno accordingly and leaving the stack unchanged. */
| 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 |
no test coverage detected