From patchwork Thu Oct 17 04:53:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13839462 Received: from fout-a3-smtp.messagingengine.com (fout-a3-smtp.messagingengine.com [103.168.172.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D375536AF5 for ; Thu, 17 Oct 2024 04:53:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.146 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140831; cv=none; b=UnocyYoM64tv+JyBpqCn16J/3hZpVlpi3kN9PApOwAA1tRgWg4uuJBcq1Aw/TFRH3WGUpeNGrpRwUSPytW/Tev36HQ3OWHAWM5zHjwtKiwDyN2OFMnM7gWFYP2XCnCXla9tJUJ2RBFsU2LfqhJ55XFgkbsgVhJEJh0xG94hWsYA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140831; c=relaxed/simple; bh=e1vDbCaIeRFSB916uV/W3/0TS9EgZkJ/HiNpgi8jZZ4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ocYzW0WXbAWnvA/DORYprI5X16kSjEotFv2zRZRBTn9BJ8KdfAxhokQHrdTcmDXOCGl+X4cetQiaL0y2gzlEGWW0cZrFK71a55fjT2E4RREhcinRzc6RUmrIXVtrDaUKqkHj6WQI882OMB3Dcy1g74NrmwxywDYh1p4CNWge8uE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=gDI4D2j3; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Po9zE8PG; arc=none smtp.client-ip=103.168.172.146 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="gDI4D2j3"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Po9zE8PG" Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id B3CE613801D2; Thu, 17 Oct 2024 00:53:47 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Thu, 17 Oct 2024 00:53:47 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1729140827; x=1729227227; bh=So2zO1PSIp +KUagfBzDouiUvBG3FI4ll3m4Bya08o1c=; b=gDI4D2j3KDgp0V99UUPjNFG1v3 Vje03ddeXmIdfFHiUe8GzbBIpEWEvNj3gZ9CJsOjX4TunFn5CDMEHqkjUzajHnV/ Z1Jn072o+6yxw080OHMEUvcUZGhGIYboWZkv9JwAd8QBSM2Q7deTtxQ9LZM3yxgL 9ZK+Z354mdCpzZJKV7jTkhRJJKxNOpYmHn6fpzrZZ0NjPVC2YIpm+NhvmgS1qC5E limV3JxrzwU2ww53ufnc7GeQDjinIMyXTB0+vbjK8YuC1ZvYz0d8UDGUZ/uhzQdE aWtiKsvB77WgdQDc2uCBAxXAN/YGP40UhmwN5H5g8F7l5QkKNp4b5BQntZzg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1729140827; x=1729227227; bh=So2zO1PSIp+KUagfBzDouiUvBG3F I4ll3m4Bya08o1c=; b=Po9zE8PGAZZW+QPyfqCdqLEj4JlWoXGDjsajmUIx9eIE KEoxftTK5uyN0d8a3CQOXgJTY7Z0ktBsYOLCQDmyi+8CD6fzVe9F15K8NKulmOq9 Nbe58Jq6STr2LVq4lllK3dzgMxlSHt0nXw40PqprvtLQHndSQWKG5bmbjPX0/GNm scunePPp32d6nGgKlSHGVapPOu4w14Yx9fsS/WP33SSqmbaPlGo4nJ20nZJ88P6D VJqDcngIB1H5ym1SqYoO2WdYNQRSvCgxRYOW/d4882539/3lK6dboWxQ3PgusNrR KY+8akHCNIny2A1XYnUC346UmGJLjIsuzoSk8GC4/A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehtddgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohephedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepmhgvsehtthgrhihlohhrrhdrtghomhdprhgtphhtth hopehkrghrthhhihhkrddukeeksehgmhgrihhlrdgtohhmpdhrtghpthhtohepshhhvghj ihgrlhhuohesghhmrghilhdrtghomhdprhgtphhtthhopegvthhhohhmshhonhesvggufi grrhguthhhohhmshhonhdrtghomhdprhgtphhtthhopehgihhtsehvghgvrhdrkhgvrhhn vghlrdhorhhg X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 17 Oct 2024 00:53:46 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 39b6d224 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 17 Oct 2024 04:52:27 +0000 (UTC) Date: Thu, 17 Oct 2024 06:53:45 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , karthik nayak , Taylor Blau , shejialuo Subject: [PATCH v3 01/10] reftable: stop using `strbuf_addbuf()` Message-ID: <7408482c152bbf465ecd098059b1477fd38c251a.1729140565.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: We're about to introduce our own `reftable_buf` type to replace `strbuf`. Get rid of the seldomly-used `strbuf_addbuf()` function such that we have to reimplement one less function. Signed-off-by: Patrick Steinhardt --- reftable/block.c | 2 +- reftable/record.c | 6 +++--- reftable/writer.c | 7 ++++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 8d41a2f99ed..cd4180eac7b 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -60,7 +60,7 @@ static int block_writer_register_restart(struct block_writer *w, int n, w->next += n; strbuf_reset(&w->last_key); - strbuf_addbuf(&w->last_key, key); + strbuf_add(&w->last_key, key->buf, key->len); w->entries++; return 0; } diff --git a/reftable/record.c b/reftable/record.c index 30d563e16d3..87157f2c386 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -1031,7 +1031,7 @@ static void reftable_index_record_key(const void *r, struct strbuf *dest) { const struct reftable_index_record *rec = r; strbuf_reset(dest); - strbuf_addbuf(dest, &rec->last_key); + strbuf_add(dest, rec->last_key.buf, rec->last_key.len); } static int reftable_index_record_copy_from(void *rec, const void *src_rec, @@ -1041,7 +1041,7 @@ static int reftable_index_record_copy_from(void *rec, const void *src_rec, const struct reftable_index_record *src = src_rec; strbuf_reset(&dst->last_key); - strbuf_addbuf(&dst->last_key, &src->last_key); + strbuf_add(&dst->last_key, src->last_key.buf, src->last_key.len); dst->offset = src->offset; return 0; @@ -1085,7 +1085,7 @@ static int reftable_index_record_decode(void *rec, struct strbuf key, int n = 0; strbuf_reset(&r->last_key); - strbuf_addbuf(&r->last_key, &key); + strbuf_add(&r->last_key, key.buf, key.len); n = get_var_int(&r->offset, &in); if (n < 0) diff --git a/reftable/writer.c b/reftable/writer.c index b032a47decb..031d8149a9c 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -225,7 +225,7 @@ static int writer_index_hash(struct reftable_writer *w, struct strbuf *hash) *key = empty; strbuf_reset(&key->hash); - strbuf_addbuf(&key->hash, hash); + strbuf_add(&key->hash, hash->buf, hash->len); tree_insert(&w->obj_index_tree, key, &obj_index_tree_node_compare); } else { @@ -256,7 +256,7 @@ static int writer_add_record(struct reftable_writer *w, } strbuf_reset(&w->last_key); - strbuf_addbuf(&w->last_key, &key); + strbuf_add(&w->last_key, key.buf, key.len); if (!w->block_writer) { err = writer_reinit_block_writer(w, reftable_record_type(rec)); if (err < 0) @@ -778,7 +778,8 @@ static int writer_flush_nonempty_block(struct reftable_writer *w) index_record.offset = w->next; strbuf_reset(&index_record.last_key); - strbuf_addbuf(&index_record.last_key, &w->block_writer->last_key); + strbuf_add(&index_record.last_key, w->block_writer->last_key.buf, + w->block_writer->last_key.len); w->index[w->index_len] = index_record; w->index_len++; From patchwork Thu Oct 17 04:53:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13839463 Received: from fhigh-a8-smtp.messagingengine.com (fhigh-a8-smtp.messagingengine.com [103.168.172.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A65F1531D8 for ; Thu, 17 Oct 2024 04:53:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.159 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140834; cv=none; b=m0kKmcW5+J5OZgFNS0OgVx/UQbkjQX1xbM0SFHc0dzdJLRI/t3puA3k6gyXyZCRZjJQ5pCOKCkCJe43MwZlw/DAZcI2wlDN6UHO4xS+ShBLeKRTvORNdnsETP+INgOnHog2idlASMefn3jh/Vysgeh+B23lIddUdxKLZgTpyNT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140834; c=relaxed/simple; bh=3IC8IvBQ/WO/2PNSiTHyry8UPrgpBtDvlMq9m76bbms=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=sOzhKtdHbgxQDCTyp+MgrRcQk5Nx0/1xChkpmQiwVhgp5UXMejWmpOBInw6qYD8eXxmt827HPYGQCDMTtu6ea35B6hIG6mW7bxi5ysObMSe7X+VyTmmq722ca9UcYWWwXDA3Cr6DZFqjTkMHPDmXyyBlH0wwQFwWSJocpa34O/U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=JgnaaiR9; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=XcqLuvYw; arc=none smtp.client-ip=103.168.172.159 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="JgnaaiR9"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="XcqLuvYw" Received: from phl-compute-07.internal (phl-compute-07.phl.internal [10.202.2.47]) by mailfhigh.phl.internal (Postfix) with ESMTP id 64BE411400A6; Thu, 17 Oct 2024 00:53:50 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-07.internal (MEProxy); Thu, 17 Oct 2024 00:53:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1729140830; x=1729227230; bh=/bNVzBSijE IK8nCo913Y+3jcI9Efm0YGi7/5Tc/NFVU=; b=JgnaaiR9FYa6uibOYIuCWNGa7h hSrfaoh20fuN/DRRXkBmGw/WfV1rFbrjfO9CacmnlZpMFwdocy/q5Gpn8pCRjZWC YmQFINro3NTYM6dC04GMyoOCOFLZfzxD8OEswdbikAYWPsbl+oo4LVer161a/gXj MLKo962o2ZkZIxlwJb8pDfNwN3CTRZ+HAyLlD1uHnjpmzF+ihT5C74rDFAlZsemn OWfUmF+fuXx5NbnxX2q7L+Ari/uHcyNP030Ogp7rK00Jrai81/8yQ61fn+pz61IY EtYu/ECvqu21D4Gt2hMH8q4N0gzcOLONa97FwOLLD0XAtzPTaKyKP7zy601Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1729140830; x=1729227230; bh=/bNVzBSijEIK8nCo913Y+3jcI9Ef m0YGi7/5Tc/NFVU=; b=XcqLuvYwqtqso78DrV0kmP42eHQQZulIgeTsRByT+Qhw Xl8EGmj62VuVDx9HFBkGwt3i+bgarE/2RsZ+WZj9b+RJR3szOdUfV3pKIxjCTWKj yGM/gUaLiKF5UiachsXyoXxI9IYlaAjnegXPRPk/m1xzuOJcR3fUiZBV2JVmm8yL 0FnQQoWrNKMfq0tU3Ol/AgsSHoAhrpgZTp66IhJjPl9avAINxcvNMrzndUq/0Cnm cIU1DhtOsxwkQiLIFnN0y1iYUkklLu3/VH3Y3Vy7mugJYLmdAWVfzmwS2iJgOP3P q6YdD4NQ5AQjKyvWoPrQKliNIzsytDnJrpkj70jLMg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehtddgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohephedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepshhhvghjihgrlhhuohesghhmrghilhdrtghomhdprh gtphhtthhopegvthhhohhmshhonhesvggufigrrhguthhhohhmshhonhdrtghomhdprhgt phhtthhopehgihhtsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepmhgvse htthgrhihlohhrrhdrtghomhdprhgtphhtthhopehkrghrthhhihhkrddukeeksehgmhgr ihhlrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 17 Oct 2024 00:53:48 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id b30a5309 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 17 Oct 2024 04:52:30 +0000 (UTC) Date: Thu, 17 Oct 2024 06:53:47 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , karthik nayak , Taylor Blau , shejialuo Subject: [PATCH v3 02/10] reftable: stop using `strbuf_addf()` Message-ID: <634fd3c35f5b314093db613d71c83f06bcae0443.1729140565.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: We're about to introduce our own `reftable_buf` type to replace `strbuf`. One function we'll have to convert is `strbuf_addf()`, which is used in a handful of places. This function uses `snprintf()` internally, which makes porting it a bit more involved: - It is not available on all platforms. - Some platforms like Windows have broken implementations. So by using `snprintf()` we'd also push the burden on downstream users of the reftable library to make available a properly working version of it. Most callsites of `strbuf_addf()` are trivial to convert to not using it. We do end up using `snprintf()` in our unit tests, but that isn't much of a problem for downstream users of the reftable library. While at it, remove a useless call to `strbuf_reset()` in `t_reftable_stack_auto_compaction_with_locked_tables()`. We don't write to the buffer before this and initialize it with `STRBUF_INIT`, so there is no need to reset anything. Signed-off-by: Patrick Steinhardt --- reftable/stack.c | 18 ++++++++----- t/unit-tests/t-reftable-block.c | 7 +++-- t/unit-tests/t-reftable-readwrite.c | 20 +++++++------- t/unit-tests/t-reftable-stack.c | 42 ++++++++++++++++------------- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index 7e617c25914..d7bc1187dfb 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -1387,12 +1387,18 @@ static int stack_compact_range(struct reftable_stack *st, * have just written. In case the compacted table became empty we * simply skip writing it. */ - for (i = 0; i < first_to_replace; i++) - strbuf_addf(&tables_list_buf, "%s\n", names[i]); - if (!is_empty_table) - strbuf_addf(&tables_list_buf, "%s\n", new_table_name.buf); - for (i = last_to_replace + 1; names[i]; i++) - strbuf_addf(&tables_list_buf, "%s\n", names[i]); + for (i = 0; i < first_to_replace; i++) { + strbuf_addstr(&tables_list_buf, names[i]); + strbuf_addstr(&tables_list_buf, "\n"); + } + if (!is_empty_table) { + strbuf_addstr(&tables_list_buf, new_table_name.buf); + strbuf_addstr(&tables_list_buf, "\n"); + } + for (i = last_to_replace + 1; names[i]; i++) { + strbuf_addstr(&tables_list_buf, names[i]); + strbuf_addstr(&tables_list_buf, "\n"); + } err = write_in_full(get_lock_file_fd(&tables_list_lock), tables_list_buf.buf, tables_list_buf.len); diff --git a/t/unit-tests/t-reftable-block.c b/t/unit-tests/t-reftable-block.c index d470060e8be..8077bbc5e7a 100644 --- a/t/unit-tests/t-reftable-block.c +++ b/t/unit-tests/t-reftable-block.c @@ -308,10 +308,13 @@ static void t_index_block_read_write(void) check(!ret); for (i = 0; i < N; i++) { - strbuf_init(&recs[i].u.idx.last_key, 9); + char buf[128]; + + snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i); + strbuf_init(&recs[i].u.idx.last_key, 9); recs[i].type = BLOCK_TYPE_INDEX; - strbuf_addf(&recs[i].u.idx.last_key, "branch%02"PRIuMAX, (uintmax_t)i); + strbuf_addstr(&recs[i].u.idx.last_key, buf); recs[i].u.idx.offset = i; ret = block_writer_add(&bw, &recs[i]); diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index 27ce84445e8..5f59b0ad6ad 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -753,12 +753,13 @@ static void t_write_multiple_indices(void) struct reftable_write_options opts = { .block_size = 100, }; - struct strbuf writer_buf = STRBUF_INIT, buf = STRBUF_INIT; + struct strbuf writer_buf = STRBUF_INIT; struct reftable_block_source source = { 0 }; struct reftable_iterator it = { 0 }; const struct reftable_stats *stats; struct reftable_writer *writer; struct reftable_reader *reader; + char buf[128]; int err, i; writer = t_reftable_strbuf_writer(&writer_buf, &opts); @@ -770,9 +771,8 @@ static void t_write_multiple_indices(void) .value.val1 = {i}, }; - strbuf_reset(&buf); - strbuf_addf(&buf, "refs/heads/%04d", i); - ref.refname = buf.buf, + snprintf(buf, sizeof(buf), "refs/heads/%04d", i); + ref.refname = buf; err = reftable_writer_add_ref(writer, &ref); check(!err); @@ -788,9 +788,8 @@ static void t_write_multiple_indices(void) }, }; - strbuf_reset(&buf); - strbuf_addf(&buf, "refs/heads/%04d", i); - log.refname = buf.buf, + snprintf(buf, sizeof(buf), "refs/heads/%04d", i); + log.refname = buf; err = reftable_writer_add_log(writer, &log); check(!err); @@ -824,7 +823,6 @@ static void t_write_multiple_indices(void) reftable_writer_free(writer); reftable_reader_decref(reader); strbuf_release(&writer_buf); - strbuf_release(&buf); } static void t_write_multi_level_index(void) @@ -848,10 +846,10 @@ static void t_write_multi_level_index(void) .value_type = REFTABLE_REF_VAL1, .value.val1 = {i}, }; + char buf[128]; - strbuf_reset(&buf); - strbuf_addf(&buf, "refs/heads/%03" PRIuMAX, (uintmax_t)i); - ref.refname = buf.buf, + snprintf(buf, sizeof(buf), "refs/heads/%03" PRIuMAX, (uintmax_t)i); + ref.refname = buf; err = reftable_writer_add_ref(writer, &ref); check(!err); diff --git a/t/unit-tests/t-reftable-stack.c b/t/unit-tests/t-reftable-stack.c index 874095b9ee2..b56ea774312 100644 --- a/t/unit-tests/t-reftable-stack.c +++ b/t/unit-tests/t-reftable-stack.c @@ -105,7 +105,6 @@ static int write_test_ref(struct reftable_writer *wr, void *arg) static void write_n_ref_tables(struct reftable_stack *st, size_t n) { - struct strbuf buf = STRBUF_INIT; int disable_auto_compact; int err; @@ -117,10 +116,10 @@ static void write_n_ref_tables(struct reftable_stack *st, .update_index = reftable_stack_next_update_index(st), .value_type = REFTABLE_REF_VAL1, }; + char buf[128]; - strbuf_reset(&buf); - strbuf_addf(&buf, "refs/heads/branch-%04"PRIuMAX, (uintmax_t)i); - ref.refname = buf.buf; + snprintf(buf, sizeof(buf), "refs/heads/branch-%04"PRIuMAX, (uintmax_t)i); + ref.refname = buf; t_reftable_set_hash(ref.value.val1, i, GIT_SHA1_FORMAT_ID); err = reftable_stack_add(st, &write_test_ref, &ref); @@ -128,7 +127,6 @@ static void write_n_ref_tables(struct reftable_stack *st, } st->opts.disable_auto_compact = disable_auto_compact; - strbuf_release(&buf); } struct write_log_arg { @@ -434,7 +432,10 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void) * Adding a new table to the stack should not be impacted by this, even * though auto-compaction will now fail. */ - strbuf_addf(&table_path, "%s/%s.lock", dir, st->readers[0]->name); + strbuf_addstr(&table_path, dir); + strbuf_addstr(&table_path, "/"); + strbuf_addstr(&table_path, st->readers[0]->name); + strbuf_addstr(&table_path, ".lock"); write_file_buf(table_path.buf, "", 0); ref.update_index = 2; @@ -1077,8 +1078,10 @@ static void t_reftable_stack_auto_compaction_with_locked_tables(void) * size, we expect that auto-compaction will want to compact all of the * tables. Locking any of the tables will keep it from doing so. */ - strbuf_reset(&buf); - strbuf_addf(&buf, "%s/%s.lock", dir, st->readers[2]->name); + strbuf_addstr(&buf, dir); + strbuf_addstr(&buf, "/"); + strbuf_addstr(&buf, st->readers[2]->name); + strbuf_addstr(&buf, ".lock"); write_file_buf(buf.buf, "", 0); /* @@ -1101,7 +1104,6 @@ static void t_reftable_stack_add_performs_auto_compaction(void) { struct reftable_write_options opts = { 0 }; struct reftable_stack *st = NULL; - struct strbuf refname = STRBUF_INIT; char *dir = get_tmp_dir(__LINE__); int err; size_t i, n = 20; @@ -1115,6 +1117,7 @@ static void t_reftable_stack_add_performs_auto_compaction(void) .value_type = REFTABLE_REF_SYMREF, .value.symref = (char *) "master", }; + char buf[128]; /* * Disable auto-compaction for all but the last runs. Like this @@ -1123,9 +1126,8 @@ static void t_reftable_stack_add_performs_auto_compaction(void) */ st->opts.disable_auto_compact = i != n; - strbuf_reset(&refname); - strbuf_addf(&refname, "branch-%04"PRIuMAX, (uintmax_t)i); - ref.refname = refname.buf; + snprintf(buf, sizeof(buf), "branch-%04"PRIuMAX, (uintmax_t)i); + ref.refname = buf; err = reftable_stack_add(st, write_test_ref, &ref); check(!err); @@ -1142,7 +1144,6 @@ static void t_reftable_stack_add_performs_auto_compaction(void) } reftable_stack_destroy(st); - strbuf_release(&refname); clear_dir(dir); } @@ -1163,8 +1164,10 @@ static void t_reftable_stack_compaction_with_locked_tables(void) check_int(st->merged->readers_len, ==, 3); /* Lock one of the tables that we're about to compact. */ - strbuf_reset(&buf); - strbuf_addf(&buf, "%s/%s.lock", dir, st->readers[1]->name); + strbuf_addstr(&buf, dir); + strbuf_addstr(&buf, "/"); + strbuf_addstr(&buf, st->readers[1]->name); + strbuf_addstr(&buf, ".lock"); write_file_buf(buf.buf, "", 0); /* @@ -1321,10 +1324,13 @@ static void t_reftable_stack_reload_with_missing_table(void) * our old readers. This should trigger a partial reload of the stack, * where we try to reuse our old readers. */ - strbuf_addf(&content, "%s\n", st->readers[0]->name); - strbuf_addf(&content, "%s\n", st->readers[1]->name); + strbuf_addstr(&content, st->readers[0]->name); + strbuf_addstr(&content, "\n"); + strbuf_addstr(&content, st->readers[1]->name); + strbuf_addstr(&content, "\n"); strbuf_addstr(&content, "garbage\n"); - strbuf_addf(&table_path, "%s.lock", st->list_file); + strbuf_addstr(&table_path, st->list_file); + strbuf_addstr(&table_path, ".lock"); write_file_buf(table_path.buf, content.buf, content.len); err = rename(table_path.buf, st->list_file); check(!err); From patchwork Thu Oct 17 04:53:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13839464 Received: from fout-a3-smtp.messagingengine.com (fout-a3-smtp.messagingengine.com [103.168.172.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0055C12C484 for ; Thu, 17 Oct 2024 04:53:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.146 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140840; cv=none; b=ANXbGyR8jT4shPDYzhhjOVkB1UWlu0f6TeJQVjKFk1ZI0wXw5ZlcBxosRb52k8pTRuwtGiITgofc0Ej/Bp2ytyLCxdA45s3kU7+AXuS/xX0Zg0gJqH2rgLekaULFarrP1GtXndEbudVkCBmEvFzMeld4WKX/4b72q9WNsrWVmkc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140840; c=relaxed/simple; bh=IElx/x3adFu5BW9BrLpHnkkgAHHI4bSVRrBcG2rNnX8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rEJkKroOgqqpIV/kgzg13qMKMlIIbt98Fyfco75cWi0FSIr4yFGDbyvQXbeblRiOY49/q1XXTFGmemjYhn5XP00WPBR3jJ8HomXVZnGFW/g86DjWCOITbB5Na9Cr8WPSi0Oz3jIV8htCJv/UkHb2BVw3KueMO29is6fHiHocq90= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=GoyoXQQa; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=d3Q0A3s2; arc=none smtp.client-ip=103.168.172.146 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="GoyoXQQa"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="d3Q0A3s2" Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id E760913801F0; Thu, 17 Oct 2024 00:53:55 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Thu, 17 Oct 2024 00:53:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1729140835; x=1729227235; bh=LrNP231hlT Pe6NzZQeOy2yl9kSjLih7ANHMpRFnefZQ=; b=GoyoXQQatSdh8Ayv7WTC2pnpa/ P0pUf4xrbWI/zg4N3tF50ontol3G/gJUb4SzvalJROznz5uHNOcBIvbN46fCJZ6w XCnyrGn0MqLM7GZoIDZ7yQ34GX+EWWjDS3Lc/Md66Sxo7D7pQ7jFsuz1+VGtx+YY jF86C1KgRCqisGpVFgnZXwZxIJh/Ri1/2YI0x1kfWofUi9qtEB9ezZQiKEF5Z+5H K4Sw0dGu6uO/6XkoZkNFWhg28id4OwOAeXBtaY5fTcmNMiMKgI96K3/S4GoSinaA HUm2nr7GiEowv3o+7AKZZ78O1YLhbOvqRnHEJXpvsDeoM+hCIt5DU/t0UCHw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1729140835; x=1729227235; bh=LrNP231hlTPe6NzZQeOy2yl9kSjL ih7ANHMpRFnefZQ=; b=d3Q0A3s23mTJXgHibcIq5ohib7fePQsjSDVbFUSPnwZk 1fD3cW1XYPxuFVVbB2a+ro1q6gQsGk+hqbh/w1hthNv8HMnJNUpGlICuOE0soGOU mFsSRjuYWLCTKAPzwuDEVnpl2dg4ky098N9v7mBlo900hmcJDMgKctRBYLHkYnPu dpVwKYgkyQMVaCUzGRDfGV8sChUUydjj/Nd2siqM7hAD7fEywLZF1v9141SP9vGV OdLfNPAGjgQKn0jbQ1rHE0S5EiofBVKOsbHAleh8f2Xg3BFeGsgoIf78qnzD5O54 hWK7KkUorEWxkqWY6+UwSH386xQ31rg5CSPhfQ7Wbw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehtddgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohephedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomhhsoh hnrdgtohhmpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgt phhtthhopehkrghrthhhihhkrddukeeksehgmhgrihhlrdgtohhmpdhrtghpthhtohepsh hhvghjihgrlhhuohesghhmrghilhdrtghomhdprhgtphhtthhopehmvgesthhtrgihlhho rhhrrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 17 Oct 2024 00:53:54 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 4d8c2a18 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 17 Oct 2024 04:52:36 +0000 (UTC) Date: Thu, 17 Oct 2024 06:53:50 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , karthik nayak , Taylor Blau , shejialuo Subject: [PATCH v3 03/10] reftable/basics: provide new `reftable_buf` interface Message-ID: <53c5f667f28c89ebcf023055423af24920e8c0ca.1729140565.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Implement a new `reftable_buf` interface that will replace Git's own `strbuf` interface. This is done due to three reasons: - The `strbuf` interfaces do not handle memory allocation failures and instead causes us to die. This is okay in the context of Git, but is not in the context of the reftable library, which is supposed to be usable by third-party applications. - The `strbuf` interface is quite deeply tied into Git, which makes it hard to use the reftable library as a standalone library. Any dependent would have to carefully extract the relevant parts of it to make things work, which is not all that sensible. - The `strbuf` interface does not use the pluggable allocators that can be set up via `reftable_set_alloc()`. So we have good reasons to use our own type, and the implementation is rather trivial. Implement our own type. Conversion of the reftable library will be handled in subsequent commits. Signed-off-by: Patrick Steinhardt --- reftable/basics.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++ reftable/basics.h | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/reftable/basics.c b/reftable/basics.c index 9a949e5cf80..65ad761da0b 100644 --- a/reftable/basics.c +++ b/reftable/basics.c @@ -9,6 +9,7 @@ license that can be found in the LICENSE file or at #define REFTABLE_ALLOW_BANNED_ALLOCATORS #include "basics.h" #include "reftable-basics.h" +#include "reftable-error.h" static void *(*reftable_malloc_ptr)(size_t sz); static void *(*reftable_realloc_ptr)(void *, size_t); @@ -69,6 +70,79 @@ void reftable_set_alloc(void *(*malloc)(size_t), reftable_free_ptr = free; } +void reftable_buf_init(struct reftable_buf *buf) +{ + struct reftable_buf empty = REFTABLE_BUF_INIT; + *buf = empty; +} + +void reftable_buf_release(struct reftable_buf *buf) +{ + reftable_free(buf->buf); + reftable_buf_init(buf); +} + +void reftable_buf_reset(struct reftable_buf *buf) +{ + if (buf->alloc) { + buf->len = 0; + buf->buf[0] = '\0'; + } +} + +int reftable_buf_setlen(struct reftable_buf *buf, size_t len) +{ + if (len > buf->len) + return -1; + if (len == buf->len) + return 0; + buf->buf[len] = '\0'; + buf->len = len; + return 0; +} + +int reftable_buf_cmp(const struct reftable_buf *a, const struct reftable_buf *b) +{ + size_t len = a->len < b->len ? a->len : b->len; + if (len) { + int cmp = memcmp(a->buf, b->buf, len); + if (cmp) + return cmp; + } + return a->len < b->len ? -1 : a->len != b->len; +} + +int reftable_buf_add(struct reftable_buf *buf, const void *data, size_t len) +{ + size_t newlen = buf->len + len; + + if (newlen + 1 > buf->alloc) { + char *reallocated = buf->buf; + REFTABLE_ALLOC_GROW(reallocated, newlen + 1, buf->alloc); + if (!reallocated) + return REFTABLE_OUT_OF_MEMORY_ERROR; + buf->buf = reallocated; + } + + memcpy(buf->buf + buf->len, data, len); + buf->buf[newlen] = '\0'; + buf->len = newlen; + + return 0; +} + +int reftable_buf_addstr(struct reftable_buf *buf, const char *s) +{ + return reftable_buf_add(buf, s, strlen(s)); +} + +char *reftable_buf_detach(struct reftable_buf *buf) +{ + char *result = buf->buf; + reftable_buf_init(buf); + return result; +} + void put_be24(uint8_t *out, uint32_t i) { out[0] = (uint8_t)((i >> 16) & 0xff); diff --git a/reftable/basics.h b/reftable/basics.h index 4c9ef0fe6c5..bd33c34deae 100644 --- a/reftable/basics.h +++ b/reftable/basics.h @@ -16,6 +16,62 @@ license that can be found in the LICENSE file or at #include "system.h" #include "reftable-basics.h" +struct reftable_buf { + size_t alloc; + size_t len; + char *buf; +}; +#define REFTABLE_BUF_INIT { 0 } + +/* + * Initialize the buffer such that it is ready for use. This is equivalent to + * using REFTABLE_BUF_INIT for stack-allocated variables. + */ +void reftable_buf_init(struct reftable_buf *buf); + +/* + * Release memory associated with the buffer. The buffer is reinitialized such + * that it can be reused for subsequent operations. + */ +void reftable_buf_release(struct reftable_buf *buf); + +/* + * Reset the buffer such that it is effectively empty, without releasing the + * memory that this structure holds on to. This is equivalent to calling + * `reftable_buf_setlen(buf, 0)`. + */ +void reftable_buf_reset(struct reftable_buf *buf); + +/* + * Trim the buffer to a shorter length by updating the `len` member and writing + * a NUL byte to `buf[len]`. Returns 0 on success, -1 when `len` points outside + * of the array. + */ +int reftable_buf_setlen(struct reftable_buf *buf, size_t len); + +/* + * Lexicographically compare the two buffers. Returns 0 when both buffers have + * the same contents, -1 when `a` is lexicographically smaller than `b`, and 1 + * otherwise. + */ +int reftable_buf_cmp(const struct reftable_buf *a, const struct reftable_buf *b); + +/* + * Add the given bytes to the buffer. Returns 0 on success, + * REFTABLE_OUT_OF_MEMORY_ERROR on allocation failure. + */ +int reftable_buf_add(struct reftable_buf *buf, const void *data, size_t len); + +/* Equivalent to `reftable_buf_add(buf, s, strlen(s))`. */ +int reftable_buf_addstr(struct reftable_buf *buf, const char *s); + +/* + * Detach the buffer from the structure such that the underlying memory is now + * owned by the caller. The buffer is reinitialized such that it can be reused + * for subsequent operations. + */ +char *reftable_buf_detach(struct reftable_buf *buf); + /* Bigendian en/decoding of integers */ void put_be24(uint8_t *out, uint32_t i); From patchwork Thu Oct 17 04:53:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13839465 Received: from fout-a3-smtp.messagingengine.com (fout-a3-smtp.messagingengine.com [103.168.172.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE1E1155A24 for ; Thu, 17 Oct 2024 04:53:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.146 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140843; cv=none; b=nsLbMp1MY2UvKnyJwpkd9DO3J6yNdR+f1HEpj8dfqMz3JXVej+mgG5A/pF8q+s1bnq9JPdTBYkDTy0O1Q7MCwsyzUAGDyCf9kbPTf3a1JC9hlHDvSTO22OKhMhOLu7SbTLx8S7hWttwfUn+YvfiEP0wXwKBkKSxwRr0pt0y0xK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140843; c=relaxed/simple; bh=IL2ga5JQ+wQLl+U9etSwZrFSb42J/6/moH2njdrBoHw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=B+/v+BqphAT5/76qOmoRA8YNGIAa2wFt7bQcUXDmzrYb1mt58uQTcMbHSsj0eKAE4Tkb1UbBmeJoSgkduEeFL/K9JO1AR/lFNO/7mPXB+IevB14Dcf91RwRqM/pK1Dexgk4bdCLNhtJpZYJlziDNjpXLrXD3RTisL/vXaDK+PHM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=eK0/DmTR; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=WfrTVsRP; arc=none smtp.client-ip=103.168.172.146 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="eK0/DmTR"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="WfrTVsRP" Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailfout.phl.internal (Postfix) with ESMTP id C1B3513801D2; Thu, 17 Oct 2024 00:53:58 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-02.internal (MEProxy); Thu, 17 Oct 2024 00:53:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1729140838; x=1729227238; bh=oNzc6YrDOd kr/MHKRe1emM8D3L8iVRT+GQcPxo5lCm4=; b=eK0/DmTRUTFU5a3jKtqLF6fS5P kH1xTpel/rIARncwcQJu6+C5icCdUD5rJ7iKG3w+zkWL9UbpacQPFLgVfgSYAyrW HM8zwksyptUf0mLrfmXtQHiAwfJX/DYAAiUzGh9VhdVNI73TH9GNf+CSWNq/WPXR FQnv2obPo3ygNviVlLqONN65vH3bnviWoRgtu4H9FwejjafJXX+EWVIvr3JwOzR3 JORfe/jO808W+fk2vnImxIPHwKB/TE/ounkOxN3tL4RqRCy5zinLoq/QA7N1X1Iz SkU9gah5uXYMjjIN2N64ShPwwMM57g7a/YCrtui0lvFwzhxeBldnGLTsh3mA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1729140838; x=1729227238; bh=oNzc6YrDOdkr/MHKRe1emM8D3L8i VRT+GQcPxo5lCm4=; b=WfrTVsRPbZB4si7+y4B38BtHP/3NuAeyOZT+H1sHJcIK 2sreEZs/08m5A9myhKBDL8qBOum5h9RbJgLZ1yH44TUdr/KHu/aTj94Uj6zMLo9t x2EZEGK0DDzFQvp3hRpIeEutaX5WUZeezai6LrYVEBjsQzXFVIaLSOAXv7MiNzic 8oMuVITaBmYXzyx8q9nQz3dFm7U5CK1Pv0n1Xn2F4PI4TEQmgOZpvCEE0TLbo/E0 58hPxSZMXAezjmaQ5MmATrE/9yV83TDi3Ju5wqeHfVlo7xzvlfT5/uAPKD7zdxcq +AgrxLSJNyac+HkLKbLr6uZI5e73eQgl8FS7y06gOQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehtddgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepgfevieehledvleevkeekvdfhjeejledvgfduvdetkeek gfeilefftdeitdehledunecuffhomhgrihhnpehuphgurghtvgdrnhgvfienucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm pdhnsggprhgtphhtthhopeehpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopegvth hhohhmshhonhesvggufigrrhguthhhohhmshhonhdrtghomhdprhgtphhtthhopehshhgv jhhirghluhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepkhgrrhhthhhikhdrudekke esghhmrghilhdrtghomhdprhgtphhtthhopehmvgesthhtrgihlhhorhhrrdgtohhmpdhr tghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrgh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 17 Oct 2024 00:53:57 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 2bc19b93 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 17 Oct 2024 04:52:38 +0000 (UTC) Date: Thu, 17 Oct 2024 06:53:56 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , karthik nayak , Taylor Blau , shejialuo Subject: [PATCH v3 04/10] reftable: convert from `strbuf` to `reftable_buf` Message-ID: <7c7ccc5d96681bc957671a26f4bc5100a95e20fc.1729140565.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Convert the reftable library to use the `reftable_buf` interface instead of the `strbuf` interface. This is mostly a mechanical change via sed(1) with some manual fixes where functions for `strbuf` and `reftable_buf` differ. The converted code does not yet handle allocation failures. This will be handled in subsequent commits. Signed-off-by: Patrick Steinhardt --- reftable/basics.c | 2 +- reftable/basics.h | 7 +- reftable/block.c | 34 ++++---- reftable/block.h | 14 ++-- reftable/blocksource.c | 6 +- reftable/blocksource.h | 3 +- reftable/iter.c | 6 +- reftable/iter.h | 8 +- reftable/reader.c | 16 ++-- reftable/record.c | 80 +++++++++---------- reftable/record.h | 21 ++--- reftable/stack.c | 120 ++++++++++++++-------------- reftable/system.h | 1 - reftable/writer.c | 66 +++++++-------- reftable/writer.h | 2 +- t/unit-tests/lib-reftable.c | 4 +- t/unit-tests/lib-reftable.h | 7 +- t/unit-tests/t-reftable-basics.c | 16 ++-- t/unit-tests/t-reftable-block.c | 42 +++++----- t/unit-tests/t-reftable-merged.c | 26 +++--- t/unit-tests/t-reftable-reader.c | 8 +- t/unit-tests/t-reftable-readwrite.c | 92 ++++++++++----------- t/unit-tests/t-reftable-record.c | 74 ++++++++--------- t/unit-tests/t-reftable-stack.c | 90 ++++++++++----------- 24 files changed, 374 insertions(+), 371 deletions(-) diff --git a/reftable/basics.c b/reftable/basics.c index 65ad761da0b..bc4fcc91446 100644 --- a/reftable/basics.c +++ b/reftable/basics.c @@ -260,7 +260,7 @@ int names_equal(const char **a, const char **b) return a[i] == b[i]; } -int common_prefix_size(struct strbuf *a, struct strbuf *b) +int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b) { int p = 0; for (; p < a->len && p < b->len; p++) { diff --git a/reftable/basics.h b/reftable/basics.h index bd33c34deae..7aa46d7c30d 100644 --- a/reftable/basics.h +++ b/reftable/basics.h @@ -57,7 +57,9 @@ int reftable_buf_setlen(struct reftable_buf *buf, size_t len); int reftable_buf_cmp(const struct reftable_buf *a, const struct reftable_buf *b); /* - * Add the given bytes to the buffer. Returns 0 on success, + * Append `len` bytes from `data` to the buffer. This function works with + * arbitrary byte sequences, including ones that contain embedded NUL + * characters. As such, we use `void *` as input type. Returns 0 on success, * REFTABLE_OUT_OF_MEMORY_ERROR on allocation failure. */ int reftable_buf_add(struct reftable_buf *buf, const void *data, size_t len); @@ -144,8 +146,7 @@ char *reftable_strdup(const char *str); #endif /* Find the longest shared prefix size of `a` and `b` */ -struct strbuf; -int common_prefix_size(struct strbuf *a, struct strbuf *b); +int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b); int hash_size(uint32_t id); diff --git a/reftable/block.c b/reftable/block.c index cd4180eac7b..4f62b823db8 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -38,7 +38,7 @@ int footer_size(int version) } static int block_writer_register_restart(struct block_writer *w, int n, - int is_restart, struct strbuf *key) + int is_restart, struct reftable_buf *key) { int rlen = w->restart_len; if (rlen >= MAX_RESTARTS) { @@ -59,8 +59,8 @@ static int block_writer_register_restart(struct block_writer *w, int n, w->next += n; - strbuf_reset(&w->last_key); - strbuf_add(&w->last_key, key->buf, key->len); + reftable_buf_reset(&w->last_key); + reftable_buf_add(&w->last_key, key->buf, key->len); w->entries++; return 0; } @@ -98,8 +98,8 @@ uint8_t block_writer_type(struct block_writer *bw) empty key. */ int block_writer_add(struct block_writer *w, struct reftable_record *rec) { - struct strbuf empty = STRBUF_INIT; - struct strbuf last = + struct reftable_buf empty = REFTABLE_BUF_INIT; + struct reftable_buf last = w->entries % w->restart_interval == 0 ? empty : w->last_key; struct string_view out = { .buf = w->buf + w->next, @@ -109,7 +109,7 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) struct string_view start = out; int is_restart = 0; - struct strbuf key = STRBUF_INIT; + struct reftable_buf key = REFTABLE_BUF_INIT; int n = 0; int err = -1; @@ -133,7 +133,7 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) err = block_writer_register_restart(w, start.len - out.len, is_restart, &key); done: - strbuf_release(&key); + reftable_buf_release(&key); return err; } @@ -325,7 +325,7 @@ uint8_t block_reader_type(const struct block_reader *r) return r->block.data[r->header_off]; } -int block_reader_first_key(const struct block_reader *br, struct strbuf *key) +int block_reader_first_key(const struct block_reader *br, struct reftable_buf *key) { int off = br->header_off + 4, n; struct string_view in = { @@ -334,7 +334,7 @@ int block_reader_first_key(const struct block_reader *br, struct strbuf *key) }; uint8_t extra = 0; - strbuf_reset(key); + reftable_buf_reset(key); n = reftable_decode_key(key, &extra, in); if (n < 0) @@ -355,13 +355,13 @@ void block_iter_seek_start(struct block_iter *it, const struct block_reader *br) it->block = br->block.data; it->block_len = br->block_len; it->hash_size = br->hash_size; - strbuf_reset(&it->last_key); + reftable_buf_reset(&it->last_key); it->next_off = br->header_off + 4; } struct restart_needle_less_args { int error; - struct strbuf needle; + struct reftable_buf needle; const struct block_reader *reader; }; @@ -433,7 +433,7 @@ int block_iter_next(struct block_iter *it, struct reftable_record *rec) void block_iter_reset(struct block_iter *it) { - strbuf_reset(&it->last_key); + reftable_buf_reset(&it->last_key); it->next_off = 0; it->block = NULL; it->block_len = 0; @@ -442,12 +442,12 @@ void block_iter_reset(struct block_iter *it) void block_iter_close(struct block_iter *it) { - strbuf_release(&it->last_key); - strbuf_release(&it->scratch); + reftable_buf_release(&it->last_key); + reftable_buf_release(&it->scratch); } int block_iter_seek_key(struct block_iter *it, const struct block_reader *br, - struct strbuf *want) + struct reftable_buf *want) { struct restart_needle_less_args args = { .needle = *want, @@ -537,7 +537,7 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br, * with themselves. */ reftable_record_key(&rec, &it->last_key); - if (strbuf_cmp(&it->last_key, want) >= 0) { + if (reftable_buf_cmp(&it->last_key, want) >= 0) { it->next_off = prev_off; goto done; } @@ -554,7 +554,7 @@ void block_writer_release(struct block_writer *bw) REFTABLE_FREE_AND_NULL(bw->zstream); REFTABLE_FREE_AND_NULL(bw->restarts); REFTABLE_FREE_AND_NULL(bw->compressed); - strbuf_release(&bw->last_key); + reftable_buf_release(&bw->last_key); /* the block is not owned. */ } diff --git a/reftable/block.h b/reftable/block.h index 18d7ea03373..9a3effa5134 100644 --- a/reftable/block.h +++ b/reftable/block.h @@ -38,7 +38,7 @@ struct block_writer { uint32_t restart_len; uint32_t restart_cap; - struct strbuf last_key; + struct reftable_buf last_key; int entries; }; @@ -98,7 +98,7 @@ void block_reader_release(struct block_reader *br); uint8_t block_reader_type(const struct block_reader *r); /* Decodes the first key in the block */ -int block_reader_first_key(const struct block_reader *br, struct strbuf *key); +int block_reader_first_key(const struct block_reader *br, struct reftable_buf *key); /* Iterate over entries in a block */ struct block_iter { @@ -109,13 +109,13 @@ struct block_iter { int hash_size; /* key for last entry we read. */ - struct strbuf last_key; - struct strbuf scratch; + struct reftable_buf last_key; + struct reftable_buf scratch; }; #define BLOCK_ITER_INIT { \ - .last_key = STRBUF_INIT, \ - .scratch = STRBUF_INIT, \ + .last_key = REFTABLE_BUF_INIT, \ + .scratch = REFTABLE_BUF_INIT, \ } /* Position `it` at start of the block */ @@ -123,7 +123,7 @@ void block_iter_seek_start(struct block_iter *it, const struct block_reader *br) /* Position `it` to the `want` key in the block */ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br, - struct strbuf *want); + struct reftable_buf *want); /* return < 0 for error, 0 for OK, > 0 for EOF. */ int block_iter_next(struct block_iter *it, struct reftable_record *rec); diff --git a/reftable/blocksource.c b/reftable/blocksource.c index a2a6a196d55..d6242d67900 100644 --- a/reftable/blocksource.c +++ b/reftable/blocksource.c @@ -27,7 +27,7 @@ static void strbuf_close(void *b UNUSED) static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off, uint32_t size) { - struct strbuf *b = v; + struct reftable_buf *b = v; assert(off + size <= b->len); REFTABLE_CALLOC_ARRAY(dest->data, size); if (!dest->data) @@ -39,7 +39,7 @@ static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off, static uint64_t strbuf_size(void *b) { - return ((struct strbuf *)b)->len; + return ((struct reftable_buf *)b)->len; } static struct reftable_block_source_vtable strbuf_vtable = { @@ -50,7 +50,7 @@ static struct reftable_block_source_vtable strbuf_vtable = { }; void block_source_from_strbuf(struct reftable_block_source *bs, - struct strbuf *buf) + struct reftable_buf *buf) { assert(!bs->ops); bs->ops = &strbuf_vtable; diff --git a/reftable/blocksource.h b/reftable/blocksource.h index 659a27b4063..ee3647c6531 100644 --- a/reftable/blocksource.h +++ b/reftable/blocksource.h @@ -12,9 +12,10 @@ license that can be found in the LICENSE file or at #include "system.h" struct reftable_block_source; +struct reftable_buf; /* Create an in-memory block source for reading reftables */ void block_source_from_strbuf(struct reftable_block_source *bs, - struct strbuf *buf); + struct reftable_buf *buf); #endif diff --git a/reftable/iter.c b/reftable/iter.c index d926db653b1..6c193fd31a9 100644 --- a/reftable/iter.c +++ b/reftable/iter.c @@ -55,7 +55,7 @@ void iterator_set_empty(struct reftable_iterator *it) static void filtering_ref_iterator_close(void *iter_arg) { struct filtering_ref_iterator *fri = iter_arg; - strbuf_release(&fri->oid); + reftable_buf_release(&fri->oid); reftable_iterator_destroy(&fri->it); } @@ -115,7 +115,7 @@ static void indexed_table_ref_iter_close(void *p) block_iter_close(&it->cur); reftable_block_done(&it->block_reader.block); reftable_free(it->offsets); - strbuf_release(&it->oid); + reftable_buf_release(&it->oid); } static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it) @@ -197,7 +197,7 @@ int indexed_table_ref_iter_new(struct indexed_table_ref_iter **dest, *itr = empty; itr->r = r; - strbuf_add(&itr->oid, oid, oid_len); + reftable_buf_add(&itr->oid, oid, oid_len); itr->offsets = offsets; itr->offset_len = offset_len; diff --git a/reftable/iter.h b/reftable/iter.h index b3225bc7add..40f98893b85 100644 --- a/reftable/iter.h +++ b/reftable/iter.h @@ -44,12 +44,12 @@ void iterator_set_empty(struct reftable_iterator *it); /* iterator that produces only ref records that point to `oid` */ struct filtering_ref_iterator { - struct strbuf oid; + struct reftable_buf oid; struct reftable_iterator it; }; #define FILTERING_REF_ITERATOR_INIT \ { \ - .oid = STRBUF_INIT \ + .oid = REFTABLE_BUF_INIT \ } void iterator_from_filtering_ref_iterator(struct reftable_iterator *, @@ -60,7 +60,7 @@ void iterator_from_filtering_ref_iterator(struct reftable_iterator *, */ struct indexed_table_ref_iter { struct reftable_reader *r; - struct strbuf oid; + struct reftable_buf oid; /* mutable */ uint64_t *offsets; @@ -75,7 +75,7 @@ struct indexed_table_ref_iter { #define INDEXED_TABLE_REF_ITER_INIT { \ .cur = BLOCK_ITER_INIT, \ - .oid = STRBUF_INIT, \ + .oid = REFTABLE_BUF_INIT, \ } void iterator_from_indexed_table_ref_iter(struct reftable_iterator *it, diff --git a/reftable/reader.c b/reftable/reader.c index 8d372539220..388f8bf6d7b 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -350,8 +350,8 @@ static int table_iter_seek_start(struct table_iter *ti, uint8_t typ, int index) static int table_iter_seek_linear(struct table_iter *ti, struct reftable_record *want) { - struct strbuf want_key = STRBUF_INIT; - struct strbuf got_key = STRBUF_INIT; + struct reftable_buf want_key = REFTABLE_BUF_INIT; + struct reftable_buf got_key = REFTABLE_BUF_INIT; struct reftable_record rec; int err; @@ -401,7 +401,7 @@ static int table_iter_seek_linear(struct table_iter *ti, if (err < 0) goto done; - if (strbuf_cmp(&got_key, &want_key) > 0) { + if (reftable_buf_cmp(&got_key, &want_key) > 0) { table_iter_block_done(&next); break; } @@ -422,8 +422,8 @@ static int table_iter_seek_linear(struct table_iter *ti, done: reftable_record_release(&rec); - strbuf_release(&want_key); - strbuf_release(&got_key); + reftable_buf_release(&want_key); + reftable_buf_release(&got_key); return err; } @@ -431,11 +431,11 @@ static int table_iter_seek_indexed(struct table_iter *ti, struct reftable_record *rec) { struct reftable_record want_index = { - .type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = STRBUF_INIT } + .type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = REFTABLE_BUF_INIT } }; struct reftable_record index_result = { .type = BLOCK_TYPE_INDEX, - .u.idx = { .last_key = STRBUF_INIT }, + .u.idx = { .last_key = REFTABLE_BUF_INIT }, }; int err; @@ -765,7 +765,7 @@ static int reftable_reader_refs_for_unindexed(struct reftable_reader *r, } *filter = empty; - strbuf_add(&filter->oid, oid, oid_len); + reftable_buf_add(&filter->oid, oid, oid_len); iterator_from_table_iter(&filter->it, ti); iterator_from_filtering_ref_iterator(it, filter); diff --git a/reftable/record.c b/reftable/record.c index 87157f2c386..0182c973437 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -98,7 +98,7 @@ const unsigned char *reftable_ref_record_val2(const struct reftable_ref_record * } } -static int decode_string(struct strbuf *dest, struct string_view in) +static int decode_string(struct reftable_buf *dest, struct string_view in) { int start_len = in.len; uint64_t tsize = 0; @@ -109,8 +109,8 @@ static int decode_string(struct strbuf *dest, struct string_view in) if (in.len < tsize) return -1; - strbuf_reset(dest); - strbuf_add(dest, in.buf, tsize); + reftable_buf_reset(dest); + reftable_buf_add(dest, in.buf, tsize); string_view_consume(&in, tsize); return start_len - in.len; @@ -133,7 +133,7 @@ static int encode_string(const char *str, struct string_view s) } int reftable_encode_key(int *restart, struct string_view dest, - struct strbuf prev_key, struct strbuf key, + struct reftable_buf prev_key, struct reftable_buf key, uint8_t extra) { struct string_view start = dest; @@ -183,7 +183,7 @@ int reftable_decode_keylen(struct string_view in, return start_len - in.len; } -int reftable_decode_key(struct strbuf *last_key, uint8_t *extra, +int reftable_decode_key(struct reftable_buf *last_key, uint8_t *extra, struct string_view in) { int start_len = in.len; @@ -200,19 +200,19 @@ int reftable_decode_key(struct strbuf *last_key, uint8_t *extra, prefix_len > last_key->len) return -1; - strbuf_setlen(last_key, prefix_len); - strbuf_add(last_key, in.buf, suffix_len); + reftable_buf_setlen(last_key, prefix_len); + reftable_buf_add(last_key, in.buf, suffix_len); string_view_consume(&in, suffix_len); return start_len - in.len; } -static void reftable_ref_record_key(const void *r, struct strbuf *dest) +static void reftable_ref_record_key(const void *r, struct reftable_buf *dest) { const struct reftable_ref_record *rec = (const struct reftable_ref_record *)r; - strbuf_reset(dest); - strbuf_addstr(dest, rec->refname); + reftable_buf_reset(dest); + reftable_buf_addstr(dest, rec->refname); } static int reftable_ref_record_copy_from(void *rec, const void *src_rec, @@ -350,9 +350,9 @@ static int reftable_ref_record_encode(const void *rec, struct string_view s, return start.len - s.len; } -static int reftable_ref_record_decode(void *rec, struct strbuf key, +static int reftable_ref_record_decode(void *rec, struct reftable_buf key, uint8_t val_type, struct string_view in, - int hash_size, struct strbuf *scratch) + int hash_size, struct reftable_buf *scratch) { struct reftable_ref_record *r = rec; struct string_view start = in; @@ -415,7 +415,7 @@ static int reftable_ref_record_decode(void *rec, struct strbuf key, goto done; } string_view_consume(&in, n); - r->value.symref = strbuf_detach(scratch, NULL); + r->value.symref = reftable_buf_detach(scratch); } break; case REFTABLE_REF_DELETION: @@ -465,12 +465,12 @@ static struct reftable_record_vtable reftable_ref_record_vtable = { .cmp = &reftable_ref_record_cmp_void, }; -static void reftable_obj_record_key(const void *r, struct strbuf *dest) +static void reftable_obj_record_key(const void *r, struct reftable_buf *dest) { const struct reftable_obj_record *rec = (const struct reftable_obj_record *)r; - strbuf_reset(dest); - strbuf_add(dest, rec->hash_prefix, rec->hash_prefix_len); + reftable_buf_reset(dest); + reftable_buf_add(dest, rec->hash_prefix, rec->hash_prefix_len); } static void reftable_obj_record_release(void *rec) @@ -547,10 +547,10 @@ static int reftable_obj_record_encode(const void *rec, struct string_view s, return start.len - s.len; } -static int reftable_obj_record_decode(void *rec, struct strbuf key, +static int reftable_obj_record_decode(void *rec, struct reftable_buf key, uint8_t val_type, struct string_view in, int hash_size UNUSED, - struct strbuf *scratch UNUSED) + struct reftable_buf *scratch UNUSED) { struct string_view start = in; struct reftable_obj_record *r = rec; @@ -664,19 +664,19 @@ static struct reftable_record_vtable reftable_obj_record_vtable = { .cmp = &reftable_obj_record_cmp_void, }; -static void reftable_log_record_key(const void *r, struct strbuf *dest) +static void reftable_log_record_key(const void *r, struct reftable_buf *dest) { const struct reftable_log_record *rec = (const struct reftable_log_record *)r; int len = strlen(rec->refname); uint8_t i64[8]; uint64_t ts = 0; - strbuf_reset(dest); - strbuf_add(dest, (uint8_t *)rec->refname, len + 1); + reftable_buf_reset(dest); + reftable_buf_add(dest, (uint8_t *)rec->refname, len + 1); ts = (~ts) - rec->update_index; put_be64(&i64[0], ts); - strbuf_add(dest, i64, sizeof(i64)); + reftable_buf_add(dest, i64, sizeof(i64)); } static int reftable_log_record_copy_from(void *rec, const void *src_rec, @@ -807,9 +807,9 @@ static int reftable_log_record_encode(const void *rec, struct string_view s, return start.len - s.len; } -static int reftable_log_record_decode(void *rec, struct strbuf key, +static int reftable_log_record_decode(void *rec, struct reftable_buf key, uint8_t val_type, struct string_view in, - int hash_size, struct strbuf *scratch) + int hash_size, struct reftable_buf *scratch) { struct string_view start = in; struct reftable_log_record *r = rec; @@ -1027,11 +1027,11 @@ static struct reftable_record_vtable reftable_log_record_vtable = { .cmp = &reftable_log_record_cmp_void, }; -static void reftable_index_record_key(const void *r, struct strbuf *dest) +static void reftable_index_record_key(const void *r, struct reftable_buf *dest) { const struct reftable_index_record *rec = r; - strbuf_reset(dest); - strbuf_add(dest, rec->last_key.buf, rec->last_key.len); + reftable_buf_reset(dest); + reftable_buf_add(dest, rec->last_key.buf, rec->last_key.len); } static int reftable_index_record_copy_from(void *rec, const void *src_rec, @@ -1040,8 +1040,8 @@ static int reftable_index_record_copy_from(void *rec, const void *src_rec, struct reftable_index_record *dst = rec; const struct reftable_index_record *src = src_rec; - strbuf_reset(&dst->last_key); - strbuf_add(&dst->last_key, src->last_key.buf, src->last_key.len); + reftable_buf_reset(&dst->last_key); + reftable_buf_add(&dst->last_key, src->last_key.buf, src->last_key.len); dst->offset = src->offset; return 0; @@ -1050,7 +1050,7 @@ static int reftable_index_record_copy_from(void *rec, const void *src_rec, static void reftable_index_record_release(void *rec) { struct reftable_index_record *idx = rec; - strbuf_release(&idx->last_key); + reftable_buf_release(&idx->last_key); } static uint8_t reftable_index_record_val_type(const void *rec UNUSED) @@ -1074,18 +1074,18 @@ static int reftable_index_record_encode(const void *rec, struct string_view out, return start.len - out.len; } -static int reftable_index_record_decode(void *rec, struct strbuf key, +static int reftable_index_record_decode(void *rec, struct reftable_buf key, uint8_t val_type UNUSED, struct string_view in, int hash_size UNUSED, - struct strbuf *scratch UNUSED) + struct reftable_buf *scratch UNUSED) { struct string_view start = in; struct reftable_index_record *r = rec; int n = 0; - strbuf_reset(&r->last_key); - strbuf_add(&r->last_key, key.buf, key.len); + reftable_buf_reset(&r->last_key); + reftable_buf_add(&r->last_key, key.buf, key.len); n = get_var_int(&r->offset, &in); if (n < 0) @@ -1101,14 +1101,14 @@ static int reftable_index_record_equal(const void *a, const void *b, struct reftable_index_record *ia = (struct reftable_index_record *) a; struct reftable_index_record *ib = (struct reftable_index_record *) b; - return ia->offset == ib->offset && !strbuf_cmp(&ia->last_key, &ib->last_key); + return ia->offset == ib->offset && !reftable_buf_cmp(&ia->last_key, &ib->last_key); } static int reftable_index_record_cmp(const void *_a, const void *_b) { const struct reftable_index_record *a = _a; const struct reftable_index_record *b = _b; - return strbuf_cmp(&a->last_key, &b->last_key); + return reftable_buf_cmp(&a->last_key, &b->last_key); } static struct reftable_record_vtable reftable_index_record_vtable = { @@ -1124,7 +1124,7 @@ static struct reftable_record_vtable reftable_index_record_vtable = { .cmp = &reftable_index_record_cmp, }; -void reftable_record_key(struct reftable_record *rec, struct strbuf *dest) +void reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest) { reftable_record_vtable(rec)->key(reftable_record_data(rec), dest); } @@ -1151,9 +1151,9 @@ uint8_t reftable_record_val_type(struct reftable_record *rec) return reftable_record_vtable(rec)->val_type(reftable_record_data(rec)); } -int reftable_record_decode(struct reftable_record *rec, struct strbuf key, +int reftable_record_decode(struct reftable_record *rec, struct reftable_buf key, uint8_t extra, struct string_view src, int hash_size, - struct strbuf *scratch) + struct reftable_buf *scratch) { return reftable_record_vtable(rec)->decode(reftable_record_data(rec), key, extra, src, hash_size, @@ -1294,7 +1294,7 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ) case BLOCK_TYPE_OBJ: return; case BLOCK_TYPE_INDEX: - strbuf_init(&rec->u.idx.last_key, 0); + reftable_buf_init(&rec->u.idx.last_key); return; default: BUG("unhandled record type"); diff --git a/reftable/record.h b/reftable/record.h index 0f53ba54434..271da3bf360 100644 --- a/reftable/record.h +++ b/reftable/record.h @@ -9,6 +9,7 @@ license that can be found in the LICENSE file or at #ifndef RECORD_H #define RECORD_H +#include "basics.h" #include "system.h" #include @@ -38,8 +39,8 @@ int put_var_int(struct string_view *dest, uint64_t val); /* Methods for records. */ struct reftable_record_vtable { - /* encode the key of to a uint8_t strbuf. */ - void (*key)(const void *rec, struct strbuf *dest); + /* encode the key of to a uint8_t reftable_buf. */ + void (*key)(const void *rec, struct reftable_buf *dest); /* The record type of ('r' for ref). */ uint8_t type; @@ -54,9 +55,9 @@ struct reftable_record_vtable { int (*encode)(const void *rec, struct string_view dest, int hash_size); /* decode data from `src` into the record. */ - int (*decode)(void *rec, struct strbuf key, uint8_t extra, + int (*decode)(void *rec, struct reftable_buf key, uint8_t extra, struct string_view src, int hash_size, - struct strbuf *scratch); + struct reftable_buf *scratch); /* deallocate and null the record. */ void (*release)(void *rec); @@ -83,7 +84,7 @@ int reftable_is_block_type(uint8_t typ); /* Encode `key` into `dest`. Sets `is_restart` to indicate a restart. Returns * number of bytes written. */ int reftable_encode_key(int *is_restart, struct string_view dest, - struct strbuf prev_key, struct strbuf key, + struct reftable_buf prev_key, struct reftable_buf key, uint8_t extra); /* Decode a record's key lengths. */ @@ -96,13 +97,13 @@ int reftable_decode_keylen(struct string_view in, * Decode into `last_key` and `extra` from `in`. `last_key` is expected to * contain the decoded key of the preceding record, if any. */ -int reftable_decode_key(struct strbuf *last_key, uint8_t *extra, +int reftable_decode_key(struct reftable_buf *last_key, uint8_t *extra, struct string_view in); /* reftable_index_record are used internally to speed up lookups. */ struct reftable_index_record { uint64_t offset; /* Offset of block */ - struct strbuf last_key; /* Last key of the block. */ + struct reftable_buf last_key; /* Last key of the block. */ }; /* reftable_obj_record stores an object ID => ref mapping. */ @@ -136,15 +137,15 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ); /* see struct record_vtable */ int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b); int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size); -void reftable_record_key(struct reftable_record *rec, struct strbuf *dest); +void reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest); int reftable_record_copy_from(struct reftable_record *rec, struct reftable_record *src, int hash_size); uint8_t reftable_record_val_type(struct reftable_record *rec); int reftable_record_encode(struct reftable_record *rec, struct string_view dest, int hash_size); -int reftable_record_decode(struct reftable_record *rec, struct strbuf key, +int reftable_record_decode(struct reftable_record *rec, struct reftable_buf key, uint8_t extra, struct string_view src, - int hash_size, struct strbuf *scratch); + int hash_size, struct reftable_buf *scratch); int reftable_record_is_deletion(struct reftable_record *rec); static inline uint8_t reftable_record_type(struct reftable_record *rec) diff --git a/reftable/stack.c b/reftable/stack.c index d7bc1187dfb..6ba48ddce5d 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -31,13 +31,13 @@ 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, +static void stack_filename(struct reftable_buf *dest, struct reftable_stack *st, const char *name) { - strbuf_reset(dest); - strbuf_addstr(dest, st->reftable_dir); - strbuf_addstr(dest, "/"); - strbuf_addstr(dest, name); + reftable_buf_reset(dest); + reftable_buf_addstr(dest, st->reftable_dir); + reftable_buf_addstr(dest, "/"); + reftable_buf_addstr(dest, name); } static ssize_t reftable_fd_write(void *arg, const void *data, size_t sz) @@ -56,7 +56,7 @@ static int reftable_fd_flush(void *arg) int reftable_new_stack(struct reftable_stack **dest, const char *dir, const struct reftable_write_options *_opts) { - struct strbuf list_file_name = STRBUF_INIT; + struct reftable_buf list_file_name = REFTABLE_BUF_INIT; struct reftable_write_options opts = { 0 }; struct reftable_stack *p; int err; @@ -74,11 +74,11 @@ int reftable_new_stack(struct reftable_stack **dest, const char *dir, *dest = NULL; - strbuf_reset(&list_file_name); - strbuf_addstr(&list_file_name, dir); - strbuf_addstr(&list_file_name, "/tables.list"); + reftable_buf_reset(&list_file_name); + reftable_buf_addstr(&list_file_name, dir); + reftable_buf_addstr(&list_file_name, "/tables.list"); - p->list_file = strbuf_detach(&list_file_name, NULL); + p->list_file = reftable_buf_detach(&list_file_name); p->list_fd = -1; p->opts = opts; p->reftable_dir = reftable_strdup(dir); @@ -208,10 +208,10 @@ void reftable_stack_destroy(struct reftable_stack *st) if (st->readers) { int i = 0; - struct strbuf filename = STRBUF_INIT; + struct reftable_buf filename = REFTABLE_BUF_INIT; for (i = 0; i < st->readers_len; i++) { const char *name = reader_name(st->readers[i]); - strbuf_reset(&filename); + reftable_buf_reset(&filename); if (names && !has_name(names, name)) { stack_filename(&filename, st, name); } @@ -222,7 +222,7 @@ void reftable_stack_destroy(struct reftable_stack *st) unlink(filename.buf); } } - strbuf_release(&filename); + reftable_buf_release(&filename); st->readers_len = 0; REFTABLE_FREE_AND_NULL(st->readers); } @@ -260,7 +260,7 @@ static int reftable_stack_reload_once(struct reftable_stack *st, size_t reused_len = 0, reused_alloc = 0, names_len; size_t new_readers_len = 0; struct reftable_merged_table *new_merged = NULL; - struct strbuf table_path = STRBUF_INIT; + struct reftable_buf table_path = REFTABLE_BUF_INIT; int err = 0; size_t i; @@ -374,7 +374,7 @@ static int reftable_stack_reload_once(struct reftable_stack *st, reftable_free(new_readers); reftable_free(reused); reftable_free(cur); - strbuf_release(&table_path); + reftable_buf_release(&table_path); return err; } @@ -623,14 +623,14 @@ int reftable_stack_add(struct reftable_stack *st, return 0; } -static void format_name(struct strbuf *dest, uint64_t min, uint64_t max) +static void format_name(struct reftable_buf *dest, uint64_t min, uint64_t max) { char buf[100]; uint32_t rnd = (uint32_t)git_rand(); snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x", min, max, rnd); - strbuf_reset(dest); - strbuf_addstr(dest, buf); + reftable_buf_reset(dest); + reftable_buf_addstr(dest, buf); } struct reftable_addition { @@ -648,7 +648,7 @@ static int reftable_stack_init_addition(struct reftable_addition *add, struct reftable_stack *st, unsigned int flags) { - struct strbuf lock_file_name = STRBUF_INIT; + struct reftable_buf lock_file_name = REFTABLE_BUF_INIT; int err; add->stack = st; @@ -690,13 +690,13 @@ static int reftable_stack_init_addition(struct reftable_addition *add, done: if (err) reftable_addition_close(add); - strbuf_release(&lock_file_name); + reftable_buf_release(&lock_file_name); return err; } static void reftable_addition_close(struct reftable_addition *add) { - struct strbuf nm = STRBUF_INIT; + struct reftable_buf nm = REFTABLE_BUF_INIT; size_t i; for (i = 0; i < add->new_tables_len; i++) { @@ -711,7 +711,7 @@ static void reftable_addition_close(struct reftable_addition *add) add->new_tables_cap = 0; rollback_lock_file(&add->tables_list_lock); - strbuf_release(&nm); + reftable_buf_release(&nm); } void reftable_addition_destroy(struct reftable_addition *add) @@ -725,7 +725,7 @@ void reftable_addition_destroy(struct reftable_addition *add) int reftable_addition_commit(struct reftable_addition *add) { - struct strbuf table_list = STRBUF_INIT; + struct reftable_buf table_list = REFTABLE_BUF_INIT; int lock_file_fd = get_lock_file_fd(&add->tables_list_lock); int err = 0; size_t i; @@ -734,16 +734,16 @@ int reftable_addition_commit(struct reftable_addition *add) goto done; for (i = 0; i < add->stack->merged->readers_len; i++) { - strbuf_addstr(&table_list, add->stack->readers[i]->name); - strbuf_addstr(&table_list, "\n"); + reftable_buf_addstr(&table_list, add->stack->readers[i]->name); + reftable_buf_addstr(&table_list, "\n"); } for (i = 0; i < add->new_tables_len; i++) { - strbuf_addstr(&table_list, add->new_tables[i]); - strbuf_addstr(&table_list, "\n"); + reftable_buf_addstr(&table_list, add->new_tables[i]); + reftable_buf_addstr(&table_list, "\n"); } err = write_in_full(lock_file_fd, table_list.buf, table_list.len); - strbuf_release(&table_list); + reftable_buf_release(&table_list); if (err < 0) { err = REFTABLE_IO_ERROR; goto done; @@ -837,19 +837,19 @@ int reftable_addition_add(struct reftable_addition *add, void *arg), void *arg) { - struct strbuf temp_tab_file_name = STRBUF_INIT; - struct strbuf tab_file_name = STRBUF_INIT; - struct strbuf next_name = STRBUF_INIT; + struct reftable_buf temp_tab_file_name = REFTABLE_BUF_INIT; + struct reftable_buf tab_file_name = REFTABLE_BUF_INIT; + struct reftable_buf next_name = REFTABLE_BUF_INIT; struct reftable_writer *wr = NULL; struct tempfile *tab_file = NULL; int err = 0; int tab_fd; - strbuf_reset(&next_name); + reftable_buf_reset(&next_name); format_name(&next_name, add->next_update_index, add->next_update_index); stack_filename(&temp_tab_file_name, add->stack, next_name.buf); - strbuf_addstr(&temp_tab_file_name, ".temp.XXXXXX"); + reftable_buf_addstr(&temp_tab_file_name, ".temp.XXXXXX"); tab_file = mks_tempfile(temp_tab_file_name.buf); if (!tab_file) { @@ -894,7 +894,7 @@ int reftable_addition_add(struct reftable_addition *add, } format_name(&next_name, wr->min_update_index, wr->max_update_index); - strbuf_addstr(&next_name, ".ref"); + reftable_buf_addstr(&next_name, ".ref"); stack_filename(&tab_file_name, add->stack, next_name.buf); /* @@ -913,13 +913,13 @@ int reftable_addition_add(struct reftable_addition *add, err = REFTABLE_OUT_OF_MEMORY_ERROR; goto done; } - add->new_tables[add->new_tables_len++] = strbuf_detach(&next_name, NULL); + add->new_tables[add->new_tables_len++] = reftable_buf_detach(&next_name); done: delete_tempfile(&tab_file); - strbuf_release(&temp_tab_file_name); - strbuf_release(&tab_file_name); - strbuf_release(&next_name); + reftable_buf_release(&temp_tab_file_name); + reftable_buf_release(&tab_file_name); + reftable_buf_release(&next_name); reftable_writer_free(wr); return err; } @@ -938,8 +938,8 @@ static int stack_compact_locked(struct reftable_stack *st, struct reftable_log_expiry_config *config, struct tempfile **tab_file_out) { - struct strbuf next_name = STRBUF_INIT; - struct strbuf tab_file_path = STRBUF_INIT; + struct reftable_buf next_name = REFTABLE_BUF_INIT; + struct reftable_buf tab_file_path = REFTABLE_BUF_INIT; struct reftable_writer *wr = NULL; struct tempfile *tab_file; int tab_fd, err = 0; @@ -948,7 +948,7 @@ static int stack_compact_locked(struct reftable_stack *st, reftable_reader_min_update_index(st->readers[first]), reftable_reader_max_update_index(st->readers[last])); stack_filename(&tab_file_path, st, next_name.buf); - strbuf_addstr(&tab_file_path, ".temp.XXXXXX"); + reftable_buf_addstr(&tab_file_path, ".temp.XXXXXX"); tab_file = mks_tempfile(tab_file_path.buf); if (!tab_file) { @@ -986,8 +986,8 @@ static int stack_compact_locked(struct reftable_stack *st, done: delete_tempfile(&tab_file); reftable_writer_free(wr); - strbuf_release(&next_name); - strbuf_release(&tab_file_path); + reftable_buf_release(&next_name); + reftable_buf_release(&tab_file_path); return err; } @@ -1111,10 +1111,10 @@ static int stack_compact_range(struct reftable_stack *st, struct reftable_log_expiry_config *expiry, unsigned int flags) { - struct strbuf tables_list_buf = STRBUF_INIT; - struct strbuf new_table_name = STRBUF_INIT; - struct strbuf new_table_path = STRBUF_INIT; - struct strbuf table_name = STRBUF_INIT; + struct reftable_buf tables_list_buf = REFTABLE_BUF_INIT; + struct reftable_buf new_table_name = REFTABLE_BUF_INIT; + struct reftable_buf new_table_path = REFTABLE_BUF_INIT; + struct reftable_buf table_name = REFTABLE_BUF_INIT; struct lock_file tables_list_lock = LOCK_INIT; struct lock_file *table_locks = NULL; struct tempfile *new_table = NULL; @@ -1372,7 +1372,7 @@ static int stack_compact_range(struct reftable_stack *st, if (!is_empty_table) { format_name(&new_table_name, st->readers[first]->min_update_index, st->readers[last]->max_update_index); - strbuf_addstr(&new_table_name, ".ref"); + reftable_buf_addstr(&new_table_name, ".ref"); stack_filename(&new_table_path, st, new_table_name.buf); err = rename_tempfile(&new_table, new_table_path.buf); @@ -1388,16 +1388,16 @@ static int stack_compact_range(struct reftable_stack *st, * simply skip writing it. */ for (i = 0; i < first_to_replace; i++) { - strbuf_addstr(&tables_list_buf, names[i]); - strbuf_addstr(&tables_list_buf, "\n"); + reftable_buf_addstr(&tables_list_buf, names[i]); + reftable_buf_addstr(&tables_list_buf, "\n"); } if (!is_empty_table) { - strbuf_addstr(&tables_list_buf, new_table_name.buf); - strbuf_addstr(&tables_list_buf, "\n"); + reftable_buf_addstr(&tables_list_buf, new_table_name.buf); + reftable_buf_addstr(&tables_list_buf, "\n"); } for (i = last_to_replace + 1; names[i]; i++) { - strbuf_addstr(&tables_list_buf, names[i]); - strbuf_addstr(&tables_list_buf, "\n"); + reftable_buf_addstr(&tables_list_buf, names[i]); + reftable_buf_addstr(&tables_list_buf, "\n"); } err = write_in_full(get_lock_file_fd(&tables_list_lock), @@ -1449,10 +1449,10 @@ static int stack_compact_range(struct reftable_stack *st, reftable_free(table_locks); delete_tempfile(&new_table); - strbuf_release(&new_table_name); - strbuf_release(&new_table_path); - strbuf_release(&tables_list_buf); - strbuf_release(&table_name); + reftable_buf_release(&new_table_name); + reftable_buf_release(&new_table_path); + reftable_buf_release(&tables_list_buf); + reftable_buf_release(&table_name); free_names(names); if (err == REFTABLE_LOCK_ERROR) @@ -1666,7 +1666,7 @@ static void remove_maybe_stale_table(struct reftable_stack *st, uint64_t max, uint64_t update_idx = 0; struct reftable_block_source src = { NULL }; struct reftable_reader *rd = NULL; - struct strbuf table_path = STRBUF_INIT; + struct reftable_buf table_path = REFTABLE_BUF_INIT; stack_filename(&table_path, st, name); err = reftable_block_source_from_file(&src, table_path.buf); @@ -1684,7 +1684,7 @@ static void remove_maybe_stale_table(struct reftable_stack *st, uint64_t max, unlink(table_path.buf); } done: - strbuf_release(&table_path); + reftable_buf_release(&table_path); } static int reftable_stack_clean_locked(struct reftable_stack *st) diff --git a/reftable/system.h b/reftable/system.h index d0cabd5d171..5ec85833434 100644 --- a/reftable/system.h +++ b/reftable/system.h @@ -13,7 +13,6 @@ license that can be found in the LICENSE file or at #include "git-compat-util.h" #include "lockfile.h" -#include "strbuf.h" #include "tempfile.h" #include "hash.h" /* hash ID, sizes.*/ #include "dir.h" /* remove_dir_recursively, for tests.*/ diff --git a/reftable/writer.c b/reftable/writer.c index 031d8149a9c..da6941a78ac 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -115,7 +115,7 @@ static int writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ) if (w->next == 0) block_start = header_size(writer_version(w)); - strbuf_reset(&w->last_key); + reftable_buf_reset(&w->last_key); ret = block_writer_init(&w->block_writer_data, typ, w->block, w->opts.block_size, block_start, hash_size(w->opts.hash_id)); @@ -146,8 +146,8 @@ int reftable_writer_new(struct reftable_writer **out, if (opts.block_size >= (1 << 24)) BUG("configured block size exceeds 16MB"); - strbuf_init(&wp->block_writer_data.last_key, 0); - strbuf_init(&wp->last_key, 0); + reftable_buf_init(&wp->block_writer_data.last_key); + reftable_buf_init(&wp->last_key); REFTABLE_CALLOC_ARRAY(wp->block, opts.block_size); if (!wp->block) { reftable_free(wp); @@ -179,7 +179,7 @@ static void writer_release(struct reftable_writer *w) block_writer_release(&w->block_writer_data); w->block_writer = NULL; writer_clear_index(w); - strbuf_release(&w->last_key); + reftable_buf_release(&w->last_key); } } @@ -190,7 +190,7 @@ void reftable_writer_free(struct reftable_writer *w) } struct obj_index_tree_node { - struct strbuf hash; + struct reftable_buf hash; uint64_t *offsets; size_t offset_len; size_t offset_cap; @@ -198,16 +198,16 @@ struct obj_index_tree_node { #define OBJ_INDEX_TREE_NODE_INIT \ { \ - .hash = STRBUF_INIT \ + .hash = REFTABLE_BUF_INIT \ } static int obj_index_tree_node_compare(const void *a, const void *b) { - return strbuf_cmp(&((const struct obj_index_tree_node *)a)->hash, + return reftable_buf_cmp(&((const struct obj_index_tree_node *)a)->hash, &((const struct obj_index_tree_node *)b)->hash); } -static int writer_index_hash(struct reftable_writer *w, struct strbuf *hash) +static int writer_index_hash(struct reftable_writer *w, struct reftable_buf *hash) { uint64_t off = w->next; struct obj_index_tree_node want = { .hash = *hash }; @@ -224,8 +224,8 @@ static int writer_index_hash(struct reftable_writer *w, struct strbuf *hash) *key = empty; - strbuf_reset(&key->hash); - strbuf_add(&key->hash, hash->buf, hash->len); + reftable_buf_reset(&key->hash); + reftable_buf_add(&key->hash, hash->buf, hash->len); tree_insert(&w->obj_index_tree, key, &obj_index_tree_node_compare); } else { @@ -246,17 +246,17 @@ static int writer_index_hash(struct reftable_writer *w, struct strbuf *hash) static int writer_add_record(struct reftable_writer *w, struct reftable_record *rec) { - struct strbuf key = STRBUF_INIT; + struct reftable_buf key = REFTABLE_BUF_INIT; int err; reftable_record_key(rec, &key); - if (strbuf_cmp(&w->last_key, &key) >= 0) { + if (reftable_buf_cmp(&w->last_key, &key) >= 0) { err = REFTABLE_API_ERROR; goto done; } - strbuf_reset(&w->last_key); - strbuf_add(&w->last_key, key.buf, key.len); + reftable_buf_reset(&w->last_key); + reftable_buf_add(&w->last_key, key.buf, key.len); if (!w->block_writer) { err = writer_reinit_block_writer(w, reftable_record_type(rec)); if (err < 0) @@ -303,7 +303,7 @@ static int writer_add_record(struct reftable_writer *w, } done: - strbuf_release(&key); + reftable_buf_release(&key); return err; } @@ -316,7 +316,7 @@ int reftable_writer_add_ref(struct reftable_writer *w, .ref = *ref }, }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; int err; if (!ref->refname || @@ -331,7 +331,7 @@ int reftable_writer_add_ref(struct reftable_writer *w, goto out; if (!w->opts.skip_index_objects && reftable_ref_record_val1(ref)) { - strbuf_add(&buf, (char *)reftable_ref_record_val1(ref), + reftable_buf_add(&buf, (char *)reftable_ref_record_val1(ref), hash_size(w->opts.hash_id)); err = writer_index_hash(w, &buf); @@ -340,8 +340,8 @@ int reftable_writer_add_ref(struct reftable_writer *w, } if (!w->opts.skip_index_objects && reftable_ref_record_val2(ref)) { - strbuf_reset(&buf); - strbuf_add(&buf, reftable_ref_record_val2(ref), + reftable_buf_reset(&buf); + reftable_buf_add(&buf, reftable_ref_record_val2(ref), hash_size(w->opts.hash_id)); err = writer_index_hash(w, &buf); @@ -352,7 +352,7 @@ int reftable_writer_add_ref(struct reftable_writer *w, err = 0; out: - strbuf_release(&buf); + reftable_buf_release(&buf); return err; } @@ -393,7 +393,7 @@ int reftable_writer_add_log(struct reftable_writer *w, struct reftable_log_record *log) { char *input_log_message = NULL; - struct strbuf cleaned_message = STRBUF_INIT; + struct reftable_buf cleaned_message = REFTABLE_BUF_INIT; int err = 0; if (log->value_type == REFTABLE_LOG_DELETION) @@ -404,24 +404,24 @@ int reftable_writer_add_log(struct reftable_writer *w, input_log_message = log->value.update.message; if (!w->opts.exact_log_message && log->value.update.message) { - strbuf_addstr(&cleaned_message, log->value.update.message); + reftable_buf_addstr(&cleaned_message, log->value.update.message); while (cleaned_message.len && cleaned_message.buf[cleaned_message.len - 1] == '\n') - strbuf_setlen(&cleaned_message, + reftable_buf_setlen(&cleaned_message, cleaned_message.len - 1); if (strchr(cleaned_message.buf, '\n')) { /* multiple lines not allowed. */ err = REFTABLE_API_ERROR; goto done; } - strbuf_addstr(&cleaned_message, "\n"); + reftable_buf_addstr(&cleaned_message, "\n"); log->value.update.message = cleaned_message.buf; } err = reftable_writer_add_log_verbatim(w, log); log->value.update.message = input_log_message; done: - strbuf_release(&cleaned_message); + reftable_buf_release(&cleaned_message); return err; } @@ -504,7 +504,7 @@ static int writer_finish_section(struct reftable_writer *w) return err; for (i = 0; i < idx_len; i++) - strbuf_release(&idx[i].last_key); + reftable_buf_release(&idx[i].last_key); reftable_free(idx); } @@ -521,13 +521,13 @@ static int writer_finish_section(struct reftable_writer *w) bstats->max_index_level = max_level; /* Reinit lastKey, as the next section can start with any key. */ - strbuf_reset(&w->last_key); + reftable_buf_reset(&w->last_key); return 0; } struct common_prefix_arg { - struct strbuf *last; + struct reftable_buf *last; int max; }; @@ -594,7 +594,7 @@ static void object_record_free(void *void_arg UNUSED, void *key) struct obj_index_tree_node *entry = key; REFTABLE_FREE_AND_NULL(entry->offsets); - strbuf_release(&entry->hash); + reftable_buf_release(&entry->hash); reftable_free(entry); } @@ -708,7 +708,7 @@ int reftable_writer_close(struct reftable_writer *w) static void writer_clear_index(struct reftable_writer *w) { for (size_t i = 0; w->index && i < w->index_len; i++) - strbuf_release(&w->index[i].last_key); + reftable_buf_release(&w->index[i].last_key); REFTABLE_FREE_AND_NULL(w->index); w->index_len = 0; w->index_cap = 0; @@ -717,7 +717,7 @@ static void writer_clear_index(struct reftable_writer *w) static int writer_flush_nonempty_block(struct reftable_writer *w) { struct reftable_index_record index_record = { - .last_key = STRBUF_INIT, + .last_key = REFTABLE_BUF_INIT, }; uint8_t typ = block_writer_type(w->block_writer); struct reftable_block_stats *bstats; @@ -777,8 +777,8 @@ static int writer_flush_nonempty_block(struct reftable_writer *w) return REFTABLE_OUT_OF_MEMORY_ERROR; index_record.offset = w->next; - strbuf_reset(&index_record.last_key); - strbuf_add(&index_record.last_key, w->block_writer->last_key.buf, + reftable_buf_reset(&index_record.last_key); + reftable_buf_add(&index_record.last_key, w->block_writer->last_key.buf, w->block_writer->last_key.len); w->index[w->index_len] = index_record; w->index_len++; diff --git a/reftable/writer.h b/reftable/writer.h index 8d0df9cc528..e8a6fbb7854 100644 --- a/reftable/writer.h +++ b/reftable/writer.h @@ -19,7 +19,7 @@ struct reftable_writer { int (*flush)(void *); void *write_arg; int pending_padding; - struct strbuf last_key; + struct reftable_buf last_key; /* offset of next block to write. */ uint64_t next; diff --git a/t/unit-tests/lib-reftable.c b/t/unit-tests/lib-reftable.c index 54c26c43e77..2ddf480588d 100644 --- a/t/unit-tests/lib-reftable.c +++ b/t/unit-tests/lib-reftable.c @@ -19,7 +19,7 @@ static int strbuf_writer_flush(void *arg UNUSED) return 0; } -struct reftable_writer *t_reftable_strbuf_writer(struct strbuf *buf, +struct reftable_writer *t_reftable_strbuf_writer(struct reftable_buf *buf, struct reftable_write_options *opts) { struct reftable_writer *writer; @@ -29,7 +29,7 @@ struct reftable_writer *t_reftable_strbuf_writer(struct strbuf *buf, return writer; } -void t_reftable_write_to_buf(struct strbuf *buf, +void t_reftable_write_to_buf(struct reftable_buf *buf, struct reftable_ref_record *refs, size_t nrefs, struct reftable_log_record *logs, diff --git a/t/unit-tests/lib-reftable.h b/t/unit-tests/lib-reftable.h index d1154190847..d4950fed3da 100644 --- a/t/unit-tests/lib-reftable.h +++ b/t/unit-tests/lib-reftable.h @@ -2,15 +2,16 @@ #define LIB_REFTABLE_H #include "git-compat-util.h" -#include "strbuf.h" #include "reftable/reftable-writer.h" +struct reftable_buf; + void t_reftable_set_hash(uint8_t *p, int i, uint32_t id); -struct reftable_writer *t_reftable_strbuf_writer(struct strbuf *buf, +struct reftable_writer *t_reftable_strbuf_writer(struct reftable_buf *buf, struct reftable_write_options *opts); -void t_reftable_write_to_buf(struct strbuf *buf, +void t_reftable_write_to_buf(struct reftable_buf *buf, struct reftable_ref_record *refs, size_t nrecords, struct reftable_log_record *logs, diff --git a/t/unit-tests/t-reftable-basics.c b/t/unit-tests/t-reftable-basics.c index 1fa77b6faff..a814e819756 100644 --- a/t/unit-tests/t-reftable-basics.c +++ b/t/unit-tests/t-reftable-basics.c @@ -99,8 +99,8 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED) } if_test ("common_prefix_size works") { - struct strbuf a = STRBUF_INIT; - struct strbuf b = STRBUF_INIT; + struct reftable_buf a = REFTABLE_BUF_INIT; + struct reftable_buf b = REFTABLE_BUF_INIT; struct { const char *a, *b; int want; @@ -113,14 +113,14 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED) }; for (size_t i = 0; i < ARRAY_SIZE(cases); i++) { - strbuf_addstr(&a, cases[i].a); - strbuf_addstr(&b, cases[i].b); + reftable_buf_addstr(&a, cases[i].a); + reftable_buf_addstr(&b, cases[i].b); check_int(common_prefix_size(&a, &b), ==, cases[i].want); - strbuf_reset(&a); - strbuf_reset(&b); + reftable_buf_reset(&a); + reftable_buf_reset(&b); } - strbuf_release(&a); - strbuf_release(&b); + reftable_buf_release(&a); + reftable_buf_release(&b); } if_test ("put_be24 and get_be24 work") { diff --git a/t/unit-tests/t-reftable-block.c b/t/unit-tests/t-reftable-block.c index 8077bbc5e7a..56514b43630 100644 --- a/t/unit-tests/t-reftable-block.c +++ b/t/unit-tests/t-reftable-block.c @@ -20,7 +20,7 @@ static void t_ref_block_read_write(void) const size_t block_size = 1024; struct reftable_block block = { 0 }; struct block_writer bw = { - .last_key = STRBUF_INIT, + .last_key = REFTABLE_BUF_INIT, }; struct reftable_record rec = { .type = BLOCK_TYPE_REF, @@ -29,7 +29,7 @@ static void t_ref_block_read_write(void) int ret; struct block_reader br = { 0 }; struct block_iter it = BLOCK_ITER_INIT; - struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT; + struct reftable_buf want = REFTABLE_BUF_INIT, buf = REFTABLE_BUF_INIT; REFTABLE_CALLOC_ARRAY(block.data, block_size); check(block.data != NULL); @@ -100,8 +100,8 @@ static void t_ref_block_read_write(void) block_iter_close(&it); reftable_record_release(&rec); reftable_block_done(&br.block); - strbuf_release(&want); - strbuf_release(&buf); + reftable_buf_release(&want); + reftable_buf_release(&buf); for (i = 0; i < N; i++) reftable_record_release(&recs[i]); } @@ -114,7 +114,7 @@ static void t_log_block_read_write(void) const size_t block_size = 2048; struct reftable_block block = { 0 }; struct block_writer bw = { - .last_key = STRBUF_INIT, + .last_key = REFTABLE_BUF_INIT, }; struct reftable_record rec = { .type = BLOCK_TYPE_LOG, @@ -123,7 +123,7 @@ static void t_log_block_read_write(void) int ret; struct block_reader br = { 0 }; struct block_iter it = BLOCK_ITER_INIT; - struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT; + struct reftable_buf want = REFTABLE_BUF_INIT, buf = REFTABLE_BUF_INIT; REFTABLE_CALLOC_ARRAY(block.data, block_size); check(block.data != NULL); @@ -166,8 +166,8 @@ static void t_log_block_read_write(void) for (i = 0; i < N; i++) { block_iter_reset(&it); - strbuf_reset(&want); - strbuf_addstr(&want, recs[i].u.log.refname); + reftable_buf_reset(&want); + reftable_buf_addstr(&want, recs[i].u.log.refname); ret = block_iter_seek_key(&it, &br, &want); check_int(ret, ==, 0); @@ -190,8 +190,8 @@ static void t_log_block_read_write(void) block_iter_close(&it); reftable_record_release(&rec); reftable_block_done(&br.block); - strbuf_release(&want); - strbuf_release(&buf); + reftable_buf_release(&want); + reftable_buf_release(&buf); for (i = 0; i < N; i++) reftable_record_release(&recs[i]); } @@ -204,7 +204,7 @@ static void t_obj_block_read_write(void) const size_t block_size = 1024; struct reftable_block block = { 0 }; struct block_writer bw = { - .last_key = STRBUF_INIT, + .last_key = REFTABLE_BUF_INIT, }; struct reftable_record rec = { .type = BLOCK_TYPE_OBJ, @@ -213,7 +213,7 @@ static void t_obj_block_read_write(void) int ret; struct block_reader br = { 0 }; struct block_iter it = BLOCK_ITER_INIT; - struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT; + struct reftable_buf want = REFTABLE_BUF_INIT, buf = REFTABLE_BUF_INIT; REFTABLE_CALLOC_ARRAY(block.data, block_size); check(block.data != NULL); @@ -273,8 +273,8 @@ static void t_obj_block_read_write(void) block_iter_close(&it); reftable_record_release(&rec); reftable_block_done(&br.block); - strbuf_release(&want); - strbuf_release(&buf); + reftable_buf_release(&want); + reftable_buf_release(&buf); for (i = 0; i < N; i++) reftable_record_release(&recs[i]); } @@ -287,17 +287,17 @@ static void t_index_block_read_write(void) const size_t block_size = 1024; struct reftable_block block = { 0 }; struct block_writer bw = { - .last_key = STRBUF_INIT, + .last_key = REFTABLE_BUF_INIT, }; struct reftable_record rec = { .type = BLOCK_TYPE_INDEX, - .u.idx.last_key = STRBUF_INIT, + .u.idx.last_key = REFTABLE_BUF_INIT, }; size_t i = 0; int ret; struct block_reader br = { 0 }; struct block_iter it = BLOCK_ITER_INIT; - struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT; + struct reftable_buf want = REFTABLE_BUF_INIT, buf = REFTABLE_BUF_INIT; REFTABLE_CALLOC_ARRAY(block.data, block_size); check(block.data != NULL); @@ -312,9 +312,9 @@ static void t_index_block_read_write(void) snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i); - strbuf_init(&recs[i].u.idx.last_key, 9); + reftable_buf_init(&recs[i].u.idx.last_key); recs[i].type = BLOCK_TYPE_INDEX; - strbuf_addstr(&recs[i].u.idx.last_key, buf); + reftable_buf_addstr(&recs[i].u.idx.last_key, buf); recs[i].u.idx.offset = i; ret = block_writer_add(&bw, &recs[i]); @@ -365,8 +365,8 @@ static void t_index_block_read_write(void) block_iter_close(&it); reftable_record_release(&rec); reftable_block_done(&br.block); - strbuf_release(&want); - strbuf_release(&buf); + reftable_buf_release(&want); + reftable_buf_release(&buf); for (i = 0; i < N; i++) reftable_record_release(&recs[i]); } diff --git a/t/unit-tests/t-reftable-merged.c b/t/unit-tests/t-reftable-merged.c index 3c84363e980..9b0162a4b32 100644 --- a/t/unit-tests/t-reftable-merged.c +++ b/t/unit-tests/t-reftable-merged.c @@ -20,7 +20,7 @@ static struct reftable_merged_table * merged_table_from_records(struct reftable_ref_record **refs, struct reftable_block_source **source, struct reftable_reader ***readers, const size_t *sizes, - struct strbuf *buf, const size_t n) + struct reftable_buf *buf, const size_t n) { struct reftable_merged_table *mt = NULL; struct reftable_write_options opts = { @@ -75,7 +75,7 @@ static void t_merged_single_record(void) struct reftable_ref_record *refs[] = { r1, r2, r3 }; size_t sizes[] = { ARRAY_SIZE(r1), ARRAY_SIZE(r2), ARRAY_SIZE(r3) }; - struct strbuf bufs[3] = { STRBUF_INIT, STRBUF_INIT, STRBUF_INIT }; + struct reftable_buf bufs[3] = { REFTABLE_BUF_INIT, REFTABLE_BUF_INIT, REFTABLE_BUF_INIT }; struct reftable_block_source *bs = NULL; struct reftable_reader **readers = NULL; struct reftable_merged_table *mt = @@ -97,7 +97,7 @@ static void t_merged_single_record(void) readers_destroy(readers, 3); reftable_merged_table_free(mt); for (size_t i = 0; i < ARRAY_SIZE(bufs); i++) - strbuf_release(&bufs[i]); + reftable_buf_release(&bufs[i]); reftable_free(bs); } @@ -152,7 +152,7 @@ static void t_merged_refs(void) struct reftable_ref_record *refs[] = { r1, r2, r3 }; size_t sizes[3] = { ARRAY_SIZE(r1), ARRAY_SIZE(r2), ARRAY_SIZE(r3) }; - struct strbuf bufs[3] = { STRBUF_INIT, STRBUF_INIT, STRBUF_INIT }; + struct reftable_buf bufs[3] = { REFTABLE_BUF_INIT, REFTABLE_BUF_INIT, REFTABLE_BUF_INIT }; struct reftable_block_source *bs = NULL; struct reftable_reader **readers = NULL; struct reftable_merged_table *mt = @@ -192,7 +192,7 @@ static void t_merged_refs(void) reftable_free(out); for (i = 0; i < 3; i++) - strbuf_release(&bufs[i]); + reftable_buf_release(&bufs[i]); readers_destroy(readers, 3); reftable_merged_table_free(mt); reftable_free(bs); @@ -234,8 +234,8 @@ static void t_merged_seek_multiple_times(void) size_t sizes[] = { ARRAY_SIZE(r1), ARRAY_SIZE(r2), }; - struct strbuf bufs[] = { - STRBUF_INIT, STRBUF_INIT, + struct reftable_buf bufs[] = { + REFTABLE_BUF_INIT, REFTABLE_BUF_INIT, }; struct reftable_block_source *sources = NULL; struct reftable_reader **readers = NULL; @@ -265,7 +265,7 @@ static void t_merged_seek_multiple_times(void) } for (size_t i = 0; i < ARRAY_SIZE(bufs); i++) - strbuf_release(&bufs[i]); + reftable_buf_release(&bufs[i]); readers_destroy(readers, ARRAY_SIZE(refs)); reftable_ref_record_release(&rec); reftable_iterator_destroy(&it); @@ -277,7 +277,7 @@ static struct reftable_merged_table * merged_table_from_log_records(struct reftable_log_record **logs, struct reftable_block_source **source, struct reftable_reader ***readers, const size_t *sizes, - struct strbuf *buf, const size_t n) + struct reftable_buf *buf, const size_t n) { struct reftable_merged_table *mt = NULL; struct reftable_write_options opts = { @@ -361,7 +361,7 @@ static void t_merged_logs(void) struct reftable_log_record *logs[] = { r1, r2, r3 }; size_t sizes[3] = { ARRAY_SIZE(r1), ARRAY_SIZE(r2), ARRAY_SIZE(r3) }; - struct strbuf bufs[3] = { STRBUF_INIT, STRBUF_INIT, STRBUF_INIT }; + struct reftable_buf bufs[3] = { REFTABLE_BUF_INIT, REFTABLE_BUF_INIT, REFTABLE_BUF_INIT }; struct reftable_block_source *bs = NULL; struct reftable_reader **readers = NULL; struct reftable_merged_table *mt = merged_table_from_log_records( @@ -412,7 +412,7 @@ static void t_merged_logs(void) reftable_free(out); for (i = 0; i < 3; i++) - strbuf_release(&bufs[i]); + reftable_buf_release(&bufs[i]); readers_destroy(readers, 3); reftable_merged_table_free(mt); reftable_free(bs); @@ -421,7 +421,7 @@ static void t_merged_logs(void) static void t_default_write_opts(void) { struct reftable_write_options opts = { 0 }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); struct reftable_ref_record rec = { .refname = (char *) "master", @@ -457,7 +457,7 @@ static void t_default_write_opts(void) reftable_reader_decref(rd); reftable_merged_table_free(merged); - strbuf_release(&buf); + reftable_buf_release(&buf); } diff --git a/t/unit-tests/t-reftable-reader.c b/t/unit-tests/t-reftable-reader.c index eea86966c0d..8a18d7f9be4 100644 --- a/t/unit-tests/t-reftable-reader.c +++ b/t/unit-tests/t-reftable-reader.c @@ -16,7 +16,7 @@ static int t_reader_seek_once(void) struct reftable_ref_record ref = { 0 }; struct reftable_iterator it = { 0 }; struct reftable_reader *reader; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; int ret; t_reftable_write_to_buf(&buf, records, ARRAY_SIZE(records), NULL, 0, NULL); @@ -40,7 +40,7 @@ static int t_reader_seek_once(void) reftable_ref_record_release(&ref); reftable_iterator_destroy(&it); reftable_reader_decref(reader); - strbuf_release(&buf); + reftable_buf_release(&buf); return 0; } @@ -57,7 +57,7 @@ static int t_reader_reseek(void) struct reftable_ref_record ref = { 0 }; struct reftable_iterator it = { 0 }; struct reftable_reader *reader; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; int ret; t_reftable_write_to_buf(&buf, records, ARRAY_SIZE(records), NULL, 0, NULL); @@ -84,7 +84,7 @@ static int t_reader_reseek(void) reftable_ref_record_release(&ref); reftable_iterator_destroy(&it); reftable_reader_decref(reader); - strbuf_release(&buf); + reftable_buf_release(&buf); return 0; } diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index 5f59b0ad6ad..c56a33f1a1e 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -18,12 +18,12 @@ static const int update_index = 5; static void t_buffer(void) { - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_block_source source = { 0 }; struct reftable_block out = { 0 }; int n; uint8_t in[] = "hello"; - strbuf_add(&buf, in, sizeof(in)); + reftable_buf_add(&buf, in, sizeof(in)); block_source_from_strbuf(&source, &buf); check_int(block_source_size(&source), ==, 6); n = block_source_read_block(&source, &out, 0, sizeof(in)); @@ -37,10 +37,10 @@ static void t_buffer(void) reftable_block_done(&out); block_source_close(&source); - strbuf_release(&buf); + reftable_buf_release(&buf); } -static void write_table(char ***names, struct strbuf *buf, int N, +static void write_table(char ***names, struct reftable_buf *buf, int N, int block_size, uint32_t hash_id) { struct reftable_write_options opts = { @@ -82,7 +82,7 @@ static void write_table(char ***names, struct strbuf *buf, int N, static void t_log_buffer_size(void) { - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_write_options opts = { .block_size = 4096, }; @@ -114,12 +114,12 @@ static void t_log_buffer_size(void) err = reftable_writer_close(w); check(!err); reftable_writer_free(w); - strbuf_release(&buf); + reftable_buf_release(&buf); } static void t_log_overflow(void) { - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; char msg[256] = { 0 }; struct reftable_write_options opts = { .block_size = ARRAY_SIZE(msg), @@ -148,7 +148,7 @@ static void t_log_overflow(void) err = reftable_writer_add_log(w, &log); check_int(err, ==, REFTABLE_ENTRY_TOO_BIG_ERROR); reftable_writer_free(w); - strbuf_release(&buf); + reftable_buf_release(&buf); } static void t_log_write_read(void) @@ -161,7 +161,7 @@ static void t_log_write_read(void) struct reftable_iterator it = { 0 }; struct reftable_reader *reader; struct reftable_block_source source = { 0 }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); const struct reftable_stats *stats = NULL; int N = 2, err, i, n; @@ -247,7 +247,7 @@ static void t_log_write_read(void) reftable_iterator_destroy(&it); /* cleanup. */ - strbuf_release(&buf); + reftable_buf_release(&buf); free_names(names); reftable_reader_decref(reader); } @@ -260,7 +260,7 @@ static void t_log_zlib_corruption(void) struct reftable_iterator it = { 0 }; struct reftable_reader *reader; struct reftable_block_source source = { 0 }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); const struct reftable_stats *stats = NULL; char message[100] = { 0 }; @@ -312,13 +312,13 @@ static void t_log_zlib_corruption(void) /* cleanup. */ reftable_reader_decref(reader); - strbuf_release(&buf); + reftable_buf_release(&buf); } static void t_table_read_write_sequential(void) { char **names; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; int N = 50; struct reftable_iterator it = { 0 }; struct reftable_block_source source = { 0 }; @@ -352,25 +352,25 @@ static void t_table_read_write_sequential(void) reftable_iterator_destroy(&it); reftable_reader_decref(reader); - strbuf_release(&buf); + reftable_buf_release(&buf); free_names(names); } static void t_table_write_small_table(void) { char **names; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; int N = 1; write_table(&names, &buf, N, 4096, GIT_SHA1_FORMAT_ID); check_int(buf.len, <, 200); - strbuf_release(&buf); + reftable_buf_release(&buf); free_names(names); } static void t_table_read_api(void) { char **names; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; int N = 50; struct reftable_reader *reader; struct reftable_block_source source = { 0 }; @@ -393,17 +393,17 @@ static void t_table_read_api(void) err = reftable_iterator_next_log(&it, &log); check_int(err, ==, REFTABLE_API_ERROR); - strbuf_release(&buf); + reftable_buf_release(&buf); free_names(names); reftable_iterator_destroy(&it); reftable_reader_decref(reader); - strbuf_release(&buf); + reftable_buf_release(&buf); } static void t_table_read_write_seek(int index, int hash_id) { char **names; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; int N = 50; struct reftable_reader *reader; struct reftable_block_source source = { 0 }; @@ -411,7 +411,7 @@ static void t_table_read_write_seek(int index, int hash_id) int i = 0; struct reftable_iterator it = { 0 }; - struct strbuf pastLast = STRBUF_INIT; + struct reftable_buf pastLast = REFTABLE_BUF_INIT; struct reftable_ref_record ref = { 0 }; write_table(&names, &buf, N, 256, hash_id); @@ -443,8 +443,8 @@ static void t_table_read_write_seek(int index, int hash_id) reftable_iterator_destroy(&it); } - strbuf_addstr(&pastLast, names[N - 1]); - strbuf_addstr(&pastLast, "/"); + reftable_buf_addstr(&pastLast, names[N - 1]); + reftable_buf_addstr(&pastLast, "/"); err = reftable_reader_init_ref_iterator(reader, &it); check(!err); @@ -457,10 +457,10 @@ static void t_table_read_write_seek(int index, int hash_id) check_int(err, >, 0); } - strbuf_release(&pastLast); + reftable_buf_release(&pastLast); reftable_iterator_destroy(&it); - strbuf_release(&buf); + reftable_buf_release(&buf); free_names(names); reftable_reader_decref(reader); } @@ -492,7 +492,7 @@ static void t_table_refs_for(int indexed) struct reftable_ref_record ref = { 0 }; struct reftable_reader *reader; struct reftable_block_source source = { 0 }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); struct reftable_iterator it = { 0 }; int N = 50, n, j, err, i; @@ -565,7 +565,7 @@ static void t_table_refs_for(int indexed) } check_int(j, ==, want_names_len); - strbuf_release(&buf); + reftable_buf_release(&buf); free_names(want_names); reftable_iterator_destroy(&it); reftable_reader_decref(reader); @@ -584,7 +584,7 @@ static void t_table_refs_for_obj_index(void) static void t_write_empty_table(void) { struct reftable_write_options opts = { 0 }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); struct reftable_block_source source = { 0 }; struct reftable_reader *rd = NULL; @@ -615,7 +615,7 @@ static void t_write_empty_table(void) reftable_iterator_destroy(&it); reftable_reader_decref(rd); - strbuf_release(&buf); + reftable_buf_release(&buf); } static void t_write_object_id_min_length(void) @@ -623,7 +623,7 @@ static void t_write_object_id_min_length(void) struct reftable_write_options opts = { .block_size = 75, }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); struct reftable_ref_record ref = { .update_index = 1, @@ -649,7 +649,7 @@ static void t_write_object_id_min_length(void) check(!err); check_int(reftable_writer_stats(w)->object_id_len, ==, 2); reftable_writer_free(w); - strbuf_release(&buf); + reftable_buf_release(&buf); } static void t_write_object_id_length(void) @@ -657,7 +657,7 @@ static void t_write_object_id_length(void) struct reftable_write_options opts = { .block_size = 75, }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); struct reftable_ref_record ref = { .update_index = 1, @@ -684,13 +684,13 @@ static void t_write_object_id_length(void) check(!err); check_int(reftable_writer_stats(w)->object_id_len, ==, 16); reftable_writer_free(w); - strbuf_release(&buf); + reftable_buf_release(&buf); } static void t_write_empty_key(void) { struct reftable_write_options opts = { 0 }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); struct reftable_ref_record ref = { .refname = (char *) "", @@ -706,13 +706,13 @@ static void t_write_empty_key(void) err = reftable_writer_close(w); check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR); reftable_writer_free(w); - strbuf_release(&buf); + reftable_buf_release(&buf); } static void t_write_key_order(void) { struct reftable_write_options opts = { 0 }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); struct reftable_ref_record refs[2] = { { @@ -745,7 +745,7 @@ static void t_write_key_order(void) reftable_writer_close(w); reftable_writer_free(w); - strbuf_release(&buf); + reftable_buf_release(&buf); } static void t_write_multiple_indices(void) @@ -753,7 +753,7 @@ static void t_write_multiple_indices(void) struct reftable_write_options opts = { .block_size = 100, }; - struct strbuf writer_buf = STRBUF_INIT; + struct reftable_buf writer_buf = REFTABLE_BUF_INIT; struct reftable_block_source source = { 0 }; struct reftable_iterator it = { 0 }; const struct reftable_stats *stats; @@ -822,7 +822,7 @@ static void t_write_multiple_indices(void) reftable_iterator_destroy(&it); reftable_writer_free(writer); reftable_reader_decref(reader); - strbuf_release(&writer_buf); + reftable_buf_release(&writer_buf); } static void t_write_multi_level_index(void) @@ -830,7 +830,7 @@ static void t_write_multi_level_index(void) struct reftable_write_options opts = { .block_size = 100, }; - struct strbuf writer_buf = STRBUF_INIT, buf = STRBUF_INIT; + struct reftable_buf writer_buf = REFTABLE_BUF_INIT, buf = REFTABLE_BUF_INIT; struct reftable_block_source source = { 0 }; struct reftable_iterator it = { 0 }; const struct reftable_stats *stats; @@ -878,13 +878,13 @@ static void t_write_multi_level_index(void) reftable_iterator_destroy(&it); reftable_writer_free(writer); reftable_reader_decref(reader); - strbuf_release(&writer_buf); - strbuf_release(&buf); + reftable_buf_release(&writer_buf); + reftable_buf_release(&buf); } static void t_corrupt_table_empty(void) { - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_block_source source = { 0 }; struct reftable_reader *reader; int err; @@ -897,17 +897,17 @@ static void t_corrupt_table_empty(void) static void t_corrupt_table(void) { uint8_t zeros[1024] = { 0 }; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; struct reftable_block_source source = { 0 }; struct reftable_reader *reader; int err; - strbuf_add(&buf, zeros, sizeof(zeros)); + reftable_buf_add(&buf, zeros, sizeof(zeros)); block_source_from_strbuf(&source, &buf); err = reftable_reader_new(&reader, &source, "file.log"); check_int(err, ==, REFTABLE_FORMAT_ERROR); - strbuf_release(&buf); + reftable_buf_release(&buf); } int cmd_main(int argc UNUSED, const char *argv[] UNUSED) diff --git a/t/unit-tests/t-reftable-record.c b/t/unit-tests/t-reftable-record.c index a7f67d4d9f2..f2dd01688f3 100644 --- a/t/unit-tests/t-reftable-record.c +++ b/t/unit-tests/t-reftable-record.c @@ -116,7 +116,7 @@ static void t_reftable_ref_record_compare_name(void) static void t_reftable_ref_record_roundtrip(void) { - struct strbuf scratch = STRBUF_INIT; + struct reftable_buf scratch = REFTABLE_BUF_INIT; for (int i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) { struct reftable_record in = { @@ -124,7 +124,7 @@ static void t_reftable_ref_record_roundtrip(void) .u.ref.value_type = i, }; struct reftable_record out = { .type = BLOCK_TYPE_REF }; - struct strbuf key = STRBUF_INIT; + struct reftable_buf key = REFTABLE_BUF_INIT; uint8_t buffer[1024] = { 0 }; struct string_view dest = { .buf = buffer, @@ -166,11 +166,11 @@ static void t_reftable_ref_record_roundtrip(void) GIT_SHA1_RAWSZ)); reftable_record_release(&in); - strbuf_release(&key); + reftable_buf_release(&key); reftable_record_release(&out); } - strbuf_release(&scratch); + reftable_buf_release(&scratch); } static void t_reftable_log_record_comparison(void) @@ -262,7 +262,7 @@ static void t_reftable_log_record_roundtrip(void) .value_type = REFTABLE_LOG_UPDATE, } }; - struct strbuf scratch = STRBUF_INIT; + struct reftable_buf scratch = REFTABLE_BUF_INIT; set_hash(in[0].value.update.new_hash, 1); set_hash(in[0].value.update.old_hash, 2); set_hash(in[2].value.update.new_hash, 3); @@ -274,7 +274,7 @@ static void t_reftable_log_record_roundtrip(void) for (size_t i = 0; i < ARRAY_SIZE(in); i++) { struct reftable_record rec = { .type = BLOCK_TYPE_LOG }; - struct strbuf key = STRBUF_INIT; + struct reftable_buf key = REFTABLE_BUF_INIT; uint8_t buffer[1024] = { 0 }; struct string_view dest = { .buf = buffer, @@ -313,11 +313,11 @@ static void t_reftable_log_record_roundtrip(void) check(reftable_log_record_equal(&in[i], &out.u.log, GIT_SHA1_RAWSZ)); reftable_log_record_release(&in[i]); - strbuf_release(&key); + reftable_buf_release(&key); reftable_record_release(&out); } - strbuf_release(&scratch); + reftable_buf_release(&scratch); } static void t_key_roundtrip(void) @@ -327,30 +327,30 @@ static void t_key_roundtrip(void) .buf = buffer, .len = sizeof(buffer), }; - struct strbuf last_key = STRBUF_INIT; - struct strbuf key = STRBUF_INIT; - struct strbuf roundtrip = STRBUF_INIT; + struct reftable_buf last_key = REFTABLE_BUF_INIT; + struct reftable_buf key = REFTABLE_BUF_INIT; + struct reftable_buf roundtrip = REFTABLE_BUF_INIT; int restart; uint8_t extra; int n, m; uint8_t rt_extra; - strbuf_addstr(&last_key, "refs/heads/master"); - strbuf_addstr(&key, "refs/tags/bla"); + reftable_buf_addstr(&last_key, "refs/heads/master"); + reftable_buf_addstr(&key, "refs/tags/bla"); extra = 6; n = reftable_encode_key(&restart, dest, last_key, key, extra); check(!restart); check_int(n, >, 0); - strbuf_addstr(&roundtrip, "refs/heads/master"); + reftable_buf_addstr(&roundtrip, "refs/heads/master"); m = reftable_decode_key(&roundtrip, &rt_extra, dest); check_int(n, ==, m); - check(!strbuf_cmp(&key, &roundtrip)); + check(!reftable_buf_cmp(&key, &roundtrip)); check_int(rt_extra, ==, extra); - strbuf_release(&last_key); - strbuf_release(&key); - strbuf_release(&roundtrip); + reftable_buf_release(&last_key); + reftable_buf_release(&key); + reftable_buf_release(&roundtrip); } static void t_reftable_obj_record_comparison(void) @@ -413,7 +413,7 @@ static void t_reftable_obj_record_roundtrip(void) .hash_prefix_len = 5, }, }; - struct strbuf scratch = STRBUF_INIT; + struct reftable_buf scratch = REFTABLE_BUF_INIT; for (size_t i = 0; i < ARRAY_SIZE(recs); i++) { uint8_t buffer[1024] = { 0 }; @@ -427,7 +427,7 @@ static void t_reftable_obj_record_roundtrip(void) .obj = recs[i], }, }; - struct strbuf key = STRBUF_INIT; + struct reftable_buf key = REFTABLE_BUF_INIT; struct reftable_record out = { .type = BLOCK_TYPE_OBJ }; int n, m; uint8_t extra; @@ -443,11 +443,11 @@ static void t_reftable_obj_record_roundtrip(void) check_int(n, ==, m); check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ)); - strbuf_release(&key); + reftable_buf_release(&key); reftable_record_release(&out); } - strbuf_release(&scratch); + reftable_buf_release(&scratch); } static void t_reftable_index_record_comparison(void) @@ -456,22 +456,22 @@ static void t_reftable_index_record_comparison(void) { .type = BLOCK_TYPE_INDEX, .u.idx.offset = 22, - .u.idx.last_key = STRBUF_INIT, + .u.idx.last_key = REFTABLE_BUF_INIT, }, { .type = BLOCK_TYPE_INDEX, .u.idx.offset = 32, - .u.idx.last_key = STRBUF_INIT, + .u.idx.last_key = REFTABLE_BUF_INIT, }, { .type = BLOCK_TYPE_INDEX, .u.idx.offset = 32, - .u.idx.last_key = STRBUF_INIT, + .u.idx.last_key = REFTABLE_BUF_INIT, }, }; - strbuf_addstr(&in[0].u.idx.last_key, "refs/heads/master"); - strbuf_addstr(&in[1].u.idx.last_key, "refs/heads/master"); - strbuf_addstr(&in[2].u.idx.last_key, "refs/heads/branch"); + reftable_buf_addstr(&in[0].u.idx.last_key, "refs/heads/master"); + reftable_buf_addstr(&in[1].u.idx.last_key, "refs/heads/master"); + reftable_buf_addstr(&in[2].u.idx.last_key, "refs/heads/branch"); check(!reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ)); check(!reftable_record_cmp(&in[0], &in[1])); @@ -493,7 +493,7 @@ static void t_reftable_index_record_roundtrip(void) .type = BLOCK_TYPE_INDEX, .u.idx = { .offset = 42, - .last_key = STRBUF_INIT, + .last_key = REFTABLE_BUF_INIT, }, }; uint8_t buffer[1024] = { 0 }; @@ -501,21 +501,21 @@ static void t_reftable_index_record_roundtrip(void) .buf = buffer, .len = sizeof(buffer), }; - struct strbuf scratch = STRBUF_INIT; - struct strbuf key = STRBUF_INIT; + struct reftable_buf scratch = REFTABLE_BUF_INIT; + struct reftable_buf key = REFTABLE_BUF_INIT; struct reftable_record out = { .type = BLOCK_TYPE_INDEX, - .u.idx = { .last_key = STRBUF_INIT }, + .u.idx = { .last_key = REFTABLE_BUF_INIT }, }; int n, m; uint8_t extra; - strbuf_addstr(&in.u.idx.last_key, "refs/heads/master"); + reftable_buf_addstr(&in.u.idx.last_key, "refs/heads/master"); reftable_record_key(&in, &key); t_copy(&in); check(!reftable_record_is_deletion(&in)); - check(!strbuf_cmp(&key, &in.u.idx.last_key)); + check(!reftable_buf_cmp(&key, &in.u.idx.last_key)); n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ); check_int(n, >, 0); @@ -527,9 +527,9 @@ static void t_reftable_index_record_roundtrip(void) check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ)); reftable_record_release(&out); - strbuf_release(&key); - strbuf_release(&scratch); - strbuf_release(&in.u.idx.last_key); + reftable_buf_release(&key); + reftable_buf_release(&scratch); + reftable_buf_release(&in.u.idx.last_key); } int cmd_main(int argc UNUSED, const char *argv[] UNUSED) diff --git a/t/unit-tests/t-reftable-stack.c b/t/unit-tests/t-reftable-stack.c index b56ea774312..f49856270d6 100644 --- a/t/unit-tests/t-reftable-stack.c +++ b/t/unit-tests/t-reftable-stack.c @@ -16,7 +16,7 @@ license that can be found in the LICENSE file or at static void clear_dir(const char *dirname) { - struct strbuf path = STRBUF_INIT; + struct strbuf path = REFTABLE_BUF_INIT; strbuf_addstr(&path, dirname); remove_dir_recursively(&path, 0); strbuf_release(&path); @@ -145,7 +145,7 @@ static int write_test_log(struct reftable_writer *wr, void *arg) static void t_reftable_stack_add_one(void) { char *dir = get_tmp_dir(__LINE__); - struct strbuf scratch = STRBUF_INIT; + struct reftable_buf scratch = REFTABLE_BUF_INIT; int mask = umask(002); struct reftable_write_options opts = { .default_permissions = 0660, @@ -172,17 +172,17 @@ static void t_reftable_stack_add_one(void) check_int(st->readers_len, >, 0); #ifndef GIT_WINDOWS_NATIVE - strbuf_addstr(&scratch, dir); - strbuf_addstr(&scratch, "/tables.list"); + reftable_buf_addstr(&scratch, dir); + reftable_buf_addstr(&scratch, "/tables.list"); err = stat(scratch.buf, &stat_result); check(!err); check_int((stat_result.st_mode & 0777), ==, opts.default_permissions); - strbuf_reset(&scratch); - strbuf_addstr(&scratch, dir); - strbuf_addstr(&scratch, "/"); + reftable_buf_reset(&scratch); + reftable_buf_addstr(&scratch, dir); + reftable_buf_addstr(&scratch, "/"); /* do not try at home; not an external API for reftable. */ - strbuf_addstr(&scratch, st->readers[0]->name); + reftable_buf_addstr(&scratch, st->readers[0]->name); err = stat(scratch.buf, &stat_result); check(!err); check_int((stat_result.st_mode & 0777), ==, opts.default_permissions); @@ -192,7 +192,7 @@ static void t_reftable_stack_add_one(void) reftable_ref_record_release(&dest); reftable_stack_destroy(st); - strbuf_release(&scratch); + reftable_buf_release(&scratch); clear_dir(dir); umask(mask); } @@ -414,7 +414,7 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void) }; struct reftable_write_options opts = { 0 }; struct reftable_stack *st; - struct strbuf table_path = STRBUF_INIT; + struct reftable_buf table_path = REFTABLE_BUF_INIT; char *dir = get_tmp_dir(__LINE__); int err; @@ -432,10 +432,10 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void) * Adding a new table to the stack should not be impacted by this, even * though auto-compaction will now fail. */ - strbuf_addstr(&table_path, dir); - strbuf_addstr(&table_path, "/"); - strbuf_addstr(&table_path, st->readers[0]->name); - strbuf_addstr(&table_path, ".lock"); + reftable_buf_addstr(&table_path, dir); + reftable_buf_addstr(&table_path, "/"); + reftable_buf_addstr(&table_path, st->readers[0]->name); + reftable_buf_addstr(&table_path, ".lock"); write_file_buf(table_path.buf, "", 0); ref.update_index = 2; @@ -446,7 +446,7 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void) check_int(st->stats.failures, ==, 1); reftable_stack_destroy(st); - strbuf_release(&table_path); + reftable_buf_release(&table_path); clear_dir(dir); } @@ -516,7 +516,7 @@ static void t_reftable_stack_add(void) char *dir = get_tmp_dir(__LINE__); struct reftable_ref_record refs[2] = { 0 }; struct reftable_log_record logs[2] = { 0 }; - struct strbuf path = STRBUF_INIT; + struct reftable_buf path = REFTABLE_BUF_INIT; struct stat stat_result; size_t i, N = ARRAY_SIZE(refs); @@ -575,17 +575,17 @@ static void t_reftable_stack_add(void) } #ifndef GIT_WINDOWS_NATIVE - strbuf_addstr(&path, dir); - strbuf_addstr(&path, "/tables.list"); + reftable_buf_addstr(&path, dir); + reftable_buf_addstr(&path, "/tables.list"); err = stat(path.buf, &stat_result); check(!err); check_int((stat_result.st_mode & 0777), ==, opts.default_permissions); - strbuf_reset(&path); - strbuf_addstr(&path, dir); - strbuf_addstr(&path, "/"); + reftable_buf_reset(&path); + reftable_buf_addstr(&path, dir); + reftable_buf_addstr(&path, "/"); /* do not try at home; not an external API for reftable. */ - strbuf_addstr(&path, st->readers[0]->name); + reftable_buf_addstr(&path, st->readers[0]->name); err = stat(path.buf, &stat_result); check(!err); check_int((stat_result.st_mode & 0777), ==, opts.default_permissions); @@ -599,7 +599,7 @@ static void t_reftable_stack_add(void) reftable_ref_record_release(&refs[i]); reftable_log_record_release(&logs[i]); } - strbuf_release(&path); + reftable_buf_release(&path); clear_dir(dir); } @@ -1063,7 +1063,7 @@ static void t_reftable_stack_auto_compaction_with_locked_tables(void) .disable_auto_compact = 1, }; struct reftable_stack *st = NULL; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; char *dir = get_tmp_dir(__LINE__); int err; @@ -1078,10 +1078,10 @@ static void t_reftable_stack_auto_compaction_with_locked_tables(void) * size, we expect that auto-compaction will want to compact all of the * tables. Locking any of the tables will keep it from doing so. */ - strbuf_addstr(&buf, dir); - strbuf_addstr(&buf, "/"); - strbuf_addstr(&buf, st->readers[2]->name); - strbuf_addstr(&buf, ".lock"); + reftable_buf_addstr(&buf, dir); + reftable_buf_addstr(&buf, "/"); + reftable_buf_addstr(&buf, st->readers[2]->name); + reftable_buf_addstr(&buf, ".lock"); write_file_buf(buf.buf, "", 0); /* @@ -1096,7 +1096,7 @@ static void t_reftable_stack_auto_compaction_with_locked_tables(void) check_int(st->merged->readers_len, ==, 4); reftable_stack_destroy(st); - strbuf_release(&buf); + reftable_buf_release(&buf); clear_dir(dir); } @@ -1153,7 +1153,7 @@ static void t_reftable_stack_compaction_with_locked_tables(void) .disable_auto_compact = 1, }; struct reftable_stack *st = NULL; - struct strbuf buf = STRBUF_INIT; + struct reftable_buf buf = REFTABLE_BUF_INIT; char *dir = get_tmp_dir(__LINE__); int err; @@ -1164,10 +1164,10 @@ static void t_reftable_stack_compaction_with_locked_tables(void) check_int(st->merged->readers_len, ==, 3); /* Lock one of the tables that we're about to compact. */ - strbuf_addstr(&buf, dir); - strbuf_addstr(&buf, "/"); - strbuf_addstr(&buf, st->readers[1]->name); - strbuf_addstr(&buf, ".lock"); + reftable_buf_addstr(&buf, dir); + reftable_buf_addstr(&buf, "/"); + reftable_buf_addstr(&buf, st->readers[1]->name); + reftable_buf_addstr(&buf, ".lock"); write_file_buf(buf.buf, "", 0); /* @@ -1180,7 +1180,7 @@ static void t_reftable_stack_compaction_with_locked_tables(void) check_int(st->merged->readers_len, ==, 3); reftable_stack_destroy(st); - strbuf_release(&buf); + reftable_buf_release(&buf); clear_dir(dir); } @@ -1306,7 +1306,7 @@ static void t_reftable_stack_reload_with_missing_table(void) struct reftable_stack *st = NULL; struct reftable_ref_record rec = { 0 }; struct reftable_iterator it = { 0 }; - struct strbuf table_path = STRBUF_INIT, content = STRBUF_INIT; + struct reftable_buf table_path = REFTABLE_BUF_INIT, content = REFTABLE_BUF_INIT; char *dir = get_tmp_dir(__LINE__); int err; @@ -1324,13 +1324,13 @@ static void t_reftable_stack_reload_with_missing_table(void) * our old readers. This should trigger a partial reload of the stack, * where we try to reuse our old readers. */ - strbuf_addstr(&content, st->readers[0]->name); - strbuf_addstr(&content, "\n"); - strbuf_addstr(&content, st->readers[1]->name); - strbuf_addstr(&content, "\n"); - strbuf_addstr(&content, "garbage\n"); - strbuf_addstr(&table_path, st->list_file); - strbuf_addstr(&table_path, ".lock"); + reftable_buf_addstr(&content, st->readers[0]->name); + reftable_buf_addstr(&content, "\n"); + reftable_buf_addstr(&content, st->readers[1]->name); + reftable_buf_addstr(&content, "\n"); + reftable_buf_addstr(&content, "garbage\n"); + reftable_buf_addstr(&table_path, st->list_file); + reftable_buf_addstr(&table_path, ".lock"); write_file_buf(table_path.buf, content.buf, content.len); err = rename(table_path.buf, st->list_file); check(!err); @@ -1355,8 +1355,8 @@ static void t_reftable_stack_reload_with_missing_table(void) reftable_ref_record_release(&rec); reftable_iterator_destroy(&it); reftable_stack_destroy(st); - strbuf_release(&table_path); - strbuf_release(&content); + reftable_buf_release(&table_path); + reftable_buf_release(&content); clear_dir(dir); } From patchwork Thu Oct 17 04:53:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13839466 Received: from fhigh-a8-smtp.messagingengine.com (fhigh-a8-smtp.messagingengine.com [103.168.172.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 344B712C484 for ; Thu, 17 Oct 2024 04:54:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.159 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140846; cv=none; b=c10R/yprma2yYrU2a/huZR2bO3Z24+pd96Fl9CCyFSe1+BYFTvaFFu+WNAcewZf1zRwaNeesZ7GXhESGeYQRSrWjxzHCr19hTmzbR12nyURrGqGY/XKyZjgIszs/qknlzwN/abnupEwTHkrkGPKZAX+FkmUreTomaM5sdMT0d6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140846; c=relaxed/simple; bh=lmX7abqlt/ZiuABfKcqpD84EuKveuhMpdqhwXwFm0WE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=HNvrG9XN68tSqEYJJXjMH40nw5or6kY6IkHDzhzicjA3Gp75F7a1Up9afx4BP8nI1jWqI1cYwQ1vFNHT0aq3IEen7dUZkwDU7XeEf/2zFlOy9WOshd9EGpyTBcahldc0GOiHOgOGxRGhbGPKpCZmVy4JkZTkFM+dEEFst3e0n3s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=dZhvFEeV; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=DZckaI/x; arc=none smtp.client-ip=103.168.172.159 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="dZhvFEeV"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="DZckaI/x" Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfhigh.phl.internal (Postfix) with ESMTP id 5DD0D11400E5; Thu, 17 Oct 2024 00:54:02 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-11.internal (MEProxy); Thu, 17 Oct 2024 00:54:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1729140842; x=1729227242; bh=cGB+p9Z0NE 2tjPwsXfDbaAxyD+silNw9SyneMxJ1Ml8=; b=dZhvFEeVEgqhAtWYCM6jbLqyl5 Xl0UaZttCIf/FbbNTTcXtZyErrtBLRa114Wbfer/+CcFS9uOW8MSt4cOX/d9+iJe lp9l5mQPC8rJQ/NPx4ov5sRbw12JSVSmle4L7qvyHvTkZXW5f8dquOTXKQvwW/GB XlvLXtTaYgehnTEcRTpuYn1H59w5BPRek0RVgPWLBWBNCez9f+y8wZP+MucP9/zm TXbdl/XPaRvrjfztGLNaN4Kjw8LF6gUoXSXbe+rB/ketjZp/NsgVYF8zQ3O2uXkU 1oi4sl6k0r0Z+0P1U5r77QqIWmsd0ZCOsnzfCe/DMNxg5BVPlbYEtRJdCLeQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1729140842; x=1729227242; bh=cGB+p9Z0NE2tjPwsXfDbaAxyD+si lNw9SyneMxJ1Ml8=; b=DZckaI/xc3M327MVj6xwxEMexvrJDcKN7rs9aGEV6i7F cWH3QnmFnghpsj6uJ5v4/s78lL2TgoDW+FdsKr9OnNwhY1UI86ynkbxLLQ2nzIYl dijQEiejaHNv7Rt0tKPdMdn5O1tGhsN0NHDKVz8pUADXINMt9PynrLrWU4C8Rm3d wWgzDetua3m8IA3zA5dqIhFv8NKJKekO3Ai6BjjcSIM6T34ivS1IxT1/au4OsKL1 Sd59dwxrqQRMAF+vTuIxWOsiWhmkDpPFlxso+V/j3v8c5GWPXV62lEZYsYa2RhAR WVaIytJ+Gi4TYp0IrIHcY6nOMKKT1RYb5loQoSD3Og== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehtddgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohephedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopehmvgesthhtrgihlhhorhhrrdgtohhmpdhrtghpthhtohepkhgrrhhthhhi khdrudekkeesghhmrghilhdrtghomhdprhgtphhtthhopegvthhhohhmshhonhesvggufi grrhguthhhohhmshhonhdrtghomhdprhgtphhtthhopehshhgvjhhirghluhhosehgmhgr ihhlrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 17 Oct 2024 00:54:00 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 56b93732 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 17 Oct 2024 04:52:42 +0000 (UTC) Date: Thu, 17 Oct 2024 06:53:59 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , karthik nayak , Taylor Blau , shejialuo Subject: [PATCH v3 05/10] reftable/blocksource: adapt interface name Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Adapt the name of the `strbuf` block source to no longer relate to this interface, but instead to the `reftable_buf` interface. Signed-off-by: Patrick Steinhardt --- reftable/blocksource.c | 26 +++++++++++++------------- reftable/blocksource.h | 4 ++-- t/unit-tests/t-reftable-block.c | 8 ++++---- t/unit-tests/t-reftable-merged.c | 6 +++--- t/unit-tests/t-reftable-reader.c | 4 ++-- t/unit-tests/t-reftable-readwrite.c | 24 ++++++++++++------------ 6 files changed, 36 insertions(+), 36 deletions(-) diff --git a/reftable/blocksource.c b/reftable/blocksource.c index d6242d67900..52e0915a67b 100644 --- a/reftable/blocksource.c +++ b/reftable/blocksource.c @@ -13,19 +13,19 @@ license that can be found in the LICENSE file or at #include "reftable-blocksource.h" #include "reftable-error.h" -static void strbuf_return_block(void *b UNUSED, struct reftable_block *dest) +static void reftable_buf_return_block(void *b UNUSED, struct reftable_block *dest) { if (dest->len) memset(dest->data, 0xff, dest->len); reftable_free(dest->data); } -static void strbuf_close(void *b UNUSED) +static void reftable_buf_close(void *b UNUSED) { } -static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off, - uint32_t size) +static int reftable_buf_read_block(void *v, struct reftable_block *dest, + uint64_t off, uint32_t size) { struct reftable_buf *b = v; assert(off + size <= b->len); @@ -37,23 +37,23 @@ static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off, return size; } -static uint64_t strbuf_size(void *b) +static uint64_t reftable_buf_size(void *b) { return ((struct reftable_buf *)b)->len; } -static struct reftable_block_source_vtable strbuf_vtable = { - .size = &strbuf_size, - .read_block = &strbuf_read_block, - .return_block = &strbuf_return_block, - .close = &strbuf_close, +static struct reftable_block_source_vtable reftable_buf_vtable = { + .size = &reftable_buf_size, + .read_block = &reftable_buf_read_block, + .return_block = &reftable_buf_return_block, + .close = &reftable_buf_close, }; -void block_source_from_strbuf(struct reftable_block_source *bs, - struct reftable_buf *buf) +void block_source_from_buf(struct reftable_block_source *bs, + struct reftable_buf *buf) { assert(!bs->ops); - bs->ops = &strbuf_vtable; + bs->ops = &reftable_buf_vtable; bs->arg = buf; } diff --git a/reftable/blocksource.h b/reftable/blocksource.h index ee3647c6531..a84a3ccd891 100644 --- a/reftable/blocksource.h +++ b/reftable/blocksource.h @@ -15,7 +15,7 @@ struct reftable_block_source; struct reftable_buf; /* Create an in-memory block source for reading reftables */ -void block_source_from_strbuf(struct reftable_block_source *bs, - struct reftable_buf *buf); +void block_source_from_buf(struct reftable_block_source *bs, + struct reftable_buf *buf); #endif diff --git a/t/unit-tests/t-reftable-block.c b/t/unit-tests/t-reftable-block.c index 56514b43630..df1d45fe8e4 100644 --- a/t/unit-tests/t-reftable-block.c +++ b/t/unit-tests/t-reftable-block.c @@ -34,7 +34,7 @@ static void t_ref_block_read_write(void) REFTABLE_CALLOC_ARRAY(block.data, block_size); check(block.data != NULL); block.len = block_size; - block_source_from_strbuf(&block.source ,&buf); + block_source_from_buf(&block.source ,&buf); ret = block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size, header_off, hash_size(GIT_SHA1_FORMAT_ID)); check(!ret); @@ -128,7 +128,7 @@ static void t_log_block_read_write(void) REFTABLE_CALLOC_ARRAY(block.data, block_size); check(block.data != NULL); block.len = block_size; - block_source_from_strbuf(&block.source ,&buf); + block_source_from_buf(&block.source ,&buf); ret = block_writer_init(&bw, BLOCK_TYPE_LOG, block.data, block_size, header_off, hash_size(GIT_SHA1_FORMAT_ID)); check(!ret); @@ -218,7 +218,7 @@ static void t_obj_block_read_write(void) REFTABLE_CALLOC_ARRAY(block.data, block_size); check(block.data != NULL); block.len = block_size; - block_source_from_strbuf(&block.source, &buf); + block_source_from_buf(&block.source, &buf); ret = block_writer_init(&bw, BLOCK_TYPE_OBJ, block.data, block_size, header_off, hash_size(GIT_SHA1_FORMAT_ID)); check(!ret); @@ -302,7 +302,7 @@ static void t_index_block_read_write(void) REFTABLE_CALLOC_ARRAY(block.data, block_size); check(block.data != NULL); block.len = block_size; - block_source_from_strbuf(&block.source, &buf); + block_source_from_buf(&block.source, &buf); ret = block_writer_init(&bw, BLOCK_TYPE_INDEX, block.data, block_size, header_off, hash_size(GIT_SHA1_FORMAT_ID)); check(!ret); diff --git a/t/unit-tests/t-reftable-merged.c b/t/unit-tests/t-reftable-merged.c index 9b0162a4b32..484c18251f3 100644 --- a/t/unit-tests/t-reftable-merged.c +++ b/t/unit-tests/t-reftable-merged.c @@ -35,7 +35,7 @@ merged_table_from_records(struct reftable_ref_record **refs, for (size_t i = 0; i < n; i++) { t_reftable_write_to_buf(&buf[i], refs[i], sizes[i], NULL, 0, &opts); - block_source_from_strbuf(&(*source)[i], &buf[i]); + block_source_from_buf(&(*source)[i], &buf[i]); err = reftable_reader_new(&(*readers)[i], &(*source)[i], "name"); @@ -293,7 +293,7 @@ merged_table_from_log_records(struct reftable_log_record **logs, for (size_t i = 0; i < n; i++) { t_reftable_write_to_buf(&buf[i], NULL, 0, logs[i], sizes[i], &opts); - block_source_from_strbuf(&(*source)[i], &buf[i]); + block_source_from_buf(&(*source)[i], &buf[i]); err = reftable_reader_new(&(*readers)[i], &(*source)[i], "name"); @@ -442,7 +442,7 @@ static void t_default_write_opts(void) check(!err); reftable_writer_free(w); - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); err = reftable_reader_new(&rd, &source, "filename"); check(!err); diff --git a/t/unit-tests/t-reftable-reader.c b/t/unit-tests/t-reftable-reader.c index 8a18d7f9be4..19cb53b6415 100644 --- a/t/unit-tests/t-reftable-reader.c +++ b/t/unit-tests/t-reftable-reader.c @@ -20,7 +20,7 @@ static int t_reader_seek_once(void) int ret; t_reftable_write_to_buf(&buf, records, ARRAY_SIZE(records), NULL, 0, NULL); - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); ret = reftable_reader_new(&reader, &source, "name"); check(!ret); @@ -61,7 +61,7 @@ static int t_reader_reseek(void) int ret; t_reftable_write_to_buf(&buf, records, ARRAY_SIZE(records), NULL, 0, NULL); - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); ret = reftable_reader_new(&reader, &source, "name"); check(!ret); diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index c56a33f1a1e..7c7c72bb162 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -24,7 +24,7 @@ static void t_buffer(void) int n; uint8_t in[] = "hello"; reftable_buf_add(&buf, in, sizeof(in)); - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); check_int(block_source_size(&source), ==, 6); n = block_source_read_block(&source, &out, 0, sizeof(in)); check_int(n, ==, sizeof(in)); @@ -207,7 +207,7 @@ static void t_log_write_read(void) reftable_writer_free(w); w = NULL; - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); err = reftable_reader_new(&reader, &source, "file.log"); check(!err); @@ -298,7 +298,7 @@ static void t_log_zlib_corruption(void) /* corrupt the data. */ buf.buf[50] ^= 0x99; - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); err = reftable_reader_new(&reader, &source, "file.log"); check(!err); @@ -328,7 +328,7 @@ static void t_table_read_write_sequential(void) write_table(&names, &buf, N, 256, GIT_SHA1_FORMAT_ID); - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); err = reftable_reader_new(&reader, &source, "file.ref"); check(!err); @@ -380,7 +380,7 @@ static void t_table_read_api(void) write_table(&names, &buf, N, 256, GIT_SHA1_FORMAT_ID); - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); err = reftable_reader_new(&reader, &source, "file.ref"); check(!err); @@ -416,7 +416,7 @@ static void t_table_read_write_seek(int index, int hash_id) write_table(&names, &buf, N, 256, hash_id); - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); err = reftable_reader_new(&reader, &source, "file.ref"); check(!err); @@ -538,7 +538,7 @@ static void t_table_refs_for(int indexed) reftable_writer_free(w); w = NULL; - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); err = reftable_reader_new(&reader, &source, "file.ref"); check(!err); @@ -600,7 +600,7 @@ static void t_write_empty_table(void) check_int(buf.len, ==, header_size(1) + footer_size(1)); - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); err = reftable_reader_new(&rd, &source, "filename"); check(!err); @@ -806,7 +806,7 @@ static void t_write_multiple_indices(void) check_int(stats->obj_stats.index_offset, >, 0); check_int(stats->log_stats.index_offset, >, 0); - block_source_from_strbuf(&source, &writer_buf); + block_source_from_buf(&source, &writer_buf); err = reftable_reader_new(&reader, &source, "filename"); check(!err); @@ -863,7 +863,7 @@ static void t_write_multi_level_index(void) stats = reftable_writer_stats(writer); check_int(stats->ref_stats.max_index_level, ==, 2); - block_source_from_strbuf(&source, &writer_buf); + block_source_from_buf(&source, &writer_buf); err = reftable_reader_new(&reader, &source, "filename"); check(!err); @@ -889,7 +889,7 @@ static void t_corrupt_table_empty(void) struct reftable_reader *reader; int err; - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); err = reftable_reader_new(&reader, &source, "file.log"); check_int(err, ==, REFTABLE_FORMAT_ERROR); } @@ -903,7 +903,7 @@ static void t_corrupt_table(void) int err; reftable_buf_add(&buf, zeros, sizeof(zeros)); - block_source_from_strbuf(&source, &buf); + block_source_from_buf(&source, &buf); err = reftable_reader_new(&reader, &source, "file.log"); check_int(err, ==, REFTABLE_FORMAT_ERROR); From patchwork Thu Oct 17 04:54:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13839467 Received: from fhigh-a8-smtp.messagingengine.com (fhigh-a8-smtp.messagingengine.com [103.168.172.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5CC6155A24 for ; Thu, 17 Oct 2024 04:54:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.159 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140848; cv=none; b=sk76byVQqVoC+a45Pv8PVau2WIpe17xwR9XAJsB9Nv+kJns6KRQGxPbTYDLgbjF75mUvrtTWMpJ2oVDFWQf1CtJRCufMneIV2pwGX3fbX+on4gGrZZSNgRNKpC+3se/ebUPiVV3cpBagEp9S6S7/6KKmIlSScI6Dy/YTe4JOKHA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140848; c=relaxed/simple; bh=vfN2VtaYz0lW56996VGQWRlyfPE3s6m1W/MVZPnYRJU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=T5NSAIrQm/Tb97Ty0A+L4PspbvWNbCYiRCyBjTvSbCMXoeg2/uVbzPZXw2TcdOkZCUUV03h5awvKR+pjNDVI7sB2O9nq4L0Hor2s81uUBZUFZef7ILi2wlvDHowgbx4VcJ41K+mZvaLjJZXIqps4ZDi7gmGA9R/ccOe78dHqwuc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=UKHwakgp; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=nkYlJ0yN; arc=none smtp.client-ip=103.168.172.159 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="UKHwakgp"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="nkYlJ0yN" Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.phl.internal (Postfix) with ESMTP id 02DEE11400A6; Thu, 17 Oct 2024 00:54:05 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-01.internal (MEProxy); Thu, 17 Oct 2024 00:54:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1729140844; x=1729227244; bh=lQjLp3+p4V ol//0QRuiWPzfWGV9Mj+5vKYylTgIj+zY=; b=UKHwakgpf9jrINFUzC/z/zXHSr wBPhh8qy/7ksoVWdzqUXBX5CPP0/lgH5MMC7Av3YVJlnmnkn6csoC0O5SHX1LNnH mqNC2ZurCy7WBWGebRfGIaBXi+Wb66IKwPpQnNckVUARUovcMP8TKtsQGqP5Ge15 5uYYJ5sgvNgF7D6x+BCgnod1hqySbpX/OeqTE+0xKkkRPog8+dqtTTNZ8OF/1TSR MlYSlKEQm+xcowvj3ZpFX/u4euj3DsO9GsBcNgOWTNLCbtmgqUC2PU7Q/M8qE972 Vdh32yVcHwbtX/PkgbJnGc4zrZroKMSr+kL7gipyrp8NLC6E2hhsAA11+2WQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1729140844; x=1729227244; bh=lQjLp3+p4Vol//0QRuiWPzfWGV9M j+5vKYylTgIj+zY=; b=nkYlJ0yNYU+23yZNVSzmKtsIazswHjitsKiP0f7lnXKr sjAuH5gJUvnxmzKVwEw6t5aVMYojVTKa36Cji/qKcKwb/iSUdEKryMBhQtnSw45X d+3oD6d99bwyLbHZBe1Ao4E4if27OXCTl65bnRtvOR5CJzVcH+H8R6Rx9o51RV/S C5t4Vn8Yw4wG2/xwWchAQLPV6PGLUu7mMHuM/nT5AbyI8UroUrHbU7Ob+nJIi1xe 6En+wVKwdzLj8yx6RFvH+KMZyu9tPa0FOwb8EkcfTCrj2bx5CRyCciOKwAPJOH3r XHPJq/3ZBn7ApdXDTjU7htO4PiCUl4JSIdRmxKCjZw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehtddgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohephedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepkhgrrhhthhhikhdrudekkeesghhmrghilhdrtghomh dprhgtphhtthhopehgihhtsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohep shhhvghjihgrlhhuohesghhmrghilhdrtghomhdprhgtphhtthhopegvthhhohhmshhonh esvggufigrrhguthhhohhmshhonhdrtghomhdprhgtphhtthhopehmvgesthhtrgihlhho rhhrrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 17 Oct 2024 00:54:03 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id ebc81c02 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 17 Oct 2024 04:52:45 +0000 (UTC) Date: Thu, 17 Oct 2024 06:54:02 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , karthik nayak , Taylor Blau , shejialuo Subject: [PATCH v3 06/10] t/unit-tests: check for `reftable_buf` allocation errors Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Adapt our unit tests to check for allocations errors. Signed-off-by: Patrick Steinhardt --- t/unit-tests/t-reftable-basics.c | 4 +- t/unit-tests/t-reftable-block.c | 4 +- t/unit-tests/t-reftable-readwrite.c | 8 ++-- t/unit-tests/t-reftable-record.c | 14 +++---- t/unit-tests/t-reftable-stack.c | 58 ++++++++++++++--------------- 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/t/unit-tests/t-reftable-basics.c b/t/unit-tests/t-reftable-basics.c index a814e819756..a329f552025 100644 --- a/t/unit-tests/t-reftable-basics.c +++ b/t/unit-tests/t-reftable-basics.c @@ -113,8 +113,8 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED) }; for (size_t i = 0; i < ARRAY_SIZE(cases); i++) { - reftable_buf_addstr(&a, cases[i].a); - reftable_buf_addstr(&b, cases[i].b); + check(!reftable_buf_addstr(&a, cases[i].a)); + check(!reftable_buf_addstr(&b, cases[i].b)); check_int(common_prefix_size(&a, &b), ==, cases[i].want); reftable_buf_reset(&a); reftable_buf_reset(&b); diff --git a/t/unit-tests/t-reftable-block.c b/t/unit-tests/t-reftable-block.c index df1d45fe8e4..f9af907117b 100644 --- a/t/unit-tests/t-reftable-block.c +++ b/t/unit-tests/t-reftable-block.c @@ -167,7 +167,7 @@ static void t_log_block_read_write(void) for (i = 0; i < N; i++) { block_iter_reset(&it); reftable_buf_reset(&want); - reftable_buf_addstr(&want, recs[i].u.log.refname); + check(!reftable_buf_addstr(&want, recs[i].u.log.refname)); ret = block_iter_seek_key(&it, &br, &want); check_int(ret, ==, 0); @@ -314,7 +314,7 @@ static void t_index_block_read_write(void) reftable_buf_init(&recs[i].u.idx.last_key); recs[i].type = BLOCK_TYPE_INDEX; - reftable_buf_addstr(&recs[i].u.idx.last_key, buf); + check(!reftable_buf_addstr(&recs[i].u.idx.last_key, buf)); recs[i].u.idx.offset = i; ret = block_writer_add(&bw, &recs[i]); diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index 7c7c72bb162..d279b86df0a 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -23,7 +23,7 @@ static void t_buffer(void) struct reftable_block out = { 0 }; int n; uint8_t in[] = "hello"; - reftable_buf_add(&buf, in, sizeof(in)); + check(!reftable_buf_add(&buf, in, sizeof(in))); block_source_from_buf(&source, &buf); check_int(block_source_size(&source), ==, 6); n = block_source_read_block(&source, &out, 0, sizeof(in)); @@ -443,8 +443,8 @@ static void t_table_read_write_seek(int index, int hash_id) reftable_iterator_destroy(&it); } - reftable_buf_addstr(&pastLast, names[N - 1]); - reftable_buf_addstr(&pastLast, "/"); + check(!reftable_buf_addstr(&pastLast, names[N - 1])); + check(!reftable_buf_addstr(&pastLast, "/")); err = reftable_reader_init_ref_iterator(reader, &it); check(!err); @@ -901,7 +901,7 @@ static void t_corrupt_table(void) struct reftable_block_source source = { 0 }; struct reftable_reader *reader; int err; - reftable_buf_add(&buf, zeros, sizeof(zeros)); + check(!reftable_buf_add(&buf, zeros, sizeof(zeros))); block_source_from_buf(&source, &buf); err = reftable_reader_new(&reader, &source, "file.log"); diff --git a/t/unit-tests/t-reftable-record.c b/t/unit-tests/t-reftable-record.c index f2dd01688f3..eb98bf2da91 100644 --- a/t/unit-tests/t-reftable-record.c +++ b/t/unit-tests/t-reftable-record.c @@ -335,14 +335,14 @@ static void t_key_roundtrip(void) int n, m; uint8_t rt_extra; - reftable_buf_addstr(&last_key, "refs/heads/master"); - reftable_buf_addstr(&key, "refs/tags/bla"); + check(!reftable_buf_addstr(&last_key, "refs/heads/master")); + check(!reftable_buf_addstr(&key, "refs/tags/bla")); extra = 6; n = reftable_encode_key(&restart, dest, last_key, key, extra); check(!restart); check_int(n, >, 0); - reftable_buf_addstr(&roundtrip, "refs/heads/master"); + check(!reftable_buf_addstr(&roundtrip, "refs/heads/master")); m = reftable_decode_key(&roundtrip, &rt_extra, dest); check_int(n, ==, m); check(!reftable_buf_cmp(&key, &roundtrip)); @@ -469,9 +469,9 @@ static void t_reftable_index_record_comparison(void) .u.idx.last_key = REFTABLE_BUF_INIT, }, }; - reftable_buf_addstr(&in[0].u.idx.last_key, "refs/heads/master"); - reftable_buf_addstr(&in[1].u.idx.last_key, "refs/heads/master"); - reftable_buf_addstr(&in[2].u.idx.last_key, "refs/heads/branch"); + check(!reftable_buf_addstr(&in[0].u.idx.last_key, "refs/heads/master")); + check(!reftable_buf_addstr(&in[1].u.idx.last_key, "refs/heads/master")); + check(!reftable_buf_addstr(&in[2].u.idx.last_key, "refs/heads/branch")); check(!reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ)); check(!reftable_record_cmp(&in[0], &in[1])); @@ -510,7 +510,7 @@ static void t_reftable_index_record_roundtrip(void) int n, m; uint8_t extra; - reftable_buf_addstr(&in.u.idx.last_key, "refs/heads/master"); + check(!reftable_buf_addstr(&in.u.idx.last_key, "refs/heads/master")); reftable_record_key(&in, &key); t_copy(&in); diff --git a/t/unit-tests/t-reftable-stack.c b/t/unit-tests/t-reftable-stack.c index f49856270d6..72f6747064f 100644 --- a/t/unit-tests/t-reftable-stack.c +++ b/t/unit-tests/t-reftable-stack.c @@ -172,17 +172,17 @@ static void t_reftable_stack_add_one(void) check_int(st->readers_len, >, 0); #ifndef GIT_WINDOWS_NATIVE - reftable_buf_addstr(&scratch, dir); - reftable_buf_addstr(&scratch, "/tables.list"); + check(!reftable_buf_addstr(&scratch, dir)); + check(!reftable_buf_addstr(&scratch, "/tables.list")); err = stat(scratch.buf, &stat_result); check(!err); check_int((stat_result.st_mode & 0777), ==, opts.default_permissions); reftable_buf_reset(&scratch); - reftable_buf_addstr(&scratch, dir); - reftable_buf_addstr(&scratch, "/"); + check(!reftable_buf_addstr(&scratch, dir)); + check(!reftable_buf_addstr(&scratch, "/")); /* do not try at home; not an external API for reftable. */ - reftable_buf_addstr(&scratch, st->readers[0]->name); + check(!reftable_buf_addstr(&scratch, st->readers[0]->name)); err = stat(scratch.buf, &stat_result); check(!err); check_int((stat_result.st_mode & 0777), ==, opts.default_permissions); @@ -432,10 +432,10 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void) * Adding a new table to the stack should not be impacted by this, even * though auto-compaction will now fail. */ - reftable_buf_addstr(&table_path, dir); - reftable_buf_addstr(&table_path, "/"); - reftable_buf_addstr(&table_path, st->readers[0]->name); - reftable_buf_addstr(&table_path, ".lock"); + check(!reftable_buf_addstr(&table_path, dir)); + check(!reftable_buf_addstr(&table_path, "/")); + check(!reftable_buf_addstr(&table_path, st->readers[0]->name)); + check(!reftable_buf_addstr(&table_path, ".lock")); write_file_buf(table_path.buf, "", 0); ref.update_index = 2; @@ -575,17 +575,17 @@ static void t_reftable_stack_add(void) } #ifndef GIT_WINDOWS_NATIVE - reftable_buf_addstr(&path, dir); - reftable_buf_addstr(&path, "/tables.list"); + check(!reftable_buf_addstr(&path, dir)); + check(!reftable_buf_addstr(&path, "/tables.list")); err = stat(path.buf, &stat_result); check(!err); check_int((stat_result.st_mode & 0777), ==, opts.default_permissions); reftable_buf_reset(&path); - reftable_buf_addstr(&path, dir); - reftable_buf_addstr(&path, "/"); + check(!reftable_buf_addstr(&path, dir)); + check(!reftable_buf_addstr(&path, "/")); /* do not try at home; not an external API for reftable. */ - reftable_buf_addstr(&path, st->readers[0]->name); + check(!reftable_buf_addstr(&path, st->readers[0]->name)); err = stat(path.buf, &stat_result); check(!err); check_int((stat_result.st_mode & 0777), ==, opts.default_permissions); @@ -1078,10 +1078,10 @@ static void t_reftable_stack_auto_compaction_with_locked_tables(void) * size, we expect that auto-compaction will want to compact all of the * tables. Locking any of the tables will keep it from doing so. */ - reftable_buf_addstr(&buf, dir); - reftable_buf_addstr(&buf, "/"); - reftable_buf_addstr(&buf, st->readers[2]->name); - reftable_buf_addstr(&buf, ".lock"); + check(!reftable_buf_addstr(&buf, dir)); + check(!reftable_buf_addstr(&buf, "/")); + check(!reftable_buf_addstr(&buf, st->readers[2]->name)); + check(!reftable_buf_addstr(&buf, ".lock")); write_file_buf(buf.buf, "", 0); /* @@ -1164,10 +1164,10 @@ static void t_reftable_stack_compaction_with_locked_tables(void) check_int(st->merged->readers_len, ==, 3); /* Lock one of the tables that we're about to compact. */ - reftable_buf_addstr(&buf, dir); - reftable_buf_addstr(&buf, "/"); - reftable_buf_addstr(&buf, st->readers[1]->name); - reftable_buf_addstr(&buf, ".lock"); + check(!reftable_buf_addstr(&buf, dir)); + check(!reftable_buf_addstr(&buf, "/")); + check(!reftable_buf_addstr(&buf, st->readers[1]->name)); + check(!reftable_buf_addstr(&buf, ".lock")); write_file_buf(buf.buf, "", 0); /* @@ -1324,13 +1324,13 @@ static void t_reftable_stack_reload_with_missing_table(void) * our old readers. This should trigger a partial reload of the stack, * where we try to reuse our old readers. */ - reftable_buf_addstr(&content, st->readers[0]->name); - reftable_buf_addstr(&content, "\n"); - reftable_buf_addstr(&content, st->readers[1]->name); - reftable_buf_addstr(&content, "\n"); - reftable_buf_addstr(&content, "garbage\n"); - reftable_buf_addstr(&table_path, st->list_file); - reftable_buf_addstr(&table_path, ".lock"); + check(!reftable_buf_addstr(&content, st->readers[0]->name)); + check(!reftable_buf_addstr(&content, "\n")); + check(!reftable_buf_addstr(&content, st->readers[1]->name)); + check(!reftable_buf_addstr(&content, "\n")); + check(!reftable_buf_addstr(&content, "garbage\n")); + check(!reftable_buf_addstr(&table_path, st->list_file)); + check(!reftable_buf_addstr(&table_path, ".lock")); write_file_buf(table_path.buf, content.buf, content.len); err = rename(table_path.buf, st->list_file); check(!err); From patchwork Thu Oct 17 04:54:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13839468 Received: from fout-a3-smtp.messagingengine.com (fout-a3-smtp.messagingengine.com [103.168.172.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B58C17D355 for ; Thu, 17 Oct 2024 04:54:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.146 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140851; cv=none; b=PNkOZ4v4rRMGkzozUZnbb7EDBzEwKCP/KKKuqJTPKF7Vuo/H6eCuhNJIPgjEJqgqknkRKZKJ2TkNpOmLQTY170zLHsOe8cM98W0bSdgXnChaCCy1wb75x1Ladj4CZ9X5Epm+Br3jBPfbSUG0EW4wGd+UxT0uAsa6CjUxMEyoMSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140851; c=relaxed/simple; bh=HQdN+VP6pp2esCQOvjtS/7n92ymvtBpsPBQLpv6w82E=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=PJxorXqimcb+mTKIatswNdHsgiRaFViHX36lTXJnYPCcyHAtr+EnTtIPrRbImstUlaWlaof/R4oufmqpGPx4dc7628w1Nb++JX57fLpovsvykQAQKxX8BcliNe5aJPdoLQZfg8jv4jjgf5X9KhtH5G/ni1b4c5UIwKjqsw8oXSQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=nZgmh2qn; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=dztUNu50; arc=none smtp.client-ip=103.168.172.146 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="nZgmh2qn"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="dztUNu50" Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailfout.phl.internal (Postfix) with ESMTP id 827A313802B2; Thu, 17 Oct 2024 00:54:07 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-02.internal (MEProxy); Thu, 17 Oct 2024 00:54:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1729140847; x=1729227247; bh=YJgTa8YwrK hi1qa7l0d9psTrqKETN5pCi8yD/RhxwPg=; b=nZgmh2qnnnZZ3lHHyNyoHrilIz +aIZIMPl9K6kz3xSzrrmw0KigKgCRDjwKxyXbAQiDyqiwrUM2gcpPCdX06UVScF3 uZnKXojB+U5vD4cKMaUDhAfHgcIwex/075CbOonLL/gvBS0DIE4jhMQmqH/n5hfS yfKakmeJXu2BfsFb/gooDG1hpcYx1t12nTy8BRXxVG1DwAzH+Alg8OOVSCdIgMgp oS1NbLYnHmYceo9u/IhkgDlNoobfV+b+96TMW+64tgM1pZEimb4Urhdf/8uvb4Iz MXpQVUy6BRrXWU2SaNQsm3DwobOB6TtGjhQFMJ4pdQgBQmF/rvkJfK0WAYzQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1729140847; x=1729227247; bh=YJgTa8YwrKhi1qa7l0d9psTrqKET N5pCi8yD/RhxwPg=; b=dztUNu50dNSv7qqPBDlXpQc/3hLk/u6fBeFcmcc7lcI/ h3IG4vUEAvRgTuAa6XbxIDf7GN9KG0d6/9gEuEjYB2k9/FM9mXEGKc8kU3MuHOJw PIOmWf+N7is6TdL17gjDQG/JEkgDxW3SxxpTZbSGeneYIb9XJX3oZnFX+sQcdo99 FbrDcSfNtt5MDSoGywe/2Z1LmXQj259ZmgrjyVDbjdHgu7hxwnmWdOC1hKQIPJ5s mbEfR9RE5kpbmYs0SXZ02yRU1k+IEWawvXHjAqEMfg4cS5VZ0nWEv93uOdfdwWSy vsRuxyVXZceBxY06lON9XKaXToNwzp9pSYMMNpVl9w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehtddgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohephedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepkhgrrhhthhhikhdrudekkeesghhmrghilhdrtghomh dprhgtphhtthhopehgihhtsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohep vghthhhomhhsohhnsegvugifrghrughthhhomhhsohhnrdgtohhmpdhrtghpthhtohepsh hhvghjihgrlhhuohesghhmrghilhdrtghomhdprhgtphhtthhopehmvgesthhtrgihlhho rhhrrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 17 Oct 2024 00:54:06 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 3044a7b7 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 17 Oct 2024 04:52:48 +0000 (UTC) Date: Thu, 17 Oct 2024 06:54:05 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , karthik nayak , Taylor Blau , shejialuo Subject: [PATCH v3 07/10] reftable/stack: adapt `format_name()` to handle allocation failures Message-ID: <8f8e2ca39624208e71aaca13e439ba7d32e77970.1729140565.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The `format_name()` function cannot pass any errors to the caller as it has a `void` return type. Adapt it and its callers such that we can handle errors and start handling allocation failures. Signed-off-by: Patrick Steinhardt --- reftable/stack.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index 6ba48ddce5d..e94eb3c4685 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -623,14 +623,14 @@ int reftable_stack_add(struct reftable_stack *st, return 0; } -static void format_name(struct reftable_buf *dest, uint64_t min, uint64_t max) +static int format_name(struct reftable_buf *dest, uint64_t min, uint64_t max) { char buf[100]; uint32_t rnd = (uint32_t)git_rand(); snprintf(buf, sizeof(buf), "0x%012" PRIx64 "-0x%012" PRIx64 "-%08x", min, max, rnd); reftable_buf_reset(dest); - reftable_buf_addstr(dest, buf); + return reftable_buf_addstr(dest, buf); } struct reftable_addition { @@ -846,7 +846,10 @@ int reftable_addition_add(struct reftable_addition *add, int tab_fd; reftable_buf_reset(&next_name); - format_name(&next_name, add->next_update_index, add->next_update_index); + + err = format_name(&next_name, add->next_update_index, add->next_update_index); + if (err < 0) + goto done; stack_filename(&temp_tab_file_name, add->stack, next_name.buf); reftable_buf_addstr(&temp_tab_file_name, ".temp.XXXXXX"); @@ -893,7 +896,9 @@ int reftable_addition_add(struct reftable_addition *add, goto done; } - format_name(&next_name, wr->min_update_index, wr->max_update_index); + err = format_name(&next_name, wr->min_update_index, wr->max_update_index); + if (err < 0) + goto done; reftable_buf_addstr(&next_name, ".ref"); stack_filename(&tab_file_name, add->stack, next_name.buf); @@ -944,9 +949,11 @@ static int stack_compact_locked(struct reftable_stack *st, struct tempfile *tab_file; int tab_fd, err = 0; - format_name(&next_name, - reftable_reader_min_update_index(st->readers[first]), - reftable_reader_max_update_index(st->readers[last])); + err = format_name(&next_name, reftable_reader_min_update_index(st->readers[first]), + reftable_reader_max_update_index(st->readers[last])); + if (err < 0) + goto done; + stack_filename(&tab_file_path, st, next_name.buf); reftable_buf_addstr(&tab_file_path, ".temp.XXXXXX"); @@ -1370,8 +1377,11 @@ static int stack_compact_range(struct reftable_stack *st, * it into place now. */ if (!is_empty_table) { - format_name(&new_table_name, st->readers[first]->min_update_index, - st->readers[last]->max_update_index); + err = format_name(&new_table_name, st->readers[first]->min_update_index, + st->readers[last]->max_update_index); + if (err < 0) + goto done; + reftable_buf_addstr(&new_table_name, ".ref"); stack_filename(&new_table_path, st, new_table_name.buf); From patchwork Thu Oct 17 04:54:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13839469 Received: from fout-a3-smtp.messagingengine.com (fout-a3-smtp.messagingengine.com [103.168.172.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 079F116E892 for ; Thu, 17 Oct 2024 04:54:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.146 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140857; cv=none; b=Cu+SjMsWKrco4m7k6auILV0is1/9soTuqQOawd5kIALsxwx8PuhdeQE7llfDDRnm9Ums6H2OMFjhjBwSbMbHVB76hNLtqn2QLpvWH/NErrQ/WVp156reuuHSp7uJJ23LTyWazSjtOhhw8P/RLmqknZ7hXaFvGlzQh0Qt3I75BlM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140857; c=relaxed/simple; bh=SFU7fnCToMKdvu5REM4dKFl5UPiPJR67vTOtSgeZtOw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Sq4925EOfi7LCuv1GxC7axABaGh2aYFLrBzMZAOjfJCCoFTAa5+TPrjftR4Q2HYzCK+zTkZMdbKfu9LPq5VbuRCwqD7vvI0MpaD1ZbMClhY7W19YI1675K/4qLY1bawHb5NUokVHSk4KP241cWr0aB9p2d9vr5BqNnmKRM9u6eI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=MauCve8X; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=jyokR3Dp; arc=none smtp.client-ip=103.168.172.146 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="MauCve8X"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="jyokR3Dp" Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfout.phl.internal (Postfix) with ESMTP id 2FEBE13801F0; Thu, 17 Oct 2024 00:54:13 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-12.internal (MEProxy); Thu, 17 Oct 2024 00:54:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1729140853; x=1729227253; bh=zvZ8jsVm7w YCJDc32qMf7cyA0g9S+Qau1lePrQs8rXU=; b=MauCve8XLeMbj606ucKaVyvmhH FzVgIc0WLraJkn/AhKe2mJHT6lbUCFRYy3n5PLPHlwIjJ66xHRQ18xdZ9gTpIGj0 kzXzf8BkJmJSqYLDsj5J3wKEE8RfJ9N+cmnTAUdeAhRiQdz2aI84rWqG7Ie+zBiE S3yeFMbTuafT6VqKMcTo2BBSnndSvprSg/kfjdJ/nE3ESTwo/vs+o9dweZQm9pVD FIk45un9SkGYbvgiZUieKpej7V7LVXTJetqwSEF2eyj827h7+JI/su8mZMwpZpND Yt7bjK6502PR1cSBc3yfkiLrJeuiTcObp3PGzYY3rIjYiY70kAC6E6Q5CM3g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1729140853; x=1729227253; bh=zvZ8jsVm7wYCJDc32qMf7cyA0g9S +Qau1lePrQs8rXU=; b=jyokR3Dp3NtTWcKW/1D3J8Lsy2VXq3OLxWXVRACOOgRd epeCHFEbC/985Y4egbFLwlG8EVqHwrZthQLy5QGXK1lUBz5fATJFflodobIzfxv2 X0B+TzVGI94uBAKi4Br5tHRrqEQwOreVzbMQSbaTNmQ0GARD93IznQWUYOFD2B67 CSXO2gShANCi159liSBP80m8wV+zQhK+8Krsn38pPvUXtx/h5AhydrIudV70H6bz goJYyGBrBYFWW67bh5UflFA9De1oKKZO9x9cTAcWUJCHlc7Z8n9EGHNv93bDvkbc Q+YmbTc+8B1FTFEBemfIVUnw5b3QNbfFV2BnzqEmzg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehtddgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohephedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepshhhvghjihgrlhhuohesghhmrghilhdrtghomhdprh gtphhtthhopegvthhhohhmshhonhesvggufigrrhguthhhohhmshhonhdrtghomhdprhgt phhtthhopehgihhtsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepkhgrrh hthhhikhdrudekkeesghhmrghilhdrtghomhdprhgtphhtthhopehmvgesthhtrgihlhho rhhrrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 17 Oct 2024 00:54:11 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 3d6199b3 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 17 Oct 2024 04:52:53 +0000 (UTC) Date: Thu, 17 Oct 2024 06:54:08 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , karthik nayak , Taylor Blau , shejialuo Subject: [PATCH v3 08/10] reftable/record: adapt `reftable_record_key()` to handle allocation failures Message-ID: <268e4cd6fc6fb0d9ab6216723dc39c5cada3734c.1729140565.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The `reftable_record_key()` function cannot pass any errors to the caller as it has a `void` return type. Adapt it and its callers such that we can handle errors and start handling allocation failures. Signed-off-by: Patrick Steinhardt --- reftable/block.c | 20 +++++++++++++++----- reftable/reader.c | 8 ++++++-- reftable/record.c | 32 ++++++++++++++++++++------------ reftable/record.h | 4 ++-- reftable/writer.c | 5 ++++- 5 files changed, 47 insertions(+), 22 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 4f62b823db8..697b8b41531 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -111,9 +111,12 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) int is_restart = 0; struct reftable_buf key = REFTABLE_BUF_INIT; int n = 0; - int err = -1; + int err; + + err = reftable_record_key(rec, &key); + if (err < 0) + goto done; - reftable_record_key(rec, &key); if (!key.len) { err = REFTABLE_API_ERROR; goto done; @@ -121,13 +124,17 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec) n = reftable_encode_key(&is_restart, out, last, key, reftable_record_val_type(rec)); - if (n < 0) + if (n < 0) { + err = -1; goto done; + } string_view_consume(&out, n); n = reftable_record_encode(rec, out, w->hash_size); - if (n < 0) + if (n < 0) { + err = -1; goto done; + } string_view_consume(&out, n); err = block_writer_register_restart(w, start.len - out.len, is_restart, @@ -522,6 +529,10 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br, goto done; } + err = reftable_record_key(&rec, &it->last_key); + if (err < 0) + goto done; + /* * Check whether the current key is greater or equal to the * sought-after key. In case it is greater we know that the @@ -536,7 +547,6 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br, * to `last_key` now, and naturally all keys share a prefix * with themselves. */ - reftable_record_key(&rec, &it->last_key); if (reftable_buf_cmp(&it->last_key, want) >= 0) { it->next_off = prev_off; goto done; diff --git a/reftable/reader.c b/reftable/reader.c index 388f8bf6d7b..ab89efd9c55 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -356,7 +356,9 @@ static int table_iter_seek_linear(struct table_iter *ti, int err; reftable_record_init(&rec, reftable_record_type(want)); - reftable_record_key(want, &want_key); + err = reftable_record_key(want, &want_key); + if (err < 0) + goto done; /* * First we need to locate the block that must contain our record. To @@ -439,7 +441,9 @@ static int table_iter_seek_indexed(struct table_iter *ti, }; int err; - reftable_record_key(rec, &want_index.u.idx.last_key); + err = reftable_record_key(rec, &want_index.u.idx.last_key); + if (err < 0) + goto done; /* * The index may consist of multiple levels, where each level may have diff --git a/reftable/record.c b/reftable/record.c index 0182c973437..672c5f909a9 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -207,12 +207,12 @@ int reftable_decode_key(struct reftable_buf *last_key, uint8_t *extra, return start_len - in.len; } -static void reftable_ref_record_key(const void *r, struct reftable_buf *dest) +static int reftable_ref_record_key(const void *r, struct reftable_buf *dest) { const struct reftable_ref_record *rec = (const struct reftable_ref_record *)r; reftable_buf_reset(dest); - reftable_buf_addstr(dest, rec->refname); + return reftable_buf_addstr(dest, rec->refname); } static int reftable_ref_record_copy_from(void *rec, const void *src_rec, @@ -465,12 +465,12 @@ static struct reftable_record_vtable reftable_ref_record_vtable = { .cmp = &reftable_ref_record_cmp_void, }; -static void reftable_obj_record_key(const void *r, struct reftable_buf *dest) +static int reftable_obj_record_key(const void *r, struct reftable_buf *dest) { const struct reftable_obj_record *rec = (const struct reftable_obj_record *)r; reftable_buf_reset(dest); - reftable_buf_add(dest, rec->hash_prefix, rec->hash_prefix_len); + return reftable_buf_add(dest, rec->hash_prefix, rec->hash_prefix_len); } static void reftable_obj_record_release(void *rec) @@ -664,19 +664,27 @@ static struct reftable_record_vtable reftable_obj_record_vtable = { .cmp = &reftable_obj_record_cmp_void, }; -static void reftable_log_record_key(const void *r, struct reftable_buf *dest) +static int reftable_log_record_key(const void *r, struct reftable_buf *dest) { const struct reftable_log_record *rec = (const struct reftable_log_record *)r; - int len = strlen(rec->refname); + int len = strlen(rec->refname), err; uint8_t i64[8]; uint64_t ts = 0; + reftable_buf_reset(dest); - reftable_buf_add(dest, (uint8_t *)rec->refname, len + 1); + err = reftable_buf_add(dest, (uint8_t *)rec->refname, len + 1); + if (err < 0) + return err; ts = (~ts) - rec->update_index; put_be64(&i64[0], ts); - reftable_buf_add(dest, i64, sizeof(i64)); + + err = reftable_buf_add(dest, i64, sizeof(i64)); + if (err < 0) + return err; + + return 0; } static int reftable_log_record_copy_from(void *rec, const void *src_rec, @@ -1027,11 +1035,11 @@ static struct reftable_record_vtable reftable_log_record_vtable = { .cmp = &reftable_log_record_cmp_void, }; -static void reftable_index_record_key(const void *r, struct reftable_buf *dest) +static int reftable_index_record_key(const void *r, struct reftable_buf *dest) { const struct reftable_index_record *rec = r; reftable_buf_reset(dest); - reftable_buf_add(dest, rec->last_key.buf, rec->last_key.len); + return reftable_buf_add(dest, rec->last_key.buf, rec->last_key.len); } static int reftable_index_record_copy_from(void *rec, const void *src_rec, @@ -1124,9 +1132,9 @@ static struct reftable_record_vtable reftable_index_record_vtable = { .cmp = &reftable_index_record_cmp, }; -void reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest) +int reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest) { - reftable_record_vtable(rec)->key(reftable_record_data(rec), dest); + return reftable_record_vtable(rec)->key(reftable_record_data(rec), dest); } int reftable_record_encode(struct reftable_record *rec, struct string_view dest, diff --git a/reftable/record.h b/reftable/record.h index 271da3bf360..25aa908c859 100644 --- a/reftable/record.h +++ b/reftable/record.h @@ -40,7 +40,7 @@ int put_var_int(struct string_view *dest, uint64_t val); /* Methods for records. */ struct reftable_record_vtable { /* encode the key of to a uint8_t reftable_buf. */ - void (*key)(const void *rec, struct reftable_buf *dest); + int (*key)(const void *rec, struct reftable_buf *dest); /* The record type of ('r' for ref). */ uint8_t type; @@ -137,7 +137,7 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ); /* see struct record_vtable */ int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b); int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size); -void reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest); +int reftable_record_key(struct reftable_record *rec, struct reftable_buf *dest); int reftable_record_copy_from(struct reftable_record *rec, struct reftable_record *src, int hash_size); uint8_t reftable_record_val_type(struct reftable_record *rec); diff --git a/reftable/writer.c b/reftable/writer.c index da6941a78ac..377db709c85 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -249,7 +249,10 @@ static int writer_add_record(struct reftable_writer *w, struct reftable_buf key = REFTABLE_BUF_INIT; int err; - reftable_record_key(rec, &key); + err = reftable_record_key(rec, &key); + if (err < 0) + goto done; + if (reftable_buf_cmp(&w->last_key, &key) >= 0) { err = REFTABLE_API_ERROR; goto done; From patchwork Thu Oct 17 04:54:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13839470 Received: from fout-a3-smtp.messagingengine.com (fout-a3-smtp.messagingengine.com [103.168.172.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF3A674C08 for ; Thu, 17 Oct 2024 04:54:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.146 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140862; cv=none; b=swygjdUKqV66EERgZpCZnGRjpXddr/HucwkX6y2Jfzghj1BPdhNfaXTbtCfAAwWBJzZqs4iKBT5m2WFx/E8UtKcU0/RnKNwBQ7u4NVhUVvDWXjzdyCfNA59PGfnJCUzS4mwhBhetFwx9gdFLhqSlCtfSBoZb5N/2GViDdTXzs+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140862; c=relaxed/simple; bh=pnZ+8cnrtHyjXJsVHGt4zJxPPAUciETXEp6XDmF03dY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=g8AyHM5JBeOmFXkJh9DApiE1BEZ5nJRah3hNVmTYwlNXECdHIxAi5WQDUwV4xz4M339DZ436UA8RcUV7IBcRB8lLSWhaUSvTL2cFmrh+hHZ6W/MXd3ceMwP3yJnrjNwzyOIMcb4bgJB944mtmHie/P9aDTc+GzaCWG7kiSMsgZQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=NCoKfF+v; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=azllkzH8; arc=none smtp.client-ip=103.168.172.146 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="NCoKfF+v"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="azllkzH8" Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id B2E0D1380206; Thu, 17 Oct 2024 00:54:16 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Thu, 17 Oct 2024 00:54:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1729140856; x=1729227256; bh=9qWytnkCCj hhyP1p88Jg5gSJ31mfebKrVsggkFABoss=; b=NCoKfF+v1S1ZxM2WilZY5iScGT HUm8hJSsgfHkACzHIDDAh06TJhq6WeaG+Mgov6YNlZ1PW6u9UsMfWiyx8LX1dmhj 8ikQxX3j9g6wdL3xvM0DFQmKfkJsexZUl1GmtTrI7cP/DcJQtqkfvI1KfZ3QlBSN yn0F8BFke1oBtE4rkMLcF0m2WOPKfzDw9qFheaecZ7li+9/xs/39LBEO2rZlBbXq L67yb1rBINQPqKMnYaxRQ//0wzJu0fiNh5PLqXpKAn4BOqT8nMZGXnpyZKlVAMsi 8EbtJ9HPkrkethTU5rI01zhn3iarYoEuzkLeXs9v4FchdKED/ESELnYmmbBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1729140856; x=1729227256; bh=9qWytnkCCjhhyP1p88Jg5gSJ31mf ebKrVsggkFABoss=; b=azllkzH8vvT/djNcNTlzPjIvetoXJSD1z5ltAIvJD4TG ztz4mttt5oeohlnBv+g64c2v8VM5HuYd9yl14sIbVV0IqgJwtMSZSsFymLxpN8EF I1dxUMIpytsLM0UEVsGEgKhQ/T5WCVi/Q0Irq6zTLqzfWcdI4H2/wWSkwtIwlFav UNFojdNXUTSv7Rzxnvs6BMSANA2ZIIKCrTKHkR7tISs33KyO3JAXRA8nUYHR1XQb QdP6+s0Qy89wgAanvXqfWLL9/RLwSJPy8HD/NHzjnFufuho5G75YUBAHKWwfINda HhDNH8Ov/wJUA4s7EHzDrJjO33ofun3qiPKJH4xurw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehtddgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepvdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohephedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepmhgvsehtthgrhihlohhrrhdrtghomhdprhgtphhtth hopehshhgvjhhirghluhhosehgmhgrihhlrdgtohhmpdhrtghpthhtohepghhithesvhhg vghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegvthhhohhmshhonhesvggufigrrh guthhhohhmshhonhdrtghomhdprhgtphhtthhopehkrghrthhhihhkrddukeeksehgmhgr ihhlrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 17 Oct 2024 00:54:15 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 90d901c8 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 17 Oct 2024 04:52:56 +0000 (UTC) Date: Thu, 17 Oct 2024 06:54:13 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , karthik nayak , Taylor Blau , shejialuo Subject: [PATCH v3 09/10] reftable/stack: adapt `stack_filename()` to handle allocation failures Message-ID: <245a428842a9ef4dfe3d8a9d5b566876ee359cf7.1729140565.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The `stack_filename()` function cannot pass any errors to the caller as it has a `void` return type. Adapt it and its callers such that we can handle errors and start handling allocation failures. There are two interesting edge cases in `reftable_stack_destroy()` and `reftable_addition_close()`. Both of these are trying to tear down their respective structures, and while doing so they try to unlink some of the tables they have been keeping alive. Any earlier attempts to do that may fail on Windows because it keeps us from deleting such tables while they are still open, and thus we re-try on close. It's okay and even expected that this can fail when the tables are still open by another process, so we handle the allocation failures gracefully and just skip over any file whose name we couldn't figure out. Signed-off-by: Patrick Steinhardt --- reftable/stack.c | 62 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index e94eb3c4685..243b10715cc 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -31,13 +31,16 @@ 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 reftable_buf *dest, struct reftable_stack *st, - const char *name) +static int stack_filename(struct reftable_buf *dest, struct reftable_stack *st, + const char *name) { + int err; reftable_buf_reset(dest); - reftable_buf_addstr(dest, st->reftable_dir); - reftable_buf_addstr(dest, "/"); - reftable_buf_addstr(dest, name); + if ((err = reftable_buf_addstr(dest, st->reftable_dir)) < 0 || + (err = reftable_buf_addstr(dest, "/")) < 0 || + (err = reftable_buf_addstr(dest, name)) < 0) + return err; + return 0; } static ssize_t reftable_fd_write(void *arg, const void *data, size_t sz) @@ -211,13 +214,16 @@ void reftable_stack_destroy(struct reftable_stack *st) struct reftable_buf filename = REFTABLE_BUF_INIT; for (i = 0; i < st->readers_len; i++) { const char *name = reader_name(st->readers[i]); + int try_unlinking = 1; + reftable_buf_reset(&filename); if (names && !has_name(names, name)) { - stack_filename(&filename, st, name); + if (stack_filename(&filename, st, name) < 0) + try_unlinking = 0; } reftable_reader_decref(st->readers[i]); - if (filename.len) { + if (try_unlinking && filename.len) { /* On Windows, can only unlink after closing. */ unlink(filename.buf); } @@ -310,7 +316,10 @@ static int reftable_stack_reload_once(struct reftable_stack *st, if (!rd) { struct reftable_block_source src = { NULL }; - stack_filename(&table_path, st, name); + + err = stack_filename(&table_path, st, name); + if (err < 0) + goto done; err = reftable_block_source_from_file(&src, table_path.buf); @@ -341,7 +350,11 @@ static int reftable_stack_reload_once(struct reftable_stack *st, for (i = 0; i < cur_len; i++) { if (cur[i]) { const char *name = reader_name(cur[i]); - stack_filename(&table_path, st, name); + + err = stack_filename(&table_path, st, name); + if (err < 0) + goto done; + reftable_reader_decref(cur[i]); unlink(table_path.buf); } @@ -700,8 +713,8 @@ static void reftable_addition_close(struct reftable_addition *add) size_t i; for (i = 0; i < add->new_tables_len; i++) { - stack_filename(&nm, add->stack, add->new_tables[i]); - unlink(nm.buf); + if (!stack_filename(&nm, add->stack, add->new_tables[i])) + unlink(nm.buf); reftable_free(add->new_tables[i]); add->new_tables[i] = NULL; } @@ -851,7 +864,9 @@ int reftable_addition_add(struct reftable_addition *add, if (err < 0) goto done; - stack_filename(&temp_tab_file_name, add->stack, next_name.buf); + err = stack_filename(&temp_tab_file_name, add->stack, next_name.buf); + if (err < 0) + goto done; reftable_buf_addstr(&temp_tab_file_name, ".temp.XXXXXX"); tab_file = mks_tempfile(temp_tab_file_name.buf); @@ -900,7 +915,10 @@ int reftable_addition_add(struct reftable_addition *add, if (err < 0) goto done; reftable_buf_addstr(&next_name, ".ref"); - stack_filename(&tab_file_name, add->stack, next_name.buf); + + err = stack_filename(&tab_file_name, add->stack, next_name.buf); + if (err < 0) + goto done; /* On windows, this relies on rand() picking a unique destination name. @@ -954,7 +972,9 @@ static int stack_compact_locked(struct reftable_stack *st, if (err < 0) goto done; - stack_filename(&tab_file_path, st, next_name.buf); + err = stack_filename(&tab_file_path, st, next_name.buf); + if (err < 0) + goto done; reftable_buf_addstr(&tab_file_path, ".temp.XXXXXX"); tab_file = mks_tempfile(tab_file_path.buf); @@ -1174,7 +1194,9 @@ static int stack_compact_range(struct reftable_stack *st, } for (i = last + 1; i > first; i--) { - stack_filename(&table_name, st, reader_name(st->readers[i - 1])); + err = stack_filename(&table_name, st, reader_name(st->readers[i - 1])); + if (err < 0) + goto done; err = hold_lock_file_for_update(&table_locks[nlocks], table_name.buf, LOCK_NO_DEREF); @@ -1383,7 +1405,10 @@ static int stack_compact_range(struct reftable_stack *st, goto done; reftable_buf_addstr(&new_table_name, ".ref"); - stack_filename(&new_table_path, st, new_table_name.buf); + + err = stack_filename(&new_table_path, st, new_table_name.buf); + if (err < 0) + goto done; err = rename_tempfile(&new_table, new_table_path.buf); if (err < 0) { @@ -1677,7 +1702,10 @@ static void remove_maybe_stale_table(struct reftable_stack *st, uint64_t max, struct reftable_block_source src = { NULL }; struct reftable_reader *rd = NULL; struct reftable_buf table_path = REFTABLE_BUF_INIT; - stack_filename(&table_path, st, name); + + err = stack_filename(&table_path, st, name); + if (err < 0) + goto done; err = reftable_block_source_from_file(&src, table_path.buf); if (err < 0) From patchwork Thu Oct 17 04:54:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13839471 Received: from fhigh-a8-smtp.messagingengine.com (fhigh-a8-smtp.messagingengine.com [103.168.172.159]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 528D01791ED for ; Thu, 17 Oct 2024 04:54:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.159 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140863; cv=none; b=NyOmYJthko2BzCA0AI9jde1Vd07mSWjtwxoVfurKkVjohnKV6KPzWIyBnsi36Z9AP4W2rJq4uGI0ZYtGYdigX+986uNffA/+OYQqGeJhjrTxoznWiZQJUjCxXTFSdee6Zdo/lo5Lt5bGmsinwbaPuOBbJrHeK84vEFIjFIMJ83E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729140863; c=relaxed/simple; bh=6PHEAvtfJFxdMuyDG85PAJ9josKKmu5k2W9jcnifMAs=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MkBrMLU8k20K5eJ86m6tAD9mNbiLSkT8Uo/xGcqSLcNiQrmcRDd1er8LIEapCnRLgQkmJCxBE8zZXIhAjcfx8jn/jSlssx1VetZVVzMAFMMI0fuPdMmBubpGByZFvFda9dSodihBKpLDdEldUZOVDg3XrIpff/Sjlmc0QGO47IA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=LNri4VTL; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=W5fMfj3R; arc=none smtp.client-ip=103.168.172.159 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="LNri4VTL"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="W5fMfj3R" Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfhigh.phl.internal (Postfix) with ESMTP id 48E6D11400E5; Thu, 17 Oct 2024 00:54:19 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-12.internal (MEProxy); Thu, 17 Oct 2024 00:54:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1729140859; x=1729227259; bh=JNQMxtczMa mQmbIrVRPxTPBRxkkdRAV1+uCS6ECpsNo=; b=LNri4VTLpeT7TODUXMBZVqH3Nl jISVPGsmFGbRSVSpKVR2uNLFtcSykOrR8eiqTRcy8uq4+z8PZ/3mmPp7n/LmLBFx E6pcAN1q2xkKohKFtX8PDcC8qCF7PkKmVfHdW22cktS4fnNfJHQNEgthdsDmZH0u bwAz19Rj036F8nQymF9L5SULVK+UgrhGptdZ/TEKCjsOsSquw4Z6fHO9Pe0WIYBV HFSylgG4KWgL28J0LJoWNo1IDJs2YdlQi48ANvWoMBM3hYDc+MpZeYZwZ4CfJPzh 1BCjJxX5vrGukBqyWE2d/2td/nFEMQTVisxpRAc1c8WCUBiE4guaTBGxad5A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1729140859; x=1729227259; bh=JNQMxtczMamQmbIrVRPxTPBRxkkd RAV1+uCS6ECpsNo=; b=W5fMfj3RSEqL3y9XU5RTJMUJe1hONuPSt3Qn7h9bA/jf E3VQmIa62y/0aWybQekHIg4Fzjj6c52IA+HkNNBU6TrU3tJv9nTwSkucbreqrLgL Y4KvV8QWS1pxVixhj3lC1rf/Yg5RV1uuYqjo/DQFGn42lDQ/lGfnYrNxplqotfLz pjnnu/eHhZrWYmiyZ6iTMS1LWYwu+RFL7B4K/La4ECXB4YatuLzkdnTHw4yS8pBu tXcFItiKvUAPefVLFAQwAjyOy9PEbbMgQxYiFFJcbQ+md1RY2FUM7jicxsY4Ovc1 /qA9n+4bF+G0tiRpmXWvb81uqBOZzISM+/PL5aerdQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehtddgkeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohephedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomhhsoh hnrdgtohhmpdhrtghpthhtohepkhgrrhhthhhikhdrudekkeesghhmrghilhdrtghomhdp rhgtphhtthhopehgihhtsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepmh gvsehtthgrhihlohhrrhdrtghomhdprhgtphhtthhopehshhgvjhhirghluhhosehgmhgr ihhlrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 17 Oct 2024 00:54:17 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 8525b988 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 17 Oct 2024 04:52:59 +0000 (UTC) Date: Thu, 17 Oct 2024 06:54:16 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , karthik nayak , Taylor Blau , shejialuo Subject: [PATCH v3 10/10] reftable: handle trivial `reftable_buf` errors Message-ID: <4b51ea4b628d298f894d120162e7e7959898892b.1729140565.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Convert the reftable library such that we handle failures with the new `reftable_buf` interfaces. Signed-off-by: Patrick Steinhardt --- reftable/block.c | 9 +++++++-- reftable/iter.c | 5 ++++- reftable/reader.c | 5 ++++- reftable/record.c | 32 +++++++++++++++++++++++-------- reftable/stack.c | 49 +++++++++++++++++++++++++++++++---------------- reftable/writer.c | 48 +++++++++++++++++++++++++++++++++------------- 6 files changed, 107 insertions(+), 41 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 697b8b41531..f5b432566a6 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -40,7 +40,9 @@ int footer_size(int version) static int block_writer_register_restart(struct block_writer *w, int n, int is_restart, struct reftable_buf *key) { - int rlen = w->restart_len; + int rlen, err; + + rlen = w->restart_len; if (rlen >= MAX_RESTARTS) { is_restart = 0; } @@ -60,7 +62,10 @@ static int block_writer_register_restart(struct block_writer *w, int n, w->next += n; reftable_buf_reset(&w->last_key); - reftable_buf_add(&w->last_key, key->buf, key->len); + err = reftable_buf_add(&w->last_key, key->buf, key->len); + if (err < 0) + return err; + w->entries++; return 0; } diff --git a/reftable/iter.c b/reftable/iter.c index 6c193fd31a9..86e801ca9fb 100644 --- a/reftable/iter.c +++ b/reftable/iter.c @@ -197,7 +197,10 @@ int indexed_table_ref_iter_new(struct indexed_table_ref_iter **dest, *itr = empty; itr->r = r; - reftable_buf_add(&itr->oid, oid, oid_len); + + err = reftable_buf_add(&itr->oid, oid, oid_len); + if (err < 0) + goto out; itr->offsets = offsets; itr->offset_len = offset_len; diff --git a/reftable/reader.c b/reftable/reader.c index ab89efd9c55..90dc950b577 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -769,7 +769,10 @@ static int reftable_reader_refs_for_unindexed(struct reftable_reader *r, } *filter = empty; - reftable_buf_add(&filter->oid, oid, oid_len); + err = reftable_buf_add(&filter->oid, oid, oid_len); + if (err < 0) + goto out; + iterator_from_table_iter(&filter->it, ti); iterator_from_filtering_ref_iterator(it, filter); diff --git a/reftable/record.c b/reftable/record.c index 672c5f909a9..fb5652ed575 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -102,7 +102,9 @@ static int decode_string(struct reftable_buf *dest, struct string_view in) { int start_len = in.len; uint64_t tsize = 0; - int n = get_var_int(&tsize, &in); + int n, err; + + n = get_var_int(&tsize, &in); if (n <= 0) return -1; string_view_consume(&in, n); @@ -110,7 +112,10 @@ static int decode_string(struct reftable_buf *dest, struct string_view in) return -1; reftable_buf_reset(dest); - reftable_buf_add(dest, in.buf, tsize); + err = reftable_buf_add(dest, in.buf, tsize); + if (err < 0) + return err; + string_view_consume(&in, tsize); return start_len - in.len; @@ -189,7 +194,7 @@ int reftable_decode_key(struct reftable_buf *last_key, uint8_t *extra, int start_len = in.len; uint64_t prefix_len = 0; uint64_t suffix_len = 0; - int n; + int err, n; n = reftable_decode_keylen(in, &prefix_len, &suffix_len, extra); if (n < 0) @@ -200,8 +205,14 @@ int reftable_decode_key(struct reftable_buf *last_key, uint8_t *extra, prefix_len > last_key->len) return -1; - reftable_buf_setlen(last_key, prefix_len); - reftable_buf_add(last_key, in.buf, suffix_len); + err = reftable_buf_setlen(last_key, prefix_len); + if (err < 0) + return err; + + err = reftable_buf_add(last_key, in.buf, suffix_len); + if (err < 0) + return err; + string_view_consume(&in, suffix_len); return start_len - in.len; @@ -1047,9 +1058,12 @@ static int reftable_index_record_copy_from(void *rec, const void *src_rec, { struct reftable_index_record *dst = rec; const struct reftable_index_record *src = src_rec; + int err; reftable_buf_reset(&dst->last_key); - reftable_buf_add(&dst->last_key, src->last_key.buf, src->last_key.len); + err = reftable_buf_add(&dst->last_key, src->last_key.buf, src->last_key.len); + if (err < 0) + return err; dst->offset = src->offset; return 0; @@ -1090,10 +1104,12 @@ static int reftable_index_record_decode(void *rec, struct reftable_buf key, { struct string_view start = in; struct reftable_index_record *r = rec; - int n = 0; + int err, n = 0; reftable_buf_reset(&r->last_key); - reftable_buf_add(&r->last_key, key.buf, key.len); + err = reftable_buf_add(&r->last_key, key.buf, key.len); + if (err < 0) + return err; n = get_var_int(&r->offset, &in); if (n < 0) diff --git a/reftable/stack.c b/reftable/stack.c index 243b10715cc..c33979536ef 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -78,8 +78,9 @@ int reftable_new_stack(struct reftable_stack **dest, const char *dir, *dest = NULL; reftable_buf_reset(&list_file_name); - reftable_buf_addstr(&list_file_name, dir); - reftable_buf_addstr(&list_file_name, "/tables.list"); + if ((err = reftable_buf_addstr(&list_file_name, dir)) < 0 || + (err = reftable_buf_addstr(&list_file_name, "/tables.list")) < 0) + goto out; p->list_file = reftable_buf_detach(&list_file_name); p->list_fd = -1; @@ -747,12 +748,14 @@ int reftable_addition_commit(struct reftable_addition *add) goto done; for (i = 0; i < add->stack->merged->readers_len; i++) { - reftable_buf_addstr(&table_list, add->stack->readers[i]->name); - reftable_buf_addstr(&table_list, "\n"); + if ((err = reftable_buf_addstr(&table_list, add->stack->readers[i]->name)) < 0 || + (err = reftable_buf_addstr(&table_list, "\n")) < 0) + goto done; } for (i = 0; i < add->new_tables_len; i++) { - reftable_buf_addstr(&table_list, add->new_tables[i]); - reftable_buf_addstr(&table_list, "\n"); + if ((err = reftable_buf_addstr(&table_list, add->new_tables[i])) < 0 || + (err = reftable_buf_addstr(&table_list, "\n")) < 0) + goto done; } err = write_in_full(lock_file_fd, table_list.buf, table_list.len); @@ -867,7 +870,10 @@ int reftable_addition_add(struct reftable_addition *add, err = stack_filename(&temp_tab_file_name, add->stack, next_name.buf); if (err < 0) goto done; - reftable_buf_addstr(&temp_tab_file_name, ".temp.XXXXXX"); + + err = reftable_buf_addstr(&temp_tab_file_name, ".temp.XXXXXX"); + if (err < 0) + goto done; tab_file = mks_tempfile(temp_tab_file_name.buf); if (!tab_file) { @@ -914,7 +920,10 @@ int reftable_addition_add(struct reftable_addition *add, err = format_name(&next_name, wr->min_update_index, wr->max_update_index); if (err < 0) goto done; - reftable_buf_addstr(&next_name, ".ref"); + + err = reftable_buf_addstr(&next_name, ".ref"); + if (err < 0) + goto done; err = stack_filename(&tab_file_name, add->stack, next_name.buf); if (err < 0) @@ -975,7 +984,10 @@ static int stack_compact_locked(struct reftable_stack *st, err = stack_filename(&tab_file_path, st, next_name.buf); if (err < 0) goto done; - reftable_buf_addstr(&tab_file_path, ".temp.XXXXXX"); + + err = reftable_buf_addstr(&tab_file_path, ".temp.XXXXXX"); + if (err < 0) + goto done; tab_file = mks_tempfile(tab_file_path.buf); if (!tab_file) { @@ -1404,7 +1416,9 @@ static int stack_compact_range(struct reftable_stack *st, if (err < 0) goto done; - reftable_buf_addstr(&new_table_name, ".ref"); + err = reftable_buf_addstr(&new_table_name, ".ref"); + if (err < 0) + goto done; err = stack_filename(&new_table_path, st, new_table_name.buf); if (err < 0) @@ -1423,16 +1437,19 @@ static int stack_compact_range(struct reftable_stack *st, * simply skip writing it. */ for (i = 0; i < first_to_replace; i++) { - reftable_buf_addstr(&tables_list_buf, names[i]); - reftable_buf_addstr(&tables_list_buf, "\n"); + if ((err = reftable_buf_addstr(&tables_list_buf, names[i])) < 0 || + (err = reftable_buf_addstr(&tables_list_buf, "\n")) < 0) + goto done; } if (!is_empty_table) { - reftable_buf_addstr(&tables_list_buf, new_table_name.buf); - reftable_buf_addstr(&tables_list_buf, "\n"); + if ((err = reftable_buf_addstr(&tables_list_buf, new_table_name.buf)) < 0 || + (err = reftable_buf_addstr(&tables_list_buf, "\n")) < 0) + goto done; } for (i = last_to_replace + 1; names[i]; i++) { - reftable_buf_addstr(&tables_list_buf, names[i]); - reftable_buf_addstr(&tables_list_buf, "\n"); + if ((err = reftable_buf_addstr(&tables_list_buf, names[i])) < 0 || + (err = reftable_buf_addstr(&tables_list_buf, "\n")) < 0) + goto done; } err = write_in_full(get_lock_file_fd(&tables_list_lock), diff --git a/reftable/writer.c b/reftable/writer.c index 377db709c85..fd136794d5a 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -217,6 +217,7 @@ static int writer_index_hash(struct reftable_writer *w, struct reftable_buf *has node = tree_search(w->obj_index_tree, &want, &obj_index_tree_node_compare); if (!node) { struct obj_index_tree_node empty = OBJ_INDEX_TREE_NODE_INIT; + int err; key = reftable_malloc(sizeof(*key)); if (!key) @@ -225,7 +226,9 @@ static int writer_index_hash(struct reftable_writer *w, struct reftable_buf *has *key = empty; reftable_buf_reset(&key->hash); - reftable_buf_add(&key->hash, hash->buf, hash->len); + err = reftable_buf_add(&key->hash, hash->buf, hash->len); + if (err < 0) + return err; tree_insert(&w->obj_index_tree, key, &obj_index_tree_node_compare); } else { @@ -259,7 +262,10 @@ static int writer_add_record(struct reftable_writer *w, } reftable_buf_reset(&w->last_key); - reftable_buf_add(&w->last_key, key.buf, key.len); + err = reftable_buf_add(&w->last_key, key.buf, key.len); + if (err < 0) + goto done; + if (!w->block_writer) { err = writer_reinit_block_writer(w, reftable_record_type(rec)); if (err < 0) @@ -334,8 +340,10 @@ int reftable_writer_add_ref(struct reftable_writer *w, goto out; if (!w->opts.skip_index_objects && reftable_ref_record_val1(ref)) { - reftable_buf_add(&buf, (char *)reftable_ref_record_val1(ref), - hash_size(w->opts.hash_id)); + err = reftable_buf_add(&buf, (char *)reftable_ref_record_val1(ref), + hash_size(w->opts.hash_id)); + if (err < 0) + goto out; err = writer_index_hash(w, &buf); if (err < 0) @@ -344,8 +352,10 @@ int reftable_writer_add_ref(struct reftable_writer *w, if (!w->opts.skip_index_objects && reftable_ref_record_val2(ref)) { reftable_buf_reset(&buf); - reftable_buf_add(&buf, reftable_ref_record_val2(ref), - hash_size(w->opts.hash_id)); + err = reftable_buf_add(&buf, reftable_ref_record_val2(ref), + hash_size(w->opts.hash_id)); + if (err < 0) + goto out; err = writer_index_hash(w, &buf); if (err < 0) @@ -407,17 +417,27 @@ int reftable_writer_add_log(struct reftable_writer *w, input_log_message = log->value.update.message; if (!w->opts.exact_log_message && log->value.update.message) { - reftable_buf_addstr(&cleaned_message, log->value.update.message); + err = reftable_buf_addstr(&cleaned_message, log->value.update.message); + if (err < 0) + goto done; + while (cleaned_message.len && - cleaned_message.buf[cleaned_message.len - 1] == '\n') - reftable_buf_setlen(&cleaned_message, - cleaned_message.len - 1); + cleaned_message.buf[cleaned_message.len - 1] == '\n') { + err = reftable_buf_setlen(&cleaned_message, + cleaned_message.len - 1); + if (err < 0) + goto done; + } if (strchr(cleaned_message.buf, '\n')) { /* multiple lines not allowed. */ err = REFTABLE_API_ERROR; goto done; } - reftable_buf_addstr(&cleaned_message, "\n"); + + err = reftable_buf_addstr(&cleaned_message, "\n"); + if (err < 0) + goto done; + log->value.update.message = cleaned_message.buf; } @@ -781,8 +801,10 @@ static int writer_flush_nonempty_block(struct reftable_writer *w) index_record.offset = w->next; reftable_buf_reset(&index_record.last_key); - reftable_buf_add(&index_record.last_key, w->block_writer->last_key.buf, - w->block_writer->last_key.len); + err = reftable_buf_add(&index_record.last_key, w->block_writer->last_key.buf, + w->block_writer->last_key.len); + if (err < 0) + return err; w->index[w->index_len] = index_record; w->index_len++;