Skip to contentSkip to conversation

index-pack: use xopen in init_thread

PATCH New René Scharfe 1 participant 1 message Sep 10, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 index-pack: use xopen in init_thread

Support an arbitrary file descriptor expression in the semantic patch for replacing open+die_errno with xopen, not just an identifier, and apply it. This makes the error message at the single affected place more consistent and reduces code duplication.

---
The semantic patch itself gains some duplicate lines in exchange, unfortunately, but I don't how to avoid that.

 builtin/index-pack.c           |  4 +---
 contrib/coccinelle/xopen.cocci | 13 ++++++++-----
 2 files changed, 9 insertions(+), 8 deletions(-)
builtin/index-pack.c +1 -3
index 6cc4890217..738a35c53c 100644
@@ -187,9 +187,7 @@ static void init_thread(void)
 	pthread_key_create(&key, NULL);
 	CALLOC_ARRAY(thread_data, nr_threads);
 	for (i = 0; i < nr_threads; i++) {
-		thread_data[i].pack_fd = open(curr_pack, O_RDONLY);
-		if (thread_data[i].pack_fd == -1)
-			die_errno(_("unable to open %s"), curr_pack);
+		thread_data[i].pack_fd = xopen(curr_pack, O_RDONLY);
 	}
 
 	threads_active = 1;
contrib/coccinelle/xopen.cocci +8 -5
index 814d7b8a1a..b71db67019 100644
@@ -2,15 +2,18 @@
 identifier fd;
 identifier die_fn =~ "^(die|die_errno)$";
 @@
-(
-  fd =
+  int fd =
 - open
 + xopen
   (...);
-|
-  int fd =
+- if ( \( fd < 0 \| fd == -1 \) ) { die_fn(...); }
+
+@@
+expression fd;
+identifier die_fn =~ "^(die|die_errno)$";
+@@
+  fd =
 - open
 + xopen
   (...);
-)
 - if ( \( fd < 0 \| fd == -1 \) ) { die_fn(...); }

--
2.33.0