[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 1228937605 28800
# Node ID 8638073ea45665c7531931515636a862f684c2a5
# Parent  e9045f3ae5b2243a9ee00f0913c2dcefdddd7c36
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
@@ -23,10 +23,30 @@
 # not be used by clients.
 #
 
+import stat
 import os
 
 from virtinst import util
 
+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 blkdev_size(path):
     """Return the size of the block device.  We can't use os.stat() as
     that returns zero on many platforms."""

_______________________________________________
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