diff options
author | Shawn O. Pearce <spearce@spearce.org> | 2008-12-31 15:35:36 -0800 |
---|---|---|
committer | Shawn O. Pearce <spearce@spearce.org> | 2008-12-31 15:35:36 -0800 |
commit | 5690f02e87e4fd31dfa9fd7c9c01aba03603cde8 (patch) | |
tree | 9396ec28d49129250ff79463cf588e1a8512984f /src/fileops.c | |
parent | 9eb7976448bf684dfb41993ef6c76098978ff933 (diff) | |
download | libgit2-5690f02e87e4fd31dfa9fd7c9c01aba03603cde8.tar.gz |
Rewrite git_foreach_dirent into gitfo_dirent
Our fileops API is currently private. We aren't planning on supplying
a cross-platform file API to applications that link to us. If we did,
we'd probably whole-sale publish fileops, not just the dirent code.
By moving it to be private we can also change the call signature to
permit the buffer to be passed down through the call chain. This is
very helpful when we are doing a recursive scan as we can reuse just
one buffer in all stack frames, reducing the impact the recursion has
on the stack frames in the data cache.
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'src/fileops.c')
-rw-r--r-- | src/fileops.c | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/src/fileops.c b/src/fileops.c index 1dd35dc8e..0867c7b57 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -187,31 +187,25 @@ int gitfo_close_cached(gitfo_cache *ioc) return gitfo_close(fd); } -/** - * Walk a directory and run 'fn' for each encountered entry - * (except '.' and '..'). - */ -int git_foreach_dirent(const char *wd, int (*fn)(void *, const char *), void *arg) +int gitfo_dirent( + char *path, + size_t path_sz, + int (*fn)(void *, char *), + void *arg) { - char path[GIT_PATH_MAX]; - size_t wd_len; + size_t wd_len = strlen(path); DIR *dir; struct dirent *de; - if (!wd) + if (!wd_len || path_sz < wd_len + 2) return GIT_ERROR; - wd_len = strlen(wd); - if (!wd_len || sizeof(path) < wd_len + 2) - return GIT_ERROR; - - strcpy(path, wd); while (path[wd_len - 1] == '/') wd_len--; path[wd_len++] = '/'; path[wd_len] = '\0'; - dir = opendir(wd); + dir = opendir(path); if (!dir) return git_os_error(); @@ -228,7 +222,7 @@ int git_foreach_dirent(const char *wd, int (*fn)(void *, const char *), void *ar } de_len = strlen(de->d_name); - if (sizeof(path) < wd_len + de_len + 1) { + if (path_sz < wd_len + de_len + 1) { closedir(dir); return GIT_ERROR; } |