summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorChee Yang Lee <chee.yang.lee@intel.com>2019-11-15 09:58:48 +0800
committerRichard Purdie <richard.purdie@linuxfoundation.org>2019-11-21 23:08:19 +0000
commit3555766ea866e66b738382c95a2f3d82ef066996 (patch)
treebadd960e96d6b64d4617906c81fe64688c6da775 /scripts
parentbdfab842eb31edc26d3f343cce6b1a9a37ba829e (diff)
downloadpoky-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')
-rw-r--r--scripts/lib/wic/engine.py62
-rw-r--r--scripts/lib/wic/help.py4
-rwxr-xr-xscripts/wic3
3 files changed, 52 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
19import tempfile 19import tempfile
20import json 20import json
21import subprocess 21import subprocess
22import re
22 23
23from collections import namedtuple, OrderedDict 24from collections import namedtuple, OrderedDict
24from distutils.spawn import find_executable 25from 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
540def wic_write(args, native_sysroot): 568def wic_write(args, native_sysroot):
541 """ 569 """
diff --git a/scripts/lib/wic/help.py b/scripts/lib/wic/help.py
index 968cc0ed6f..812ebe3ec8 100644
--- a/scripts/lib/wic/help.py
+++ b/scripts/lib/wic/help.py
@@ -422,6 +422,7 @@ NAME
422SYNOPSIS 422SYNOPSIS
423 wic rm <src> <image>:<partition><path> 423 wic rm <src> <image>:<partition><path>
424 wic rm <src> <image>:<partition><path> --native-sysroot <path> 424 wic rm <src> <image>:<partition><path> --native-sysroot <path>
425 wic rm -r <image>:<partition><path>
425 426
426DESCRIPTION 427DESCRIPTION
427 This command removes files or directories from the vfat or ext* partition of the 428 This command removes files or directories from the vfat or ext* partition of the
@@ -456,6 +457,9 @@ DESCRIPTION
456 457
457 The -n option is used to specify the path to the native sysroot 458 The -n option is used to specify the path to the native sysroot
458 containing the tools(parted and mtools) to use. 459 containing the tools(parted and mtools) to use.
460
461 The -r option is used to remove directories and their contents
462 recursively,this only applies to ext* partition.
459""" 463"""
460 464
461wic_write_usage = """ 465wic_write_usage = """
diff --git a/scripts/wic b/scripts/wic
index 1a717300f5..ea614100c2 100755
--- a/scripts/wic
+++ b/scripts/wic
@@ -403,6 +403,9 @@ def wic_init_parser_rm(subparser):
403 help="path: <image>:<vfat partition><path>") 403 help="path: <image>:<vfat partition><path>")
404 subparser.add_argument("-n", "--native-sysroot", 404 subparser.add_argument("-n", "--native-sysroot",
405 help="path to the native sysroot containing the tools") 405 help="path to the native sysroot containing the tools")
406 subparser.add_argument("-r", dest="recursive_delete", action="store_true", default=False,
407 help="remove directories and their contents recursively, "
408 " this only applies to ext* partition")
406 409
407def expandtype(rules): 410def expandtype(rules):
408 """ 411 """