|
|
0386c2 |
From 3176671662bda79d4b4059b8cc22b4b31a6547e0 Mon Sep 17 00:00:00 2001
|
|
|
0386c2 |
From: Maurizio Lombardi <mlombard@redhat.com>
|
|
|
0386c2 |
Date: Fri, 13 Nov 2020 11:44:40 +0100
|
|
|
0386c2 |
Subject: [PATCH] fileio backstore: fix sparse file creation
|
|
|
0386c2 |
|
|
|
0386c2 |
fallocate() can't be used to create sparse files because it
|
|
|
0386c2 |
actually preallocates all the disk space that will be used by the file
|
|
|
0386c2 |
backstore, sparse files do not have preallocated disk space
|
|
|
0386c2 |
by definition.
|
|
|
0386c2 |
We must therefore use ftruncate().
|
|
|
0386c2 |
|
|
|
0386c2 |
We can, on the other hand, use fallocate() to create non-sparse
|
|
|
0386c2 |
files and fall back to the slower "while() fwrite()" if we
|
|
|
0386c2 |
are running on Python version < 3.3 where fallocate() is not available
|
|
|
0386c2 |
|
|
|
0386c2 |
Fixes 3bd4d8ef7c9b154c53e8b8dd863a570bce7f5c2c
|
|
|
0386c2 |
|
|
|
0386c2 |
Signed-off-by: Maurizio Lombardi <mlombard@redhat.com>
|
|
|
0386c2 |
---
|
|
|
0386c2 |
targetcli/ui_backstore.py | 16 ++++++++--------
|
|
|
0386c2 |
1 file changed, 8 insertions(+), 8 deletions(-)
|
|
|
0386c2 |
|
|
|
0386c2 |
diff --git a/targetcli/ui_backstore.py b/targetcli/ui_backstore.py
|
|
|
0386c2 |
index 8692f22..9bc0c58 100644
|
|
|
0386c2 |
--- a/targetcli/ui_backstore.py
|
|
|
0386c2 |
+++ b/targetcli/ui_backstore.py
|
|
|
0386c2 |
@@ -423,17 +423,17 @@ class UIFileIOBackstore(UIBackstore):
|
|
|
0386c2 |
raise ExecutionError("Could not open %s" % filename)
|
|
|
0386c2 |
try:
|
|
|
0386c2 |
if sparse:
|
|
|
0386c2 |
+ os.ftruncate(f.fileno(), size)
|
|
|
0386c2 |
+ else:
|
|
|
0386c2 |
+ self.shell.log.info("Writing %d bytes" % size)
|
|
|
0386c2 |
try:
|
|
|
0386c2 |
+ # Prior to version 3.3, Python does not provide fallocate
|
|
|
0386c2 |
os.posix_fallocate(f.fileno(), 0, size)
|
|
|
0386c2 |
except AttributeError:
|
|
|
0386c2 |
- # Prior to version 3.3, Python does not provide fallocate
|
|
|
0386c2 |
- os.ftruncate(f.fileno(), size)
|
|
|
0386c2 |
- else:
|
|
|
0386c2 |
- self.shell.log.info("Writing %d bytes" % size)
|
|
|
0386c2 |
- while size > 0:
|
|
|
0386c2 |
- write_size = min(size, 1024)
|
|
|
0386c2 |
- f.write("\0" * write_size)
|
|
|
0386c2 |
- size -= write_size
|
|
|
0386c2 |
+ while size > 0:
|
|
|
0386c2 |
+ write_size = min(size, 1024)
|
|
|
0386c2 |
+ f.write("\0" * write_size)
|
|
|
0386c2 |
+ size -= write_size
|
|
|
0386c2 |
except (OSError, IOError):
|
|
|
0386c2 |
os.remove(filename)
|
|
|
0386c2 |
raise ExecutionError("Could not expand file to %d bytes" % size)
|
|
|
0386c2 |
--
|
|
|
0386c2 |
2.28.0
|
|
|
0386c2 |
|