Commit ce8cd006ce3adcd012a38401b8a7555ba3307b3f

Brodie Rao 2011-09-07T15:32:44

fileops/repository: create (most) directories with 0777 permissions To further match how Git behaves, this change makes most of the directories libgit2 creates in a git repo have a file mode of 0777. Specifically: - Intermediate directories created with git_futils_mkpath2file() have 0777 permissions. This affects odb_loose, reflog, and refs. - The top level folder for bare repos is created with 0777 permissions. - The top level folder for non-bare repos is created with 0755 permissions. - /objects/info/, /objects/pack/, /refs/heads/, and /refs/tags/ are created with 0777 permissions. Additionally, the following changes have been made: - fileops functions that create intermediate directories have grown a new dirmode parameter. The only exception to this is filebuf's lock_file(), which unconditionally creates intermediate directories with 0777 permissions when GIT_FILEBUF_FORCE is set. - The test runner now sets the umask to 0 before running any tests. This ensurses all file mode checks are consistent across systems. - t09-tree.c now does a directory permissions check. I've avoided adding this check to other tests that might reuse existing directories from the prefabricated test repos. Because they're checked into the repo, they have 0755 permissions. - Other assorted directories created by tests have 0777 permissions.

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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
diff --git a/src/filebuf.c b/src/filebuf.c
index 1a98e3f..e6167d0 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -23,7 +23,8 @@ static int lock_file(git_filebuf *file, int flags)
 
 	/* create path to the file buffer is required */
 	if (flags & GIT_FILEBUF_FORCE) {
-		file->fd = git_futils_creat_locked_withpath(file->path_lock, 0644);
+		/* XXX: Should dirmode here be configurable? Or is 0777 always fine? */
+		file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, 0644);
 	} else {
 		file->fd = git_futils_creat_locked(file->path_lock, 0644);
 	}
diff --git a/src/fileops.c b/src/fileops.c
index da9c55f..ff36b00 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -31,9 +31,8 @@ int git_futils_mv_atomic(const char *from, const char *to)
 #endif
 }
 
-int git_futils_mkpath2file(const char *file_path)
+int git_futils_mkpath2file(const char *file_path, const mode_t mode)
 {
-	const mode_t mode = 0755; /* or 0777 ? */
 	int error = GIT_SUCCESS;
 	char target_folder_path[GIT_PATH_MAX];
 
@@ -67,9 +66,9 @@ int git_futils_mktmp(char *path_out, const char *filename)
 	return fd;
 }
 
-int git_futils_creat_withpath(const char *path, const mode_t mode)
+int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode)
 {
-	if (git_futils_mkpath2file(path) < GIT_SUCCESS)
+	if (git_futils_mkpath2file(path, dirmode) < GIT_SUCCESS)
 		return git__throw(GIT_EOSERR, "Failed to create file %s", path);
 
 	return p_creat(path, mode);
@@ -81,9 +80,9 @@ int git_futils_creat_locked(const char *path, const mode_t mode)
 	return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to create locked file. Could not open %s", path);
 }
 
-int git_futils_creat_locked_withpath(const char *path, const mode_t mode)
+int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode)
 {
-	if (git_futils_mkpath2file(path) < GIT_SUCCESS)
+	if (git_futils_mkpath2file(path, dirmode) < GIT_SUCCESS)
 		return git__throw(GIT_EOSERR, "Failed to create locked file %s", path);
 
 	return git_futils_creat_locked(path, mode);
@@ -212,9 +211,9 @@ void git_futils_freebuffer(git_fbuffer *obj)
 }
 
 
-int git_futils_mv_withpath(const char *from, const char *to)
+int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
 {
-	if (git_futils_mkpath2file(to) < GIT_SUCCESS)
+	if (git_futils_mkpath2file(to, dirmode) < GIT_SUCCESS)
 		return GIT_EOSERR;	/* The callee already takes care of setting the correct error message. */
 
 	return git_futils_mv_atomic(from, to);	/* The callee already takes care of setting the correct error message. */
diff --git a/src/fileops.h b/src/fileops.h
index 1768888..56c4770 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -48,7 +48,7 @@ extern int git_futils_exists(const char *path);
  * Create and open a file, while also
  * creating all the folders in its path
  */
-extern int git_futils_creat_withpath(const char *path, const mode_t mode);
+extern int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode);
 
 /**
  * Create an open a process-locked file
@@ -59,7 +59,7 @@ extern int git_futils_creat_locked(const char *path, const mode_t mode);
  * Create an open a process-locked file, while
  * also creating all the folders in its path
  */
