Skip to content

Commit cd1799d

Browse files
hanwengitster
authored andcommitted
reftable: support preset file mode for writing
Create files with mode 0666, so umask works as intended. Provides an override, which is useful to support shared repos (test t1301-shared-repo.sh). Signed-off-by: Han-Wen Nienhuys <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0dd4458 commit cd1799d

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

reftable/reftable-writer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ struct reftable_write_options {
3535
*/
3636
uint32_t hash_id;
3737

38+
/* Default mode for creating files. If unset, use 0666 (+umask) */
39+
unsigned int default_permissions;
40+
3841
/* boolean: do not check ref names for validity or dir/file conflicts.
3942
*/
4043
unsigned skip_name_check : 1;

reftable/stack.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
469469
strbuf_addstr(&add->lock_file_name, ".lock");
470470

471471
add->lock_file_fd = open(add->lock_file_name.buf,
472-
O_EXCL | O_CREAT | O_WRONLY, 0644);
472+
O_EXCL | O_CREAT | O_WRONLY, 0666);
473473
if (add->lock_file_fd < 0) {
474474
if (errno == EEXIST) {
475475
err = REFTABLE_LOCK_ERROR;
@@ -478,6 +478,13 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
478478
}
479479
goto done;
480480
}
481+
if (st->config.default_permissions) {
482+
if (chmod(add->lock_file_name.buf, st->config.default_permissions) < 0) {
483+
err = REFTABLE_IO_ERROR;
484+
goto done;
485+
}
486+
}
487+
481488
err = stack_uptodate(st);
482489
if (err < 0)
483490
goto done;
@@ -644,7 +651,12 @@ int reftable_addition_add(struct reftable_addition *add,
644651
err = REFTABLE_IO_ERROR;
645652
goto done;
646653
}
647-
654+
if (add->stack->config.default_permissions) {
655+
if (chmod(temp_tab_file_name.buf, add->stack->config.default_permissions)) {
656+
err = REFTABLE_IO_ERROR;
657+
goto done;
658+
}
659+
}
648660
wr = reftable_new_writer(reftable_fd_write, &tab_fd,
649661
&add->stack->config);
650662
err = write_table(wr, arg);
@@ -900,7 +912,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last,
900912
strbuf_addstr(&lock_file_name, ".lock");
901913

902914
lock_file_fd =
903-
open(lock_file_name.buf, O_EXCL | O_CREAT | O_WRONLY, 0644);
915+
open(lock_file_name.buf, O_EXCL | O_CREAT | O_WRONLY, 0666);
904916
if (lock_file_fd < 0) {
905917
if (errno == EEXIST) {
906918
err = 1;
@@ -931,8 +943,8 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last,
931943
strbuf_addstr(&subtab_lock, ".lock");
932944

933945
sublock_file_fd = open(subtab_lock.buf,
934-
O_EXCL | O_CREAT | O_WRONLY, 0644);
935-
if (sublock_file_fd > 0) {
946+
O_EXCL | O_CREAT | O_WRONLY, 0666);
947+
if (sublock_file_fd >= 0) {
936948
close(sublock_file_fd);
937949
} else if (sublock_file_fd < 0) {
938950
if (errno == EEXIST) {
@@ -967,7 +979,7 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last,
967979
goto done;
968980

969981
lock_file_fd =
970-
open(lock_file_name.buf, O_EXCL | O_CREAT | O_WRONLY, 0644);
982+
open(lock_file_name.buf, O_EXCL | O_CREAT | O_WRONLY, 0666);
971983
if (lock_file_fd < 0) {
972984
if (errno == EEXIST) {
973985
err = 1;
@@ -977,6 +989,12 @@ static int stack_compact_range(struct reftable_stack *st, int first, int last,
977989
goto done;
978990
}
979991
have_lock = 1;
992+
if (st->config.default_permissions) {
993+
if (chmod(lock_file_name.buf, st->config.default_permissions) < 0) {
994+
err = REFTABLE_IO_ERROR;
995+
goto done;
996+
}
997+
}
980998

981999
format_name(&new_table_name, st->readers[first]->min_update_index,
9821000
st->readers[last]->max_update_index);

reftable/stack_test.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ license that can be found in the LICENSE file or at
1717
#include "record.h"
1818
#include "test_framework.h"
1919
#include "reftable-tests.h"
20+
#include "reader.h"
2021

2122
#include <sys/types.h>
2223
#include <dirent.h>
@@ -138,8 +139,11 @@ static int write_test_log(struct reftable_writer *wr, void *arg)
138139
static void test_reftable_stack_add_one(void)
139140
{
140141
char *dir = get_tmp_dir(__LINE__);
141-
142-
struct reftable_write_options cfg = { 0 };
142+
struct strbuf scratch = STRBUF_INIT;
143+
int mask = umask(002);
144+
struct reftable_write_options cfg = {
145+
.default_permissions = 0660,
146+
};
143147
struct reftable_stack *st = NULL;
144148
int err;
145149
struct reftable_ref_record ref = {
@@ -149,8 +153,7 @@ static void test_reftable_stack_add_one(void)
149153
.value.symref = "master",
150154
};
151155
struct reftable_ref_record dest = { NULL };
152-
153-
156+
struct stat stat_result = { 0 };
154157
err = reftable_new_stack(&st, dir, cfg);
155158
EXPECT_ERR(err);
156159

@@ -160,6 +163,7 @@ static void test_reftable_stack_add_one(void)
160163
err = reftable_stack_read_ref(st, ref.refname, &dest);
161164
EXPECT_ERR(err);
162165
EXPECT(0 == strcmp("master", dest.value.symref));
166+
EXPECT(st->readers_len > 0);
163167

164168
printf("testing print functionality:\n");
165169
err = reftable_stack_print_directory(dir, GIT_SHA1_FORMAT_ID);
@@ -168,9 +172,30 @@ static void test_reftable_stack_add_one(void)
168172
err = reftable_stack_print_directory(dir, GIT_SHA256_FORMAT_ID);
169173
EXPECT(err == REFTABLE_FORMAT_ERROR);
170174

175+
#ifndef GIT_WINDOWS_NATIVE
176+
strbuf_addstr(&scratch, dir);
177+
strbuf_addstr(&scratch, "/tables.list");
178+
err = stat(scratch.buf, &stat_result);
179+
EXPECT(!err);
180+
EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions);
181+
182+
strbuf_reset(&scratch);
183+
strbuf_addstr(&scratch, dir);
184+
strbuf_addstr(&scratch, "/");
185+
/* do not try at home; not an external API for reftable. */
186+
strbuf_addstr(&scratch, st->readers[0]->name);
187+
err = stat(scratch.buf, &stat_result);
188+
EXPECT(!err);
189+
EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions);
190+
#else
191+
(void) stat_result;
192+
#endif
193+
171194
reftable_ref_record_release(&dest);
172195
reftable_stack_destroy(st);
196+
strbuf_release(&scratch);
173197
clear_dir(dir);
198+
umask(mask);
174199
}
175200

176201
static void test_reftable_stack_uptodate(void)

0 commit comments

Comments
 (0)