summaryrefslogtreecommitdiff
path: root/src/path.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/path.c')
-rw-r--r--src/path.c169
1 files changed, 126 insertions, 43 deletions
diff --git a/src/path.c b/src/path.c
index 750dd3ef7..5beab97ed 100644
--- a/src/path.c
+++ b/src/path.c
@@ -9,10 +9,10 @@
#include "posix.h"
#ifdef GIT_WIN32
#include "win32/posix.h"
+#include "win32/w32_util.h"
#else
#include <dirent.h>
#endif
-#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
@@ -436,8 +436,12 @@ int git_path_walk_up(
while (scan >= stop) {
error = cb(data, &iter);
iter.ptr[scan] = oldc;
- if (error < 0)
+
+ if (error) {
+ giterr_set_after_callback(error);
break;
+ }
+
scan = git_buf_rfind_next(&iter, '/');
if (scan >= 0) {
scan++;
@@ -483,33 +487,33 @@ bool git_path_isfile(const char *path)
bool git_path_is_empty_dir(const char *path)
{
- HANDLE hFind = INVALID_HANDLE_VALUE;
- git_win32_path wbuf;
- int wbufsz;
- WIN32_FIND_DATAW ffd;
- bool retval = true;
-
- if (!git_path_isdir(path))
- return false;
-
- wbufsz = git_win32_path_from_c(wbuf, path);
- if (!wbufsz || wbufsz + 2 > GIT_WIN_PATH_UTF16)
- return false;
- memcpy(&wbuf[wbufsz - 1], L"\\*", 3 * sizeof(wchar_t));
-
- hFind = FindFirstFileW(wbuf, &ffd);
- if (INVALID_HANDLE_VALUE == hFind)
- return false;
-
- do {
- if (!git_path_is_dot_or_dotdotW(ffd.cFileName)) {
- retval = false;
- break;
+ git_win32_path filter_w;
+ bool empty = false;
+
+ if (git_win32__findfirstfile_filter(filter_w, path)) {
+ WIN32_FIND_DATAW findData;
+ HANDLE hFind = FindFirstFileW(filter_w, &findData);
+
+ /* If the find handle was created successfully, then it's a directory */
+ if (hFind != INVALID_HANDLE_VALUE) {
+ empty = true;
+
+ do {
+ /* Allow the enumeration to return . and .. and still be considered
+ * empty. In the special case of drive roots (i.e. C:\) where . and
+ * .. do not occur, we can still consider the path to be an empty
+ * directory if there's nothing there. */
+ if (!git_path_is_dot_or_dotdotW(findData.cFileName)) {
+ empty = false;
+ break;
+ }
+ } while (FindNextFileW(hFind, &findData));
+
+ FindClose(hFind);
}
- } while (FindNextFileW(hFind, &ffd) != 0);
+ }
- FindClose(hFind);
- return retval;
+ return empty;
}
#else
@@ -528,7 +532,9 @@ bool git_path_is_empty_dir(const char *path)
if (!git_path_isdir(path))
return false;
- if (!(error = git_buf_sets(&dir, path)))
+ if ((error = git_buf_sets(&dir, path)) != 0)
+ giterr_clear();
+ else
error = git_path_direach(&dir, 0, path_found_entry, NULL);
git_buf_free(&dir);
@@ -619,7 +625,7 @@ int git_path_find_dir(git_buf *dir, const char *path, const char *base)
/* call dirname if this is not a directory */
if (!error) /* && git_path_isdir(dir->ptr) == false) */
- error = git_path_dirname_r(dir, dir->ptr);
+ error = (git_path_dirname_r(dir, dir->ptr) < 0) ? -1 : 0;
if (!error)
error = git_path_to_dir(dir);
@@ -777,8 +783,10 @@ int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen)
!git_path_has_non_ascii(*in, *inlen))
return 0;
+ git_buf_clear(&ic->buf);
+
while (1) {
- if (git_buf_grow(&ic->buf, wantlen) < 0)
+ if (git_buf_grow(&ic->buf, wantlen + 1) < 0)
return -1;
nfc = ic->buf.ptr + ic->buf.size;
@@ -791,8 +799,11 @@ int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen)
if (rv != (size_t)-1)
break;
+ /* if we cannot convert the data (probably because iconv thinks
+ * it is not valid UTF-8 source data), then use original data
+ */
if (errno != E2BIG)
- goto fail;
+ return 0;
/* make space for 2x the remaining data to be converted
* (with per retry overhead to avoid infinite loops)
@@ -815,6 +826,64 @@ fail:
return -1;
}
+static const char *nfc_file = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D.XXXXXX";
+static const char *nfd_file = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D.XXXXXX";
+
+/* Check if the platform is decomposing unicode data for us. We will
+ * emulate core Git and prefer to use precomposed unicode data internally
+ * on these platforms, composing the decomposed unicode on the fly.
+ *
+ * This mainly happens on the Mac where HDFS stores filenames as
+ * decomposed unicode. Even on VFAT and SAMBA file systems, the Mac will
+ * return decomposed unicode from readdir() even when the actual
+ * filesystem is storing precomposed unicode.
+ */
+bool git_path_does_fs_decompose_unicode(const char *root)
+{
+ git_buf path = GIT_BUF_INIT;
+ int fd;
+ bool found_decomposed = false;
+ char tmp[6];
+
+ /* Create a file using a precomposed path and then try to find it
+ * using the decomposed name. If the lookup fails, then we will mark
+ * that we should precompose unicode for this repository.
+ */
+ if (git_buf_joinpath(&path, root, nfc_file) < 0 ||
+ (fd = p_mkstemp(path.ptr)) < 0)
+ goto done;
+ p_close(fd);
+
+ /* record trailing digits generated by mkstemp */
+ memcpy(tmp, path.ptr + path.size - sizeof(tmp), sizeof(tmp));
+
+ /* try to look up as NFD path */
+ if (git_buf_joinpath(&path, root, nfd_file) < 0)
+ goto done;
+ memcpy(path.ptr + path.size - sizeof(tmp), tmp, sizeof(tmp));
+
+ found_decomposed = git_path_exists(path.ptr);
+
+ /* remove temporary file (using original precomposed path) */
+ if (git_buf_joinpath(&path, root, nfc_file) < 0)
+ goto done;
+ memcpy(path.ptr + path.size - sizeof(tmp), tmp, sizeof(tmp));
+
+ (void)p_unlink(path.ptr);
+
+done:
+ git_buf_free(&path);
+ return found_decomposed;
+}
+
+#else
+
+bool git_path_does_fs_decompose_unicode(const char *root)
+{
+ GIT_UNUSED(root);
+ return false;
+}
+
#endif
#if defined(__sun) || defined(__GNU__)
@@ -848,6 +917,9 @@ int git_path_direach(
if ((dir = opendir(path->ptr)) == NULL) {
giterr_set(GITERR_OS, "Failed to open directory '%s'", path->ptr);
+ if (errno == ENOENT)
+ return GIT_ENOTFOUND;
+
return -1;
}
@@ -867,7 +939,7 @@ int git_path_direach(
if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0)
break;
#endif
-
+
if ((error = git_buf_put(path, de_path, de_len)) < 0)
break;
@@ -875,8 +947,8 @@ int git_path_direach(
git_buf_truncate(path, wd_len); /* restore path */
- if (error) {
- error = GIT_EUSER;
+ if (error != 0) {
+ giterr_set_after_callback(error);
break;
}
}
@@ -1043,15 +1115,8 @@ int git_path_dirload_with_stat(
}
if (S_ISDIR(ps->st.st_mode)) {
- if ((error = git_buf_joinpath(&full, full.ptr, ".git")) < 0)
- break;
-
- if (p_access(full.ptr, F_OK) == 0) {
- ps->st.st_mode = GIT_FILEMODE_COMMIT;
- } else {
- ps->path[ps->path_len++] = '/';
- ps->path[ps->path_len] = '\0';
- }
+ ps->path[ps->path_len++] = '/';
+ ps->path[ps->path_len] = '\0';
}
}
@@ -1062,3 +1127,21 @@ int git_path_dirload_with_stat(
return error;
}
+
+int git_path_from_url_or_path(git_buf *local_path_out, const char *url_or_path)
+{
+ int error;
+
+ /* If url_or_path begins with file:// treat it as a URL */
+ if (!git__prefixcmp(url_or_path, "file://")) {
+ if ((error = git_path_fromurl(local_path_out, url_or_path)) < 0) {
+ return error;
+ }
+ } else { /* We assume url_or_path is already a path */
+ if ((error = git_buf_sets(local_path_out, url_or_path)) < 0) {
+ return error;
+ }
+ }
+
+ return 0;
+}