Commit 3ae66ef1af2af9132c96c57bf5c6e2cfdb1f617c

Edward Thomson 2013-11-05T04:48:45

Merge pull request #1940 from ethomson/filebuf_umask Take umask into account in filebuf_commit

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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h
index d3dd733..4d772ca 100644
--- a/include/git2/odb_backend.h
+++ b/include/git2/odb_backend.h
@@ -50,8 +50,8 @@ GIT_EXTERN(int) git_odb_backend_loose(
 	const char *objects_dir,
 	int compression_level,
 	int do_fsync,
-	mode_t dir_mode,
-	mode_t file_mode);
+	unsigned int dir_mode,
+	unsigned int file_mode);
 
 /**
  * Create a backend out of a single packfile
diff --git a/src/blob.c b/src/blob.c
index e18db4d..2c6d528 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -284,7 +284,7 @@ int git_blob_create_fromchunks(
 	content = git__malloc(BUFFER_SIZE);
 	GITERR_CHECK_ALLOC(content);
 
-	if (git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY) < 0)
+	if (git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY, 0666) < 0)
 		goto cleanup;
 
 	while (1) {
diff --git a/src/checkout.c b/src/checkout.c
index a4f3266..6d7e3cf 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -1673,9 +1673,9 @@ static int checkout_write_merge(
 		goto done;
 
 	if ((error = git_futils_mkpath2file(path_workdir.ptr, 0755)) < 0 ||
-		(error = git_filebuf_open(&output, path_workdir.ptr, GIT_FILEBUF_DO_NOT_BUFFER)) < 0 ||
+		(error = git_filebuf_open(&output, path_workdir.ptr, GIT_FILEBUF_DO_NOT_BUFFER, result.mode)) < 0 ||
 		(error = git_filebuf_write(&output, result.data, result.len)) < 0 ||
-		(error = git_filebuf_commit(&output, result.mode)) < 0)
+		(error = git_filebuf_commit(&output)) < 0)
 		goto done;
 
 done:
diff --git a/src/config_file.c b/src/config_file.c
index c7fc320..40dcc5a 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1210,7 +1210,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
 	write_start = data_start;
 
 	/* Lock the file */
-	if (git_filebuf_open(&file, cfg->file_path, 0) < 0)
+	if (git_filebuf_open(&file, cfg->file_path, 0, GIT_CONFIG_FILE_MODE) < 0)
 		return -1;
 
 	skip_bom(reader);
@@ -1369,7 +1369,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
 	/* refresh stats - if this errors, then commit will error too */
 	(void)git_filebuf_stats(&reader->file_mtime, &reader->file_size, &file);
 
-	result = git_filebuf_commit(&file, GIT_CONFIG_FILE_MODE);
+	result = git_filebuf_commit(&file);
 	git_buf_free(&reader->buffer);
 
 	return result;
diff --git a/src/fetchhead.c b/src/fetchhead.c
index 4dcebb8..9672623 100644
--- a/src/fetchhead.c
+++ b/src/fetchhead.c
@@ -112,7 +112,7 @@ int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
 	if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0)
 		return -1;
 
-	if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_FORCE) < 0) {
+	if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE) < 0) {
 		git_buf_free(&path);
 		return -1;
 	}
@@ -124,7 +124,7 @@ int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
 	git_vector_foreach(fetchhead_refs, i, fetchhead_ref)
 		fetchhead_ref_write(&file, fetchhead_ref);
 
-	return git_filebuf_commit(&file, GIT_REFS_FILE_MODE);
+	return git_filebuf_commit(&file);
 }
 
 static int fetchhead_ref_parse(
diff --git a/src/filebuf.c b/src/filebuf.c
index 714a323..9c3dae8 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -10,8 +10,6 @@
 #include "filebuf.h"
 #include "fileops.h"
 
-#define GIT_LOCK_FILE_MODE 0644
-
 static const size_t WRITE_BUFFER_SIZE = (4096 * 2);
 
 enum buferr_t {
@@ -44,7 +42,7 @@ static int verify_last_error(git_filebuf *file)
 	}
 }
 
