Pino Toscano
2016-Feb-04 10:26 UTC
[Libguestfs] [PATCH v2 1/2] daemon: glob: add optarg to control trailing slash for dirs
Add a new optional bool "directoryslash" to indicate whether the caller wants trailing slashes in names of directories, defaulting to true (the current behaviour); this helps with interoperability with other tools (such as rm). Related to RHBZ#1293271. --- daemon/glob.c | 11 +++++++++-- generator/actions.ml | 21 ++++++++++++++++----- gobject/Makefile.inc | 2 ++ po/POTFILES | 1 + 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/daemon/glob.c b/daemon/glob.c index 45fb30f..a22fd33 100644 --- a/daemon/glob.c +++ b/daemon/glob.c @@ -26,14 +26,21 @@ #include "actions.h" char ** -do_glob_expand (const char *pattern) +do_glob_expand (const char *pattern, int directoryslash) { int r; glob_t buf = { .gl_pathc = 0, .gl_pathv = NULL, .gl_offs = 0 }; + int flags = GLOB_BRACE | GLOB_MARK; + + /* GLOB_MARK is default, unless the user explicitly disabled it. */ + if ((optargs_bitmask & GUESTFS_GLOB_EXPAND_DIRECTORYSLASH_BITMASK) + && !directoryslash) { + flags &= ~GLOB_MARK; + } /* glob(3) in glibc never calls chdir, so this seems to be safe: */ CHROOT_IN; - r = glob (pattern, GLOB_MARK|GLOB_BRACE, NULL, &buf); + r = glob (pattern, flags, NULL, &buf); CHROOT_OUT; if (r == GLOB_NOMATCH) { /* Return an empty list instead of an error. */ diff --git a/generator/actions.ml b/generator/actions.ml index 0755fef..36b8680 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -5969,27 +5969,34 @@ See also: C<guestfs_command_lines>" }; * start with "/". There is no concept of "cwd" in libguestfs, * hence no "."-relative names. *) - style = RStringList "paths", [Pathname "pattern"], []; + style = RStringList "paths", [Pathname "pattern"], [OBool "directoryslash"]; proc_nr = Some 113; + once_had_no_optargs = true; tests = [ InitScratchFS, Always, TestResult ( [["mkdir_p"; "/glob_expand/b/c"]; ["touch"; "/glob_expand/b/c/d"]; ["touch"; "/glob_expand/b/c/e"]; - ["glob_expand"; "/glob_expand/b/c/*"]], + ["glob_expand"; "/glob_expand/b/c/*"; ""]], "is_string_list (ret, 2, \"/glob_expand/b/c/d\", \"/glob_expand/b/c/e\")"), []; InitScratchFS, Always, TestResult ( [["mkdir_p"; "/glob_expand2/b/c"]; ["touch"; "/glob_expand2/b/c/d"]; ["touch"; "/glob_expand2/b/c/e"]; - ["glob_expand"; "/glob_expand2/*/c/*"]], + ["glob_expand"; "/glob_expand2/*/c/*"; ""]], "is_string_list (ret, 2, \"/glob_expand2/b/c/d\", \"/glob_expand2/b/c/e\")"), []; InitScratchFS, Always, TestResult ( [["mkdir_p"; "/glob_expand3/b/c"]; ["touch"; "/glob_expand3/b/c/d"]; ["touch"; "/glob_expand3/b/c/e"]; - ["glob_expand"; "/glob_expand3/*/x/*"]], - "is_string_list (ret, 0)"), [] + ["glob_expand"; "/glob_expand3/*/x/*"; ""]], + "is_string_list (ret, 0)"), []; + InitScratchFS, Always, TestResult ( + [["mkdir_p"; "/glob_expand4/b/c"]; + ["touch"; "/glob_expand4/b1"]; + ["touch"; "/glob_expand4/c1"]; + ["glob_expand"; "/glob_expand4/b*"; "false"]], + "is_string_list (ret, 2, \"/glob_expand4/b\", \"/glob_expand4/b1\")"), []; ]; shortdesc = "expand a wildcard path"; longdesc = "\ @@ -6004,6 +6011,10 @@ It is just a wrapper around the C L<glob(3)> function with flags C<GLOB_MARK|GLOB_BRACE>. See that manual page for more details. +C<directoryslash> controls whether use the C<GLOB_MARK> flag for +L<glob(3)>, and it defaults to true. It can be explicitly set as +off to return no trailing slashes in filenames of directories. + Notice that there is no equivalent command for expanding a device name (eg. F</dev/sd*>). Use C<guestfs_list_devices>, C<guestfs_list_partitions> etc functions instead." }; diff --git a/gobject/Makefile.inc b/gobject/Makefile.inc index 3522eb2..5ba0fc9 100644 --- a/gobject/Makefile.inc +++ b/gobject/Makefile.inc @@ -68,6 +68,7 @@ guestfs_gobject_headers= \ include/guestfs-gobject/optargs-disk_create.h \ include/guestfs-gobject/optargs-e2fsck.h \ include/guestfs-gobject/optargs-fstrim.h \ + include/guestfs-gobject/optargs-glob_expand.h \ include/guestfs-gobject/optargs-grep.h \ include/guestfs-gobject/optargs-hivex_open.h \ include/guestfs-gobject/optargs-inspect_get_icon.h \ @@ -154,6 +155,7 @@ guestfs_gobject_sources= \ src/optargs-disk_create.c \ src/optargs-e2fsck.c \ src/optargs-fstrim.c \ + src/optargs-glob_expand.c \ src/optargs-grep.c \ src/optargs-hivex_open.c \ src/optargs-inspect_get_icon.c \ diff --git a/po/POTFILES b/po/POTFILES index aca174d..2a1e313 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -195,6 +195,7 @@ gobject/src/optargs-cpio_out.c gobject/src/optargs-disk_create.c gobject/src/optargs-e2fsck.c gobject/src/optargs-fstrim.c +gobject/src/optargs-glob_expand.c gobject/src/optargs-grep.c gobject/src/optargs-hivex_open.c gobject/src/optargs-inspect_get_icon.c -- 2.5.0
Pino Toscano
2016-Feb-04 10:26 UTC
[Libguestfs] [PATCH v2 2/2] customize: add globbing for --delete
Support globbing in paths passed to --delete, telling glob to not return directories with leading slash. This re-adds back globbing for --delete in virt-sysprep, which was available before the integration with common code from virt-customize. --- customize/customize_run.ml | 2 +- generator/customize.ml | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/customize/customize_run.ml b/customize/customize_run.ml index 84f634b..82c4edd 100644 --- a/customize/customize_run.ml +++ b/customize/customize_run.ml @@ -191,7 +191,7 @@ exec >>%s 2>&1 | `Delete path -> message (f_"Deleting: %s") path; - g#rm_rf path + Array.iter g#rm_rf (g#glob_expand ~directoryslash:false path) | `Edit (path, expr) -> message (f_"Editing: %s") path; diff --git a/generator/customize.ml b/generator/customize.ml index 19aec81..d272ffb 100644 --- a/generator/customize.ml +++ b/generator/customize.ml @@ -115,6 +115,12 @@ Wildcards cannot be used."; Delete a file from the guest. Or delete a directory (and all its contents, recursively). +You can use shell glob characters in the specified path. Be careful +to escape glob characters from the host shell, if that is required. +For example: + + virt-customize --delete '/var/log/*.log'. + See also: I<--upload>, I<--scrub>."; }; -- 2.5.0
Richard W.M. Jones
2016-Feb-04 11:35 UTC
Re: [Libguestfs] [PATCH v2 2/2] customize: add globbing for --delete
On Thu, Feb 04, 2016 at 11:26:52AM +0100, Pino Toscano wrote:> Support globbing in paths passed to --delete, telling glob to not > return directories with leading slash. > > This re-adds back globbing for --delete in virt-sysprep, which was > available before the integration with common code from virt-customize. > --- > customize/customize_run.ml | 2 +- > generator/customize.ml | 6 ++++++ > 2 files changed, 7 insertions(+), 1 deletion(-) > > diff --git a/customize/customize_run.ml b/customize/customize_run.ml > index 84f634b..82c4edd 100644 > --- a/customize/customize_run.ml > +++ b/customize/customize_run.ml > @@ -191,7 +191,7 @@ exec >>%s 2>&1 > > | `Delete path -> > message (f_"Deleting: %s") path; > - g#rm_rf path > + Array.iter g#rm_rf (g#glob_expand ~directoryslash:false path) > > | `Edit (path, expr) -> > message (f_"Editing: %s") path; > diff --git a/generator/customize.ml b/generator/customize.ml > index 19aec81..d272ffb 100644 > --- a/generator/customize.ml > +++ b/generator/customize.ml > @@ -115,6 +115,12 @@ Wildcards cannot be used."; > Delete a file from the guest. Or delete a directory (and all its > contents, recursively). > > +You can use shell glob characters in the specified path. Be careful > +to escape glob characters from the host shell, if that is required. > +For example: > + > + virt-customize --delete '/var/log/*.log'. > + > See also: I<--upload>, I<--scrub>."; > };ACK series. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Possibly Parallel Threads
- [PATCH 2/3] daemon: glob: add option for not returning dirs with trailing slash
- [PATCH 1/3] sysprep, get-kernel: explicit the Guestfs parameter
- [PATCH 1/2] daemon: glob: do not return directories with trailing slash
- [PATCH v2 2/2] customize: add globbing for --delete
- [PATCH 3/3] customize: add globbing for --delete