Skip to content

Commit 78e696c

Browse files
committed
Merge branch 'hn/reftable-fixes'
Assorted fixlets in reftable code. * hn/reftable-fixes: reftable: support preset file mode for writing reftable: signal overflow reftable: fix typo in header
2 parents 626f2ca + cd1799d commit 78e696c

File tree

8 files changed

+101
-11
lines changed

8 files changed

+101
-11
lines changed

reftable/block.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct block_writer {
2121
uint8_t *buf;
2222
uint32_t block_size;
2323

24-
/* Offset ofof the global header. Nonzero in the first block only. */
24+
/* Offset of the global header. Nonzero in the first block only. */
2525
uint32_t header_off;
2626

2727
/* How often to restart keys. */

reftable/error.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const char *reftable_error_str(int err)
3232
return "wrote empty table";
3333
case REFTABLE_REFNAME_ERROR:
3434
return "invalid refname";
35+
case REFTABLE_ENTRY_TOO_BIG_ERROR:
36+
return "entry too large";
3537
case -1:
3638
return "general error";
3739
default:

reftable/readwrite_test.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,40 @@ static void test_log_buffer_size(void)
155155
strbuf_release(&buf);
156156
}
157157

158+
static void test_log_overflow(void)
159+
{
160+
struct strbuf buf = STRBUF_INIT;
161+
char msg[256] = { 0 };
162+
struct reftable_write_options opts = {
163+
.block_size = ARRAY_SIZE(msg),
164+
};
165+
int err;
166+
struct reftable_log_record
167+
log = { .refname = "refs/heads/master",
168+
.update_index = 0xa,
169+
.value_type = REFTABLE_LOG_UPDATE,
170+
.value = { .update = {
171+
.name = "Han-Wen Nienhuys",
172+
.email = "[email protected]",
173+
.tz_offset = 100,
174+
.time = 0x5e430672,
175+
.message = msg,
176+
} } };
177+
struct reftable_writer *w =
178+
reftable_new_writer(&strbuf_add_void, &buf, &opts);
179+
180+
uint8_t hash1[GIT_SHA1_RAWSZ] = {1}, hash2[GIT_SHA1_RAWSZ] = { 2 };
181+
182+
memset(msg, 'x', sizeof(msg) - 1);
183+
log.value.update.old_hash = hash1;
184+
log.value.update.new_hash = hash2;
185+
reftable_writer_set_limits(w, update_index, update_index);
186+
err = reftable_writer_add_log(w, &log);
187+
EXPECT(err == REFTABLE_ENTRY_TOO_BIG_ERROR);
188+
reftable_writer_free(w);
189+
strbuf_release(&buf);
190+
}
191+
158192
static void test_log_write_read(void)
159193
{
160194
int N = 2;
@@ -648,5 +682,6 @@ int readwrite_test_main(int argc, const char *argv[])
648682
RUN_TEST(test_table_refs_for_no_index);
649683
RUN_TEST(test_table_refs_for_obj_index);
650684
RUN_TEST(test_write_empty_table);
685+
RUN_TEST(test_log_overflow);
651686
return 0;
652687
}

reftable/reftable-error.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ enum reftable_error {
5353

5454
/* Invalid ref name. */
5555
REFTABLE_REFNAME_ERROR = -10,
56+
57+
/* Entry does not fit. This can happen when writing outsize reflog
58+
messages. */
59+
REFTABLE_ENTRY_TOO_BIG_ERROR = -11,
5660
};
5761

5862
/* convert the numeric error code to a string. The string should not be

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)

reftable/writer.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ static int writer_add_record(struct reftable_writer *w,
239239
writer_reinit_block_writer(w, reftable_record_type(rec));
240240
err = block_writer_add(w->block_writer, rec);
241241
if (err < 0) {
242+
/* we are writing into memory, so an error can only mean it
243+
* doesn't fit. */
244+
err = REFTABLE_ENTRY_TOO_BIG_ERROR;
242245
goto done;
243246
}
244247

0 commit comments

Comments
 (0)