[PATCH] Add utility function for stat() of disks

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



# HG changeset patch
# User john.levon@xxxxxxx
# Date 1228851945 28800
# Node ID 50ca2624e7ef5713153ef85c012326fdae125b7f
# Parent  29d8886362e2993eaf26cf8d4e948b2de4b8d9ec
Add utility function for stat() of disks

Signed-off-by: John Levon <john.levon@xxxxxxx>

diff --git a/virtinst/CloneManager.py b/virtinst/CloneManager.py
--- a/virtinst/CloneManager.py
+++ b/virtinst/CloneManager.py
@@ -19,7 +19,6 @@
 # MA 02110-1301 USA.
 
 import os
-import stat
 import libxml2
 import logging
 import urlgrabber.progress as progress
@@ -394,13 +393,9 @@ class CloneDesign(object):
         logging.debug("original device list: %s" % (lst))
 
         for i in lst:
-            mode = os.stat(i)[stat.ST_MODE]
-            if stat.S_ISBLK(mode):
-                size.append(_util.blkdev_size(i))
-                typ.append(False)
-            elif stat.S_ISREG(mode):
-                size.append(os.path.getsize(i))
-                typ.append(True)
+            (t, sz) = _util.stat_disk(i)
+            typ.append(t)
+            size.append(sz)
         logging.debug("original device size: %s" % (size))
         logging.debug("original device type: %s" % (typ))
 
@@ -443,18 +438,9 @@ class CloneDesign(object):
         typ  = []
 
         for i in cln_dev_lst:
-            if os.path.exists(i) ==  False:
-                size.append(0)
-                # if not exists, create file necessary
-                typ.append(True)
-                continue
-            mode = os.stat(i)[stat.ST_MODE]
-            if stat.S_ISBLK(mode):
-                size.append(_util.blkdev_size(i))
-                typ.append(False)
-            elif stat.S_ISREG(mode):
-                size.append(os.path.getsize(i))
-                typ.append(True)
+            (t, sz) = _util.stat_disk(i)
+            typ.append(t)
+            size.append(sz)
 
         logging.debug("clone device list: %s" % (cln_dev_lst))
         logging.debug("clone device size: %s" % (size))
diff --git a/virtinst/_util.py b/virtinst/_util.py
--- a/virtinst/_util.py
+++ b/virtinst/_util.py
@@ -22,6 +22,7 @@ import platform
 import platform
 import random
 import os.path
+import stat
 import re
 import libxml2
 import logging
@@ -280,14 +281,24 @@ def xml_escape(str):
     str = str.replace(">", "&gt;")
     return str
  
-def blkdev_size(path):
-    """Return the size of the block device.  We can't use os.stat() as
-    that returns zero on many platforms."""
-    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)
-    return size
+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):
+        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)
+        return False, size
+    elif stat.S_ISREG(mode):
+        return True, os.path.getsize(path)
+
+    return True, 0
 
 def compareMAC(p, q):
     """Compare two MAC addresses"""

_______________________________________________
et-mgmt-tools mailing list
et-mgmt-tools@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/et-mgmt-tools

[Index of Archives]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux