/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
#include "stack.h"
#include "system.h"
#include "merged.h"
#include "reader.h"
#include "refname.h"
#include "reftable-error.h"
#include "reftable-record.h"
#include "reftable-merged.h"
#include "writer.h"
static int stack_try_add(struct reftable_stack *st,
int (*write_table)(struct reftable_writer *wr,
void *arg),
void *arg);
static int stack_write_compact(struct reftable_stack *st,
struct reftable_writer *wr, int first, int last,
struct reftable_log_expiry_config *config);
static int stack_check_addition(struct reftable_stack *st,
const char *new_tab_name);
static void reftable_addition_close(struct reftable_addition *add);
static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
int reuse_open);
static void stack_filename(struct strbuf *dest, struct reftable_stack *st,
const char *name)
{
strbuf_reset(dest);
strbuf_addstr(dest, st->reftable_dir);
strbuf_addstr(dest, "/");
strbuf_addstr(dest, name);
}
static ssize_t reftable_fd_write(void *arg, const void *data, size_t sz)
{
int *fdp = (int *)arg;
return write(*fdp, data, sz);
}
int reftable_new_stack(struct reftable_stack **dest, const char *dir,
struct reftable_write_options config)
{
struct reftable_stack *p =
reftable_calloc(sizeof(struct reftable_stack));
struct strbuf list_file_name = STRBUF_INIT;
int err = 0;
if (config.hash_id == 0) {
config.hash_id = GIT_SHA1_FORMAT_ID;
}
*dest = NULL;
strbuf_reset(&list_file_name);
strbuf_addstr(&list_file_name, dir);
strbuf_addstr(&list_file_name, "/tables.list");
p->list_file = strbuf_detach(&list_file_name, NULL);
p->reftable_dir = xstrdup(dir);
p->config = config;
err = reftable_stack_reload_maybe_reuse(p, 1);
if (err < 0) {
reftable_stack_destroy(p);
} else {
*dest = p;
}
return err;
}
static int fd_read_lines(int fd, char ***namesp)
{
off_t size = lseek(fd, 0, SEEK_END);
char *buf = NULL;
int err = 0;
if (size < 0) {
err = REFTABLE_IO_ERROR;
goto done;
}
err = lseek(fd, 0, SEEK_SET);
if (err < 0) {
err = REFTABLE_IO_ERROR;
goto done;
}
buf = reftable_malloc(size + 1);
if (read(fd, buf, size) != size) {
|