Openembedded Core Discussions
 help / color / mirror / Atom feed
* [PATCH 1/2] selftest/wic: test wic rm with -r flag
@ 2019-11-15  1:58 chee.yang.lee
  2019-11-15  1:58 ` [PATCH 2/2] wic: rm with -r flag support chee.yang.lee
  0 siblings, 1 reply; 4+ messages in thread
From: chee.yang.lee @ 2019-11-15  1:58 UTC (permalink / raw)
  To: openembedded-core

From: Chee Yang Lee <chee.yang.lee@intel.com>

selftest to make sure wic rm with -r flag for ext partition
could remove directory and all its content.

Signed-off-by: Chee Yang Lee <chee.yang.lee@intel.com>
---
 meta/lib/oeqa/selftest/cases/wic.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/meta/lib/oeqa/selftest/cases/wic.py b/meta/lib/oeqa/selftest/cases/wic.py
index ea75300..0c03b4b 100644
--- a/meta/lib/oeqa/selftest/cases/wic.py
+++ b/meta/lib/oeqa/selftest/cases/wic.py
@@ -1025,3 +1025,10 @@ class Wic2(WicTestCase):
         # check if it's removed
         result = runCmd("wic ls %s:2/etc/ -n %s" % (images[0], sysroot))
         self.assertTrue('fstab' not in [line.split()[-1] for line in result.output.split('\n') if line])
+
+        # remove non-empty directory
+        runCmd("wic rm -r %s:2/etc/ -n %s" % (images[0], sysroot))
+
+        # check if it's removed
+        result = runCmd("wic ls %s:2/ -n %s" % (images[0], sysroot))
+        self.assertTrue('etc' not in [line.split()[-1] for line in result.output.split('\n') if line])
-- 
2.7.4



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] wic: rm with -r flag support
  2019-11-15  1:58 [PATCH 1/2] selftest/wic: test wic rm with -r flag chee.yang.lee
@ 2019-11-15  1:58 ` chee.yang.lee
  2019-11-15 11:47   ` Richard Purdie
  0 siblings, 1 reply; 4+ messages in thread
From: chee.yang.lee @ 2019-11-15  1:58 UTC (permalink / raw)
  To: openembedded-core

From: Chee Yang Lee <chee.yang.lee@intel.com>

wic currently unable to remove non-empty directory in ext* partition.
enable wic rm to remove non-empty directory and all the sub-content
with -r flag.
update help documents for 'wic rm'.
[YOCTO #12404]

Signed-off-by: Chee Yang Lee <chee.yang.lee@intel.com>
---
 scripts/lib/wic/engine.py | 62 ++++++++++++++++++++++++++++++++++-------------
 scripts/lib/wic/help.py   |  4 +++
 scripts/wic               |  3 +++
 3 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/scripts/lib/wic/engine.py b/scripts/lib/wic/engine.py
index 18776fa..7e66207 100644
--- a/scripts/lib/wic/engine.py
+++ b/scripts/lib/wic/engine.py
@@ -19,6 +19,7 @@ import os
 import tempfile
 import json
 import subprocess
+import re
 
 from collections import namedtuple, OrderedDict
 from distutils.spawn import find_executable
@@ -335,25 +336,52 @@ class Disk:
         exec_cmd(cmd, as_shell=True)
         self._put_part_image(pnum)
 
-    def remove(self, pnum, path):
+    def remove_ext(self, pnum, path, recursive):
+        """
+        Remove files/dirs and their contents from the partition.
+        This only applies to ext* partition.
+        """
+        abs_path = re.sub('\/\/+', '/', path)
+        cmd = "{} {} -wR 'rm \"{}\"'".format(self.debugfs,
+                                            self._get_part_image(pnum),
+                                            abs_path)
+        out = exec_cmd(cmd , as_shell=True)
+        for line in out.splitlines():
+            if line.startswith("rm:"):
+                if "file is a directory" in line:
+                    if recursive:
+                        # loop through content and delete them one by one if
+                        # flaged with -r
+                        subdirs = iter(self.dir(pnum, abs_path).splitlines())
+                        next(subdirs)
+                        for subdir in subdirs:
+                            dir = subdir.split(':')[1].split(" ", 1)[1]
+                            if not dir == "." and not dir == "..":
+                                self.remove_ext(pnum, "%s/%s" % (abs_path, dir), recursive)
+
+                    rmdir_out = exec_cmd("{} {} -wR 'rmdir \"{}\"'".format(self.debugfs,
+                                                    self._get_part_image(pnum),
+                                                    abs_path.rstrip('/'))
+                                                    , as_shell=True)
+
+                    for rmdir_line in rmdir_out.splitlines():
+                        if "directory not empty" in rmdir_line:
+                            raise WicError("Could not complete operation: \n%s \n"
+                                            "use -r to remove non-empty directory" % rmdir_line)
+                        if rmdir_line.startswith("rmdir:"):
+                            raise WicError("Could not complete operation: \n%s "
+                                            "\n%s" % (str(line), rmdir_line))
+
+                else:
+                    raise WicError("Could not complete operation: \n%s "
+                                    "\nUnable to remove %s" % (str(line), abs_path))
+
+    def remove(self, pnum, path, recursive):
         """Remove files/dirs from the partition."""
         partimg = self._get_part_image(pnum)
         if self.partitions[pnum].fstype.startswith('ext'):
-            cmd = "{} {} -wR 'rm {}'".format(self.debugfs,
-                                                self._get_part_image(pnum),
-                                                path)
-            out = exec_cmd(cmd , as_shell=True)
-            for line in out.splitlines():
-                if line.startswith("rm:"):
-                    if "file is a directory" in line:
-                        # Try rmdir to see if this is an empty directory. This won't delete
-                        # any non empty directory so let user know about any error that this might
-                        # generate.
-                        print(exec_cmd("{} {} -wR 'rmdir {}'".format(self.debugfs,
-                                                    self._get_part_image(pnum),
-                                                    path), as_shell=True))
-                    else:
-                        raise WicError("Could not complete operation: wic %s" % str(line))
+            self.remove_ext(pnum, path, recursive)
+
         else: # fat
             cmd = "{} -i {} ::{}".format(self.mdel, partimg, path)
             try:
@@ -535,7 +563,7 @@ def wic_rm(args, native_sysroot):
     partitioned image.
     """
     disk = Disk(args.path.image, native_sysroot)
-    disk.remove(args.path.part, args.path.path)
+    disk.remove(args.path.part, args.path.path, args.recursive_delete)
 
 def wic_write(args, native_sysroot):
     """
diff --git a/scripts/lib/wic/help.py b/scripts/lib/wic/help.py
index 968cc0e..812ebe3 100644
--- a/scripts/lib/wic/help.py
+++ b/scripts/lib/wic/help.py
@@ -422,6 +422,7 @@ NAME
 SYNOPSIS
     wic rm <src> <image>:<partition><path>
     wic rm <src> <image>:<partition><path> --native-sysroot <path>
+    wic rm -r <image>:<partition><path>
 
 DESCRIPTION
     This command removes files or directories from the vfat or ext* partition of the
@@ -456,6 +457,9 @@ DESCRIPTION
 
     The -n option is used to specify the path to the native sysroot
     containing the tools(parted and mtools) to use.
+
+    The -r option is used to remove directories and their contents
+    recursively,this only applies to ext* partition.
 """
 
 wic_write_usage = """
diff --git a/scripts/wic b/scripts/wic
index 1a71730..ea61410 100755
--- a/scripts/wic
+++ b/scripts/wic
@@ -403,6 +403,9 @@ def wic_init_parser_rm(subparser):
                         help="path: <image>:<vfat partition><path>")
     subparser.add_argument("-n", "--native-sysroot",
                         help="path to the native sysroot containing the tools")
+    subparser.add_argument("-r", dest="recursive_delete", action="store_true", default=False,
+                        help="remove directories and their contents recursively, "
+                        " this only applies to ext* partition")
 
 def expandtype(rules):
     """
-- 
2.7.4



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] wic: rm with -r flag support
  2019-11-15  1:58 ` [PATCH 2/2] wic: rm with -r flag support chee.yang.lee
