| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
""" |
| 19 |
This module contains miscellaneous collection of things, like the |
| 20 |
:func:`pretty_print` function which provides a quick readable textual dump of |
| 21 |
most of the object types in this library. Besides that, some functions to |
| 22 |
pretty print and parse size strings and some other stuff. |
| 23 |
""" |
| 24 |
|
| 25 |
import btrfs |
| 26 |
import collections.abc |
| 27 |
import re |
| 28 |
import types |
| 29 |
from btrfs.ctree import ( |
| 30 |
BLOCK_GROUP_DATA, BLOCK_GROUP_SYSTEM, BLOCK_GROUP_METADATA, |
| 31 |
SPACE_INFO_GLOBAL_RSV, BLOCK_GROUP_TYPE_MASK, |
| 32 |
BLOCK_GROUP_RAID0, BLOCK_GROUP_RAID1, BLOCK_GROUP_RAID5, |
| 33 |
BLOCK_GROUP_RAID1C3, BLOCK_GROUP_RAID1C4, |
| 34 |
BLOCK_GROUP_RAID6, BLOCK_GROUP_DUP, BLOCK_GROUP_RAID10, |
| 35 |
BLOCK_GROUP_SINGLE, |
| 36 |
BLOCK_GROUP_PROFILE_MASK, |
| 37 |
) |
| 38 |
|
| 39 |
SZ_1K = 0x00000400 |
| 40 |
SZ_2K = 0x00000800 |
| 41 |
SZ_4K = 0x00001000 |
| 42 |
SZ_8K = 0x00002000 |
| 43 |
SZ_16K = 0x00004000 |
| 44 |
SZ_32K = 0x00008000 |
| 45 |
SZ_64K = 0x00010000 |
| 46 |
SZ_128K = 0x00020000 |
| 47 |
SZ_256K = 0x00040000 |
| 48 |
SZ_512K = 0x00080000 |
| 49 |
|
| 50 |
SZ_1M = 0x00100000 |
| 51 |
SZ_2M = 0x00200000 |
| 52 |
SZ_4M = 0x00400000 |
| 53 |
SZ_8M = 0x00800000 |
| 54 |
SZ_16M = 0x01000000 |
| 55 |
SZ_32M = 0x02000000 |
| 56 |
SZ_64M = 0x04000000 |
| 57 |
SZ_128M = 0x08000000 |
| 58 |
SZ_256M = 0x10000000 |
| 59 |
SZ_512M = 0x20000000 |
| 60 |
|
| 61 |
SZ_1G = 0x40000000 |
| 62 |
|
| 63 |
|
| 64 |
def mounted_filesystem_paths(): |
| 65 |
"""Discover mountpoints for online btrfs filesystems |
| 66 |
|
| 67 |
:returns: Filesystem paths where btrfs filesystems are mounted. |
| 68 |
:rtype: List[str] |
| 69 |
""" |
| 70 |
filesystems = {} |
| 71 |
mounts = [line.split() for line in open('/proc/self/mounts', 'r').read().splitlines()] |
| 72 |
for path in [mount[1] for mount in mounts if mount[2] == 'btrfs']: |
| 73 |
with btrfs.ctree.FileSystem(path) as fs: |
| 74 |
filesystems.setdefault(fs.fsid, path) |
| 75 |
return list(filesystems.values()) |
| 76 |
|
| 77 |
|
| 78 |
_space_type_description_map = { |
| 79 |
BLOCK_GROUP_DATA: 'Data', |
| 80 |
BLOCK_GROUP_SYSTEM: 'System', |
| 81 |
BLOCK_GROUP_METADATA: 'Metadata', |
| 82 |
BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA: 'Data+Metadata', |
| 83 |
SPACE_INFO_GLOBAL_RSV: 'GlobalReserve', |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
def space_type_description(flags): |
| 88 |
""" |
| 89 |
:param int flags: Space flags. |
| 90 |
:returns: String representation of the space type only. |
| 91 |
:rtype: str |
| 92 |
|
| 93 |
Example:: |
| 94 |
|
| 95 |
>>> btrfs.utils.space_type_description(0x14) |
| 96 |
'Metadata' |
| 97 |
""" |
| 98 |
return _space_type_description_map.get( |
| 99 |
flags & (BLOCK_GROUP_TYPE_MASK | SPACE_INFO_GLOBAL_RSV), |
| 100 |
'unknown' |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
_space_profile_description_map = { |
| 105 |
BLOCK_GROUP_SINGLE: 'single', |
| 106 |
BLOCK_GROUP_RAID0: 'RAID0', |
| 107 |
BLOCK_GROUP_RAID1: 'RAID1', |
| 108 |
BLOCK_GROUP_RAID1C3: 'RAID1C3', |
| 109 |
BLOCK_GROUP_RAID1C4: 'RAID1C4', |
| 110 |
BLOCK_GROUP_RAID5: 'RAID5', |
| 111 |
BLOCK_GROUP_RAID6: 'RAID6', |
| 112 |
BLOCK_GROUP_DUP: 'DUP', |
| 113 |
BLOCK_GROUP_RAID10: 'RAID10', |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
def space_profile_description(flags): |
| 118 |
""" |
| 119 |
:param int flags: Space flags. |
| 120 |
:returns: String representation of the space profile only. |
| 121 |
:rtype: str |
| 122 |
|
| 123 |
Example:: |
| 124 |
|
| 125 |
>>> btrfs.utils.space_profile_description(0x14) |
| 126 |
'RAID1' |
| 127 |
""" |
| 128 |
return _space_profile_description_map.get( |
| 129 |
flags & BLOCK_GROUP_PROFILE_MASK, |
| 130 |
'unknown' |
| 131 |
) |
| 132 |
|
| 133 |
|
| 134 |
def space_flags_description(flags): |
| 135 |
""" |
| 136 |
:param int flags: Space flags. |
| 137 |
:returns: String representation of the space type and profile. |
| 138 |
:rtype: str |
| 139 |
|
| 140 |
Example:: |
| 141 |
|
| 142 |
>>> btrfs.utils.space_flags_description(0x14) |
| 143 |
'Metadata, RAID1' |
| 144 |
""" |
| 145 |
return "{}, {}".format( |
| 146 |
btrfs.utils.space_type_description(flags), |
| 147 |
btrfs.utils.space_profile_description(flags)) |
| 148 |
|
| 149 |
|
| 150 |
pretty_size_units = '_KMGTPE' |
| 151 |
|
| 152 |
|
| 153 |
def pretty_size(size, unit=None, binary=True): |
| 154 |
""" |
| 155 |
:param int size: Size in bytes. |
| 156 |
:param str unit: Target unit to display the size in. One of 'kMGTPE'. |
| 157 |
:param bool binary: If True, use base 1024, else base 1000. |
| 158 |
|
| 159 |
Example:: |
| 160 |
|
| 161 |
>>> btrfs.utils.pretty_size(1610612736, unit='G') |
| 162 |
'1.50GiB' |
| 163 |
""" |
| 164 |
if unit == '': |
| 165 |
return str(size) |
| 166 |
base = 1024 if binary else 1000 |
| 167 |
if unit is None: |
| 168 |
if size == 0: |
| 169 |
unit = '' |
| 170 |
unit_offset = 0 |
| 171 |
base = 1000 |
| 172 |
else: |
| 173 |
unit_offset = 0 |
| 174 |
tmp = size |
| 175 |
while tmp >= 1024 and unit_offset < len(pretty_size_units) - 1: |
| 176 |
unit_offset = unit_offset + 1 |
| 177 |
tmp = tmp / 1024 |
| 178 |
unit = pretty_size_units[unit_offset] if unit_offset > 0 else '' |
| 179 |
else: |
| 180 |
unit = unit.upper() |
| 181 |
unit_offset = pretty_size_units.index(unit) |
| 182 |
if unit == 'K' and base == 1000: |
| 183 |
unit = 'k' |
| 184 |
divide_by = base ** unit_offset |
| 185 |
if divide_by > 0: |
| 186 |
size = size / divide_by |
| 187 |
return "{0:.2f}{1}{2}B".format(size, unit, 'i' if base == 1024 and unit != '' else '') |
| 188 |
|
| 189 |
|
| 190 |
_re_parse_pretty_size = re.compile(r'^(?P<size>\d+)(?:(?P<unit>[kMGTPE])((?P<i>i)?B)?)?$') |
| 191 |
|
| 192 |
|
| 193 |
def parse_pretty_size(size_str): |
| 194 |
"""Parse pretty printed size strings. |
| 195 |
|
| 196 |
This is the opposite of the :func:`pretty_size` function, but not 100%. |
| 197 |
|
| 198 |
:param str size_str: String literal to be parsed. |
| 199 |
:raises ValueError: If the input cannot be parsed as pretty size. |
| 200 |
|
| 201 |
Example:: |
| 202 |
|
| 203 |
>>> btrfs.utils.parse_pretty_size('234MB') |
| 204 |
234000000 |
| 205 |
>>> btrfs.utils.parse_pretty_size('234MiB') |
| 206 |
245366784 |
| 207 |
>>> btrfs.utils.parse_pretty_size('234TB') |
| 208 |
234000000000000 |
| 209 |
>>> btrfs.utils.parse_pretty_size('234TiB') |
| 210 |
257285720899584 |
| 211 |
>>> btrfs.utils.parse_pretty_size('1048576') |
| 212 |
1048576 |
| 213 |
""" |
| 214 |
match = _re_parse_pretty_size.match(size_str) |
| 215 |
if match is None: |
| 216 |
raise ValueError('literal cannot be parsed as pretty size') |
| 217 |
groupdict = match.groupdict() |
| 218 |
if groupdict['unit'] is None: |
| 219 |
return int(size_str) |
| 220 |
base = 1024 if groupdict['i'] == 'i' else 1000 |
| 221 |
unit_offset = pretty_size_units.index(groupdict['unit'].upper()) |
| 222 |
multiply_by = base ** unit_offset |
| 223 |
return int(groupdict['size']) * multiply_by |
| 224 |
|
| 225 |
|
| 226 |
def flags_str(flags, flags_str_map): |
| 227 |
"""Generic helper to convert flags to a description. |
| 228 |
|
| 229 |
This function is used by more specific helper functions below. You probably |
| 230 |
don't need to call it directly. |
| 231 |
|
| 232 |
:param int flags: Flags. |
| 233 |
:param flags_str_map: Mapping from flag bit value to description. |
| 234 |
:type flags_str_map: dict(int, str) |
| 235 |
""" |
| 236 |
ret = [] |
| 237 |
for flag in sorted(flags_str_map.keys()): |
| 238 |
if flags & flag: |
| 239 |
ret.append(flags_str_map[flag]) |
| 240 |
flags ^= flag |
| 241 |
if flags != 0: |
| 242 |
ret.append("unknown(0x{:0x})".format(flags)) |
| 243 |
elif len(ret) == 0: |
| 244 |
ret.append("none") |
| 245 |
return '|'.join(ret) |
| 246 |
|
| 247 |
|
| 248 |
def block_group_flags_str(flags): |
| 249 |
""" |
| 250 |
:param int flags: Block Group flags. |
| 251 |
:returns: String representation of the block group type and profile. |
| 252 |
:rtype: str |
| 253 |
|
| 254 |
Example:: |
| 255 |
|
| 256 |
>>> btrfs.utils.block_group_flags_str(0x14) |
| 257 |
'METADATA|RAID1' |
| 258 |
""" |
| 259 |
return flags_str(flags, btrfs.ctree._block_group_flags_str_map) |
| 260 |
|
| 261 |
|
| 262 |
def block_group_type_str(flags): |
| 263 |
""" |
| 264 |
:param int flags: Block Group flags. |
| 265 |
:returns: String representation of the block group type only. |
| 266 |
:rtype: str |
| 267 |
|
| 268 |
Example:: |
| 269 |
|
| 270 |
>>> btrfs.utils.block_group_type_str(0x14) |
| 271 |
'METADATA' |
| 272 |
""" |
| 273 |
return block_group_flags_str(flags & BLOCK_GROUP_TYPE_MASK) |
| 274 |
|
| 275 |
|
| 276 |
def block_group_profile_str(flags): |
| 277 |
""" |
| 278 |
:param int flags: Block Group flags. |
| 279 |
:returns: String representation of the block group profile only. |
| 280 |
:rtype: str |
| 281 |
|
| 282 |
Example:: |
| 283 |
|
| 284 |
>>> btrfs.utils.block_group_profile_str(0x14) |
| 285 |
'RAID1' |
| 286 |
""" |
| 287 |
return block_group_flags_str(flags & BLOCK_GROUP_PROFILE_MASK) |
| 288 |
|
| 289 |
|
| 290 |
def extent_flags_str(flags): |
| 291 |
""" |
| 292 |
:param int flags: :class:`~btrfs.ctree.ExtentItem` flags. |
| 293 |
:returns: String representation of the extent item flags. |
| 294 |
:rtype: str |
| 295 |
|
| 296 |
Example:: |
| 297 |
|
| 298 |
>>> btrfs.utils.extent_flags_str(0x102) |
| 299 |
'TREE_BLOCK|FULL_BACKREF' |
| 300 |
""" |
| 301 |
return flags_str(flags, btrfs.ctree._extent_flags_str_map) |
| 302 |
|
| 303 |
|
| 304 |
def inode_mode_str(mode): |
| 305 |
""" |
| 306 |
:param int mode: File mode as number. |
| 307 |
:returns: String representation of file mode in octal format. |
| 308 |
:rtype: str |
| 309 |
|
| 310 |
Example:: |
| 311 |
|
| 312 |
>>> btrfs.utils.inode_mode_str(16877) |
| 313 |
'040755' |
| 314 |
|
| 315 |
""" |
| 316 |
return "0{:05o}".format(mode) |
| 317 |
|
| 318 |
|
| 319 |
def inode_flags_str(flags): |
| 320 |
""" |
| 321 |
:param int flags: :class:`~btrfs.ctree.InodeItem` flags. |
| 322 |
:returns: String representation of the inode item flags. |
| 323 |
:rytpe: str |
| 324 |
|
| 325 |
Example:: |
| 326 |
|
| 327 |
>>> btrfs.utils.inode_flags_str(72) |
| 328 |
'NOCOMPRESS|IMMUTABLE' |
| 329 |
""" |
| 330 |
return flags_str(flags, btrfs.ctree._inode_flags_str_map) |
| 331 |
|
| 332 |
|
| 333 |
def dir_item_type_str(type_): |
| 334 |
""" |
| 335 |
:param int type_: :class:`~btrfs.ctree.DirItem` type. |
| 336 |
:returns: String representation of DirItem type. |
| 337 |
:rtype: str |
| 338 |
|
| 339 |
Example:: |
| 340 |
|
| 341 |
>>> btrfs.utils.dir_item_type_str(4) |
| 342 |
'BLKDEV' |
| 343 |
""" |
| 344 |
return btrfs.ctree._dir_item_type_str_map[type_] |
| 345 |
|
| 346 |
|
| 347 |
def root_item_flags_str(flags): |
| 348 |
""" |
| 349 |
:param int flags: :class:`~btrfs.ctree.RootItem` flags. |
| 350 |
:returns: String representation of RootItem flags. |
| 351 |
:rtype: str |
| 352 |
|
| 353 |
Example:: |
| 354 |
|
| 355 |
>>> btrfs.utils.root_item_flags_str(1) |
| 356 |
'RDONLY' |
| 357 |
""" |
| 358 |
return flags_str(flags, btrfs.ctree._root_flags_str_map) |
| 359 |
|
| 360 |
|
| 361 |
def compress_type_str(compression): |
| 362 |
""" |
| 363 |
:param int compression: :class:`~btrfs.ctree.FileExtentItem` compression |
| 364 |
type. |
| 365 |
:returns: String representation of compression type. |
| 366 |
:rtype: str |
| 367 |
|
| 368 |
Example:: |
| 369 |
|
| 370 |
>>> btrfs.utils.compress_type_str(3) |
| 371 |
'zstd' |
| 372 |
""" |
| 373 |
return btrfs.ctree._compress_type_str_map.get(compression, 'unknown') |
| 374 |
|
| 375 |
|
| 376 |
def file_extent_type_str(type_): |
| 377 |
""" |
| 378 |
:param int type_: :class:`~btrfs.ctree.FileExtentItem` type. |
| 379 |
:returns: String representation of FileExtentItem type. |
| 380 |
:rtype: str |
| 381 |
|
| 382 |
Example:: |
| 383 |
|
| 384 |
>>> btrfs.utils.file_extent_type_str(0) |
| 385 |
'inline' |
| 386 |
""" |
| 387 |
return btrfs.ctree._file_extent_type_str_map.get(type_, 'unknown') |
| 388 |
|
| 389 |
|
| 390 |
def free_space_info_flags_str(flags): |
| 391 |
""" |
| 392 |
:param int flags: :class:`~btrfs.ctree.FreeSpaceInfo` flags. |
| 393 |
:returns: String representation of FreeSpaceInfo flags. |
| 394 |
:rtype: str |
| 395 |
|
| 396 |
Example:: |
| 397 |
|
| 398 |
>>> btrfs.utils.free_space_info_flags_str(1) |
| 399 |
'bitmaps' |
| 400 |
""" |
| 401 |
return flags_str(flags, btrfs.ctree._free_space_info_flags_str_map) |
| 402 |
|
| 403 |
|
| 404 |
def embedded_text_for_str(text): |
| 405 |
""" |
| 406 |
:param bytes text: bytes from a name of data field of an object from the |
| 407 |
:class:`btrfs.ctree.DirItem` family. |
| 408 |
:returns: String representation of the bytes. |
| 409 |
:rtype: str |
| 410 |
|
| 411 |
This function is not intended to be used for anything else than a |
| 412 |
convenient way of displaying filenames and xattr keys and values in the |
| 413 |
pretty printer. |
| 414 |
|
| 415 |
Example:: |
| 416 |
|
| 417 |
>>> name_bytes = b'\\xce\\xbc\\xce\\xbf\\xcf\\x85\\xcf\\x84\\xce\\xbf\\xce\\xbd' |
| 418 |
>>> btrfs.utils.embedded_text_for_str(name_bytes) |
| 419 |
"utf-8 'μουτον'" |
| 420 |
""" |
| 421 |
if len(text) == 0: |
| 422 |
return "<empty>" |
| 423 |
try: |
| 424 |
return "utf-8 '{}'".format(text.decode('utf-8')) |
| 425 |
except UnicodeDecodeError: |
| 426 |
return "raw '{}'".format(repr(text)) |
| 427 |
|
| 428 |
|
| 429 |
_tree_name_id_map = { |
| 430 |
'root': btrfs.ctree.ROOT_TREE_OBJECTID, |
| 431 |
'extent': btrfs.ctree.EXTENT_TREE_OBJECTID, |
| 432 |
'chunk': btrfs.ctree.CHUNK_TREE_OBJECTID, |
| 433 |
'dev': btrfs.ctree.DEV_TREE_OBJECTID, |
| 434 |
'fs': btrfs.ctree.FS_TREE_OBJECTID, |
| 435 |
'csum': btrfs.ctree.CSUM_TREE_OBJECTID, |
| 436 |
'quota': btrfs.ctree.QUOTA_TREE_OBJECTID, |
| 437 |
'uuid': btrfs.ctree.UUID_TREE_OBJECTID, |
| 438 |
'free_space': btrfs.ctree.FREE_SPACE_TREE_OBJECTID, |
| 439 |
'block_group': btrfs.ctree.BLOCK_GROUP_TREE_OBJECTID, |
| 440 |
'tree_log': btrfs.ctree.TREE_LOG_OBJECTID, |
| 441 |
'tree_log_fixup': btrfs.ctree.TREE_LOG_FIXUP_OBJECTID, |
| 442 |
'tree_reloc': btrfs.ctree.TREE_RELOC_OBJECTID, |
| 443 |
'data_reloc': btrfs.ctree.DATA_RELOC_TREE_OBJECTID, |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
def parse_tree_name(name): |
| 448 |
"""Convert a tree name to an actual root tree objectid number |
| 449 |
|
| 450 |
:param string name: tree name with optional _tree suffix or number |
| 451 |
formatted as string |
| 452 |
:returns: Tree objectid that is valid for the root tree |
| 453 |
:rtype: int |
| 454 |
|
| 455 |
Example:: |
| 456 |
|
| 457 |
>>> btrfs.utils.parse_tree_name('EXTENT_TREE') |
| 458 |
2 |
| 459 |
>>> btrfs.utils.parse_tree_name('extent_tree') |
| 460 |
2 |
| 461 |
>>> btrfs.utils.parse_tree_name('extent') |
| 462 |
2 |
| 463 |
>>> btrfs.utils.parse_tree_name('2') |
| 464 |
2 |
| 465 |
>>> btrfs.utils.parse_tree_name(2) |
| 466 |
2 |
| 467 |
""" |
| 468 |
try: |
| 469 |
return int(name) |
| 470 |
except ValueError: |
| 471 |
pass |
| 472 |
lower_name = name.lower() |
| 473 |
if lower_name[-5:] == '_tree': |
| 474 |
lookup_name = lower_name[:-5] |
| 475 |
else: |
| 476 |
lookup_name = lower_name |
| 477 |
if lookup_name in _tree_name_id_map: |
| 478 |
return _tree_name_id_map[lookup_name] |
| 479 |
raise ValueError("Unknown metadata tree name " + name) |
| 480 |
|
| 481 |
|
| 482 |
def parse_key_string(key_str): |
| 483 |
"""Create a Key object from a pretty printed string representation |
| 484 |
|
| 485 |
:param string key_str: String representation of a key, e.g. '(31832 INODE_REF 31798)' |
| 486 |
:returns: A Key object with the parsed values set |
| 487 |
:rtype: :class:`~btrfs.ctree.Key` |
| 488 |
:raises: :class:`ValueError` if the key string is an invalid key representation |
| 489 |
|
| 490 |
The parentheses around the key triplet are optional. |
| 491 |
|
| 492 |
Example:: |
| 493 |
|
| 494 |
>>> btrfs.utils.parse_key_string('(31832 INODE_REF 31798)') |
| 495 |
Key(31832, 12, 31798) |
| 496 |
>>> btrfs.utils.parse_key_string('(535 EXTENT_DATA 0)') |
| 497 |
Key(535, 108, 0) |
| 498 |
|
| 499 |
""" |
| 500 |
if key_str[0] == '(' and key_str[-1] == ')': |
| 501 |
key_str = key_str[1:-1] |
| 502 |
try: |
| 503 |
objectid_str, type_str, offset_str = key_str.split() |
| 504 |
except ValueError: |
| 505 |
raise ValueError( |
| 506 |
"Key representation needs 3 fields: objectid, type, offset: {}".format(key_str)) \ |
| 507 |
from None |
| 508 |
return btrfs.ctree.Key(objectid_str, type_str, offset_str) |
| 509 |
|
| 510 |
|
| 511 |
def _pretty_attr_value(obj, attr_name, stringify_fn=None): |
| 512 |
if stringify_fn is not None: |
| 513 |
return "{}: {}".format(attr_name, stringify_fn(getattr(obj, attr_name))) |
| 514 |
cls = obj.__class__ |
| 515 |
attr_name_str = '_{}_str'.format(attr_name) |
| 516 |
stringify_property = getattr(cls, attr_name_str, None) |
| 517 |
if stringify_property is None: |
| 518 |
|
| 519 |
attr_name_str = '{}_str'.format(attr_name) |
| 520 |
stringify_property = getattr(cls, attr_name_str, None) |
| 521 |
if stringify_property is not None and isinstance(stringify_property, property): |
| 522 |
return "{}: {}".format(attr_name, getattr(obj, attr_name_str)) |
| 523 |
value = getattr(obj, attr_name) |
| 524 |
if isinstance(value, (memoryview, bytes)): |
| 525 |
return "{}: {} bytes of data".format(attr_name, len(value)) |
| 526 |
return "{}: {}".format(attr_name, value) |
| 527 |
|
| 528 |
|
| 529 |
pretty_print_modules = 'btrfs.ctree', 'btrfs.ioctl', 'btrfs.fs_usage', 'btrfs.free_space_tree' |
| 530 |
|
| 531 |
|
| 532 |
def _pretty_obj_tuples(obj, level=0, seen=None): |
| 533 |
if seen is None: |
| 534 |
seen = [] |
| 535 |
cls = obj.__class__ |
| 536 |
if isinstance(obj, btrfs.ctree.ItemData) and \ |
| 537 |
hasattr(obj, 'key') and isinstance(obj.key, btrfs.ctree.Key): |
| 538 |
yield level, "<{}.{} {}>".format(cls.__module__, cls.__name__, str(obj.key)) |
| 539 |
elif cls.__module__ in pretty_print_modules: |
| 540 |
yield level, "<{}.{}>".format(cls.__module__, cls.__name__) |
| 541 |
if obj in seen: |
| 542 |
yield level, "[... object already seen, aborting recursion]" |
| 543 |
return |
| 544 |
seen.append(obj) |
| 545 |
known = False |
| 546 |
if cls.__module__ in pretty_print_modules and \ |
| 547 |
not isinstance(obj, btrfs.ctree.Key): |
| 548 |
known = True |
| 549 |
if isinstance(obj, btrfs.ctree.ItemData): |
| 550 |
try: |
| 551 |
objectid_attr, type_attr, offset_attr = obj._key_attrs |
| 552 |
if objectid_attr is not None: |
| 553 |
yield level, "{} (key objectid)".format(_pretty_attr_value(obj, objectid_attr)) |
| 554 |
if type_attr is not None: |
| 555 |
yield level, "{} (key type)".format(_pretty_attr_value(obj, type_attr)) |
| 556 |
if offset_attr is not None: |
| 557 |
yield level, "{} (key offset)".format(_pretty_attr_value(obj, offset_attr)) |
| 558 |
except AttributeError: |
| 559 |
pass |
| 560 |
for attr_name, attr_value in obj.__dict__.items(): |
| 561 |
if attr_name.startswith('_'): |
| 562 |
continue |
| 563 |
if isinstance(obj, btrfs.ctree.ItemData): |
| 564 |
try: |
| 565 |
if attr_name in obj._key_attrs: |
| 566 |
continue |
| 567 |
except AttributeError: |
| 568 |
pass |
| 569 |
if attr_name == 'key' and isinstance(attr_value, btrfs.ctree.Key): |
| 570 |
continue |
| 571 |
if isinstance(attr_value, list): |
| 572 |
if len(attr_value) == 0: |
| 573 |
continue |
| 574 |
yield level, "{}:".format(attr_name) |
| 575 |
for item in attr_value: |
| 576 |
yield level, '-' |
| 577 |
yield from _pretty_obj_tuples(item, level+1, seen) |
| 578 |
elif isinstance(attr_value, dict): |
| 579 |
if len(attr_value) == 0: |
| 580 |
continue |
| 581 |
yield level, "{}:".format(attr_name) |
| 582 |
for k, v in attr_value.items(): |
| 583 |
dict_key_str = '_{}_key_str'.format(attr_name) |
| 584 |
stringify_fn = getattr(cls, dict_key_str, None) |
| 585 |
if stringify_fn is None: |
| 586 |
|
| 587 |
dict_key_str = '{}_key_str'.format(attr_name) |
| 588 |
stringify_fn = getattr(cls, dict_key_str, None) |
| 589 |
if stringify_fn is not None and callable(stringify_fn): |
| 590 |
yield level+1, "{}:".format(stringify_fn(k)) |
| 591 |
else: |
| 592 |
yield level+1, "{}:".format(k) |
| 593 |
yield from _pretty_obj_tuples(v, level+2, seen) |
| 594 |
elif attr_value.__class__.__module__ in pretty_print_modules and \ |
| 595 |
not isinstance(attr_value, (btrfs.ctree.Key, btrfs.ctree.TimeSpec)): |
| 596 |
yield level, "{}:".format(attr_name) |
| 597 |
yield from _pretty_obj_tuples(attr_value, level+1, seen) |
| 598 |
else: |
| 599 |
yield level, _pretty_attr_value(obj, attr_name) |
| 600 |
if isinstance(obj, (list, types.GeneratorType)) or \ |
| 601 |
(isinstance(obj, btrfs.ctree.ItemData) and |
| 602 |
isinstance(obj, collections.abc.MutableSequence)): |
| 603 |
known = True |
| 604 |
for item in obj: |
| 605 |
yield level, '-' |
| 606 |
yield from _pretty_obj_tuples(item, level+1, seen) |
| 607 |
elif isinstance(obj, dict) and len(obj) != 0: |
| 608 |
known = True |
| 609 |
for k, v in obj.items(): |
| 610 |
yield level, "{}:".format(k) |
| 611 |
yield from _pretty_obj_tuples(v, level+1, seen) |
| 612 |
if not known: |
| 613 |
yield level, str(obj) |
| 614 |
seen.pop() |
| 615 |
|
| 616 |
|
| 617 |
def _pretty_obj_lines(obj, level=0): |
| 618 |
for level, line in _pretty_obj_tuples(obj, level): |
| 619 |
yield "{}{}".format(' ' * level, line) |
| 620 |
|
| 621 |
|
| 622 |
def _pretty_print(obj, level=0): |
| 623 |
for line in _pretty_obj_lines(obj, level): |
| 624 |
print(line) |
| 625 |
|
| 626 |
|
| 627 |
def pretty_print(obj): |
| 628 |
""" |
| 629 |
The pretty printer dumps content of objects on the screen. It is mainly |
| 630 |
designed to work with objects from the btrfs library. It is also possible |
| 631 |
to provide a lists of objects, or a generator, or even nested structures. |
| 632 |
|
| 633 |
:param obj: An object to pretty print, or a collection of them. |
| 634 |
:type obj: anything goes |
| 635 |
|
| 636 |
Example:: |
| 637 |
|
| 638 |
>>> import btrfs |
| 639 |
>>> with btrfs.FileSystem('/') as fs: |
| 640 |
... btrfs.utils.pretty_print(fs.block_group(1687427219456)) |
| 641 |
... |
| 642 |
<btrfs.ctree.BlockGroupItem (1687427219456 BLOCK_GROUP_ITEM 1073741824)> |
| 643 |
vaddr: 1687427219456 (key objectid) |
| 644 |
length: 1.00GiB (key offset) |
| 645 |
flags: DATA |
| 646 |
chunk_objectid: 256 |
| 647 |
used: 960.50MiB |
| 648 |
""" |
| 649 |
_pretty_print(obj) |
| 650 |
|
| 651 |
|
| 652 |
def _str_obj_tuples(obj, level=0, seen=None): |
| 653 |
if seen is None: |
| 654 |
seen = [] |
| 655 |
cls = obj.__class__ |
| 656 |
if cls.__module__ in pretty_print_modules and \ |
| 657 |
not isinstance(obj, (btrfs.ctree.Key, btrfs.ctree.TimeSpec)): |
| 658 |
yield level, str(obj) |
| 659 |
if obj in seen: |
| 660 |
yield level, "[... object already seen, aborting recursion]" |
| 661 |
return |
| 662 |
seen.append(obj) |
| 663 |
if isinstance(obj, btrfs.ctree.ItemData): |
| 664 |
for attr_name, attr_value in obj.__dict__.items(): |
| 665 |
if attr_name.startswith('_'): |
| 666 |
continue |
| 667 |
if isinstance(obj, (btrfs.ctree.ItemData, btrfs.ctree.SubItem)): |
| 668 |
yield from _str_obj_tuples(attr_value, level+1, seen) |
| 669 |
elif isinstance(attr_value, list): |
| 670 |
for item in attr_value: |
| 671 |
yield from _str_obj_tuples(item, level+1, seen) |
| 672 |
if isinstance(obj, (list, types.GeneratorType)): |
| 673 |
for item in obj: |
| 674 |
yield from _str_obj_tuples(item, level, seen) |
| 675 |
elif isinstance(obj, btrfs.ctree.ItemData) and \ |
| 676 |
isinstance(obj, collections.abc.MutableSequence): |
| 677 |
for item in obj: |
| 678 |
yield from _str_obj_tuples(item, level+1, seen) |
| 679 |
seen.pop() |
| 680 |
|
| 681 |
|
| 682 |
def _str_print_lines(obj, level=0): |
| 683 |
for level, line in _str_obj_tuples(obj, level): |
| 684 |
yield "{}{}".format(' ' * level, line) |
| 685 |
|
| 686 |
|
| 687 |
def str_print(obj): |
| 688 |
""" |
| 689 |
Print the usual str() of an object, but look inside to see if more ItemData |
| 690 |
objects are hidden there. If so, also print their str(). |
| 691 |
|
| 692 |
:param obj: An object to pretty print, or a collection of them. |
| 693 |
:type obj: anything goes |
| 694 |
|
| 695 |
Example:: |
| 696 |
|
| 697 |
>>> with btrfs.FileSystem('/') as fs: |
| 698 |
... btrfs.utils.str_print(fs.block_group(63381176320)) |
| 699 |
... |
| 700 |
block group vaddr 63381176320 length 1073741824 flags DATA used 1073741824 used_pct 100 |
| 701 |
""" |
| 702 |
for line in _str_print_lines(obj): |
| 703 |
print(line) |