copy.c 4.67 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
3 4
#include "posix.h"

5
void test_copy__file(void)
6
{
7
	struct stat st = {0};
8 9 10 11
	const char *content = "This is some stuff to copy\n";

	cl_git_mkfile("copy_me", content);

12
	cl_git_pass(git_futils_cp("copy_me", "copy_me_two", 0664));
13

14
	cl_git_pass(git_fs_path_lstat("copy_me_two", &st));
15
	cl_assert(S_ISREG(st.st_mode));
16 17 18

	if (!cl_is_env_set("GITTEST_FLAKY_STAT"))
		cl_assert_equal_sz(strlen(content), (size_t)st.st_size);
19 20 21 22 23

	cl_git_pass(p_unlink("copy_me_two"));
	cl_git_pass(p_unlink("copy_me"));
}

24
void test_copy__file_in_dir(void)
25
{
26
	struct stat st = {0};
27 28
	const char *content = "This is some other stuff to copy\n";

29
	cl_git_pass(git_futils_mkdir("an_dir/in_a_dir", 0775, GIT_MKDIR_PATH));
30
	cl_git_mkfile("an_dir/in_a_dir/copy_me", content);
31
	cl_assert(git_fs_path_isdir("an_dir"));
32

33 34 35 36
	cl_git_pass(git_futils_mkpath2file
		("an_dir/second_dir/and_more/copy_me_two", 0775));

	cl_git_pass(git_futils_cp
37 38
		("an_dir/in_a_dir/copy_me",
		 "an_dir/second_dir/and_more/copy_me_two",
39
		 0664));
40

41
	cl_git_pass(git_fs_path_lstat("an_dir/second_dir/and_more/copy_me_two", &st));
42
	cl_assert(S_ISREG(st.st_mode));
43 44 45

	if (!cl_is_env_set("GITTEST_FLAKY_STAT"))
		cl_assert_equal_sz(strlen(content), (size_t)st.st_size);
46

47
	cl_git_pass(git_futils_rmdir_r("an_dir", NULL, GIT_RMDIR_REMOVE_FILES));
48
	cl_assert(!git_fs_path_isdir("an_dir"));
49 50
}

51 52
#ifndef GIT_WIN32
static void assert_hard_link(const char *path)
53 54 55 56
{
	/* we assert this by checking that there's more than one link to the file */
	struct stat st;

57
	cl_assert(git_fs_path_isfile(path));
58 59 60
	cl_git_pass(p_stat(path, &st));
	cl_assert(st.st_nlink > 1);
}
61
#endif
62