-static int lock_file(git_filebuf *file, int flags)
+static int lock_file(git_filebuf *file, int flags, mode_t mode)
 {
 	if (git_path_exists(file->path_lock) == true) {
 		if (flags & GIT_FILEBUF_FORCE)
@@ -60,9 +58,9 @@ static int lock_file(git_filebuf *file, int flags)
 	/* create path to the file buffer is required */
 	if (flags & GIT_FILEBUF_FORCE) {
 		/* XXX: Should dirmode here be configurable? Or is 0777 always fine? */
-		file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, GIT_LOCK_FILE_MODE);
+		file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, mode);
 	} else {
-		file->fd = git_futils_creat_locked(file->path_lock, GIT_LOCK_FILE_MODE);
+		file->fd = git_futils_creat_locked(file->path_lock, mode);
 	}
 
 	if (file->fd < 0)
@@ -195,7 +193,7 @@ static int write_deflate(git_filebuf *file, void *source, size_t len)
 	return 0;
 }
 
-int git_filebuf_open(git_filebuf *file, const char *path, int flags)
+int git_filebuf_open(git_filebuf *file, const char *path, int flags, mode_t mode)
 {
 	int compression, error = -1;
 	size_t path_len;
@@ -255,7 +253,7 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags)
 		git_buf tmp_path = GIT_BUF_INIT;
 
 		/* Open the file as temporary for locking */
-		file->fd = git_futils_mktmp(&tmp_path, path);
+		file->fd = git_futils_mktmp(&tmp_path, path, mode);
 
 		if (file->fd < 0) {
 			git_buf_free(&tmp_path);
@@ -282,7 +280,7 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags)
 		memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH);
 
 		/* open the file for locking */
-		if ((error = lock_file(file, flags)) < 0)
+		if ((error = lock_file(file, flags, mode)) < 0)
 			goto cleanup;
 	}
 
@@ -309,16 +307,16 @@ int git_filebuf_hash(git_oid *oid, git_filebuf *file)
 	return 0;
 }
 
-int git_filebuf_commit_at(git_filebuf *file, const char *path, mode_t mode)
+int git_filebuf_commit_at(git_filebuf *file, const char *path)
 {
 	git__free(file->path_original);
 	file->path_original = git__strdup(path);
 	GITERR_CHECK_ALLOC(file->path_original);
 
-	return git_filebuf_commit(file, mode);
+	return git_filebuf_commit(file);
 }
 
-int git_filebuf_commit(git_filebuf *file, mode_t mode)
+int git_filebuf_commit(git_filebuf *file)
 {
 	/* temporary files cannot be committed */
 	assert(file && file->path_original);
@@ -338,11 +336,6 @@ int git_filebuf_commit(git_filebuf *file, mode_t mode)
 
 	file->fd = -1;
 
-	if (p_chmod(file->path_lock, mode)) {
-		giterr_set(GITERR_OS, "Failed to set attributes for file at '%s'", file->path_lock);
-		goto on_error;
-	}
-
 	p_unlink(file->path_original);
 
 	if (p_rename(file->path_lock, file->path_original) < 0) {
diff --git a/src/filebuf.h b/src/filebuf.h
index 823af81..044af54 100644
--- a/src/filebuf.h
+++ b/src/filebuf.h
@@ -77,9 +77,9 @@ int git_filebuf_write(git_filebuf *lock, const void *buff, size_t len);
 int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len);
 int git_filebuf_printf(git_filebuf *file, const char *format, ...) GIT_FORMAT_PRINTF(2, 3);
 
-int git_filebuf_open(git_filebuf *lock, const char *path, int flags);
-int git_filebuf_commit(git_filebuf *lock, mode_t mode);
-int git_filebuf_commit_at(git_filebuf *lock, const char *path, mode_t mode);
+int git_filebuf_open(git_filebuf *lock, const char *path, int flags, mode_t mode);
+int git_filebuf_commit(git_filebuf *lock);
+int git_filebuf_commit_at(git_filebuf *lock, const char *path);
 void git_filebuf_cleanup(git_filebuf *lock);
 int git_filebuf_hash(git_oid *oid, git_filebuf *file);
 int git_filebuf_flush(git_filebuf *file);
diff --git a/src/fileops.c b/src/fileops.c
index 54bf5d8..1c27b27 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -19,9 +19,12 @@ int git_futils_mkpath2file(const char *file_path, const mode_t mode)
 		GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
 }
 
-int git_futils_mktmp(git_buf *path_out, const char *filename)
+int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode)
 {
 	int fd;
+	mode_t mask;
+
+	p_umask(mask = p_umask(0));
 
 	git_buf_sets(path_out, filename);
 	git_buf_puts(path_out, "_git2_XXXXXX");
@@ -35,6 +38,12 @@ int git_futils_mktmp(git_buf *path_out, const char *filename)
 		return -1;
 	}
 
+	if (p_chmod(path_out->ptr, (mode & ~mask))) {
+		giterr_set(GITERR_OS,
+			"Failed to set permissions on file '%s'", path_out->ptr);
+		return -1;
+	}
+
 	return fd;
 }
 
diff --git a/src/fileops.h b/src/fileops.h
index 1b2728e..59a6a21 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -141,7 +141,7 @@ extern int git_futils_rmdir_r(const char *path, const char *base, uint32_t flags
  * Writes the filename into path_out.
  * @return On success, an open file descriptor, else an error code < 0.
  */
-extern int git_futils_mktmp(git_buf *path_out, const char *filename);
+extern int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode);
 
 /**
  * Move a file on the filesystem, create the
diff --git a/src/index.c b/src/index.c
index dbf1ab5..ecab150 100644
--- a/src/index.c
+++ b/src/index.c
@@ -500,7 +500,7 @@ int git_index_write(git_index *index)
 	git_vector_sort(&index->reuc);
 
 	if ((error = git_filebuf_open(
-		     &file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS)) < 0) {
+		&file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) {
 		if (error == GIT_ELOCKED)
 			giterr_set(GITERR_INDEX, "The index is locked. This might be due to a concurrrent or crashed process");
 
@@ -512,7 +512,7 @@ int git_index_write(git_index *index)
 		return error;
 	}
 
-	if ((error = git_filebuf_commit(&file, GIT_INDEX_FILE_MODE)) < 0)
+	if ((error = git_filebuf_commit(&file)) < 0)
 		return error;
 
 	error = git_futils_filestamp_check(&index->stamp, index->index_file_path);
diff --git a/src/indexer.c b/src/indexer.c
index 0873c8c..90fb521 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -140,7 +140,8 @@ int git_indexer_new(
 		goto cleanup;
 
 	error = git_filebuf_open(&idx->pack_file, path.ptr,
-				 GIT_FILEBUF_TEMPORARY | GIT_FILEBUF_DO_NOT_BUFFER);
+		GIT_FILEBUF_TEMPORARY | GIT_FILEBUF_DO_NOT_BUFFER,
+		GIT_PACK_FILE_MODE);
 	git_buf_free(&path);
 	if (error < 0)
 		goto cleanup;
@@ -903,7 +904,8 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
 	if (git_buf_oom(&filename))
 		return -1;
 
-	if (git_filebuf_open(&index_file, filename.ptr, GIT_FILEBUF_HASH_CONTENTS) < 0)
+	if (git_filebuf_open(&index_file, filename.ptr,
+		GIT_FILEBUF_HASH_CONTENTS, GIT_PACK_FILE_MODE) < 0)
 		goto on_error;
 
 	/* Write out the header */
@@ -969,7 +971,7 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
 		goto on_error;
 
 	/* Commit file */
-	if (git_filebuf_commit_at(&index_file, filename.ptr, GIT_PACK_FILE_MODE) < 0)
+	if (git_filebuf_commit_at(&index_file, filename.ptr) < 0)
 		goto on_error;
 
 	git_mwindow_free_all(&idx->pack->mwf);
@@ -980,7 +982,7 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
 	if (index_path(&filename, idx, ".pack") < 0)
 		goto on_error;
 	/* And don't forget to rename the packfile to its new place. */
-	if (git_filebuf_commit_at(&idx->pack_file, filename.ptr, GIT_PACK_FILE_MODE) < 0)
+	if (git_filebuf_commit_at(&idx->pack_file, filename.ptr) < 0)
 		return -1;
 
 	git_buf_free(&filename);
