[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 <mcb30@ipxe.org>
pull/1165/merge
Michael Brown 2025-03-12 14:25:05 +00:00
parent d6ee9a9242
commit da3024d257
2 changed files with 21 additions and 6 deletions

View File

@ -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 ) );

View File

@ -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 );
}