63
void test_copy__tree(void)
64 65 66 67
{
	struct stat st;
	const char *content = "File content\n";

68 69 70
	cl_git_pass(git_futils_mkdir("src/b", 0775, GIT_MKDIR_PATH));
	cl_git_pass(git_futils_mkdir("src/c/d", 0775, GIT_MKDIR_PATH));
	cl_git_pass(git_futils_mkdir("src/c/e", 0775, GIT_MKDIR_PATH));
71 72 73 74 75 76 77 78 79 80 81

	cl_git_mkfile("src/f1", content);
	cl_git_mkfile("src/b/f2", content);
	cl_git_mkfile("src/c/f3", content);
	cl_git_mkfile("src/c/d/f4", content);
	cl_git_mkfile("src/c/d/.f5", content);

#ifndef GIT_WIN32
	cl_assert(p_symlink("../../b/f2", "src/c/d/l1") == 0);
#endif

82 83 84 85
	cl_assert(git_fs_path_isdir("src"));
	cl_assert(git_fs_path_isdir("src/b"));
	cl_assert(git_fs_path_isdir("src/c/d"));
	cl_assert(git_fs_path_isfile("src/c/d/f4"));
86 87 88 89 90 91

	/* copy with no empty dirs, yes links, no dotfiles, no overwrite */

	cl_git_pass(
		git_futils_cp_r("src", "t1", GIT_CPDIR_COPY_SYMLINKS, 0) );

92 93 94 95 96
	cl_assert(git_fs_path_isdir("t1"));
	cl_assert(git_fs_path_isdir("t1/b"));
	cl_assert(git_fs_path_isdir("t1/c"));
	cl_assert(git_fs_path_isdir("t1/c/d"));
	cl_assert(!git_fs_path_isdir("t1/c/e"));
97

98 99 100 101 102
	cl_assert(git_fs_path_isfile("t1/f1"));
	cl_assert(git_fs_path_isfile("t1/b/f2"));
	cl_assert(git_fs_path_isfile("t1/c/f3"));
	cl_assert(git_fs_path_isfile("t1/c/d/f4"));
	cl_assert(!git_fs_path_isfile("t1/c/d/.f5"));
103

104
	memset(&st, 0, sizeof(struct stat));
105
	cl_git_pass(git_fs_path_lstat("t1/c/f3", &st));
106
	cl_assert(S_ISREG(st.st_mode));
107 108 109

	if (!cl_is_env_set("GITTEST_FLAKY_STAT"))
		cl_assert_equal_sz(strlen(content), (size_t)st.st_size);
110 111

#ifndef GIT_WIN32
112
	memset(&st, 0, sizeof(struct stat));
113
	cl_git_pass(git_fs_path_lstat("t1/c/d/l1", &st));
114 115 116
	cl_assert(S_ISLNK(st.st_mode));
#endif

117
	cl_git_pass(git_futils_rmdir_r("t1", NULL, GIT_RMDIR_REMOVE_FILES));
118
	cl_assert(!git_fs_path_isdir("t1"));
119 120 121 122 123 124

	/* copy with empty dirs, no links, yes dotfiles, no overwrite */

	cl_git_pass(
		git_futils_cp_r("src", "t2", GIT_CPDIR_CREATE_EMPTY_DIRS | GIT_CPDIR_COPY_DOTFILES, 0) );

125 126 127 128 129
	cl_assert(git_fs_path_isdir("t2"));
	cl_assert(git_fs_path_isdir("t2/b"));
	cl_assert(git_fs_path_isdir("t2/c"));
	cl_assert(git_fs_path_isdir("t2/c/d"));
	cl_assert(git_fs_path_isdir("t2/c/e"));
130

131 132 133 134 135
	cl_assert(git_fs_path_isfile("t2/f1"));
	cl_assert(git_fs_path_isfile("t2/b/f2"));
	cl_assert(git_fs_path_isfile("t2/c/f3"));
	cl_assert(git_fs_path_isfile("t2/c/d/f4"));
	cl_assert(git_fs_path_isfile("t2/c/d/.f5"));
136 137

#ifndef GIT_WIN32
138
	memset(&st, 0, sizeof(struct stat));
139
	cl_git_fail(git_fs_path_lstat("t2/c/d/l1", &st));
140 141
#endif

142
	cl_git_pass(git_futils_rmdir_r("t2", NULL, GIT_RMDIR_REMOVE_FILES));
143
	cl_assert(!git_fs_path_isdir("t2"));
144

145 146
#ifndef GIT_WIN32
	cl_git_pass(git_futils_cp_r("src", "t3", GIT_CPDIR_CREATE_EMPTY_DIRS | GIT_CPDIR_LINK_FILES, 0));
147
	cl_assert(git_fs_path_isdir("t3"));
148

149 150 151 152 153
	cl_assert(git_fs_path_isdir("t3"));
	cl_assert(git_fs_path_isdir("t3/b"));
	cl_assert(git_fs_path_isdir("t3/c"));
	cl_assert(git_fs_path_isdir("t3/c/d"));
	cl_assert(git_fs_path_isdir("t3/c/e"));
154 155 156 157 158 159 160

	assert_hard_link("t3/f1");
	assert_hard_link("t3/b/f2");
	assert_hard_link("t3/c/f3");
	assert_hard_link("t3/c/d/f4");
#endif

161
	cl_git_pass(git_futils_rmdir_r("src", NULL, GIT_RMDIR_REMOVE_FILES));
162
}