Skip to content

Commit 83ae558

Browse files
authored
Merge pull request #1229 from mulkieran/fix-used-free-fs-size-bug
Add SizeTriple class; remove size_triple function
2 parents 552e4f4 + dcaf205 commit 83ae558

File tree

4 files changed

+83
-41
lines changed

4 files changed

+83
-41
lines changed

src/stratis_cli/_actions/_formatting.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
# isort: THIRDPARTY
2424
from dbus import Struct
25-
from justbytes import Range
2625
from wcwidth import wcswidth
2726

2827
# placeholder for tables where a desired value was not obtained from stratisd
@@ -36,26 +35,6 @@
3635
TOTAL_USED_FREE = "Total / Used / Free"
3736

3837

39-
def size_triple(size: Range, used: Optional[Range]) -> str:
40-
"""
41-
Given size and used, return a properly formatted string Total/ Used / Free
42-
43-
:param size: total size
44-
:type size: Range or NoneType
45-
:param used: total amount used
46-
:type used: Range or NoneType
47-
:rtype: str
48-
:returns: formatted string for display
49-
"""
50-
free = None if used is None else size - used
51-
52-
return (
53-
f"{size} / "
54-
f"{TABLE_FAILURE_STRING if used is None else used} / "
55-
f"{TABLE_FAILURE_STRING if free is None else free}"
56-
)
57-
58-
5938
def get_property(prop: Struct, to_repr: Callable, default: Optional[Any]):
6039
"""
6140
Get a representation of an optional D-Bus property. An optional

src/stratis_cli/_actions/_list_filesystem.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626

2727
from ._connection import get_object
2828
from ._constants import TOP_OBJECT
29-
from ._formatting import TOTAL_USED_FREE, get_property, print_table, size_triple
29+
from ._formatting import (
30+
TABLE_FAILURE_STRING,
31+
TOTAL_USED_FREE,
32+
get_property,
33+
print_table,
34+
)
35+
from ._utils import SizeTriple
3036

3137

3238
def list_filesystems(
@@ -133,7 +139,18 @@ def filesystem_size_quartet(
133139
:returns: a string a formatted string showing all three values
134140
:rtype: str
135141
"""
136-
return f'{size_triple(total, used)} / {"None" if limit is None else limit}'
142+
size_triple = SizeTriple(total, used)
143+
triple_str = " / ".join(
144+
(
145+
TABLE_FAILURE_STRING if x is None else str(x)
146+
for x in (
147+
size_triple.total(),
148+
size_triple.used(),
149+
size_triple.free(),
150+
)
151+
)
152+
)
153+
return f'{triple_str} / {"None" if limit is None else limit}'
137154