diff --git a/src/merge.c b/src/merge.c
index a22801e..ea2224d 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -1623,9 +1623,9 @@ static int write_orig_head(
 	git_oid_tostr(orig_oid_str, GIT_OID_HEXSZ+1, &our_head->oid);
 
 	if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_ORIG_HEAD_FILE)) == 0 &&
-		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE)) == 0 &&
+		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) == 0 &&
 		(error = git_filebuf_printf(&file, "%s\n", orig_oid_str)) == 0)
-		error = git_filebuf_commit(&file, 0666);
+		error = git_filebuf_commit(&file);
 
 	if (error < 0)
 		git_filebuf_cleanup(&file);
@@ -1649,7 +1649,7 @@ static int write_merge_head(
 	assert(repo && heads);
 
 	if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_HEAD_FILE)) < 0 ||
-		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE)) < 0)
+		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) < 0)
 		goto cleanup;
 
 	for (i = 0; i < heads_len; i++) {
@@ -1659,7 +1659,7 @@ static int write_merge_head(
 			goto cleanup;
 	}
 
-	error = git_filebuf_commit(&file, 0666);
+	error = git_filebuf_commit(&file);
 
 cleanup:
 	if (error < 0)
@@ -1682,10 +1682,10 @@ static int write_merge_mode(git_repository *repo, unsigned int flags)
 	assert(repo);
 
 	if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MODE_FILE)) < 0 ||
-		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE)) < 0)
+		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) < 0)
 		goto cleanup;
 
-	error = git_filebuf_commit(&file, 0666);
+	error = git_filebuf_commit(&file);
 
 cleanup:
 	if (error < 0)
@@ -1911,7 +1911,7 @@ static int write_merge_msg(
 		entries[i].merge_head = heads[i];
 
 	if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
-		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE)) < 0 ||
+		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) < 0 ||
 		(error = git_filebuf_write(&file, "Merge ", 6)) < 0)
 		goto cleanup;
 
@@ -1988,7 +1988,7 @@ static int write_merge_msg(
 	}
 
 	if ((error = git_filebuf_printf(&file, "\n")) < 0 ||
-		(error = git_filebuf_commit(&file, 0666)) < 0)
+		(error = git_filebuf_commit(&file)) < 0)
 		goto cleanup;
 
 cleanup:
diff --git a/src/merge.h b/src/merge.h
index ba6725d..0ecad6e 100644
--- a/src/merge.h
+++ b/src/merge.h
@@ -16,6 +16,7 @@
 
 #define GIT_MERGE_MSG_FILE		"MERGE_MSG"
 #define GIT_MERGE_MODE_FILE		"MERGE_MODE"
+#define GIT_MERGE_FILE_MODE		0666
 
 #define GIT_MERGE_TREE_RENAME_THRESHOLD	50
 #define GIT_MERGE_TREE_TARGET_LIMIT		1000
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 0dfd78a..ced272b 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -789,7 +789,7 @@ static int loose_backend__stream_fwrite(git_odb_stream *_stream, const git_oid *
 		error = -1;
 	else
 		error = git_filebuf_commit_at(
-			&stream->fbuf, final_path.ptr, backend->object_file_mode);
+			&stream->fbuf, final_path.ptr);
 
 	git_buf_free(&final_path);
 
