[Libguestfs] [PATCH nbdkit 1/2] tmpdisk: Generate the default command from a shell script fragment.

Richard W.M. Jones rjones at redhat.com
Tue Apr 7 15:18:43 UTC 2020


On Sat, Apr 04, 2020 at 05:06:31PM +0100, Richard W.M. Jones wrote:
> Neutral refactoring.  Makes the code a bit easier to handle and
> simplifies future commits.
> ---
>  plugins/tmpdisk/Makefile.am           | 17 ++++++++-
>  plugins/tmpdisk/tmpdisk.c             | 22 +++---------
>  plugins/tmpdisk/default-command.sh.in | 51 +++++++++++++++++++++++++++
>  .gitignore                            |  1 +
>  4 files changed, 72 insertions(+), 19 deletions(-)

I pushed this one since it's an uncontroversial simplification of the code.

I'm going to have another go at part 2 of this patch series, since
I've thought about the security implications and think I have a way
that may be safe (will post for review first obviously).

Rich.

> diff --git a/plugins/tmpdisk/Makefile.am b/plugins/tmpdisk/Makefile.am
> index 34061771..20aeb05f 100644
> --- a/plugins/tmpdisk/Makefile.am
> +++ b/plugins/tmpdisk/Makefile.am
> @@ -31,11 +31,26 @@
>  
>  include $(top_srcdir)/common-rules.mk
>  
> -EXTRA_DIST = nbdkit-tmpdisk-plugin.pod
> +EXTRA_DIST = \
> +	default-command.sh.in \
> +	nbdkit-tmpdisk-plugin.pod \
> +	$(NULL)
> +
> +# The default command we use (if we don't use command=) comes from a
> +# shell script which is turned into a C source file.
> +BUILT_SOURCES = default-command.c
> +
> +default-command.c: default-command.sh.in Makefile
> +	rm -f $@ $@-t
> +	echo 'const char *command =' > $@-t
> +	$(SED) -e '/^#/d' -e 's/"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < $< >> $@-t
> +	echo ';' >> $@-t
> +	mv $@-t $@
>  
>  plugin_LTLIBRARIES = nbdkit-tmpdisk-plugin.la
>  
>  nbdkit_tmpdisk_plugin_la_SOURCES = \
> +	default-command.c \
>  	tmpdisk.c \
>  	$(top_srcdir)/include/nbdkit-plugin.h \
>  	$(NULL)
> diff --git a/plugins/tmpdisk/tmpdisk.c b/plugins/tmpdisk/tmpdisk.c
> index a5aacc9d..5e8df151 100644
> --- a/plugins/tmpdisk/tmpdisk.c
> +++ b/plugins/tmpdisk/tmpdisk.c
> @@ -55,24 +55,10 @@ static int64_t size = -1;
>  static const char *label = NULL;
>  static const char *type = "ext4";
>  
> -static const char *command =
> -  "labelopt='-L'\n"
> -  "case \"$type\" in\n"
> -  "    ext?)\n"
> -  "        extra='-F' ;;\n"
> -  "    *fat|msdos)\n"
> -  "        extra='-I' ;;\n"
> -  "    ntfs)\n"
> -  "        extra='-Q -F'\n"
> -  "        labelopt='-n' ;;\n"
> -  "    xfs)\n"
> -  "        extra='-f' ;;\n"
> -  "esac\n"
> -  "if [ \"x$label\" = \"x\" ]; then\n"
> -  "    mkfs -t \"$type\" $extra \"$disk\"\n"
> -  "else\n"
> -  "    mkfs -t \"$type\" $extra $labelopt \"$label\" \"$disk\"\n"
> -  "fi\n";
> +/* This comes from default-command.c which is generated from
> + * default-command.sh.in.
> + */
> +extern const char *command;
>  
>  static void
>  tmpdisk_load (void)
> diff --git a/plugins/tmpdisk/default-command.sh.in b/plugins/tmpdisk/default-command.sh.in
> new file mode 100644
> index 00000000..251e0b7b
> --- /dev/null
> +++ b/plugins/tmpdisk/default-command.sh.in
> @@ -0,0 +1,51 @@
> +# nbdkit
> +# -*- mode: shell-script -*-
> +# Copyright (C) 2017-2020 Red Hat Inc.
> +#
> +# Redistribution and use in source and binary forms, with or without
> +# modification, are permitted provided that the following conditions are
> +# met:
> +#
> +# * Redistributions of source code must retain the above copyright
> +# notice, this list of conditions and the following disclaimer.
> +#
> +# * Redistributions in binary form must reproduce the above copyright
> +# notice, this list of conditions and the following disclaimer in the
> +# documentation and/or other materials provided with the distribution.
> +#
> +# * Neither the name of Red Hat nor the names of its contributors may be
> +# used to endorse or promote products derived from this software without
> +# specific prior written permission.
> +#
> +# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
> +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
> +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
> +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
> +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
> +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
> +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
> +# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
> +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
> +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
> +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
> +# SUCH DAMAGE.
> +
> +labelopt='-L'
> +
> +case "$type" in
> +    ext?)
> +        extra='-F' ;;
> +    *fat|msdos)
> +        extra='-I' ;;
> +    ntfs)
> +        extra='-Q -F'
> +        labelopt='-n' ;;
> +    xfs)
> +        extra='-f' ;;
> +esac
> +
> +if [ "x$label" = "x" ]; then
> +    mkfs -t "$type" $extra "$disk"
> +else
> +    mkfs -t "$type" $extra $labelopt "$label" "$disk"
> +fi
> diff --git a/.gitignore b/.gitignore
> index 4bb035e1..c44fb40d 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -76,6 +76,7 @@ plugins/*/*.3
>  /plugins/rust/Cargo.toml
>  /plugins/rust/target
>  /plugins/tar/nbdkit-tar-plugin
> +/plugins/tmpdisk/default-command.c
>  /podwrapper.pl
>  /server/nbdkit
>  /server/nbdkit.pc
> -- 
> 2.25.0
> 
> _______________________________________________
> Libguestfs mailing list
> Libguestfs at redhat.com
> https://www.redhat.com/mailman/listinfo/libguestfs

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW




More information about the Libguestfs mailing list