[rhel7-branch 29/29] Create missing parent directories for user's home directory (#1163775)

bcl installerbot-noreply at redhat.com
Tue Jun 2 00:31:30 UTC 2015


From: "Brian C. Lane" <bcl at redhat.com>

libuser doesn't create parent directories. This simplifies the homedir
creation slightly and adds a utility function, with a test, that returns
the home directory's parent's path, suitable for passing to mkdirChain.

Resolves: rhbz#1163775

(cherry picked from commit 275a1cb247c8ff20129224451b33fbac51c6c116)

Related: rhbz#1196721
---
 pyanaconda/iutil.py                  |  4 ++++
 pyanaconda/ui/gui/spokes/user.py     |  3 +++
 pyanaconda/users.py                  | 13 ++++++++-----
 tests/pyanaconda_tests/iutil_test.py |  9 +++++++++
 4 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index 6c6a12d..a3f4797 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -956,3 +956,7 @@ def get_platform_groupid():
         return ""
 
     return "platform-" + platform.lower()
+
+def parent_dir(directory):
+    """Return the parent's path"""
+    return "/".join(os.path.normpath(directory).split("/")[:-1])
diff --git a/pyanaconda/ui/gui/spokes/user.py b/pyanaconda/ui/gui/spokes/user.py
index bc276d4..a1de424 100644
--- a/pyanaconda/ui/gui/spokes/user.py
+++ b/pyanaconda/ui/gui/spokes/user.py
@@ -22,6 +22,7 @@
 
 import re
 
+import os
 from pyanaconda.flags import flags
 from pyanaconda.i18n import _, CN_
 from pyanaconda.users import cryptPassword, validatePassword, guess_username
@@ -174,6 +175,8 @@ def run(self):
             if self._homeSet or self._origHome != homedir:
                 self._homeSet = True
                 self._user.homedir = homedir
+            if not os.path.isabs(self._user.homedir):
+                self._user.homedir = "/" + self._user.homedir
 
             if self._cUid.get_active():
                 self._user.uid = int(self._uid.get_value())
diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index 68e497a..2cb776d 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -298,11 +298,14 @@ def createUser (self, user_name, *args, **kwargs):
             userEnt.set(libuser.GIDNUMBER, [groupEnt.get(libuser.GIDNUMBER)[0]] +
                         map(lambda grp: grp.get(libuser.GIDNUMBER)[0], grpLst))
 
-            if kwargs.get("homedir", False):
-                userEnt.set(libuser.HOMEDIRECTORY, kwargs["homedir"])
-            else:
-                iutil.mkdirChain('/home')
-                userEnt.set(libuser.HOMEDIRECTORY, "/home/" + user_name)
+            homedir = kwargs.get("homedir", None)
+            if not homedir:
+                homedir = "/home/" + user_name
+            # libuser expects the parent directory tree to exist.
+            parent_dir = iutil.parent_dir(homedir)
+            if parent_dir:
+                iutil.mkdirChain(parent_dir)
+            userEnt.set(libuser.HOMEDIRECTORY, homedir)
 
             if kwargs.get("shell", False):
                 userEnt.set(libuser.LOGINSHELL, kwargs["shell"])
diff --git a/tests/pyanaconda_tests/iutil_test.py b/tests/pyanaconda_tests/iutil_test.py
index 957cd16..802ec52 100644
--- a/tests/pyanaconda_tests/iutil_test.py
+++ b/tests/pyanaconda_tests/iutil_test.py
@@ -438,3 +438,12 @@ def have_word_match_test(self):
         # Compare unicode and str and make sure nothing crashes
         self.assertTrue(iutil.have_word_match("fête", u"fête champêtre"))
         self.assertTrue(iutil.have_word_match(u"fête", "fête champêtre"))
+
+    def parent_dir_test(self):
+        """Test the parent_dir function"""
+        dirs = [("", ""), ("/", ""), ("/home/", ""), ("/home/bcl", "/home"), ("home/bcl", "home"),
+                ("/home/bcl/", "/home"), ("/home/extra/bcl", "/home/extra"),
+                ("/home/extra/bcl/", "/home/extra"), ("/home/extra/../bcl/", "/home")]
+
+        for d, r in dirs:
+            self.assertEquals(iutil.parent_dir(d), r)


-- 
To view this commit on github, visit https://github.com/rhinstaller/anaconda/commit/b96a1e11141b770f45a64a3305013296f4a07d34


More information about the anaconda-patches mailing list