added support for btrfs free space readout

This commit is contained in:
Martinez 2016-10-03 15:05:46 +02:00
parent 22600d3b31
commit 79b47e1130

View file

@ -313,7 +313,10 @@ def command(cmd):
Executes a command, returns stdout, suppresses stderr." Executes a command, returns stdout, suppresses stderr."
""" """
s = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) if type(cmd) == str:
cmd = cmd.split()
s = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
s.wait() s.wait()
return s.stdout.read().decode("utf-8") return s.stdout.read().decode("utf-8")
@ -549,7 +552,7 @@ class StatsPart(Part):
if "rw" not in options.split(","): if "rw" not in options.split(","):
continue continue
# /proc/self/mounts uses a literal \\040 string to escape spaces # /proc/self/mounts uses a literal \040 string to escape spaces
dir = dir.replace("\\040", " ") dir = dir.replace("\\040", " ")
basename = os.path.basename(dir) basename = os.path.basename(dir)
@ -565,13 +568,25 @@ class StatsPart(Part):
else: else:
short_name = " /" short_name = " /"
try: # handle btrfs separately
stat = os.statvfs(dir) if type == "btrfs":
except PermissionError: btrfs_raw = command(["/sbin/btrfs", "fi", "usage", "-b", dir])
continue
stor_total = stat.f_blocks * stat.f_bsize for line in btrfs_raw.split("\n"):
stor_free = stat.f_bavail * stat.f_bsize if "Device size" in line:
stor_total = int(line.split()[-1])
elif "Free" in line:
stor_free = int(line.split()[-3])
break
else:
try:
stat = os.statvfs(dir)
except PermissionError:
continue
stor_total = stat.f_blocks * stat.f_bsize
stor_free = stat.f_bavail * stat.f_bsize
# ignore virtual filesystems # ignore virtual filesystems
if stor_total == 0: if stor_total == 0: