mkdir.c 9.17 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
3 4 5 6 7 8
#include "path.h"
#include "posix.h"

static void cleanup_basic_dirs(void *ref)
{
	GIT_UNUSED(ref);
9 10 11 12 13
	git_futils_rmdir_r("d0", NULL, GIT_RMDIR_EMPTY_HIERARCHY);
	git_futils_rmdir_r("d1", NULL, GIT_RMDIR_EMPTY_HIERARCHY);
	git_futils_rmdir_r("d2", NULL, GIT_RMDIR_EMPTY_HIERARCHY);
	git_futils_rmdir_r("d3", NULL, GIT_RMDIR_EMPTY_HIERARCHY);
	git_futils_rmdir_r("d4", NULL, GIT_RMDIR_EMPTY_HIERARCHY);
14 15
}

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
void test_core_mkdir__absolute(void)
{
	git_buf path = GIT_BUF_INIT;

	cl_set_cleanup(cleanup_basic_dirs, NULL);

	git_buf_joinpath(&path, clar_sandbox_path(), "d0");

	/* make a directory */
	cl_assert(!git_path_isdir(path.ptr));
	cl_git_pass(git_futils_mkdir(path.ptr, 0755, 0));
	cl_assert(git_path_isdir(path.ptr));

	git_buf_joinpath(&path, path.ptr, "subdir");
	cl_assert(!git_path_isdir(path.ptr));
	cl_git_pass(git_futils_mkdir(path.ptr, 0755, 0));
	cl_assert(git_path_isdir(path.ptr));

34
	/* ensure mkdir_r works for a single subdir */
35 36 37 38 39
	git_buf_joinpath(&path, path.ptr, "another");
	cl_assert(!git_path_isdir(path.ptr));
	cl_git_pass(git_futils_mkdir_r(path.ptr, 0755));
	cl_assert(git_path_isdir(path.ptr));

40
	/* ensure mkdir_r works */
41 42 43 44
	git_buf_joinpath(&path, clar_sandbox_path(), "d1/foo/bar/asdf");
	cl_assert(!git_path_isdir(path.ptr));
	cl_git_pass(git_futils_mkdir_r(path.ptr, 0755));
	cl_assert(git_path_isdir(path.ptr));
45 46 47 48 49 50

	/* ensure we don't imply recursive */
	git_buf_joinpath(&path, clar_sandbox_path(), "d2/foo/bar/asdf");
	cl_assert(!git_path_isdir(path.ptr));
	cl_git_fail(git_futils_mkdir(path.ptr, 0755, 0));
	cl_assert(!git_path_isdir(path.ptr));
Carlos Martín Nieto committed
51

52
	git_buf_dispose(&path);
53 54
}

55 56 57 58 59 60
void test_core_mkdir__basic(void)
{
	cl_set_cleanup(cleanup_basic_dirs, NULL);

	/* make a directory */
	cl_assert(!git_path_isdir("d0"));
61
	cl_git_pass(git_futils_mkdir("d0", 0755, 0));
62 63 64 65
	cl_assert(git_path_isdir("d0"));

	/* make a path */
	cl_assert(!git_path_isdir("d1"));
66
	cl_git_pass(git_futils_mkdir("d1/d1.1/d1.2", 0755, GIT_MKDIR_PATH));
67 68 69 70 71 72
	cl_assert(git_path_isdir("d1"));
	cl_assert(git_path_isdir("d1/d1.1"));
	cl_assert(git_path_isdir("d1/d1.1/d1.2"));

	/* make a dir exclusively */
	cl_assert(!git_path_isdir("d2"));
73
	cl_git_pass(git_futils_mkdir("d2", 0755, GIT_MKDIR_EXCL));
74 75 76
	cl_assert(git_path_isdir("d2"));

	/* make exclusive failure */
77
	cl_git_fail(git_futils_mkdir("d2", 0755, GIT_MKDIR_EXCL));
78 79 80

	/* make a path exclusively */
	cl_assert(!git_path_isdir("d3"));
81
	cl_git_pass(git_futils_mkdir("d3/d3.1/d3.2", 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL));
82 83 84 85
	cl_assert(git_path_isdir("d3"));
	cl_assert(git_path_isdir("d3/d3.1/d3.2"));

	/* make exclusive path failure */
86
	cl_git_fail(git_futils_mkdir("d3/d3.1/d3.2", 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL));
87 88 89 90
	/* ??? Should EXCL only apply to the last item in the path? */

	/* path with trailing slash? */
	cl_assert(!git_path_isdir("d4"));
91
	cl_git_pass(git_futils_mkdir("d4/d4.1/", 0755, GIT_MKDIR_PATH));
92 93 94 95 96 97
	cl_assert(git_path_isdir("d4/d4.1"));
}

static void cleanup_basedir(void *ref)
{
	GIT_UNUSED(ref);
98
	git_futils_rmdir_r("base", NULL, GIT_RMDIR_EMPTY_HIERARCHY);
99 100 101 102 103 104 105 106
}

void test_core_mkdir__with_base(void)
{
#define BASEDIR "base/dir/here"

	cl_set_cleanup(cleanup_basedir, NULL);

107
	cl_git_pass(git_futils_mkdir(BASEDIR, 0755, GIT_MKDIR_PATH));
108

109
	cl_git_pass(git_futils_mkdir_relative("a", BASEDIR, 0755, 0, NULL));
110 111
	cl_assert(git_path_isdir(BASEDIR "/a"));

112
	cl_git_pass(git_futils_mkdir_relative("b/b1/b2", BASEDIR, 0755, GIT_MKDIR_PATH, NULL));
113 114 115
	cl_assert(git_path_isdir(BASEDIR "/b/b1/b2"));

	/* exclusive with existing base */
116
	cl_git_pass(git_futils_mkdir_relative("c/c1/c2", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL, NULL));
117 118

	/* fail: exclusive with duplicated suffix */
119
	cl_git_fail(git_futils_mkdir_relative("c/c1/c3", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL, NULL));
120 121

	/* fail: exclusive with any duplicated component */
122
	cl_git_fail(git_futils_mkdir_relative("c/cz/cz", BASEDIR, 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL, NULL));
123 124

	/* success: exclusive without path */
125
	cl_git_pass(git_futils_mkdir_relative("c/c1/c3", BASEDIR, 0755, GIT_MKDIR_EXCL, NULL));
126 127

	/* path with shorter base and existing dirs */
128
	cl_git_pass(git_futils_mkdir_relative("dir/here/d/", "base", 0755, GIT_MKDIR_PATH, NULL));
129 130 131
	cl_assert(git_path_isdir("base/dir/here/d"));

	/* fail: path with shorter base and existing dirs */
132
	cl_git_fail(git_futils_mkdir_relative("dir/here/e/", "base", 0755, GIT_MKDIR_PATH | GIT_MKDIR_EXCL, NULL));
133 134

	/* fail: base with missing components */
135
	cl_git_fail(git_futils_mkdir_relative("f/", "base/missing", 0755, GIT_MKDIR_PATH, NULL));
136 137

	/* success: shift missing component to path */
138
	cl_git_pass(git_futils_mkdir_relative("missing/f/", "base/", 0755, GIT_MKDIR_PATH, NULL));
139 140 141 142 143
}

static void cleanup_chmod_root(void *ref)
{
	mode_t *mode = ref;
144

145
	if (mode != NULL) {
146
		(void)p_umask(*mode);
147 148
		git__free(mode);
	}
149

150
	git_futils_rmdir_r("r", NULL, GIT_RMDIR_EMPTY_HIERARCHY);
151 152
}

153
#define check_mode(X,A) check_mode_at_line((X), (A), __FILE__, __func__, __LINE__)
154 155

static void check_mode_at_line(
156 157
	mode_t expected, mode_t actual,
	const char *file, const char *func, int line)
158
{
159 160 161 162 163 164 165
	/* FAT filesystems don't support exec bit, nor group/world bits */
	if (!cl_is_chmod_supported()) {
		expected &= 0600;
		actual &= 0600;
	}

	clar__assert_equal(
166
		file, func, line, "expected_mode != actual_mode", 1,
167
		"%07o", (int)expected, (int)(actual & 0777));
168 169
}

170 171 172
void test_core_mkdir__chmods(void)
{
	struct stat st;
173 174
	mode_t *old = git__malloc(sizeof(mode_t));
	*old = p_umask(022);
175

176
	cl_set_cleanup(cleanup_chmod_root, old);
177

178
	cl_git_pass(git_futils_mkdir("r", 0777, 0));
179

180
	cl_git_pass(git_futils_mkdir_relative("mode/is/important", "r", 0777, GIT_MKDIR_PATH, NULL));
181 182

	cl_git_pass(git_path_lstat("r/mode", &st));
183
	check_mode(0755, st.st_mode);
184
	cl_git_pass(git_path_lstat("r/mode/is", &st));
185
	check_mode(0755, st.st_mode);
186
	cl_git_pass(git_path_lstat("r/mode/is/important", &st));
187
	check_mode(0755, st.st_mode);
188

189
	cl_git_pass(git_futils_mkdir_relative("mode2/is2/important2", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD, NULL));
190 191

	cl_git_pass(git_path_lstat("r/mode2", &st));
192
	check_mode(0755, st.st_mode);
193
	cl_git_pass(git_path_lstat("r/mode2/is2", &st));
194
	check_mode(0755, st.st_mode);
195
	cl_git_pass(git_path_lstat("r/mode2/is2/important2", &st));
196
	check_mode(0777, st.st_mode);
197

198
	cl_git_pass(git_futils_mkdir_relative("mode3/is3/important3", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD_PATH, NULL));
199 200

	cl_git_pass(git_path_lstat("r/mode3", &st));
201
	check_mode(0777, st.st_mode);
202
	cl_git_pass(git_path_lstat("r/mode3/is3", &st));
203
	check_mode(0777, st.st_mode);
204
	cl_git_pass(git_path_lstat("r/mode3/is3/important3", &st));
205
	check_mode(0777, st.st_mode);
206 207 208

	/* test that we chmod existing dir */

209
	cl_git_pass(git_futils_mkdir_relative("mode/is/important", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD, NULL));
210 211

	cl_git_pass(git_path_lstat("r/mode", &st));
212
	check_mode(0755, st.st_mode);
213
	cl_git_pass(git_path_lstat("r/mode/is", &st));
214
	check_mode(0755, st.st_mode);
215
	cl_git_pass(git_path_lstat("r/mode/is/important", &st));
216
	check_mode(0777, st.st_mode);
217 218 219

	/* test that we chmod even existing dirs if CHMOD_PATH is set */

220
	cl_git_pass(git_futils_mkdir_relative("mode2/is2/important2.1", "r", 0777, GIT_MKDIR_PATH | GIT_MKDIR_CHMOD_PATH, NULL));
221 222

	cl_git_pass(git_path_lstat("r/mode2", &st));
223
	check_mode(0777, st.st_mode);
224
	cl_git_pass(git_path_lstat("r/mode2/is2", &st));
225
	check_mode(0777, st.st_mode);
226
	cl_git_pass(git_path_lstat("r/mode2/is2/important2.1", &st));
227
	check_mode(0777, st.st_mode);
228
}
229

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
void test_core_mkdir__keeps_parent_symlinks(void)
{
#ifndef GIT_WIN32
	git_buf path = GIT_BUF_INIT;

	cl_set_cleanup(cleanup_basic_dirs, NULL);

	/* make a directory */
	cl_assert(!git_path_isdir("d0"));
	cl_git_pass(git_futils_mkdir("d0", 0755, 0));
	cl_assert(git_path_isdir("d0"));

	cl_must_pass(symlink("d0", "d1"));
	cl_assert(git_path_islink("d1"));

	cl_git_pass(git_futils_mkdir("d1/foo/bar", 0755, GIT_MKDIR_PATH|GIT_MKDIR_REMOVE_SYMLINKS));
	cl_assert(git_path_islink("d1"));
	cl_assert(git_path_isdir("d1/foo/bar"));
	cl_assert(git_path_isdir("d0/foo/bar"));

	cl_must_pass(symlink("d0", "d2"));
	cl_assert(git_path_islink("d2"));

	git_buf_joinpath(&path, clar_sandbox_path(), "d2/other/dir");

	cl_git_pass(git_futils_mkdir(path.ptr, 0755, GIT_MKDIR_PATH|GIT_MKDIR_REMOVE_SYMLINKS));
	cl_assert(git_path_islink("d2"));
	cl_assert(git_path_isdir("d2/other/dir"));
	cl_assert(git_path_isdir("d0/other/dir"));

260
	git_buf_dispose(&path);
261 262 263
#endif
}

264 265 266 267 268 269 270 271 272 273 274 275 276
void test_core_mkdir__mkdir_path_inside_unwriteable_parent(void)
{
	struct stat st;
	mode_t *old;

	/* FAT filesystems don't support exec bit, nor group/world bits */
	if (!cl_is_chmod_supported())
		return;

	cl_assert((old = git__malloc(sizeof(mode_t))) != NULL);
	*old = p_umask(022);
	cl_set_cleanup(cleanup_chmod_root, old);

277 278
	cl_git_pass(git_futils_mkdir("r", 0777, 0));
	cl_git_pass(git_futils_mkdir_relative("mode/is/important", "r", 0777, GIT_MKDIR_PATH, NULL));
279 280 281 282 283 284 285 286
	cl_git_pass(git_path_lstat("r/mode", &st));
	check_mode(0755, st.st_mode);

	cl_must_pass(p_chmod("r/mode", 0111));
	cl_git_pass(git_path_lstat("r/mode", &st));
	check_mode(0111, st.st_mode);

	cl_git_pass(
287
		git_futils_mkdir_relative("mode/is/okay/inside", "r", 0777, GIT_MKDIR_PATH, NULL));
288 289 290 291 292
	cl_git_pass(git_path_lstat("r/mode/is/okay/inside", &st));
	check_mode(0755, st.st_mode);

	cl_must_pass(p_chmod("r/mode", 0777));
}