util: privatize stat_disk()

This commit is contained in:
Cole Robinson 2016-08-24 15:43:04 -04:00
parent 49ad2df0cb
commit 5dea1c2d80
2 changed files with 28 additions and 27 deletions

View File

@ -21,11 +21,11 @@
import logging import logging
import os import os
import re import re
import stat
import statvfs import statvfs
import libvirt import libvirt
from . import util
from .storage import StoragePool, StorageVolume from .storage import StoragePool, StorageVolume
@ -73,6 +73,31 @@ def _lookup_vol_by_basename(pool, path):
return pool.storageVolLookupByName(name) return pool.storageVolLookupByName(name)
def _stat_disk(path):
"""
Returns the tuple (isreg, size)
"""
if not os.path.exists(path):
return True, 0
mode = os.stat(path)[stat.ST_MODE]
# os.path.getsize('/dev/..') can be zero on some platforms
if stat.S_ISBLK(mode):
try:
fd = os.open(path, os.O_RDONLY)
# os.SEEK_END is not present on all systems
size = os.lseek(fd, 0, 2)
os.close(fd)
except:
size = 0
return False, size
elif stat.S_ISREG(mode):
return True, os.path.getsize(path)
return True, 0
def check_if_path_managed(conn, path): def check_if_path_managed(conn, path):
""" """
Try to lookup storage objects for the passed path. Try to lookup storage objects for the passed path.
@ -497,7 +522,7 @@ class StorageBackend(_StorageBase):
if self._vol_object: if self._vol_object:
ret = self.get_vol_xml().capacity ret = self.get_vol_xml().capacity
elif self._path: elif self._path:
ignore, ret = util.stat_disk(self._path) ret = _stat_disk(self._path)[1]
self._size = (float(ret) / 1024.0 / 1024.0 / 1024.0) self._size = (float(ret) / 1024.0 / 1024.0 / 1024.0)
return self._size return self._size
@ -551,7 +576,7 @@ class StorageBackend(_StorageBase):
elif self._path and not self._conn.is_remote(): elif self._path and not self._conn.is_remote():
if os.path.isdir(self._path): if os.path.isdir(self._path):
self._dev_type = "dir" self._dev_type = "dir"
elif util.stat_disk(self._path)[0]: elif _stat_disk(self._path)[0]:
self._dev_type = "file" self._dev_type = "file"
else: else:
self._dev_type = "block" self._dev_type = "block"

View File

@ -22,7 +22,6 @@ import logging
import os import os
import random import random
import re import re
import stat
import sys import sys
import libvirt import libvirt
@ -46,29 +45,6 @@ def xml_indent(xmlstr, level):
return "\n".join((" " * level + l) for l in xmlstr.splitlines()) return "\n".join((" " * level + l) for l in xmlstr.splitlines())
def stat_disk(path):
"""Returns the tuple (isreg, size)."""
if not os.path.exists(path):
return True, 0
mode = os.stat(path)[stat.ST_MODE]
# os.path.getsize('/dev/..') can be zero on some platforms
if stat.S_ISBLK(mode):
try:
fd = os.open(path, os.O_RDONLY)
# os.SEEK_END is not present on all systems
size = os.lseek(fd, 0, 2)
os.close(fd)
except:
size = 0
return False, size
elif stat.S_ISREG(mode):
return True, os.path.getsize(path)
return True, 0
def vm_uuid_collision(conn, uuid): def vm_uuid_collision(conn, uuid):
""" """
Check if passed UUID string is in use by another guest of the connection Check if passed UUID string is in use by another guest of the connection