git_path: introduce 'git_path_diriter' Introduce a new `git_path_diriter` that can iterate directories efficiently for each platform.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
diff --git a/src/path.c b/src/path.c
index 6a636bb..2390b4f 100644
--- a/src/path.c
+++ b/src/path.c
@@ -260,6 +260,20 @@ int git_path_root(const char *path)
return -1; /* Not a real error - signals that path is not rooted */
}
+void git_path_trim_slashes(git_buf *path)
+{
+ int ceiling = git_path_root(path->ptr) + 1;
+ assert(ceiling >= 0);
+
+ while (path->size > (size_t)ceiling) {
+ if (path->ptr[path->size-1] != '/')
+ break;
+
+ path->ptr[path->size-1] = '\0';
+ path->size--;
+ }
+}
+
int git_path_join_unrooted(
git_buf *path_out, const char *path, const char *base, ssize_t *root_at)
{
@@ -1181,6 +1195,122 @@ int git_path_with_stat_cmp_icase(const void *a, const void *b)
return strcasecmp(psa->path, psb->path);
}
+int git_path_diriter_init(
+ git_path_diriter *diriter,
+ const char *path,
+ unsigned int flags)
+{
+ assert(diriter && path);
+
+ memset(diriter, 0, sizeof(git_path_diriter));
+
+ if (git_buf_puts(&diriter->path, path) < 0)
+ return -1;
+
+ git_path_mkposix(diriter->path.ptr);
+ git_path_trim_slashes(&diriter->path);
+
+ if ((diriter->dir = opendir(diriter->path.ptr)) == NULL) {
+ git_buf_free(&diriter->path);
+
+ giterr_set(GITERR_OS, "Failed to open directory '%s'", path);
+ return -1;
+ }
+
+#ifdef GIT_USE_ICONV
+ if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
+ (void)git_path_iconv_init_precompose(&ic);
+#endif
+
+ diriter->parent_len = diriter->path.size;
+ diriter->flags = flags;
+
+ return 0;
+}
+
+int git_path_diriter_next(
+ const char **out,
+ size_t *out_len,
+ git_path_diriter *diriter)
+{
+ struct dirent *de;
+ const char *filename;
+ size_t filename_len;
+ bool skip_dot = !(diriter->flags & GIT_PATH_DIR_INCLUDE_DOT_AND_DOTDOT);
+ int error = 0;
+
+ assert(out && out_len && diriter);
+
+ *out = NULL;
+ *out_len = 0;
+
+ errno = 0;
+
+ do {
+ if ((de = readdir(diriter->dir)) == NULL) {
+ if (!errno)
+ return GIT_ITEROVER;
+
+ giterr_set(GITERR_OS,
+ "Could not read directory '%s'", diriter->path);
+ return -1;
+ }
+ } while (skip_dot && git_path_is_dot_or_dotdot(de->d_name));
+
+ filename = de->d_name;
+ filename_len = strlen(filename);
+
+#ifdef GIT_USE_ICONV
+ if ((error = git_path_iconv(&diriter->ic, &filename, &filename_len)) < 0)
+ return error;
+#endif
+
+ git_buf_truncate(&diriter->path, diriter->parent_len);
+ git_buf_putc(&diriter->path, '/');
+ git_buf_put(&diriter->path, filename, filename_len);
+
+ if (git_buf_oom(&diriter->path))
+ return -1;
+
+ *out = &diriter->path.ptr[diriter->parent_len+1];
+ *out_len = filename_len;
+
+ return error;
+}
+
+int git_path_diriter_fullpath(
+ const char **out,
+ size_t *out_len,
+ git_path_diriter *diriter)
+{
+ assert(out && out_len && diriter);
+
+ *out = diriter->path.ptr;
+ *out_len = diriter->path.size;
+ return 0;
+}
+
+int git_path_diriter_stat(struct stat *out, git_path_diriter *diriter)
+{
+ assert(out && diriter);
+
+ return git_path_lstat(diriter->path.ptr, out);
+}
+
+void git_path_diriter_free(git_path_diriter *diriter)
+{
+ if (diriter == NULL)
+ return;
+
+ closedir(diriter->dir);
+
+#ifdef GIT_USE_ICONV
+ git_path_iconv_clear(&diriter->ic);
+#endif
+
+ git_buf_free(&diriter->path);
+}
+
int git_path_dirload_with_stat(
const char *path,
size_t prefix_len,
diff --git a/src/path.h b/src/path.h
index 440b542..3a25d4a 100644
--- a/src/path.h
+++ b/src/path.h
@@ -273,6 +273,7 @@ extern int git_path_apply_relative(git_buf *target, const char *relpath);
enum {
GIT_PATH_DIR_IGNORE_CASE = (1u << 0),
GIT_PATH_DIR_PRECOMPOSE_UNICODE = (1u << 1),
+ GIT_PATH_DIR_INCLUDE_DOT_AND_DOTDOT = (1u << 2),
};
/**
@@ -326,6 +327,37 @@ extern int git_path_walk_up(
int (*callback)(void *payload, const char *path),
void *payload);
+typedef struct git_path_diriter git_path_diriter;
+
+struct git_path_diriter
+{
+ git_buf path;
+ size_t parent_len;
+
+ unsigned int flags;
+
+ DIR *dir;
+};
+
+extern int git_path_diriter_init(
+ git_path_diriter *diriter,
+ const char *path,
+ unsigned int flags);
+
+extern int git_path_diriter_next(
+ const char **out,
+ size_t *out_len,
+ git_path_diriter *diriter);
+
+extern int git_path_diriter_fullpath(
+ const char **out,
+ size_t *out_len,
+ git_path_diriter *diriter);
+
+extern int git_path_diriter_stat(struct stat *out, git_path_diriter *diriter);
+
+extern void git_path_diriter_free(git_path_diriter *diriter);
+
/**
* Load all directory entries (except '.' and '..') into a vector.
*
diff --git a/src/posix.h b/src/posix.h
index 22f472c..8785a4c 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -122,7 +122,6 @@ extern int git__page_size(size_t *page_size);
#include "strnlen.h"
#ifdef NO_READDIR_R
-# include <dirent.h>
GIT_INLINE(int) p_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
{
GIT_UNUSED(entry);
diff --git a/src/unix/posix.h b/src/unix/posix.h
index e4f3ac6..8b4f427 100644
--- a/src/unix/posix.h
+++ b/src/unix/posix.h
@@ -8,6 +8,7 @@
#define INCLUDE_posix__unix_h__
#include <stdio.h>
+#include <dirent.h>
#include <sys/param.h>
typedef int GIT_SOCKET;