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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
260
261
|
#include "clar_libgit2.h"
#include "odb.h"
#include "git2/odb_backend.h"
#include "posix.h"
#include "loose_data.h"
#include "repository.h"
#ifdef __ANDROID_API__
# define S_IREAD S_IRUSR
# define S_IWRITE S_IWUSR
#endif
static void write_object_files(object_data *d)
{
int fd;
if (p_mkdir(d->dir, GIT_OBJECT_DIR_MODE) < 0)
cl_assert(errno == EEXIST);
cl_assert((fd = p_creat(d->file, S_IREAD | S_IWRITE)) >= 0);
cl_must_pass(p_write(fd, d->bytes, d->blen));
p_close(fd);
}
static void cmp_objects(git_rawobj *o, object_data *d)
{
cl_assert(o->type == git_object_string2type(d->type));
cl_assert(o->len == d->dlen);
if (o->len > 0)
cl_assert(memcmp(o->data, d->data, o->len) == 0);
}
static void test_read_object(object_data *data)
{
git_oid id;
git_odb_object *obj;
git_odb *odb;
git_rawobj tmp;
write_object_files(data);
cl_git_pass(git_odb_open(&odb, "test-objects"));
cl_git_pass(git_oid_fromstr(&id, data->id));
cl_git_pass(git_odb_read(&obj, odb, &id));
tmp.data = obj->buffer;
tmp.len = obj->cached.size;
tmp.type = obj->cached.type;
cmp_objects(&tmp, data);
git_odb_object_free(obj);
git_odb_free(odb);
}
static void test_readstream_object(object_data *data, size_t blocksize)
{
git_oid id;
git_odb *odb;
git_odb_stream *stream;
git_rawobj tmp;
char buf[2048], *ptr = buf;
size_t remain;
int ret;
write_object_files(data);
cl_git_pass(git_odb_open(&odb, "test-objects"));
cl_git_pass(git_oid_fromstr(&id, data->id));
cl_git_pass(git_odb_open_rstream(&stream, &tmp.len, &tmp.type, odb, &id));
remain = tmp.len;
while (remain) {
cl_assert((ret = git_odb_stream_read(stream, ptr, blocksize)) >= 0);
if (ret == 0)
break;
cl_assert(remain >= (size_t)ret);
remain -= ret;
ptr += ret;
}
cl_assert(remain == 0);
tmp.data = buf;
cmp_objects(&tmp, data);
git_odb_stream_free(stream);
git_odb_free(odb);
}
void test_odb_loose__initialize(void)
{
p_fsync__cnt = 0;
cl_must_pass(p_mkdir("test-objects", GIT_OBJECT_DIR_MODE));
}
void test_odb_loose__cleanup(void)
{
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 0));
cl_fixture_cleanup("test-objects");
}
void test_odb_loose__exists(void)
{
git_oid id, id2;
git_odb *odb;
write_object_files(&one);
cl_git_pass(git_odb_open(&odb, "test-objects"));
cl_git_pass(git_oid_fromstr(&id, one.id));
cl_assert(git_odb_exists(odb, &id));
cl_git_pass(git_oid_fromstrp(&id, "8b137891"));
cl_git_pass(git_odb_exists_prefix(&id2, odb, &id, 8));
cl_assert_equal_i(0, git_oid_streq(&id2, one.id));
/* Test for a missing object */
cl_git_pass(git_oid_fromstr(&id, "8b137891791fe96927ad78e64b0aad7bded08baa"));
cl_assert(!git_odb_exists(odb, &id));
cl_git_pass(git_oid_fromstrp(&id, "8b13789a"));
cl_assert_equal_i(GIT_ENOTFOUND, git_odb_exists_prefix(&id2, odb, &id, 8));
git_odb_free(odb);
}
void test_odb_loose__simple_reads(void)
{
test_read_object(&commit);
test_read_object(&tree);
test_read_object(&tag);
test_read_object(&zero);
test_read_object(&one);
test_read_object(&two);
test_read_object(&some);
}
void test_odb_loose__streaming_reads(void)
{
size_t blocksizes[] = { 1, 2, 4, 16, 99, 1024, 123456789 };
size_t i;
for (i = 0; i < ARRAY_SIZE(blocksizes); i++) {
test_readstream_object(&commit, blocksizes[i]);
test_readstream_object(&tree, blocksizes[i]);
test_readstream_object(&tag, blocksizes[i]);
test_readstream_object(&zero, blocksizes[i]);
test_readstream_object(&one, blocksizes[i]);
test_readstream_object(&two, blocksizes[i]);
test_readstream_object(&some, blocksizes[i]);
}
}
void test_write_object_permission(
mode_t dir_mode, mode_t file_mode,
mode_t expected_dir_mode, mode_t expected_file_mode)
{
git_odb *odb;
git_odb_backend *backend;
git_oid oid;
struct stat statbuf;
mode_t mask, os_mask;
/* Windows does not return group/user bits from stat,
* files are never executable.
*/
#ifdef GIT_WIN32
os_mask = 0600;
#else
os_mask = 0777;
#endif
mask = p_umask(0);
p_umask(mask);
cl_git_pass(git_odb_new(&odb));
cl_git_pass(git_odb_backend_loose(&backend, "test-objects", -1, 0, dir_mode, file_mode));
cl_git_pass(git_odb_add_backend(odb, backend, 1));
cl_git_pass(git_odb_write(&oid, odb, "Test data\n", 10, GIT_OBJ_BLOB));
cl_git_pass(p_stat("test-objects/67", &statbuf));
cl_assert_equal_i(statbuf.st_mode & os_mask, (expected_dir_mode & ~mask) & os_mask);
cl_git_pass(p_stat("test-objects/67/b808feb36201507a77f85e6d898f0a2836e4a5", &statbuf));
cl_assert_equal_i(statbuf.st_mode & os_mask, (expected_file_mode & ~mask) & os_mask);
git_odb_free(odb);
}
void test_odb_loose__permissions_standard(void)
{
test_write_object_permission(0, 0, GIT_OBJECT_DIR_MODE, GIT_OBJECT_FILE_MODE);
}
void test_odb_loose_permissions_readonly(void)
{
test_write_object_permission(0777, 0444, 0777, 0444);
}
void test_odb_loose__permissions_readwrite(void)
{
test_write_object_permission(0777, 0666, 0777, 0666);
}
static void write_object_to_loose_odb(int fsync)
{
git_odb *odb;
git_odb_backend *backend;
git_oid oid;
cl_git_pass(git_odb_new(&odb));
cl_git_pass(git_odb_backend_loose(&backend, "test-objects", -1, fsync, 0777, 0666));
cl_git_pass(git_odb_add_backend(odb, backend, 1));
cl_git_pass(git_odb_write(&oid, odb, "Test data\n", 10, GIT_OBJ_BLOB));
git_odb_free(odb);
}
void test_odb_loose__does_not_fsync_by_default(void)
{
write_object_to_loose_odb(0);
cl_assert_equal_sz(0, p_fsync__cnt);
}
void test_odb_loose__fsync_obeys_odb_option(void)
{
write_object_to_loose_odb(1);
cl_assert(p_fsync__cnt > 0);
}
void test_odb_loose__fsync_obeys_global_setting(void)
{
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_FSYNC_GITDIR, 1));
write_object_to_loose_odb(0);
cl_assert(p_fsync__cnt > 0);
}
void test_odb_loose__fsync_obeys_repo_setting(void)
{
git_repository *repo;
git_odb *odb;
git_oid oid;
cl_git_pass(git_repository_init(&repo, "test-objects", 1));
cl_git_pass(git_repository_odb__weakptr(&odb, repo));
cl_git_pass(git_odb_write(&oid, odb, "No fsync here\n", 14, GIT_OBJ_BLOB));
cl_assert(p_fsync__cnt == 0);
git_repository_free(repo);
cl_git_pass(git_repository_open(&repo, "test-objects"));
cl_repo_set_bool(repo, "core.fsyncObjectFiles", true);
cl_git_pass(git_repository_odb__weakptr(&odb, repo));
cl_git_pass(git_odb_write(&oid, odb, "Now fsync\n", 10, GIT_OBJ_BLOB));
cl_assert(p_fsync__cnt > 0);
git_repository_free(repo);
}
|