-extern int git_futils_creat_locked_withpath(const char *path, const mode_t mode);
+extern int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode);
 
 /**
  * Check if the given path points to a directory
@@ -80,7 +80,7 @@ extern int git_futils_mkdir_r(const char *path, const mode_t mode);
  * Create all the folders required to contain
  * the full path of a file
  */
-extern int git_futils_mkpath2file(const char *path);
+extern int git_futils_mkpath2file(const char *path, const mode_t mode);
 
 extern int git_futils_rmdir_r(const char *path, int force);
 
@@ -98,7 +98,7 @@ extern int git_futils_mv_atomic(const char *from, const char *to);
  * Move a file on the filesystem, create the
  * destination path if it doesn't exist
  */
-extern int git_futils_mv_withpath(const char *from, const char *to);
+extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode);
 
 
 /**
diff --git a/src/odb.h b/src/odb.h
index 4e85091..7c8c9f9 100644
--- a/src/odb.h
+++ b/src/odb.h
@@ -14,6 +14,9 @@
 #include "vector.h"
 #include "cache.h"
 
+#define GIT_OBJECTS_DIR "objects/"
+#define GIT_OBJECT_DIR_MODE 0777
+
 /* DO NOT EXPORT */
 typedef struct {
 	void *data;			/**< Raw, decompressed object data. */
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 80f0aa9..a3013d3 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -666,7 +666,7 @@ static int loose_backend__stream_fwrite(git_oid *oid, git_odb_stream *_stream)
 	if (object_file_name(final_path, sizeof(final_path), backend->objects_dir, oid))
 		return GIT_ENOMEM;
 
-	if ((error = git_futils_mkpath2file(final_path)) < GIT_SUCCESS)
+	if ((error = git_futils_mkpath2file(final_path, GIT_OBJECT_DIR_MODE)) < GIT_SUCCESS)
 		return git__rethrow(error, "Failed to write loose backend");
 
 	stream->finished = 1;
@@ -787,7 +787,7 @@ static int loose_backend__write(git_oid *oid, git_odb_backend *_backend, const v
 	if ((error = object_file_name(final_path, sizeof(final_path), backend->objects_dir, oid)) < GIT_SUCCESS)
 		goto cleanup;
 
-	if ((error = git_futils_mkpath2file(final_path)) < GIT_SUCCESS)
+	if ((error = git_futils_mkpath2file(final_path, GIT_OBJECT_DIR_MODE)) < GIT_SUCCESS)
 		goto cleanup;
 
 	return git_filebuf_commit_at(&fbuf, final_path);
diff --git a/src/posix.h b/src/posix.h
index 389578d..55cd35a 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -40,6 +40,7 @@ extern int p_write(git_file fd, const void *buf, size_t cnt);
 #define p_fstat(f,b) fstat(f, b)
 #define p_lseek(f,n,w) lseek(f, n, w)
 #define p_close(fd) close(fd)
+#define p_umask(m) umask(m)
 
 extern int p_open(const char *path, int flags);
 extern int p_creat(const char *path, mode_t mode);
diff --git a/src/reflog.c b/src/reflog.c
index 594963c..6cdb353 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -226,7 +226,7 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old,
 	git_path_join_n(log_path, 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name);
 
 	if (git_futils_exists(log_path)) {
-		if ((error = git_futils_mkpath2file(log_path)) < GIT_SUCCESS)
+		if ((error = git_futils_mkpath2file(log_path, GIT_REFLOG_DIR_MODE)) < GIT_SUCCESS)
 			return git__rethrow(error, "Failed to write reflog. Cannot create reflog directory");
 	} else if (git_futils_isfile(log_path)) {
 		return git__throw(GIT_ERROR, "Failed to write reflog. `%s` is directory", log_path);
diff --git a/src/reflog.h b/src/reflog.h
index 093874e..16e9a94 100644
--- a/src/reflog.h
+++ b/src/reflog.h
@@ -12,6 +12,7 @@
 #include "vector.h"
 
 #define GIT_REFLOG_DIR "logs/"
+#define GIT_REFLOG_DIR_MODE 0777
 
 #define GIT_REFLOG_SIZE_MIN (2*GIT_OID_HEXSZ+2+17)
 
diff --git a/src/refs.h b/src/refs.h
index c4b0b0e..f802cfe 100644
--- a/src/refs.h
+++ b/src/refs.h
@@ -16,6 +16,7 @@
 #define GIT_REFS_HEADS_DIR GIT_REFS_DIR "heads/"
 #define GIT_REFS_TAGS_DIR GIT_REFS_DIR "tags/"
 #define GIT_REFS_REMOTES_DIR GIT_REFS_DIR "remotes/"
+#define GIT_REFS_DIR_MODE 0777
 
 #define GIT_RENAMED_REF_FILE GIT_REFS_DIR "RENAMED-REF"
 
diff --git a/src/repository.c b/src/repository.c
index 36642e5..d583cad 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -609,12 +609,11 @@ static int repo_init_createhead(git_repository *repo)
 
 static int repo_init_structure(const char *git_dir, int is_bare)
 {
-	const mode_t mode = 0755; /* or 0777 ? */
 	int error;
 
 	char temp_path[GIT_PATH_MAX];
 
-	if (git_futils_mkdir_r(git_dir, mode))
+	if (git_futils_mkdir_r(git_dir, is_bare ? GIT_BARE_DIR_MODE : GIT_DIR_MODE))
 		return git__throw(GIT_ERROR, "Failed to initialize repository structure. Could not mkdir");
 
 	/* Hides the ".git" directory */
@@ -628,25 +627,25 @@ static int repo_init_structure(const char *git_dir, int is_bare)
 
 	/* Creates the '/objects/info/' directory */
 	git_path_join(temp_path, git_dir, GIT_OBJECTS_INFO_DIR);
-	error = git_futils_mkdir_r(temp_path, mode);
+	error = git_futils_mkdir_r(temp_path, GIT_OBJECT_DIR_MODE);
 	if (error < GIT_SUCCESS)
 		return git__rethrow(error, "Failed to initialize repository structure");
 
 	/* Creates the '/objects/pack/' directory */
 	git_path_join(temp_path, git_dir, GIT_OBJECTS_PACK_DIR);
-	error = p_mkdir(temp_path, mode);
+	error = p_mkdir(temp_path, GIT_OBJECT_DIR_MODE);
 	if (error < GIT_SUCCESS)
 		return git__throw(error, "Unable to create `%s` folder", temp_path);
 
 	/* Creates the '/refs/heads/' directory */
 	git_path_join(temp_path, git_dir, GIT_REFS_HEADS_DIR);
-	error = git_futils_mkdir_r(temp_path, mode);
+	error = git_futils_mkdir_r(temp_path, GIT_REFS_DIR_MODE);
 	if (error < GIT_SUCCESS)
 		return git__rethrow(error, "Failed to initialize repository structure");
 
 	/* Creates the '/refs/tags/' directory */
 	git_path_join(temp_path, git_dir, GIT_REFS_TAGS_DIR);
-	error = p_mkdir(temp_path, mode);
+	error = p_mkdir(temp_path, GIT_REFS_DIR_MODE);
 	if (error < GIT_SUCCESS)
 		return git__throw(error, "Unable to create `%s` folder", temp_path);
 
diff --git a/src/repository.h b/src/repository.h
index 99217e5..a12dd9d 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -18,10 +18,12 @@
 #include "cache.h"
 #include "refs.h"
 #include "buffer.h"
+#include "odb.h"
 
 #define DOT_GIT ".git"
 #define GIT_DIR DOT_GIT "/"
-#define GIT_OBJECTS_DIR "objects/"
+#define GIT_DIR_MODE 0755
+#define GIT_BARE_DIR_MODE 0777
 #define GIT_INDEX_FILE "index"
 
 struct git_object {
diff --git a/tests-clay/core/dirent.c b/tests-clay/core/dirent.c
index 73f5715..898b122 100644
--- a/tests-clay/core/dirent.c
+++ b/tests-clay/core/dirent.c
@@ -20,12 +20,12 @@ static void setup(walk_data *d)
 {
 	name_data *n;
 
-	cl_must_pass(p_mkdir(top_dir, 0755));
+	cl_must_pass(p_mkdir(top_dir, 0777));
 
 	cl_must_pass(p_chdir(top_dir));
 
 	if (strcmp(d->sub, ".") != 0)
-		cl_must_pass(p_mkdir(d->sub, 0755));
+		cl_must_pass(p_mkdir(d->sub, 0777));
 
 	strcpy(path_buffer, d->sub);
 	state_loc = d;
diff --git a/tests-clay/core/rmdir.c b/tests-clay/core/rmdir.c
index aa21c6a..03baecf 100644
--- a/tests-clay/core/rmdir.c
+++ b/tests-clay/core/rmdir.c
@@ -7,22 +7,22 @@ void test_core_rmdir__initialize(void)
 {
 	char path[GIT_PATH_MAX];
 
-	cl_must_pass(p_mkdir(empty_tmp_dir, 0755));
+	cl_must_pass(p_mkdir(empty_tmp_dir, 0777));
 
 	git_path_join(path, empty_tmp_dir, "/one");
-	cl_must_pass(p_mkdir(path, 0755));
+	cl_must_pass(p_mkdir(path, 0777));
 
 	git_path_join(path, empty_tmp_dir, "/one/two_one");
-	cl_must_pass(p_mkdir(path, 0755));
+	cl_must_pass(p_mkdir(path, 0777));
 
 	git_path_join(path, empty_tmp_dir, "/one/two_two");
-	cl_must_pass(p_mkdir(path, 0755));
+	cl_must_pass(p_mkdir(path, 0777));
 
 	git_path_join(path, empty_tmp_dir, "/one/two_two/three");
-	cl_must_pass(p_mkdir(path, 0755));
+	cl_must_pass(p_mkdir(path, 0777));
 
 	git_path_join(path, empty_tmp_dir, "/two");
-	cl_must_pass(p_mkdir(path, 0755));
+	cl_must_pass(p_mkdir(path, 0777));
 }
 
 /* make sure empty dir can be deleted recusively */
diff --git a/tests/t00-core.c b/tests/t00-core.c
index 703504b..1856815 100644
--- a/tests/t00-core.c
+++ b/tests/t00-core.c
@@ -250,14 +250,14 @@ static int setup(walk_data *d)
 {
 	name_data *n;
 
-	if (p_mkdir(top_dir, 0755) < 0)
+	if (p_mkdir(top_dir, 0777) < 0)
 		return error("can't mkdir(\"%s\")", top_dir);
 
 	if (p_chdir(top_dir) < 0)
 		return error("can't chdir(\"%s\")", top_dir);
 
 	if (strcmp(d->sub, ".") != 0)
-		if (p_mkdir(d->sub, 0755) < 0)
+		if (p_mkdir(d->sub, 0777) < 0)
 			return error("can't mkdir(\"%s\")", d->sub);
 
 	strcpy(path_buffer, d->sub);
@@ -510,27 +510,27 @@ static int setup_empty_tmp_dir(void)
 {
 	char path[GIT_PATH_MAX];
 
-	if (p_mkdir(empty_tmp_dir, 0755))
+	if (p_mkdir(empty_tmp_dir, 0777))
 		return -1;
 
 	git_path_join(path, empty_tmp_dir, "/one");
-	if (p_mkdir(path, 0755))
+	if (p_mkdir(path, 0777))
 		return -1;
 
 	git_path_join(path, empty_tmp_dir, "/one/two_one");
-	if (p_mkdir(path, 0755))
+	if (p_mkdir(path, 0777))
 		return -1;
 
 	git_path_join(path, empty_tmp_dir, "/one/two_two");
-	if (p_mkdir(path, 0755))
+	if (p_mkdir(path, 0777))
 		return -1;
 
 	git_path_join(path, empty_tmp_dir, "/one/two_two/three");
-	if (p_mkdir(path, 0755))
+	if (p_mkdir(path, 0777))
 		return -1;
 
 	git_path_join(path, empty_tmp_dir, "/two");
-	if (p_mkdir(path, 0755))
+	if (p_mkdir(path, 0777))
 		return -1;
 
 	return 0;
@@ -547,7 +547,7 @@ BEGIN_TEST(rmdir1, "make sure non-empty dir cannot be deleted recusively")
 
 	must_pass(setup_empty_tmp_dir());
 	git_path_join(file, empty_tmp_dir, "/two/file.txt");
-	fd = p_creat(file, 0755);
+	fd = p_creat(file, 0777);
 	must_pass(fd);
 	must_pass(p_close(fd));
 	must_fail(git_futils_rmdir_r(empty_tmp_dir, 0));
diff --git a/tests/t03-objwrite.c b/tests/t03-objwrite.c
index 31f611a..7563d0e 100644
--- a/tests/t03-objwrite.c
+++ b/tests/t03-objwrite.c
@@ -31,7 +31,7 @@ static char *odb_dir = "test-objects";
 
 static int make_odb_dir(void)
 {
-	if (p_mkdir(odb_dir, 0755) < 0) {
+	if (p_mkdir(odb_dir, GIT_OBJECT_DIR_MODE) < 0) {
 		int err = errno;
 		fprintf(stderr, "can't make directory \"%s\"", odb_dir);
 		if (err == EEXIST)
diff --git a/tests/t06-index.c b/tests/t06-index.c
index 621e742..dde98ab 100644
--- a/tests/t06-index.c
+++ b/tests/t06-index.c
@@ -176,7 +176,7 @@ BEGIN_TEST(add0, "add a new file to the index")
 	must_pass(git_index_entrycount(index) == 0);
 
 	/* Create a new file in the working directory */
-	must_pass(git_futils_mkpath2file(TEMP_REPO_FOLDER "myrepo/test.txt"));
+	must_pass(git_futils_mkpath2file(TEMP_REPO_FOLDER "myrepo/test.txt", 0777));
 	must_pass(git_filebuf_open(&file, TEMP_REPO_FOLDER "myrepo/test.txt", 0));
 	must_pass(git_filebuf_write(&file, "hey there\n", 10));
 	must_pass(git_filebuf_commit(&file));
diff --git a/tests/t09-tree.c b/tests/t09-tree.c
index 3af06ea..1f2fe3f 100644
--- a/tests/t09-tree.c
+++ b/tests/t09-tree.c
@@ -200,6 +200,9 @@ BEGIN_TEST(write3, "write a hierarchical tree from a memory")
 	// check data is correct
 	must_pass(git_tree_lookup(&tree, repo, &id_hiearar));
 	must_be_true(2 == git_tree_entrycount(tree));
+#ifndef GIT_WIN32
+	must_be_true((loose_object_dir_mode(TEMP_REPO_FOLDER, (git_object *)tree) & 0777) == GIT_OBJECT_DIR_MODE);
+#endif
 	git_tree_close(tree);
 
 	close_temp_repo(repo);
diff --git a/tests/t10-refs.c b/tests/t10-refs.c
index aa6735a..4cb31d5 100644
--- a/tests/t10-refs.c
+++ b/tests/t10-refs.c
@@ -431,12 +431,11 @@ END_TEST
 BEGIN_TEST(pack0, "create a packfile for an empty folder")
 	git_repository *repo;
 	char temp_path[GIT_PATH_MAX];
-	const mode_t mode = 0755; /* or 0777 ? */
 
 	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));
 
 	git_path_join_n(temp_path, 3, repo->path_repository, GIT_REFS_HEADS_DIR, "empty_dir");
-	must_pass(git_futils_mkdir_r(temp_path, mode));
+	must_pass(git_futils_mkdir_r(temp_path, GIT_REFS_DIR_MODE));
 
 	must_pass(git_reference_packall(repo));
 
diff --git a/tests/t12-repo.c b/tests/t12-repo.c
index 6884188..c07150c 100644
--- a/tests/t12-repo.c
+++ b/tests/t12-repo.c
@@ -173,7 +173,7 @@ END_TEST
 BEGIN_TEST(init2, "Initialize and open a bare repo with a relative path escaping out of the current working directory")
 	char path_repository[GIT_PATH_MAX];
 	char current_workdir[GIT_PATH_MAX];
-	const mode_t mode = 0755; /* or 0777 ? */
+	const mode_t mode = 0777;
 	git_repository* repo;
 
 	must_pass(p_getcwd(current_workdir, sizeof(current_workdir)));
@@ -232,7 +232,7 @@ BEGIN_TEST(open2, "Open a bare repository with a relative path escaping out of t
 	char current_workdir[GIT_PATH_MAX];
 	char path_repository[GIT_PATH_MAX];
 
-	const mode_t mode = 0755; /* or 0777 ? */
+	const mode_t mode = 0777;
 	git_repository* repo;
 
 	/* Setup the repository to open */
@@ -351,7 +351,7 @@ static int write_file(const char *path, const char *content)
 			return error;
 	}
 
-	file = git_futils_creat_withpath(path, 0644);
+	file = git_futils_creat_withpath(path, 0777, 0644);
 	if (file < GIT_SUCCESS)
 		return file;
 
@@ -384,7 +384,7 @@ BEGIN_TEST(discover0, "test discover")
 	char repository_path[GIT_PATH_MAX];
 	char sub_repository_path[GIT_PATH_MAX];
 	char found_path[GIT_PATH_MAX];
-	mode_t mode = 0755;
+	const mode_t mode = 0777;
 
 	git_futils_mkdir_r(DISCOVER_FOLDER, mode);
 	must_pass(append_ceiling_dir(ceiling_dirs, TEMP_REPO_FOLDER));
diff --git a/tests/test_helpers.c b/tests/test_helpers.c
index 0900430..7074e48 100644
--- a/tests/test_helpers.c
+++ b/tests/test_helpers.c
@@ -42,7 +42,7 @@ int write_object_data(char *file, void *data, size_t len)
 
 int write_object_files(const char *odb_dir, object_data *d)
 {
-	if (p_mkdir(odb_dir, 0755) < 0) {
+	if (p_mkdir(odb_dir, GIT_OBJECT_DIR_MODE) < 0) {
 		int err = errno;
 		fprintf(stderr, "can't make directory \"%s\"", odb_dir);
 		if (err == EEXIST)
@@ -51,7 +51,7 @@ int write_object_files(const char *odb_dir, object_data *d)
 		return -1;
 	}
 
-	if ((p_mkdir(d->dir, 0755) < 0) && (errno != EEXIST)) {
+	if ((p_mkdir(d->dir, GIT_OBJECT_DIR_MODE) < 0) && (errno != EEXIST)) {
 		fprintf(stderr, "can't make object directory \"%s\"\n", d->dir);
 		return -1;
 	}
@@ -82,7 +82,7 @@ int remove_object_files(const char *odb_dir, object_data *d)
 	return 0;
 }
 
-int remove_loose_object(const char *repository_folder, git_object *object)
+void locate_loose_object(const char *repository_folder, git_object *object, char **out, char **out_folder)
 {
 	static const char *objects_folder = "objects/";
 
@@ -104,6 +104,40 @@ int remove_loose_object(const char *repository_folder, git_object *object)
 	ptr += GIT_OID_HEXSZ + 1;
 	*ptr = 0;
 
+	*out = full_path;
+
+	if (out_folder)
+		*out_folder = top_folder;
+}
+
+int loose_object_dir_mode(const char *repository_folder, git_object *object)
+{
+	char *object_path;
+	size_t pos;
+	struct stat st;
+
+	locate_loose_object(repository_folder, object, &object_path, NULL);
+
+	pos = strlen(object_path);
+	while (pos--) {
+		if (object_path[pos] == '/') {
+			object_path[pos] = 0;
+			break;
+		}
+	}
+
+	assert(p_stat(object_path, &st) == 0);
+	free(object_path);
+
+	return st.st_mode;
+}
+
+int remove_loose_object(const char *repository_folder, git_object *object)
+{
+	char *full_path, *top_folder;
+
+	locate_loose_object(repository_folder, object, &full_path, &top_folder);
+
 	if (p_unlink(full_path) < 0) {
 		fprintf(stderr, "can't delete object file \"%s\"\n", full_path);
 		return -1;
@@ -141,7 +175,7 @@ int copy_file(const char *src, const char *dst)
 	if (git_futils_readbuffer(&source_buf, src) < GIT_SUCCESS)
 		return GIT_ENOTFOUND;
 
-	dst_fd = git_futils_creat_withpath(dst, 0644);
+	dst_fd = git_futils_creat_withpath(dst, 0777, 0644);
 	if (dst_fd < 0)
 		goto cleanup;
 
diff --git a/tests/test_helpers.h b/tests/test_helpers.h
index 53361b7..2e5194d 100644
--- a/tests/test_helpers.h
+++ b/tests/test_helpers.h
@@ -63,6 +63,10 @@ extern int remove_object_files(const char *odb_dir, object_data *d);
 
 extern int cmp_objects(git_rawobj *o, object_data *d);
 
+extern void locate_loose_object(const char *odb_dir, git_object *object, char **out, char **out_folder);
+
+extern int loose_object_dir_mode(const char *odb_dir, git_object *object);
+
 extern int remove_loose_object(const char *odb_dir, git_object *object);
 
 extern int cmp_files(const char *a, const char *b);
diff --git a/tests/test_main.c b/tests/test_main.c
index c9f8da3..9961ffd 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -26,6 +26,8 @@
 #include <string.h>
 #include <git2.h>
 
+#include "posix.h"
+
 #include "test_lib.h"
 #include "test_helpers.h"
 
@@ -81,6 +83,8 @@ main(int GIT_UNUSED(argc), char *GIT_UNUSED(argv[]))
 	GIT_UNUSED_ARG(argc);
 	GIT_UNUSED_ARG(argv);
 
+	p_umask(0);
+
 	failures = 0;
 
 	for (i = 0; i < GIT_SUITE_COUNT; ++i)