138155
tables = [
139156
(
@@ -176,8 +193,7 @@ def display(self):
176193

177194
fs = self.filesystems_with_props[0]
178195

179-
total = Range(fs.Size())
180-
used = get_property(fs.Used(), Range, None)
196+
size_triple = SizeTriple(Range(fs.Size()), get_property(fs.Used(), Range, None))
181197
limit = get_property(fs.SizeLimit(), Range, None)
182198
created = (
183199
date_parser.isoparse(fs.Created()).astimezone().strftime("%b %d %Y %H:%M")
@@ -199,8 +215,14 @@ def display(self):
199215
print(f" Revert scheduled: {scheduled}")
200216
print()
201217
print("Sizes:")
202-
print(f" Logical size of thin device: {total}")
203-
print(f" Total used (including XFS metadata): {used}")
204-
print(f" Free: {total - used}")
218+
print(f" Logical size of thin device: {size_triple.total()}")
219+
print(
220+
" Total used (including XFS metadata): "
221+
f"{TABLE_FAILURE_STRING if size_triple.used() is None else size_triple.used()}"
222+
)
223+
print(
224+
" Free: "
225+
f"{TABLE_FAILURE_STRING if size_triple.free() is None else size_triple.free()}"
226+
)
205227
print()
206228
print(f" Size Limit: {'None' if limit is None else limit}")

src/stratis_cli/_actions/_list_pool.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@
3939
TOTAL_USED_FREE,
4040
get_property,
4141
print_table,
42-
size_triple,
4342
)
4443
from ._utils import (
4544
EncryptionInfoClevis,
4645
EncryptionInfoKeyDescription,
4746
PoolFeature,
47+
SizeTriple,
4848
StoppedPool,
4949
fetch_stopped_pools_property,
5050
)
@@ -399,19 +399,19 @@ def _print_detail_view(
399399
else:
400400
print("Encryption Enabled: No")
401401

402-
total_physical_used = get_property(mopool.TotalPhysicalUsed(), Range, None)
402+
size_triple = SizeTriple(
403+
Range(mopool.TotalPhysicalSize()),
404+
get_property(mopool.TotalPhysicalUsed(), Range, None),
405+
)
403406

404407
print(f"Fully Allocated: {'Yes' if mopool.NoAllocSpace() else 'No'}")
405-
print(f" Size: {Range(mopool.TotalPhysicalSize())}")
408+
print(f" Size: {size_triple.total()}")
406409
print(f" Allocated: {Range(mopool.AllocatedSize())}")
407-
408-
total_physical_used = get_property(mopool.TotalPhysicalUsed(), Range, None)
409-
total_physical_used_str = (
410-
TABLE_FAILURE_STRING if total_physical_used is None else total_physical_used
410+
print(
411+
" Used: "
412+
f"{TABLE_FAILURE_STRING if size_triple.used() is None else size_triple.used()}"
411413
)
412414

413-
print(f" Used: {total_physical_used_str}")
414-
415415
def display(self):
416416
"""
417417
List a single pool in detail.
@@ -471,9 +471,21 @@ def physical_size_triple(mopool):
471471
:returns: a string to display in the resulting list output
472472
:rtype: str
473473
"""
474-
total_physical_size = Range(mopool.TotalPhysicalSize())
475-
total_physical_used = get_property(mopool.TotalPhysicalUsed(), Range, None)
476-
return size_triple(total_physical_size, total_physical_used)
474+
size_triple = SizeTriple(
475+
Range(mopool.TotalPhysicalSize()),
476+
get_property(mopool.TotalPhysicalUsed(), Range, None),
477+
)
478+
479+
return " / ".join(
480+
(
481+
TABLE_FAILURE_STRING if x is None else str(x)
482+
for x in (
483+
size_triple.total(),
484+
size_triple.used(),
485+
size_triple.free(),
486+
)
487+
)
488+
)
477489

478490
def properties_string(mopool):
479491
"""

src/stratis_cli/_actions/_utils.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
import sys
2424
import termios
2525
from enum import Enum
26-
from typing import Any, Tuple
26+
from typing import Any, Optional, Tuple
2727
from uuid import UUID
2828

2929
# isort: THIRDPARTY
3030
from dbus import Dictionary, Struct
3131
from dbus.proxies import ProxyObject
32+
from justbytes import Range
3233

3334
from .._errors import (
3435
StratisCliKeyfileNotFoundError,
@@ -266,3 +267,31 @@ def fetch_stopped_pools_property(proxy: ProxyObject) -> Dictionary:
266267
from ._data import Manager
267268

268269
return Manager.Properties.StoppedPools.Get(proxy)
270+
271+
272+
class SizeTriple:
273+
"""
274+
Manage values in a size triple.
275+
"""
276+
277+
def __init__(self, total: Range, used: Optional[Range]):
278+
self._total = total
279+
self._used = used
280+
281+
def total(self) -> Range:
282+
"""
283+
Total.
284+
"""
285+
return self._total
286+
287+
def used(self) -> Optional[Range]:
288+
"""
289+
Used.
290+
"""
291+
return self._used
292+
293+
def free(self) -> Optional[Range]:
294+
"""
295+
Total - used.
296+
"""
297+
return None if self._used is None else self._total - self._used

0 commit comments

Comments
 (0)