]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[cpio] Allow for the construction of pure directories
authorMichael Brown <mcb30@ipxe.org>
Wed, 12 Mar 2025 14:25:05 +0000 (14:25 +0000)
committerMichael Brown <mcb30@ipxe.org>
Wed, 12 Mar 2025 14:32:41 +0000 (14:32 +0000)
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>
src/core/cpio.c
src/tests/cpio_test.c

index 63ac28ee6f199a093b80834137309d367c794ee6..9f5d772e9ac56850867c5799c325082b663af057 100644 (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 ) );
index 3498c0f954f8fbc02e9781bfd6d0d0f1294acf8d..7eb8b2c74ca95d6ece841367c8cb363f7f34e5a2 100644 (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 );
 }