| 759 | } |
| 760 | |
| 761 | int reftable_addition_commit(struct reftable_addition *add) |
| 762 | { |
| 763 | struct reftable_buf table_list = REFTABLE_BUF_INIT; |
| 764 | int err = 0; |
| 765 | size_t i; |
| 766 | |
| 767 | if (add->new_tables_len == 0) |
| 768 | goto done; |
| 769 | |
| 770 | for (i = 0; i < add->stack->merged->tables_len; i++) { |
| 771 | if ((err = reftable_buf_addstr(&table_list, add->stack->tables[i]->name)) < 0 || |
| 772 | (err = reftable_buf_addstr(&table_list, "\n")) < 0) |
| 773 | goto done; |
| 774 | } |
| 775 | for (i = 0; i < add->new_tables_len; i++) { |
| 776 | if ((err = reftable_buf_addstr(&table_list, add->new_tables[i])) < 0 || |
| 777 | (err = reftable_buf_addstr(&table_list, "\n")) < 0) |
| 778 | goto done; |
| 779 | } |
| 780 | |
| 781 | err = reftable_write_data(add->tables_list_lock.fd, |
| 782 | table_list.buf, table_list.len); |
| 783 | reftable_buf_release(&table_list); |
| 784 | if (err < 0) { |
| 785 | err = REFTABLE_IO_ERROR; |
| 786 | goto done; |
| 787 | } |
| 788 | |
| 789 | err = fsync(add->tables_list_lock.fd); |
| 790 | if (err < 0) { |
| 791 | err = REFTABLE_IO_ERROR; |
| 792 | goto done; |
| 793 | } |
| 794 | |
| 795 | err = flock_commit(&add->tables_list_lock); |
| 796 | if (err < 0) { |
| 797 | err = REFTABLE_IO_ERROR; |
| 798 | goto done; |
| 799 | } |
| 800 | |
| 801 | /* success, no more state to clean up. */ |
| 802 | for (i = 0; i < add->new_tables_len; i++) |
| 803 | reftable_free(add->new_tables[i]); |
| 804 | reftable_free(add->new_tables); |
| 805 | add->new_tables = NULL; |
| 806 | add->new_tables_len = 0; |
| 807 | add->new_tables_cap = 0; |
| 808 | |
| 809 | err = reftable_stack_reload_maybe_reuse(add->stack, 1); |
| 810 | if (err) |
| 811 | goto done; |
| 812 | |
| 813 | if (!add->stack->opts.disable_auto_compact) { |
| 814 | /* |
| 815 | * Auto-compact the stack to keep the number of tables in |
| 816 | * control. It is possible that a concurrent writer is already |
| 817 | * trying to compact parts of the stack, which would lead to a |
| 818 | * `REFTABLE_LOCK_ERROR` because parts of the stack are locked |