Yeela Kaplan has uploaded a new change for review.

Change subject: Add createdir, cleanupdir, simplewalk functionality
......................................................................

Add createdir, cleanupdir, simplewalk functionality

Change-Id: Ica6cb0fee5802c664548011c38e358529b155333
Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=1083771
Signed-off-by: Yeela Kaplan <ykap...@redhat.com>
---
M vdsm/storage/outOfProcess.py
1 file changed, 78 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/35/27735/1

diff --git a/vdsm/storage/outOfProcess.py b/vdsm/storage/outOfProcess.py
index 85f0f3c..b350c08 100644
--- a/vdsm/storage/outOfProcess.py
+++ b/vdsm/storage/outOfProcess.py
@@ -29,6 +29,7 @@
 from ioprocess import IOProcess
 
 from remoteFileHandler import RemoteFileHandlerPool
+import storage_exception as se
 
 RFH = 'rfh'
 IOPROC = 'ioprocess'
@@ -81,6 +82,64 @@
     def __init__(self, iop):
         self._iop = iop
 
+    def cleanupdir(self, path, ignoreErrors=True):
+        cleanupdir_errors = []
+
+        try:
+            files = self._iop.listdir(path)
+        except OSError:
+            if not ignoreErrors:
+                raise
+
+        for f in files:
+            fullpath = os.path.join(path, f)
+            if _IOProcessOs(self._iop).path.isdir(fullpath):
+                try:
+                    self.cleanupdir(fullpath, False)
+                except OSError as e:
+                    cleanupdir_errors.append(e)
+            else:
+                try:
+                    self._iop.unlink(fullpath)
+                except Exception as e:
+                    cleanupdir_errors.append('%s: %s' % ("unlink", e))
+        try:
+            self._iop.rmdir(path)
+        except Exception as e:
+            cleanupdir_errors.append('%s: %s' % ("rmdir", e))
+
+        if not ignoreErrors and cleanupdir_errors:
+            raise se.MiscDirCleanupFailure("%s %s" % (path, cleanupdir_errors))
+
+    def createdir(self, path, mode=None):
+        parts = path.split("/")
+        tmpPath = ""
+        for part in parts:
+            tmpPath = os.path.join(tmpPath, part)
+            if tmpPath == "":
+                tmpPath = "/"
+
+            try:
+                if mode:
+                    self._iop.mkdir(tmpPath, mode)
+                else:
+                    self._iop.mkdir(tmpPath)
+            except OSError as e:
+                if e.errno != errno.EEXIST:
+                    raise
+                else:
+                    if tmpPath == path and mode is not None:
+                        statinfo = self._iop.stat(path)
+                        curMode = statinfo[stat.ST_MODE]
+                        if curMode != mode:
+                            raise OSError(errno.EPERM,
+                                          ("Existing %s "
+                                           "permissions %s are not as "
+                                           "requested %s") % (path,
+                                                              oct(curMode),
+                                                              oct(mode)))
+
+
     def validateAccess(self, targetPath, perms=(os.R_OK | os.W_OK | os.X_OK)):
         if not self._iop.access(targetPath, perms):
             log.warning("Permission denied for directory: %s with permissions:"
@@ -127,7 +186,11 @@
 
         def isdir(self, path):
             res = self._iop.stat(path)
-            return res.st_mode and stat.S_IFDIR
+            return stat.S_ISDIR(res.st_mode)
+
+        def islink(self, path):
+            res = self._iop.stat(path)
+            return stat.S_ISLNK(res.st_mode)
 
         def lexists(self, path):
             return self._iop.lexists(path)
@@ -144,6 +207,19 @@
     for l in lines:
         data += l
     return ioprocess.writefile(path, data)
+
+
+def simpleWalk(ioproc, path):
+    files = []
+    for f in ioproc.listdir(path):
+        fullpath = os.path.join(path, f)
+        osPath = _IOProcessOs(ioproc).path
+        if osPath.isdir(fullpath) and not osPath.islink(fullpath):
+            files.extend(simpleWalk(ioproc, fullpath))
+        else:
+            files.append(fullpath)
+
+    return files
 
 
 class _ModuleWrapper(types.ModuleType):
@@ -180,6 +256,7 @@
             self.os.path = _IOProcessOs(ioproc).path
             self.directReadLines = partial(directReadLines, ioproc)
             self.writeLines = partial(writeLines, ioproc)
+            self.simpleWalk = partial(simpleWalk, ioproc)
 
     def __getattr__(self, name):
         # Root modules is fake, we need to remove it


-- 
To view, visit http://gerrit.ovirt.org/27735
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ica6cb0fee5802c664548011c38e358529b155333
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Yeela Kaplan <ykap...@redhat.com>
_______________________________________________
vdsm-patches mailing list
vdsm-patches@lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to