summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/filemap.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/wic/filemap.py')
-rw-r--r--scripts/lib/wic/filemap.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/scripts/lib/wic/filemap.py b/scripts/lib/wic/filemap.py
index 1d04328322..f8b2ba71bb 100644
--- a/scripts/lib/wic/filemap.py
+++ b/scripts/lib/wic/filemap.py
@@ -529,3 +529,33 @@ def filemap(image, log=None):
529 return FilemapFiemap(image, log) 529 return FilemapFiemap(image, log)
530 except ErrorNotSupp: 530 except ErrorNotSupp:
531 return FilemapSeek(image, log) 531 return FilemapSeek(image, log)
532
533def sparse_copy(src_fname, dst_fname, offset=0, skip=0):
534 """Efficiently copy sparse file to or into another file."""
535 fmap = filemap(src_fname)
536 try:
537 dst_file = open(dst_fname, 'r+b')
538 except IOError:
539 dst_file = open(dst_fname, 'wb')
540
541 for first, last in fmap.get_mapped_ranges(0, fmap.blocks_cnt):
542 start = first * fmap.block_size
543 end = (last + 1) * fmap.block_size
544
545 if start < skip < end:
546 start = skip
547
548 fmap._f_image.seek(start, os.SEEK_SET)
549 dst_file.seek(offset + start, os.SEEK_SET)
550
551 chunk_size = 1024 * 1024
552 to_read = end - start
553 read = 0
554
555 while read < to_read:
556 if read + chunk_size > to_read:
557 chunk_size = to_read - read
558 chunk = fmap._f_image.read(chunk_size)
559 dst_file.write(chunk)
560 read += chunk_size
561 dst_file.close()