]> git.proxmox.com Git - libgit2.git/blame - tests/core/copy.c
fileops: allow linking files when copying directory structures
[libgit2.git] / tests / core / copy.c
CommitLineData
ca1b6e54
RB
1#include "clar_libgit2.h"
2#include "fileops.h"
3#include "path.h"
4#include "posix.h"
5
6void test_core_copy__file(void)
7{
8 struct stat st;
9 const char *content = "This is some stuff to copy\n";
10
11 cl_git_mkfile("copy_me", content);
12
85bd1746 13 cl_git_pass(git_futils_cp("copy_me", "copy_me_two", 0664));
ca1b6e54
RB
14
15 cl_git_pass(git_path_lstat("copy_me_two", &st));
16 cl_assert(S_ISREG(st.st_mode));
17 cl_assert(strlen(content) == (size_t)st.st_size);
18
19 cl_git_pass(p_unlink("copy_me_two"));
20 cl_git_pass(p_unlink("copy_me"));
21}
22
23void test_core_copy__file_in_dir(void)
24{
25 struct stat st;
26 const char *content = "This is some other stuff to copy\n";
27
28 cl_git_pass(git_futils_mkdir("an_dir/in_a_dir", NULL, 0775, GIT_MKDIR_PATH));
29 cl_git_mkfile("an_dir/in_a_dir/copy_me", content);
30 cl_assert(git_path_isdir("an_dir"));
31
85bd1746
RB
32 cl_git_pass(git_futils_mkpath2file
33 ("an_dir/second_dir/and_more/copy_me_two", 0775));
34
35 cl_git_pass(git_futils_cp
ca1b6e54
RB
36 ("an_dir/in_a_dir/copy_me",
37 "an_dir/second_dir/and_more/copy_me_two",
85bd1746 38 0664));
ca1b6e54
RB
39
40 cl_git_pass(git_path_lstat("an_dir/second_dir/and_more/copy_me_two", &st));
41 cl_assert(S_ISREG(st.st_mode));
42 cl_assert(strlen(content) == (size_t)st.st_size);
43
331e7de9 44 cl_git_pass(git_futils_rmdir_r("an_dir", NULL, GIT_RMDIR_REMOVE_FILES));
ca1b6e54
RB
45 cl_assert(!git_path_isdir("an_dir"));
46}
47
94f742ba
CMN
48void assert_hard_link(const char *path)
49{
50 /* we assert this by checking that there's more than one link to the file */
51 struct stat st;
52
53 cl_assert(git_path_isfile(path));
54 cl_git_pass(p_stat(path, &st));
55 cl_assert(st.st_nlink > 1);
56}
57
ca1b6e54
RB
58void test_core_copy__tree(void)
59{
60 struct stat st;
61 const char *content = "File content\n";
62
63 cl_git_pass(git_futils_mkdir("src/b", NULL, 0775, GIT_MKDIR_PATH));
64 cl_git_pass(git_futils_mkdir("src/c/d", NULL, 0775, GIT_MKDIR_PATH));
65 cl_git_pass(git_futils_mkdir("src/c/e", NULL, 0775, GIT_MKDIR_PATH));
66
67 cl_git_mkfile("src/f1", content);
68 cl_git_mkfile("src/b/f2", content);
69 cl_git_mkfile("src/c/f3", content);
70 cl_git_mkfile("src/c/d/f4", content);
71 cl_git_mkfile("src/c/d/.f5", content);
72
73#ifndef GIT_WIN32
74 cl_assert(p_symlink("../../b/f2", "src/c/d/l1") == 0);
75#endif
76
77 cl_assert(git_path_isdir("src"));
78 cl_assert(git_path_isdir("src/b"));
79 cl_assert(git_path_isdir("src/c/d"));
80 cl_assert(git_path_isfile("src/c/d/f4"));
81
82 /* copy with no empty dirs, yes links, no dotfiles, no overwrite */
83
84 cl_git_pass(
85 git_futils_cp_r("src", "t1", GIT_CPDIR_COPY_SYMLINKS, 0) );
86
87 cl_assert(git_path_isdir("t1"));
88 cl_assert(git_path_isdir("t1/b"));
89 cl_assert(git_path_isdir("t1/c"));
90 cl_assert(git_path_isdir("t1/c/d"));
91 cl_assert(!git_path_isdir("t1/c/e"));
92
93 cl_assert(git_path_isfile("t1/f1"));
94 cl_assert(git_path_isfile("t1/b/f2"));
95 cl_assert(git_path_isfile("t1/c/f3"));
96 cl_assert(git_path_isfile("t1/c/d/f4"));
97 cl_assert(!git_path_isfile("t1/c/d/.f5"));
98
99 cl_git_pass(git_path_lstat("t1/c/f3", &st));
100 cl_assert(S_ISREG(st.st_mode));
101 cl_assert(strlen(content) == (size_t)st.st_size);
102
103#ifndef GIT_WIN32
104 cl_git_pass(git_path_lstat("t1/c/d/l1", &st));
105 cl_assert(S_ISLNK(st.st_mode));
106#endif
107
331e7de9 108 cl_git_pass(git_futils_rmdir_r("t1", NULL, GIT_RMDIR_REMOVE_FILES));
ca1b6e54
RB
109 cl_assert(!git_path_isdir("t1"));
110
111 /* copy with empty dirs, no links, yes dotfiles, no overwrite */
112
113 cl_git_pass(
114 git_futils_cp_r("src", "t2", GIT_CPDIR_CREATE_EMPTY_DIRS | GIT_CPDIR_COPY_DOTFILES, 0) );
115
116 cl_assert(git_path_isdir("t2"));
117 cl_assert(git_path_isdir("t2/b"));
118 cl_assert(git_path_isdir("t2/c"));
119 cl_assert(git_path_isdir("t2/c/d"));
120 cl_assert(git_path_isdir("t2/c/e"));
121
122 cl_assert(git_path_isfile("t2/f1"));
123 cl_assert(git_path_isfile("t2/b/f2"));
124 cl_assert(git_path_isfile("t2/c/f3"));
125 cl_assert(git_path_isfile("t2/c/d/f4"));
126 cl_assert(git_path_isfile("t2/c/d/.f5"));
127
128#ifndef GIT_WIN32
129 cl_git_fail(git_path_lstat("t2/c/d/l1", &st));
130#endif
131
331e7de9 132 cl_git_pass(git_futils_rmdir_r("t2", NULL, GIT_RMDIR_REMOVE_FILES));
ca1b6e54
RB
133 cl_assert(!git_path_isdir("t2"));
134
94f742ba
CMN
135#ifndef GIT_WIN32
136 cl_git_pass(git_futils_cp_r("src", "t3", GIT_CPDIR_CREATE_EMPTY_DIRS | GIT_CPDIR_LINK_FILES, 0));
137 cl_assert(git_path_isdir("t3"));
138
139 cl_assert(git_path_isdir("t3"));
140 cl_assert(git_path_isdir("t3/b"));
141 cl_assert(git_path_isdir("t3/c"));
142 cl_assert(git_path_isdir("t3/c/d"));
143 cl_assert(git_path_isdir("t3/c/e"));
144
145 assert_hard_link("t3/f1");
146 assert_hard_link("t3/b/f2");
147 assert_hard_link("t3/c/f3");
148 assert_hard_link("t3/c/d/f4");
149#endif
150
331e7de9 151 cl_git_pass(git_futils_rmdir_r("src", NULL, GIT_RMDIR_REMOVE_FILES));
ca1b6e54 152}