@@ -838,7 +838,8 @@ static int loose_backend__stream(git_odb_stream **stream_out, git_odb_backend *_
 	if (git_buf_joinpath(&tmp_path, backend->objects_dir, "tmp_object") < 0 ||
 		git_filebuf_open(&stream->fbuf, tmp_path.ptr,
 			GIT_FILEBUF_TEMPORARY |
-			(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT)) < 0 ||
+			(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT),
+			backend->object_file_mode) < 0 ||
 		stream->stream.write((git_odb_stream *)stream, hdr, hdrlen) < 0)
 	{
 		git_filebuf_cleanup(&stream->fbuf);
@@ -867,7 +868,8 @@ static int loose_backend__write(git_odb_backend *_backend, const git_oid *oid, c
 	if (git_buf_joinpath(&final_path, backend->objects_dir, "tmp_object") < 0 ||
 		git_filebuf_open(&fbuf, final_path.ptr,
 			GIT_FILEBUF_TEMPORARY |
-			(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT)) < 0)
+			(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT),
+			backend->object_file_mode) < 0)
 	{
 		error = -1;
 		goto cleanup;
@@ -878,7 +880,7 @@ static int loose_backend__write(git_odb_backend *_backend, const git_oid *oid, c
 
 	if (object_file_name(&final_path, backend, oid) < 0 ||
 		object_mkdir(&final_path, backend) < 0 ||
-		git_filebuf_commit_at(&fbuf, final_path.ptr, backend->object_file_mode) < 0)
+		git_filebuf_commit_at(&fbuf, final_path.ptr) < 0)
 		error = -1;
 
 cleanup:
@@ -902,8 +904,8 @@ int git_odb_backend_loose(
 	const char *objects_dir,
 	int compression_level,
 	int do_fsync,
-	mode_t dir_mode,
-	mode_t file_mode)
+	unsigned int dir_mode,
+	unsigned int file_mode)
 {
 	loose_backend *backend;
 	size_t objects_dirlen;
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 7ce09ba..62d5c10 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -702,7 +702,7 @@ static int loose_write(refdb_fs_backend *backend, const git_reference *ref)
 	if (git_buf_joinpath(&ref_path, backend->path, ref->name) < 0)
 		return -1;
 
-	if (git_filebuf_open(&file, ref_path.ptr, GIT_FILEBUF_FORCE) < 0) {
+	if (git_filebuf_open(&file, ref_path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE) < 0) {
 		git_buf_free(&ref_path);
 		return -1;
 	}
@@ -720,7 +720,7 @@ static int loose_write(refdb_fs_backend *backend, const git_reference *ref)
 		assert(0); /* don't let this happen */
 	}
 
-	return git_filebuf_commit(&file, GIT_REFS_FILE_MODE);
+	return git_filebuf_commit(&file);
 }
 
 /*
@@ -865,7 +865,7 @@ static int packed_write(refdb_fs_backend *backend)
 		return -1;
 
 	/* Open the file! */
-	if (git_filebuf_open(&pack_file, git_sortedcache_path(refcache), 0) < 0)
+	if (git_filebuf_open(&pack_file, git_sortedcache_path(refcache), 0, GIT_PACKEDREFS_FILE_MODE) < 0)
 		goto fail;
 
 	/* Packfiles have a header... apparently
@@ -886,7 +886,7 @@ static int packed_write(refdb_fs_backend *backend)
 
 	/* if we've written all the references properly, we can commit
 	 * the packfile to make the changes effective */
