From da3024d257d73d2c40e65c6fb9db5a2a6bc86dbb Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Wed, 12 Mar 2025 14:25:05 +0000 Subject: [PATCH] [cpio] Allow for the construction of pure directories Allow for the possibility of creating empty directories (without having to include a dummy file inside the directory) using a zero-length image and a CPIO filename with a trailing slash, such as: initrd emptyfile /usr/share/oem/ Signed-off-by: Michael Brown --- src/core/cpio.c | 11 +++++------ src/tests/cpio_test.c | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/core/cpio.c b/src/core/cpio.c index 63ac28ee6..9f5d772e9 100644 --- a/src/core/cpio.c +++ b/src/core/cpio.c @@ -179,17 +179,16 @@ size_t cpio_header ( struct image *image, unsigned int index, /* Get filename length */ name_len = cpio_name_len ( image, depth ); - /* Calculate mode and length */ - if ( depth < max ) { - /* Directory */ + /* Set directory mode or file mode as appropriate */ + if ( name[name_len] == '/' ) { mode = ( CPIO_MODE_DIR | CPIO_DEFAULT_DIR_MODE ); - len = 0; } else { - /* File */ mode |= CPIO_MODE_FILE; - len = image->len; } + /* Set length on final header */ + len = ( ( depth < max ) ? 0 : image->len ); + /* Construct CPIO header */ memset ( cpio, '0', sizeof ( *cpio ) ); memcpy ( cpio->c_magic, CPIO_MAGIC, sizeof ( cpio->c_magic ) ); diff --git a/src/tests/cpio_test.c b/src/tests/cpio_test.c index 3498c0f95..7eb8b2c74 100644 --- a/src/tests/cpio_test.c +++ b/src/tests/cpio_test.c @@ -221,6 +221,20 @@ CPIO_TEST ( path_mkdir_all, 0x341, "/usr/share/oem/config.ign mkdir=-1", 4, CPIO_HEADER ( "000081a4", "00000341", "0000001a", "/usr/share/oem/config.ign" PAD1 ) ); +/* Simple directory */ +CPIO_TEST ( dir, 0, "/opt/", 1, + CPIO_HEADER ( "000041ed", "00000000", "00000005", + "/opt" PAD2 ) ); + +/* Directory tree */ +CPIO_TEST ( tree, 0, "/opt/oem/scripts/ mkdir=-1", 3, + CPIO_HEADER ( "000041ed", "00000000", "00000005", + "/opt" PAD2 ) + CPIO_HEADER ( "000041ed", "00000000", "00000009", + "/opt/oem" PAD2 ) + CPIO_HEADER ( "000041ed", "00000000", "00000011", + "/opt/oem/scripts" PAD2 ) ); + /* Custom mode */ CPIO_TEST ( mode, 39, "/sbin/init mode=755", 1, CPIO_HEADER ( "000081ed", "00000027", "0000000b", @@ -252,6 +266,8 @@ static void cpio_test_exec ( void ) { cpio_ok ( &path_mkdir_1 ); cpio_ok ( &path_mkdir_2 ); cpio_ok ( &path_mkdir_all ); + cpio_ok ( &dir ); + cpio_ok ( &tree ); cpio_ok ( &mode ); cpio_ok ( &chaos ); }