@ 2019-11-15 11:47   ` Richard Purdie
  2019-11-15 11:48     ` Richard Purdie
  0 siblings, 1 reply; 4+ messages in thread
From: Richard Purdie @ 2019-11-15 11:47 UTC (permalink / raw)
  To: chee.yang.lee, openembedded-core

On Fri, 2019-11-15 at 09:58 +0800, chee.yang.lee@intel.com wrote:
> From: Chee Yang Lee <chee.yang.lee@intel.com>
> 
> wic currently unable to remove non-empty directory in ext* partition.
> enable wic rm to remove non-empty directory and all the sub-content
> with -r flag.
> update help documents for 'wic rm'.
> [YOCTO #12404]

This kind of new functionality means we need new tests. Could you
resend this with new tests for the new functionality being added
please?

Cheers,

Richard



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] wic: rm with -r flag support
  2019-11-15 11:47   ` Richard Purdie
@ 2019-11-15 11:48     ` Richard Purdie
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Purdie @ 2019-11-15 11:48 UTC (permalink / raw)
  To: chee.yang.lee, openembedded-core

On Fri, 2019-11-15 at 11:47 +0000, Richard Purdie wrote:
> On Fri, 2019-11-15 at 09:58 +0800, chee.yang.lee@intel.com wrote:
> > From: Chee Yang Lee <chee.yang.lee@intel.com>
> > 
> > wic currently unable to remove non-empty directory in ext*
> > partition.
> > enable wic rm to remove non-empty directory and all the sub-content
> > with -r flag.
> > update help documents for 'wic rm'.
> > [YOCTO #12404]
> 
> This kind of new functionality means we need new tests. Could you
> resend this with new tests for the new functionality being added
> please?

Sorry, I see this is in a different patch. The two patches probably
should be together as the tests in one doesn't work without the new
code (or at least needs to come after the new code).

Cheers,

Richard



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-11-15 11:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-11-15  1:58 [PATCH 1/2] selftest/wic: test wic rm with -r flag chee.yang.lee
2019-11-15  1:58 ` [PATCH 2/2] wic: rm with -r flag support chee.yang.lee
2019-11-15 11:47   ` Richard Purdie
2019-11-15 11:48     ` Richard Purdie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox