diff options
author | Chee Yang Lee <chee.yang.lee@intel.com> | 2019-11-15 09:58:48 +0800 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2019-11-21 23:08:19 +0000 |
commit | 3555766ea866e66b738382c95a2f3d82ef066996 (patch) | |
tree | badd960e96d6b64d4617906c81fe64688c6da775 /scripts/lib/wic/engine.py | |
parent | bdfab842eb31edc26d3f343cce6b1a9a37ba829e (diff) | |
download | poky-3555766ea866e66b738382c95a2f3d82ef066996.tar.gz |
wic: rm with -r flag support
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]
(From OE-Core rev: 5cb7a329d0aaac8fe5328eb2001692c540aa5ade)
Signed-off-by: Chee Yang Lee <chee.yang.lee@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/wic/engine.py')
-rw-r--r-- | scripts/lib/wic/engine.py | 62 |
1 files changed, 45 insertions, 17 deletions
diff --git a/scripts/lib/wic/engine.py b/scripts/lib/wic/engine.py index 18776fa8a0..7e6620747d 100644 --- a/scripts/lib/wic/engine.py +++ b/scripts/lib/wic/engine.py | |||
@@ -19,6 +19,7 @@ import os | |||
19 | import tempfile | 19 | import tempfile |
20 | import json | 20 | import json |
21 | import subprocess | 21 | import subprocess |
22 | import re | ||
22 | 23 | ||
23 | from collections import namedtuple, OrderedDict | 24 | from collections import namedtuple, OrderedDict |
24 | from distutils.spawn import find_executable | 25 | from distutils.spawn import find_executable |
@@ -335,25 +336,52 @@ class Disk: | |||
335 | exec_cmd(cmd, as_shell=True) | 336 | exec_cmd(cmd, as_shell=True) |
336 | self._put_part_image(pnum) | 337 | self._put_part_image(pnum) |
337 | 338 | ||
338 | def remove(self, pnum, path): | 339 | def remove_ext(self, pnum, path, recursive): |
340 | """ | ||
341 | Remove files/dirs and their contents from the partition. | ||
342 | This only applies to ext* partition. | ||
343 | """ | ||
344 | abs_path = re.sub('\/\/+', '/', path) | ||
345 | cmd = "{} {} -wR 'rm \"{}\"'".format(self.debugfs, | ||
346 | self._get_part_image(pnum), | ||
347 | abs_path) | ||
348 | out = exec_cmd(cmd , as_shell=True) | ||
349 | for line in out.splitlines(): | ||
350 | if line.startswith("rm:"): | ||
351 | if "file is a directory" in line: | ||
352 | if recursive: | ||
353 | # loop through content and delete them one by one if | ||
354 | # flaged with -r | ||
355 | subdirs = iter(self.dir(pnum, abs_path).splitlines()) | ||
356 | next(subdirs) | ||
357 | for subdir in subdirs: | ||
358 | dir = subdir.split(':')[1].split(" ", 1)[1] | ||
359 | if not dir == "." and not dir == "..": | ||
360 | self.remove_ext(pnum, "%s/%s" % (abs_path, dir), recursive) | ||
361 | |||
362 | rmdir_out = exec_cmd("{} {} -wR 'rmdir \"{}\"'".format(self.debugfs, | ||
363 | self._get_part_image(pnum), | ||
364 | abs_path.rstrip('/')) | ||
365 | , as_shell=True) | ||
366 | |||
367 | for rmdir_line in rmdir_out.splitlines(): | ||
368 | if "directory not empty" in rmdir_line: | ||
369 | raise WicError("Could not complete operation: \n%s \n" | ||
370 | "use -r to remove non-empty directory" % rmdir_line) | ||
371 | if rmdir_line.startswith("rmdir:"): | ||
372 | raise WicError("Could not complete operation: \n%s " | ||
373 | "\n%s" % (str(line), rmdir_line)) | ||
374 | |||
375 | else: | ||
376 | raise WicError("Could not complete operation: \n%s " | ||
377 | "\nUnable to remove %s" % (str(line), abs_path)) | ||
378 | |||
379 | def remove(self, pnum, path, recursive): | ||
339 | """Remove files/dirs from the partition.""" | 380 | """Remove files/dirs from the partition.""" |
340 | partimg = self._get_part_image(pnum) | 381 | partimg = self._get_part_image(pnum) |
341 | if self.partitions[pnum].fstype.startswith('ext'): | 382 | if self.partitions[pnum].fstype.startswith('ext'): |
342 | cmd = "{} {} -wR 'rm {}'".format(self.debugfs, | 383 | self.remove_ext(pnum, path, recursive) |
343 | self._get_part_image(pnum), | 384 | |
344 | path) | ||
345 | out = exec_cmd(cmd , as_shell=True) | ||
346 | for line in out.splitlines(): | ||
347 | if line.startswith("rm:"): | ||
348 | if "file is a directory" in line: | ||
349 | # Try rmdir to see if this is an empty directory. This won't delete | ||
350 | # any non empty directory so let user know about any error that this might | ||
351 | # generate. | ||
352 | print(exec_cmd("{} {} -wR 'rmdir {}'".format(self.debugfs, | ||
353 | self._get_part_image(pnum), | ||
354 | path), as_shell=True)) | ||
355 | else: | ||
356 | raise WicError("Could not complete operation: wic %s" % str(line)) | ||
357 | else: # fat | 385 | else: # fat |
358 | cmd = "{} -i {} ::{}".format(self.mdel, partimg, path) | 386 | cmd = "{} -i {} ::{}".format(self.mdel, partimg, path) |
359 | try: | 387 | try: |
@@ -535,7 +563,7 @@ def wic_rm(args, native_sysroot): | |||
535 | partitioned image. | 563 | partitioned image. |
536 | """ | 564 | """ |
537 | disk = Disk(args.path.image, native_sysroot) | 565 | disk = Disk(args.path.image, native_sysroot) |
538 | disk.remove(args.path.part, args.path.path) | 566 | disk.remove(args.path.part, args.path.path, args.recursive_delete) |
539 | 567 | ||
540 | def wic_write(args, native_sysroot): | 568 | def wic_write(args, native_sysroot): |
541 | """ | 569 | """ |