Skip to contentSkip to conversation

selftest/wic: test wic rm with -r flag

PATCH In review [email protected] 2 participants 4 messages Nov 15, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 selftest/wic: test wic rm with -r flag

From: Chee Yang Lee <[email protected]>

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

---
 meta/lib/oeqa/selftest/cases/wic.py | 7 +++++++
 1 file changed, 7 insertions(+)
meta/lib/oeqa/selftest/cases/wic.py +7 -0
index ea75300..0c03b4b 100644
@@ -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])
Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/2 wic: rm with -r flag support

From: Chee Yang Lee <[email protected]>

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]

---
 scripts/lib/wic/engine.py | 62 ++++++++++++++++++++++++++++++++++-------------
 scripts/lib/wic/help.py   |  4 +++
 scripts/wic               |  3 +++
 3 files changed, 52 insertions(+), 17 deletions(-)
scripts/lib/wic/engine.py +45 -17
index 18776fa..7e66207 100644
@@ -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):
     """
scripts/lib/wic/help.py +4 -0
index 968cc0e..812ebe3 100644
@@ -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 = """
scripts/wic +3 -0
index 1a71730..ea61410 100755
@@ -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):
     """
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 2/2] wic: rm with -r flag support

Quoted 7 lines · On Fri, 2019-11-15 at 09:58 +0800, [email protected] wrote:

From: Chee Yang Lee <[email protected]>

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

#3 lore raw

Re: [PATCH 2/2] wic: rm with -r flag support

Quoted 10 lines · Richard Purdie
Quoted 8 lines · On Fri, 2019-11-15 at 09:58 +0800, [email protected] wrote:

From: Chee Yang Lee <[email protected]>

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]

Quoted 3 lines

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