From: David Disseldorp <ddiss@suse•de>
To: linux-kbuild@vger•kernel.org, linux-fsdevel@vger•kernel.org
Cc: linux-next@vger•kernel.org, David Disseldorp <ddiss@suse•de>
Subject: [PATCH v2 4/7] gen_init_cpio: avoid duplicate strlen calls
Date: Thu, 14 Aug 2025 15:18:02 +1000 [thread overview]
Message-ID: <20250814054818.7266-5-ddiss@suse.de> (raw)
In-Reply-To: <20250814054818.7266-1-ddiss@suse.de>
We determine the filename length for the cpio header, so shouldn't
recalculate it when writing out the filename.
Signed-off-by: David Disseldorp <ddiss@suse•de>
---
usr/gen_init_cpio.c | 40 ++++++++++++++++++++++++----------------
1 file changed, 24 insertions(+), 16 deletions(-)
diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c
index 64421d410a88b..40f4cbd95844e 100644
--- a/usr/gen_init_cpio.c
+++ b/usr/gen_init_cpio.c
@@ -25,6 +25,7 @@
#define str(s) xstr(s)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define CPIO_HDR_LEN 110
+#define CPIO_TRAILER "TRAILER!!!"
#define padlen(_off, _align) (((_align) - ((_off) & ((_align) - 1))) % (_align))
static char padding[512];
@@ -40,9 +41,8 @@ struct file_handler {
int (*handler)(const char *line);
};
-static int push_string(const char *name)
+static int push_buf(const char *name, size_t name_len)
{
- unsigned int name_len = strlen(name) + 1;
ssize_t len;
len = write(outfd, name, name_len);
@@ -69,9 +69,8 @@ static int push_pad(size_t padlen)
return 0;
}
-static int push_rest(const char *name)
+static int push_rest(const char *name, size_t name_len)
{
- unsigned int name_len = strlen(name) + 1;
ssize_t len;
len = write(outfd, name, name_len);
@@ -85,8 +84,8 @@ static int push_rest(const char *name)
static int cpio_trailer(void)
{
- const char name[] = "TRAILER!!!";
int len;
+ unsigned int namesize = sizeof(CPIO_TRAILER);
len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX"
"%08X%08X%08X%08X%08X%08X%08X",
@@ -102,12 +101,12 @@ static int cpio_trailer(void)
0, /* minor */
0, /* rmajor */
0, /* rminor */
- (unsigned)strlen(name)+1, /* namesize */
+ namesize, /* namesize */
0); /* chksum */
offset += len;
if (len != CPIO_HDR_LEN
- || push_rest(name) < 0
+ || push_rest(CPIO_TRAILER, namesize) < 0
|| push_pad(padlen(offset, 512)) < 0)
return -1;
@@ -118,9 +117,12 @@ static int cpio_mkslink(const char *name, const char *target,
unsigned int mode, uid_t uid, gid_t gid)
{
int len;
+ unsigned int namesize, targetsize = strlen(target) + 1;
if (name[0] == '/')
name++;
+ namesize = strlen(name) + 1;
+
len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX"
"%08X%08X%08X%08X%08X%08X%08X",
do_csum ? "070702" : "070701", /* magic */
@@ -130,19 +132,19 @@ static int cpio_mkslink(const char *name, const char *target,
(long) gid, /* gid */
1, /* nlink */
(long) default_mtime, /* mtime */
- (unsigned)strlen(target)+1, /* filesize */
+ targetsize, /* filesize */
3, /* major */
1, /* minor */
0, /* rmajor */
0, /* rminor */
- (unsigned)strlen(name) + 1,/* namesize */
+ namesize, /* namesize */
0); /* chksum */
offset += len;
if (len != CPIO_HDR_LEN
- || push_string(name) < 0
+ || push_buf(name, namesize) < 0
|| push_pad(padlen(offset, 4)) < 0
- || push_string(target) < 0
+ || push_buf(target, targetsize) < 0
|| push_pad(padlen(offset, 4)) < 0)
return -1;
@@ -172,9 +174,12 @@ static int cpio_mkgeneric(const char *name, unsigned int mode,
uid_t uid, gid_t gid)
{
int len;
+ unsigned int namesize;
if (name[0] == '/')
name++;
+ namesize = strlen(name) + 1;
+
len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX"
"%08X%08X%08X%08X%08X%08X%08X",
do_csum ? "070702" : "070701", /* magic */
@@ -189,12 +194,12 @@ static int cpio_mkgeneric(const char *name, unsigned int mode,
1, /* minor */
0, /* rmajor */
0, /* rminor */
- (unsigned)strlen(name) + 1,/* namesize */
+ namesize, /* namesize */
0); /* chksum */
offset += len;
if (len != CPIO_HDR_LEN
- || push_rest(name) < 0)
+ || push_rest(name, namesize) < 0)
return -1;
return 0;
@@ -265,6 +270,7 @@ static int cpio_mknod(const char *name, unsigned int mode,
unsigned int maj, unsigned int min)
{
int len;
+ unsigned int namesize;
if (dev_type == 'b')
mode |= S_IFBLK;
@@ -273,6 +279,8 @@ static int cpio_mknod(const char *name, unsigned int mode,
if (name[0] == '/')
name++;
+ namesize = strlen(name) + 1;
+
len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX"
"%08X%08X%08X%08X%08X%08X%08X",
do_csum ? "070702" : "070701", /* magic */
@@ -287,12 +295,12 @@ static int cpio_mknod(const char *name, unsigned int mode,
1, /* minor */
maj, /* rmajor */
min, /* rminor */
- (unsigned)strlen(name) + 1,/* namesize */
+ namesize, /* namesize */
0); /* chksum */
offset += len;
if (len != CPIO_HDR_LEN
- || push_rest(name) < 0)
+ || push_rest(name, namesize) < 0)
return -1;
return 0;
@@ -426,7 +434,7 @@ static int cpio_mkfile(const char *name, const char *location,
offset += len;
if (len != CPIO_HDR_LEN
- || push_string(name) < 0
+ || push_buf(name, namesize) < 0
|| push_pad(padlen(offset, 4)) < 0)
goto error;
--
2.43.0
next prev parent reply other threads:[~2025-08-14 5:48 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-14 5:17 [PATCH v2 0/7] gen_init_cpio: add copy_file_range / reflink support David Disseldorp
2025-08-14 5:17 ` [PATCH v2 1/7] gen_init_cpio: write to fd instead of stdout stream David Disseldorp
2025-08-18 18:40 ` Nicolas Schier
2025-08-14 5:18 ` [PATCH v2 2/7] gen_init_cpio: support -o <output_path> parameter David Disseldorp
2025-08-18 18:40 ` Nicolas Schier
2025-08-14 5:18 ` [PATCH v2 3/7] gen_init_cpio: attempt copy_file_range for file data David Disseldorp
2025-08-18 18:40 ` Nicolas Schier
2025-08-19 0:20 ` David Disseldorp
2025-08-14 5:18 ` David Disseldorp [this message]
2025-08-14 5:18 ` [PATCH v2 5/7] gen_initramfs.sh: use gen_init_cpio -o parameter David Disseldorp
2025-08-18 18:40 ` Nicolas Schier
2025-08-14 5:18 ` [PATCH v2 6/7] docs: initramfs: file data alignment via name padding David Disseldorp
2025-08-14 5:18 ` [PATCH v2 7/7] gen_init_cpio: add -a <data_align> as reflink optimization David Disseldorp
2025-08-18 19:23 ` Nicolas Schier
2025-08-21 8:10 ` David Disseldorp
2025-08-18 18:40 ` [PATCH v2 0/7] gen_init_cpio: add copy_file_range / reflink support Nicolas Schier
2025-08-18 23:46 ` David Disseldorp
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250814054818.7266-5-ddiss@suse.de \
--to=ddiss@suse$(echo .)de \
--cc=linux-fsdevel@vger$(echo .)kernel.org \
--cc=linux-kbuild@vger$(echo .)kernel.org \
--cc=linux-next@vger$(echo .)kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox