Merge pull request #18507 from theksk/file-diskusage

File diskusage
This commit is contained in:
Thomas S Hatch 2014-12-01 10:06:04 -07:00
commit 26e76250a6

View File

@ -4310,3 +4310,41 @@ def move(src, dst):
)
return ret
def diskusage(path):
'''
Recursivly calculate diskusage of path and return it
in bytes
CLI Example:
.. code-block:: bash
salt '*' file.diskusage /path/to/check
'''
total_size = 0
seen = set()
if os.path.isfile(path):
ret = stat.st_size
return ret
for dirpath, dirnames, filenames in os.walk(path):
for f in filenames:
fp = os.path.join(dirpath, f)
try:
stat = os.stat(fp)
except OSError:
continue
if stat.st_ino in seen:
continue
seen.add(stat.st_ino)
total_size += stat.st_size
ret = total_size
return ret