reftable/stack: adjust permissions of compacted tables

When creating a new compacted table from a range of preexisting ones we don't set the default permissions on the resulting table when specified by the user. This has the effect that the "core.sharedRepository" config will not be honored correctly. Fix this bug and add a test to catch this issue. Note that we only test on non-Windows platforms because Windows does not use POSIX permissions natively. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jan 26, 2024 at 11:09 UTC b3a79dd4e97a76317b528437e7413452b285ee88
2 files changed +29 -2
reftable/stack.c
+6
@@ -731,6 +731,12 @@ static int stack_compact_locked(struct reftable_stack *st, int first, int last,
731 strbuf_addstr(temp_tab, ".temp.XXXXXX");
732
733 tab_fd = mkstemp(temp_tab->buf);
734 + if (st->config.default_permissions &&
735 + chmod(temp_tab->buf, st->config.default_permissions) < 0) {
736 + err = REFTABLE_IO_ERROR;
737 + goto done;
738 + }
739 +
740 wr = reftable_new_writer(reftable_fd_write, &tab_fd, &st->config);
741
742 err = stack_write_compact(st, wr, first, last, config);
reftable/stack_test.c
+23 -2
@@ -443,15 +443,16 @@ static void test_reftable_stack_add(void)
443 int err = 0;
444 struct reftable_write_options cfg = {
445 .exact_log_message = 1,
446 + .default_permissions = 0660,
447 };
448 struct reftable_stack *st = NULL;
449 char *dir = get_tmp_dir(__LINE__);
449 -
450 struct reftable_ref_record refs[2] = { { NULL } };
451 struct reftable_log_record logs[2] = { { NULL } };
452 + struct strbuf path = STRBUF_INIT;
453 + struct stat stat_result;
454 int N = ARRAY_SIZE(refs);
455
454 -
456 err = reftable_new_stack(&st, dir, cfg);
457 EXPECT_ERR(err);
458 st->disable_auto_compact = 1;
@@ -509,12 +510,32 @@ static void test_reftable_stack_add(void)
510 reftable_log_record_release(&dest);
511 }
512
513 +#ifndef GIT_WINDOWS_NATIVE
514 + strbuf_addstr(&path, dir);
515 + strbuf_addstr(&path, "/tables.list");
516 + err = stat(path.buf, &stat_result);
517 + EXPECT(!err);
518 + EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions);
519 +
520 + strbuf_reset(&path);
521 + strbuf_addstr(&path, dir);
522 + strbuf_addstr(&path, "/");
523 + /* do not try at home; not an external API for reftable. */
524 + strbuf_addstr(&path, st->readers[0]->name);
525 + err = stat(path.buf, &stat_result);
526 + EXPECT(!err);
527 + EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions);
528 +#else
529 + (void) stat_result;
530 +#endif
531 +
532 /* cleanup */
533 reftable_stack_destroy(st);
534 for (i = 0; i < N; i++) {
535 reftable_ref_record_release(&refs[i]);
536 reftable_log_record_release(&logs[i]);
537 }
538 + strbuf_release(&path);
539 clear_dir(dir);
540 }
541