path: extract function to check whether a path supports symlinks When initializing a repository, we need to check whether its working directory supports symlinks to correctly set the initial value of the "core.symlinks" config variable. The code to check the filesystem is reusable in other parts of our codebase, like for example in our tests to determine whether certain tests can be expected to succeed or not. Extract the code into a new function `git_path_supports_symlinks` to avoid duplicate implementations. Remove a duplicate implementation in the repo test helper code.
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
diff --git a/src/path.c b/src/path.c
index df6b623..41232c2 100644
--- a/src/path.c
+++ b/src/path.c
@@ -1924,3 +1924,25 @@ extern int git_path_is_gitfile(const char *path, size_t pathlen, git_path_gitfil
return -1;
}
}
+
+bool git_path_supports_symlinks(const char *dir)
+{
+ git_buf path = GIT_BUF_INIT;
+ bool supported = false;
+ struct stat st;
+ int fd;
+
+ if ((fd = git_futils_mktmp(&path, dir, 0666)) < 0 ||
+ p_close(fd) < 0 ||
+ p_unlink(path.ptr) < 0 ||
+ p_symlink("testing", path.ptr) < 0 ||
+ p_lstat(path.ptr, &st) < 0)
+ goto done;
+
+ supported = (S_ISLNK(st.st_mode) != 0);
+done:
+ if (path.size)
+ (void)p_unlink(path.ptr);
+ git_buf_dispose(&path);
+ return supported;
+}
diff --git a/src/path.h b/src/path.h
index e29a7f6..624ca03 100644
--- a/src/path.h
+++ b/src/path.h
@@ -647,4 +647,6 @@ extern bool git_path_isvalid(
*/
int git_path_normalize_slashes(git_buf *out, const char *path);
+bool git_path_supports_symlinks(const char *dir);
+
#endif
diff --git a/src/repository.c b/src/repository.c
index 6a49899..f9c4ef4 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1419,9 +1419,6 @@ static bool are_symlinks_supported(const char *wd_path)
git_buf xdg_buf = GIT_BUF_INIT;
git_buf system_buf = GIT_BUF_INIT;
git_buf programdata_buf = GIT_BUF_INIT;
- git_buf path = GIT_BUF_INIT;
- int fd;
- struct stat st;
int symlinks = 0;
/*
@@ -1448,23 +1445,14 @@ static bool are_symlinks_supported(const char *wd_path)
goto done;
#endif
- if ((fd = git_futils_mktmp(&path, wd_path, 0666)) < 0 ||
- p_close(fd) < 0 ||
- p_unlink(path.ptr) < 0 ||
- p_symlink("testing", path.ptr) < 0 ||
- p_lstat(path.ptr, &st) < 0)
+ if (!(symlinks = git_path_supports_symlinks(wd_path)))
goto done;
- symlinks = (S_ISLNK(st.st_mode) != 0);
-
- (void)p_unlink(path.ptr);
-
done:
git_buf_dispose(&global_buf);
git_buf_dispose(&xdg_buf);
git_buf_dispose(&system_buf);
git_buf_dispose(&programdata_buf);
- git_buf_dispose(&path);
git_config_free(config);
return symlinks != 0;
}
diff --git a/tests/checkout/index.c b/tests/checkout/index.c
index dcacdd5..a76c471 100644
--- a/tests/checkout/index.c
+++ b/tests/checkout/index.c
@@ -181,7 +181,7 @@ void test_checkout_index__honor_coresymlinks_default_true(void)
cl_must_pass(p_mkdir("symlink", 0777));
- if (!filesystem_supports_symlinks("symlink/test"))
+ if (!git_path_supports_symlinks("symlink/test"))
cl_skip();
#ifdef GIT_WIN32
@@ -214,7 +214,7 @@ void test_checkout_index__honor_coresymlinks_default_false(void)
* supports symlinks. Bail entirely on POSIX platforms that
* do support symlinks.
*/
- if (filesystem_supports_symlinks("symlink/test"))
+ if (git_path_supports_symlinks("symlink/test"))
cl_skip();
#endif
@@ -226,7 +226,7 @@ void test_checkout_index__coresymlinks_set_to_true_fails_when_unsupported(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
- if (filesystem_supports_symlinks("testrepo/test")) {
+ if (git_path_supports_symlinks("testrepo/test")) {
cl_skip();
}
@@ -242,7 +242,7 @@ void test_checkout_index__honor_coresymlinks_setting_set_to_true(void)
char link_data[GIT_PATH_MAX];
size_t link_size = GIT_PATH_MAX;
- if (!filesystem_supports_symlinks("testrepo/test")) {
+ if (!git_path_supports_symlinks("testrepo/test")) {
cl_skip();
}
diff --git a/tests/repo/init.c b/tests/repo/init.c
index 9eee830..5a95229 100644
--- a/tests/repo/init.c
+++ b/tests/repo/init.c
@@ -253,7 +253,7 @@ void test_repo_init__symlinks_win32_enabled_by_global_config(void)
git_config *config, *repo_config;
int val;
- if (!filesystem_supports_symlinks("link"))
+ if (!git_path_supports_symlinks("link"))
cl_skip();
create_tmp_global_config("tmp_global_config", "core.symlinks", "true");
@@ -296,7 +296,7 @@ void test_repo_init__symlinks_posix_detected(void)
cl_skip();
#else
assert_config_entry_on_init(
- "core.symlinks", filesystem_supports_symlinks("link") ? GIT_ENOTFOUND : false);
+ "core.symlinks", git_path_supports_symlinks("link") ? GIT_ENOTFOUND : false);
#endif
}
diff --git a/tests/repo/repo_helpers.c b/tests/repo/repo_helpers.c
index 4256314..b22f3f6 100644
--- a/tests/repo/repo_helpers.c
+++ b/tests/repo/repo_helpers.c
@@ -21,21 +21,6 @@ void delete_head(git_repository* repo)
git_buf_dispose(&head_path);
}
-int filesystem_supports_symlinks(const char *path)
-{
- struct stat st;
- bool support = 0;
-
- if (p_symlink("target", path) == 0) {
- if (p_lstat(path, &st) == 0 && S_ISLNK(st.st_mode))
- support = 1;
-
- p_unlink(path);
- }
-
- return support;
-}
-
void create_tmp_global_config(const char *dirname, const char *key, const char *val)
{
git_buf path = GIT_BUF_INIT;
diff --git a/tests/repo/repo_helpers.h b/tests/repo/repo_helpers.h
index 2c9aeab..a93bf36 100644
--- a/tests/repo/repo_helpers.h
+++ b/tests/repo/repo_helpers.h
@@ -4,5 +4,4 @@
extern void make_head_unborn(git_repository* repo, const char *target);
extern void delete_head(git_repository* repo);
-extern int filesystem_supports_symlinks(const char *path);
extern void create_tmp_global_config(const char *path, const char *key, const char *val);