From b265422bc5c1d9653f925c383a914f07ae61972f Mon Sep 17 00:00:00 2001
From: Matteo Croce <teknoraver@meta.com>
Date: Mon, 4 Nov 2024 01:42:44 +0100
Subject: [PATCH] Missing O_TRUNC flag in rmtcreat()

Commit cd720d0fdcbd changed rmtcreat() to use rmtopen() instead of creat(),
but doesn't pass the O_TRUNC flag. This means that tar would not truncate
an existing archive if the file already exists:

$ dd if=/dev/zero of=test.tar bs=100k count=1
1+0 records in
1+0 records out
102400 bytes (102 kB, 100 KiB) copied, 0.000390167 s, 262 MB/s

$ ll README test.tar
-rw-r--r--. 1 matteo matteo 9.6K Oct 31 00:18 README
-rw-r--r--. 1 matteo matteo 100K Nov  4 01:33 test.tar

$ src/tar -c -f test.tar README

$ ll test.tar
-rw-r--r--. 1 matteo matteo 100K Nov  4 01:33 test.tar

Passing the flag fixes it.
---
 lib/rmt.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/rmt.h b/lib/rmt.h
index d93dd40..868b0c3 100644
--- a/lib/rmt.h
+++ b/lib/rmt.h
@@ -68,7 +68,7 @@ rmtopen (char const *__name, int __flags, mode_t __mode, char const *__command)
 RMT_INLINE int
 rmtcreat (char const *__name, mode_t __mode, char const *__command)
 {
-  return rmtopen (__name, O_CREAT | O_WRONLY, __mode, __command);
+  return rmtopen (__name, O_CREAT | O_WRONLY | O_TRUNC, __mode, __command);
 }
 
 RMT_INLINE ptrdiff_t
-- 
2.47.0

