From 9993a1c0fc143b1390227340beb04abd765f50eb Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 26 Sep 2018 23:31:48 +0200 Subject: [PATCH] Add --append-to kernel(7) multiboot module option. --- kernel/initrd.cpp | 24 +++++++++++++++++++----- share/man/man7/kernel.7 | 11 +++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/kernel/initrd.cpp b/kernel/initrd.cpp index 3955e039..f4e8d5a4 100644 --- a/kernel/initrd.cpp +++ b/kernel/initrd.cpp @@ -703,9 +703,10 @@ static int ExtractTo_mkdir(Ref desc, ioctx_t* ctx, static void ExtractTo(Ref desc, struct initrd_context* ctx, - const char* path) + const char* path, + int extra_oflags) { - int oflags = O_WRITE | O_CREATE | O_TRUNC; + int oflags = O_WRITE | O_CREATE | extra_oflags; Ref file(desc->open(&ctx->ioctx, path, oflags, 0644)); if ( !file && errno == ENOENT ) { @@ -718,9 +719,16 @@ static void ExtractTo(Ref desc, file = desc->open(&ctx->ioctx, path, oflags, 0644); } if ( !file ) + { + if ( errno == EEXIST && (oflags & O_EXCL) ) + return; PanicF("%s: %m", path); - if ( file->truncate(&ctx->ioctx, ctx->initrd_size) != 0 ) - PanicF("truncate: %s: %m", path); + } + if ( !(oflags & O_APPEND) ) + { + if ( file->truncate(&ctx->ioctx, ctx->initrd_size) != 0 ) + PanicF("truncate: %s: %m", path); + } size_t sofar = 0; while ( sofar < ctx->initrd_size ) { @@ -771,7 +779,13 @@ static void ExtractModule(struct multiboot_mod_list* module, if ( !strncmp(cmdline, "--to ", strlen("--to ")) || !strncmp(cmdline, "--to=", strlen("--to=")) ) - ExtractTo(desc, ctx, cmdline + strlen("--to ")); + ExtractTo(desc, ctx, cmdline + strlen("--to "), O_TRUNC); + else if ( !strncmp(cmdline, "--append-to ", strlen("--append-to ")) || + !strncmp(cmdline, "--append-to=", strlen("--append-to=")) ) + ExtractTo(desc, ctx, cmdline + strlen("--append-to "), O_APPEND); + else if ( !strncmp(cmdline, "--create-to ", strlen("--create-to ")) || + !strncmp(cmdline, "--create-to=", strlen("--create-to=")) ) + ExtractTo(desc, ctx, cmdline + strlen("--create-to "), O_EXCL); else if ( sizeof(struct initrd_superblock) <= ctx->initrd_size && !memcmp(ctx->initrd, "sortix-initrd-2", strlen("sortix-initrd-2")) ) ExtractInitrd(desc, ctx); diff --git a/share/man/man7/kernel.7 b/share/man/man7/kernel.7 index c95bfc92..198c4895 100644 --- a/share/man/man7/kernel.7 +++ b/share/man/man7/kernel.7 @@ -78,6 +78,17 @@ binary package. .Pp Each multiboot module has its own command line where the options are as follows: .Bl -tag -width "12345678" +.It Fl \-append-to Ns "=" Ns Ar file +Append the contents of the module to the specifed +.Ar file , +creating it with mode 644 if it doesn't exist. +Non-existent parent directories are created with mode 755 as needed. +.It Fl \-create-to Ns "=" Ns Ar file +Create the specified +.Ar file +with mode 644 with contents of the module, but only if it doesn't already exist. +No action is taken if the file already existed. +Non-existent parent directories are created with mode 755 as needed. .It Fl \-random-seed The module contains random data used to seed the kernel entropy gathering. This file is supposed to contain 256 bytes of secret randomness that hasn't been