-	if (git_filebuf_commit(&pack_file, GIT_PACKEDREFS_FILE_MODE) < 0)
+	if (git_filebuf_commit(&pack_file) < 0)
 		goto fail;
 
 	/* when and only when the packfile has been properly written,
@@ -1289,7 +1289,7 @@ static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflo
 		goto cleanup;
 	}
 
-	if ((error = git_filebuf_open(&fbuf, git_buf_cstr(&log_path), 0)) < 0)
+	if ((error = git_filebuf_open(&fbuf, git_buf_cstr(&log_path), 0, GIT_REFLOG_FILE_MODE)) < 0)
 		goto cleanup;
 
 	git_vector_foreach(&reflog->entries, i, entry) {
@@ -1300,7 +1300,7 @@ static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflo
 			goto cleanup;
 	}
 
-	error = git_filebuf_commit(&fbuf, GIT_REFLOG_FILE_MODE);
+	error = git_filebuf_commit(&fbuf);
 	goto success;
 
 cleanup:
@@ -1350,7 +1350,7 @@ static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_
 	if (git_buf_joinpath(&temp_path, git_buf_cstr(&temp_path), "temp_reflog") < 0)
 		return -1;
 
-	if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path))) < 0) {
+	if ((fd = git_futils_mktmp(&temp_path, git_buf_cstr(&temp_path), GIT_REFLOG_FILE_MODE)) < 0) {
 		error = -1;
 		goto cleanup;
 	}
diff --git a/src/repository.c b/src/repository.c
index c5ce842..dcc02e4 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -816,7 +816,7 @@ static int repo_init_create_head(const char *git_dir, const char *ref_name)
 	const char *fmt;
 
 	if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 ||
-		git_filebuf_open(&ref, ref_path.ptr, 0) < 0)
+		git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE) < 0)
 		goto fail;
 
 	if (!ref_name)
@@ -828,7 +828,7 @@ static int repo_init_create_head(const char *git_dir, const char *ref_name)
 		fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n";
 
 	if (git_filebuf_printf(&ref, fmt, ref_name) < 0 ||
-		git_filebuf_commit(&ref, GIT_REFS_FILE_MODE) < 0)
+		git_filebuf_commit(&ref) < 0)
 		goto fail;
 
 	git_buf_free(&ref_path);
@@ -875,7 +875,7 @@ static bool are_symlinks_supported(const char *wd_path)
 	struct stat st;
 	int symlinks_supported = -1;
 
-	if ((fd = git_futils_mktmp(&path, wd_path)) < 0 ||
+	if ((fd = git_futils_mktmp(&path, wd_path, 0666)) < 0 ||
 		p_close(fd) < 0 ||
 		p_unlink(path.ptr) < 0 ||
 		p_symlink("testing", path.ptr) < 0 ||
diff --git a/tests-clar/config/stress.c b/tests-clar/config/stress.c
index 8cc64d2..eeca54f 100644
--- a/tests-clar/config/stress.c
+++ b/tests-clar/config/stress.c
@@ -10,12 +10,12 @@ void test_config_stress__initialize(void)
 {
 	git_filebuf file = GIT_FILEBUF_INIT;
 
-	cl_git_pass(git_filebuf_open(&file, TEST_CONFIG, 0));
+	cl_git_pass(git_filebuf_open(&file, TEST_CONFIG, 0, 0666));
 
 	git_filebuf_printf(&file, "[color]\n\tui = auto\n");
 	git_filebuf_printf(&file, "[core]\n\teditor = \n");
 
-	cl_git_pass(git_filebuf_commit(&file, 0666));
+	cl_git_pass(git_filebuf_commit(&file));
 }
 
 void test_config_stress__cleanup(void)
diff --git a/tests-clar/core/filebuf.c b/tests-clar/core/filebuf.c
index bf21670..5a3e751 100644
--- a/tests-clar/core/filebuf.c
+++ b/tests-clar/core/filebuf.c
@@ -13,7 +13,7 @@ void test_core_filebuf__0(void)
 	cl_must_pass(fd);
 	cl_must_pass(p_close(fd));
 
-	cl_git_fail(git_filebuf_open(&file, test, 0));
+	cl_git_fail(git_filebuf_open(&file, test, 0, 0666));
 	cl_assert(git_path_exists(testlock));
 
 	cl_must_pass(p_unlink(testlock));
@@ -28,9 +28,9 @@ void test_core_filebuf__1(void)
 
 	cl_git_mkfile(test, "libgit2 rocks\n");
 
-	cl_git_pass(git_filebuf_open(&file, test, GIT_FILEBUF_APPEND));
+	cl_git_pass(git_filebuf_open(&file, test, GIT_FILEBUF_APPEND, 0666));
 	cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
-	cl_git_pass(git_filebuf_commit(&file, 0666));
+	cl_git_pass(git_filebuf_commit(&file));
 
 	cl_assert_equal_file("libgit2 rocks\nlibgit2 rocks\n", 0, test);
 
@@ -47,9 +47,9 @@ void test_core_filebuf__2(void)
 
 	memset(buf, 0xfe, sizeof(buf));
 
-	cl_git_pass(git_filebuf_open(&file, test, 0));
+	cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
 	cl_git_pass(git_filebuf_write(&file, buf, sizeof(buf)));
-	cl_git_pass(git_filebuf_commit(&file, 0666));
+	cl_git_pass(git_filebuf_commit(&file));
 
 	cl_assert_equal_file((char *)buf, sizeof(buf), test);
 
@@ -64,7 +64,7 @@ void test_core_filebuf__4(void)
 
 	cl_assert(file.buffer == NULL);
 
-	cl_git_pass(git_filebuf_open(&file, test, 0));
+	cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
 	cl_assert(file.buffer != NULL);
 
 	git_filebuf_cleanup(&file);
@@ -80,13 +80,47 @@ void test_core_filebuf__5(void)
 
 	cl_assert(file.buffer == NULL);
 
-	cl_git_pass(git_filebuf_open(&file, test, 0));
+	cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
 	cl_assert(file.buffer != NULL);
 	cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
 	cl_assert(file.buffer != NULL);
 
-	cl_git_pass(git_filebuf_commit(&file, 0666));
+	cl_git_pass(git_filebuf_commit(&file));
 	cl_assert(file.buffer == NULL);
 
 	cl_must_pass(p_unlink(test));
 }
+
+
+/* make sure git_filebuf_commit takes umask into account */
+void test_core_filebuf__umask(void)
+{
+	git_filebuf file = GIT_FILEBUF_INIT;
+	char test[] = "test";
+	struct stat statbuf;
+	mode_t mask, os_mask;
+
+#ifdef GIT_WIN32
+	os_mask = 0600;
+#else
+	os_mask = 0777;
+#endif
+
+	p_umask(mask = p_umask(0));
+
+	cl_assert(file.buffer == NULL);
+
+	cl_git_pass(git_filebuf_open(&file, test, 0, 0666));
+	cl_assert(file.buffer != NULL);
+	cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks"));
+	cl_assert(file.buffer != NULL);
+
+	cl_git_pass(git_filebuf_commit(&file));
+	cl_assert(file.buffer == NULL);
+
+	cl_must_pass(p_stat("test", &statbuf));
+	cl_assert_equal_i(statbuf.st_mode & os_mask, (0666 & ~mask) & os_mask);
+
+	cl_must_pass(p_unlink(test));
+}
+
diff --git a/tests-clar/index/tests.c b/tests-clar/index/tests.c
index 09b05bf..e520298 100644
--- a/tests-clar/index/tests.c
+++ b/tests-clar/index/tests.c
@@ -224,9 +224,9 @@ void test_index_tests__add(void)
 
 	/* Create a new file in the working directory */
 	cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777));
-	cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0));
+	cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0, 0666));
 	cl_git_pass(git_filebuf_write(&file, "hey there\n", 10));
-	cl_git_pass(git_filebuf_commit(&file, 0666));
+	cl_git_pass(git_filebuf_commit(&file));
 
 	/* Store the expected hash of the file/blob
 	 * This has been generated by executing the following
@@ -474,7 +474,7 @@ void test_index_tests__elocked(void)
 	cl_git_pass(git_repository_index(&index, repo));
 
 	/* Lock the index file so we fail to lock it */
-	cl_git_pass(git_filebuf_open(&file, index->index_file_path, 0));
+	cl_git_pass(git_filebuf_open(&file, index->index_file_path, 0, 0666));
 	error = git_index_write(index);
 	cl_assert_equal_i(GIT_ELOCKED, error);
 
diff --git a/tests-clar/odb/alternates.c b/tests-clar/odb/alternates.c
index 4e876c2..c75f6fe 100644
--- a/tests-clar/odb/alternates.c
+++ b/tests-clar/odb/alternates.c
@@ -32,9 +32,9 @@ static void init_linked_repo(const char *path, const char *alternate)
 	cl_git_pass(git_futils_mkdir(filepath.ptr, NULL, 0755, GIT_MKDIR_PATH));
 	cl_git_pass(git_buf_joinpath(&filepath, filepath.ptr , "alternates"));
 
-	cl_git_pass(git_filebuf_open(&file, git_buf_cstr(&filepath), 0));
+	cl_git_pass(git_filebuf_open(&file, git_buf_cstr(&filepath), 0, 0666));
 	git_filebuf_printf(&file, "%s\n", git_buf_cstr(&destpath));
-	cl_git_pass(git_filebuf_commit(&file, 0644));
+	cl_git_pass(git_filebuf_commit(&file));
 
 	git_repository_free(repo);
 }