This source file includes following definitions.
- copy_file_range
- ioctl
- my_clonefile
- reflink
- setup
- teardown
- prepare_files
- cleanup_files
- START_TEST
- START_TEST
- main
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 #define TEST_SUITE_NAME "/lib/vfs"
27
28 #include "tests/mctest.h"
29
30 #include <stdarg.h>
31 #include <stdlib.h>
32
33 #ifdef HAVE_FICLONERANGE
34 #include <linux/fs.h>
35 #include <sys/ioctl.h>
36 #elif defined(HAVE_COPY_FILE_RANGE)
37 #include <unistd.h>
38 #elif defined(HAVE_REFLINK)
39 #include <unistd.h>
40 #elif defined(HAVE_SYS_CLONEFILE_H)
41 #include <sys/clonefile.h>
42 #endif
43
44 #include "lib/strutil.h"
45 #include "lib/util.h"
46 #include "src/vfs/local/local.c"
47
48
49
50 static int clone_syscall__call_count = 0;
51 static gboolean clone_syscall__call_arguments_are_proper = FALSE;
52
53 static const char test_filename1[] = "mctestclone1.tst";
54 static const char test_filename2[] = "mctestclone2.tst";
55
56 #ifdef HAVE_COPY_FILE_RANGE
57
58 ssize_t
59 copy_file_range (int infd, off_t *inoffp, int outfd, off_t *outoffp, size_t len, unsigned int flags)
60 {
61 (void) infd;
62 (void) inoffp;
63 (void) outfd;
64 (void) outoffp;
65 (void) len;
66
67 clone_syscall__call_count++;
68 clone_syscall__call_arguments_are_proper = (flags == COPY_FILE_RANGE_CLONE);
69
70 return -1;
71 }
72 #endif
73
74 #ifdef HAVE_FICLONERANGE
75 #ifdef __GLIBC__
76
77 int
78 ioctl (int fd, unsigned long request, ...)
79 #else
80
81 int
82 ioctl (int fd, int request, ...)
83 #endif
84 {
85 (void) fd;
86
87 clone_syscall__call_count++;
88 clone_syscall__call_arguments_are_proper = (request == FICLONERANGE);
89 return -1;
90 }
91 #endif
92
93 #ifdef HAVE_SYS_CLONEFILE_H
94
95 int
96 my_clonefile (const char *src, const char *dst, uint32_t flags)
97 {
98 (void) src;
99 (void) dst;
100
101 clone_syscall__call_count++;
102 clone_syscall__call_arguments_are_proper = (flags == 0);
103 return -1;
104 }
105 #endif
106
107 #ifdef HAVE_REFLINK
108
109 int
110 reflink (const char *src, const char *dst, int preserve)
111 {
112 (void) src;
113 (void) dst;
114
115 clone_syscall__call_count++;
116 clone_syscall__call_arguments_are_proper = (preserve != 0);
117 return -1;
118 }
119 #endif
120
121
122
123
124 static void
125 setup (void)
126 {
127 str_init_strings (NULL);
128
129 vfs_init ();
130 vfs_init_localfs ();
131 vfs_setup_work_dir ();
132 }
133
134
135
136
137 static void
138 teardown (void)
139 {
140 vfs_shut ();
141 str_uninit_strings ();
142 }
143
144
145 extern GPtrArray *vfs_openfiles;
146 static void
147 prepare_files (vfs_path_t **vpath1, vfs_path_t **vpath2)
148 {
149 unlink (test_filename1);
150 g_file_set_contents (test_filename1, "test", sizeof ("test") - 1, NULL);
151 unlink (test_filename2);
152
153 *vpath1 = vfs_path_from_str (test_filename1);
154 *vpath2 = vfs_path_from_str (test_filename2);
155 }
156
157 static void
158 cleanup_files (vfs_path_t *vpath1, vfs_path_t *vpath2)
159 {
160 vfs_path_free (vpath1, TRUE);
161 vfs_path_free (vpath2, TRUE);
162 unlink (test_filename1);
163 unlink (test_filename2);
164 }
165
166
167 START_TEST (test_vfs_clone_file)
168 {
169 vfs_path_t *vpath1;
170 vfs_path_t *vpath2;
171 int fdin;
172 int fdout;
173
174
175 clone_syscall__call_count = 0;
176 clone_syscall__call_arguments_are_proper = FALSE;
177 prepare_files (&vpath1, &vpath2);
178 fdin = mc_open (vpath1, O_RDONLY | O_BINARY);
179 fdout = mc_open (vpath2, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0600);
180
181
182 vfs_clone_file (fdout, fdin);
183
184
185 #ifdef HAVE_FILE_CLONING_BY_RANGE
186 ck_assert (clone_syscall__call_count > 0);
187 ck_assert (clone_syscall__call_arguments_are_proper);
188 #else
189 ck_assert (errno == ENOTSUP);
190 #endif
191
192
193 mc_close (fdout);
194 mc_close (fdin);
195 cleanup_files (vpath1, vpath2);
196 }
197 END_TEST
198
199
200
201
202 START_TEST (test_vfs_clone_file_by_path)
203 {
204 vfs_path_t *vpath1;
205 vfs_path_t *vpath2;
206
207
208 clone_syscall__call_count = 0;
209 clone_syscall__call_arguments_are_proper = FALSE;
210 prepare_files (&vpath1, &vpath2);
211
212
213 vfs_clone_file_by_path (vpath1, vpath2, TRUE);
214
215
216 #ifdef HAVE_FILE_CLONING_BY_PATH
217 ck_assert (clone_syscall__call_count > 0);
218 ck_assert (clone_syscall__call_arguments_are_proper);
219 #else
220 ck_assert (errno == ENOTSUP);
221 #endif
222
223
224 cleanup_files (vpath1, vpath2);
225 }
226 END_TEST
227
228
229
230 int
231 main (void)
232 {
233 TCase *tc_core;
234
235 tc_core = tcase_create ("Core");
236
237 tcase_add_checked_fixture (tc_core, setup, teardown);
238
239
240 tcase_add_test (tc_core, test_vfs_clone_file);
241 tcase_add_test (tc_core, test_vfs_clone_file_by_path);
242
243
244 return mctest_run_all (tc_core);
245 }