diff options
| author | Ben Straub <bstraub@github.com> | 2012-07-11 10:40:53 -0700 |
|---|---|---|
| committer | Ben Straub <bstraub@github.com> | 2012-07-11 10:40:53 -0700 |
| commit | d024419f165e81f59d919bd56d84abf8e9fb9f57 (patch) | |
| tree | 93c6fe213a8249c8308a3ee607da6af769125d55 /src | |
| parent | c3b5099fe46e1191784cc1890cd35f167305f47a (diff) | |
| download | libgit2-d024419f165e81f59d919bd56d84abf8e9fb9f57.tar.gz | |
Add git_path_is_empty_dir.
Diffstat (limited to 'src')
| -rw-r--r-- | src/clone.c | 44 | ||||
| -rw-r--r-- | src/path.c | 52 | ||||
| -rw-r--r-- | src/path.h | 9 |
3 files changed, 62 insertions, 43 deletions
diff --git a/src/clone.c b/src/clone.c index 3f161c810..803338ebb 100644 --- a/src/clone.c +++ b/src/clone.c @@ -195,54 +195,14 @@ static int setup_remotes_and_fetch(git_repository *repo, /* TODO: p_opendir, p_closedir */ static bool path_is_okay(const char *path) { -#ifdef GIT_WIN32 - HANDLE hFind = INVALID_HANDLE_VALUE; - wchar_t *wbuf; - WIN32_FIND_DATAW ffd; -#else - DIR *dir = NULL; - struct dirent *e; -#endif - - bool retval = true; - /* The path must either not exist, or be an empty directory */ if (!git_path_exists(path)) return true; - - if (!git_path_isdir(path)) { + if (!git_path_is_empty_dir(path)) { giterr_set(GITERR_INVALID, "'%s' exists and is not an empty directory", path); return false; } - -#ifdef GIT_WIN32 - wbuf = gitwin_to_utf16(path); - gitwin_append_utf16(wbuf, "\\*", 2); - hFind = FindFirstFileW(wbuf, &ffd); - if (INVALID_HANDLE_VALUE != hFind) { - retval = false; - FindClose(hFind); - } - git__free(wbuf); -#else - dir = opendir(path); - if (!dir) { - giterr_set(GITERR_OS, "Couldn't open '%s'", path); - return false; - } - - while ((e = readdir(dir)) != NULL) { - if (!git_path_is_dot_or_dotdot(e->d_name)) { - giterr_set(GITERR_INVALID, - "'%s' exists and is not an empty directory", path); - retval = false; - break; - } - } - closedir(dir); -#endif - - return retval; + return true; } diff --git a/src/path.c b/src/path.c index 3de4b1100..e667ec357 100644 --- a/src/path.c +++ b/src/path.c @@ -389,6 +389,58 @@ bool git_path_isfile(const char *path) return S_ISREG(st.st_mode) != 0; } +#ifdef GIT_WIN32 + +bool git_path_is_empty_dir(const char *path) +{ + HANDLE hFind = INVALID_HANDLE_VALUE; + wchar_t *wbuf; + WIN32_FIND_DATAW ffd; + bool retval = true; + + if (!git_path_isdir(path)) return false; + + wbuf = gitwin_to_utf16(path); + gitwin_append_utf16(wbuf, "\\*", 2); + hFind = FindFirstFileW(wbuf, &ffd); + if (INVALID_HANDLE_VALUE != hFind) { + retval = false; + FindClose(hFind); + } + git__free(wbuf); + return retval; +} + +#else + +bool git_path_is_empty_dir(const char *path) +{ + DIR *dir = NULL; + struct dirent *e; + bool retval = true; + + if (!git_path_isdir(path)) return false; + + dir = opendir(path); + if (!dir) { + giterr_set(GITERR_OS, "Couldn't open '%s'", path); + return false; + } + + while ((e = readdir(dir)) != NULL) { + if (!git_path_is_dot_or_dotdot(e->d_name)) { + giterr_set(GITERR_INVALID, + "'%s' exists and is not an empty directory", path); + retval = false; + break; + } + } + closedir(dir); + + return retval; +} +#endif + int git_path_lstat(const char *path, struct stat *st) { int err = 0; diff --git a/src/path.h b/src/path.h index 76e01fc8f..116477043 100644 --- a/src/path.h +++ b/src/path.h @@ -80,7 +80,9 @@ extern int git_path_to_dir(git_buf *path); */ extern void git_path_string_to_dir(char* path, size_t size); -/* Taken from git.git */ +/** + * Taken from git.git; returns nonzero if the given path is "." or "..". + */ GIT_INLINE(int) git_path_is_dot_or_dotdot(const char *name) { return (name[0] == '.' && @@ -138,6 +140,11 @@ extern bool git_path_isdir(const char *path); extern bool git_path_isfile(const char *path); /** + * Check if the given path is a directory, and is empty. + */ +extern bool git_path_is_empty_dir(const char *path); + +/** * Stat a file and/or link and set error if needed. */ extern int git_path_lstat(const char *path, struct stat *st); |
