Browse Source

Avoid ENOENT in nilmdb.utils.diskusage.du

ENOENT might show up if we're actively deleting files in the nilmdb
thread while trying to read available space from e.g. the server
thread.
tags/nilmdb-1.5.4^0
Jim Paris 10 years ago
parent
commit
00e6ba1124
1 changed files with 15 additions and 7 deletions
  1. +15
    -7
      nilmdb/utils/diskusage.py

+ 15
- 7
nilmdb/utils/diskusage.py View File

@@ -1,4 +1,5 @@
import os
import errno
from math import log

def human_size(num):
@@ -16,10 +17,17 @@ def human_size(num):
return '1 byte'

def du(path):
"""Like du -sb, returns total size of path in bytes."""
size = os.path.getsize(path)
if os.path.isdir(path):
for thisfile in os.listdir(path):
filepath = os.path.join(path, thisfile)
size += du(filepath)
return size
"""Like du -sb, returns total size of path in bytes. Ignore
errors that might occur if we encounter broken symlinks or
files in the process of being removed."""
try:
size = os.path.getsize(path)
if os.path.isdir(path):
for thisfile in os.listdir(path):
filepath = os.path.join(path, thisfile)
size += du(filepath)
return size
except OSError as e: # pragma: no cover
if e.errno != errno.ENOENT:
raise
return 0

Loading…
Cancel
Save