ensure-dirs: Fix epydoc error
[ganeti-local] / lib / tools / ensure_dirs.py
index 02a59a2..7abcce2 100644 (file)
@@ -56,7 +56,7 @@ class EnsureError(errors.GenericError):
 
 
 def EnsurePermission(path, mode, uid=-1, gid=-1, must_exist=True,
-                     _chmod_fn=os.chmod, _chown_fn=os.chown):
+                     _chmod_fn=os.chmod, _chown_fn=os.chown, _stat_fn=os.stat):
   """Ensures that given path has given mode.
 
   @param path: The path to the file
@@ -70,10 +70,20 @@ def EnsurePermission(path, mode, uid=-1, gid=-1, must_exist=True,
   """
   logging.debug("Checking %s", path)
   try:
-    _chmod_fn(path, mode)
+    st = _stat_fn(path)
+
+    fmode = stat.S_IMODE(st[stat.ST_MODE])
+    if fmode != mode:
+      logging.debug("Changing mode of %s from %#o to %#o", path, fmode, mode)
+      _chmod_fn(path, mode)
 
     if max(uid, gid) > -1:
-      _chown_fn(path, uid, gid)
+      fuid = st[stat.ST_UID]
+      fgid = st[stat.ST_GID]
+      if fuid != uid or fgid != gid:
+        logging.debug("Changing owner of %s from UID %s/GID %s to"
+                      " UID %s/GID %s", path, fuid, fgid, uid, gid)
+        _chown_fn(path, uid, gid)
   except EnvironmentError, err:
     if err.errno == errno.ENOENT:
       if must_exist:
@@ -83,7 +93,7 @@ def EnsurePermission(path, mode, uid=-1, gid=-1, must_exist=True,
                         (path, err))
 
 
-def EnsureDir(path, mode, uid, gid, _stat_fn=os.lstat, _mkdir_fn=os.mkdir,
+def EnsureDir(path, mode, uid, gid, _lstat_fn=os.lstat, _mkdir_fn=os.mkdir,
               _ensure_fn=EnsurePermission):
   """Ensures that given path is a dir and has given mode, uid and gid set.
 
@@ -91,7 +101,7 @@ def EnsureDir(path, mode, uid, gid, _stat_fn=os.lstat, _mkdir_fn=os.mkdir,
   @param mode: The mode of the file
   @param uid: The uid of the owner of this file
   @param gid: The gid of the owner of this file
-  @param _stat_fn: Stat function to use (unittest only)
+  @param _lstat_fn: Stat function to use (unittest only)
   @param _mkdir_fn: mkdir function to use (unittest only)
   @param _ensure_fn: ensure function to use (unittest only)
 
@@ -99,16 +109,15 @@ def EnsureDir(path, mode, uid, gid, _stat_fn=os.lstat, _mkdir_fn=os.mkdir,
   logging.debug("Checking directory %s", path)
   try:
     # We don't want to follow symlinks
-    st_mode = _stat_fn(path)[stat.ST_MODE]
-
-    if not stat.S_ISDIR(st_mode):
-      raise EnsureError("Path %s is expected to be a directory, but it's not" %
-                        path)
+    st = _lstat_fn(path)
   except EnvironmentError, err:
-    if err.errno == errno.ENOENT:
-      _mkdir_fn(path)
-    else:
-      raise EnsureError("Error while do a stat() on %s: %s" % (path, err))
+    if err.errno != errno.ENOENT:
+      raise EnsureError("stat(2) on %s failed: %s" % (path, err))
+    _mkdir_fn(path)
+  else:
+    if not stat.S_ISDIR(st[stat.ST_MODE]):
+      raise EnsureError("Path %s is expected to be a directory, but isn't" %
+                        path)
 
   _ensure_fn(path, mode, uid=uid, gid=gid)