[PATCH 3/3] Handle the case where home directory already exists (#970628)

Vratislav Podzimek vpodzime at redhat.com
Wed Jun 5 12:17:21 UTC 2013


Instead of failing to add the user whose home directory already exists, we
should just fix the owner of the home directory to the UID of the newly created
user.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 pyanaconda/users.py | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index 7ec594c..7fe0a7c 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -281,9 +281,27 @@ class Users:
                 if kwargs.get("gecos", False):
                     userEnt.set(libuser.GECOS, kwargs["gecos"])
 
-                self.admin.addUser(userEnt, mkmailspool=kwargs.get("mkmailspool", True))
+                # need to create home directory for the user or does it already exist?
+                # userEnt.get returns lists (usually with a single item)
+                mk_homedir = not os.path.exists(userEnt.get(libuser.HOMEDIRECTORY)[0])
+
+                self.admin.addUser(userEnt, mkmailspool=kwargs.get("mkmailspool", True),
+                                   mkhomedir=mk_homedir)
                 self.admin.addGroup(groupEnt)
 
+                if not mk_homedir:
+                    stats = os.stat(userEnt.get(libuser.HOMEDIRECTORY)[0])
+                    orig_uid = stats.st_uid
+                    orig_gid = stats.st_gid
+
+                    log.info("Home directory for the user %s already existed, "
+                             "fixing the owner." % user_name)
+                    # home directory already existed, change owner of it properly
+                    iutil.chown_dir_tree(userEnt.get(libuser.HOMEDIRECTORY)[0],
+                                         userEnt.get(libuser.UIDNUMBER)[0],
+                                         groupEnt.get(libuser.GIDNUMBER)[0],
+                                         orig_uid, orig_gid)
+
                 if kwargs.get("password", False):
                     if kwargs.get("isCrypted", False):
                         password = kwargs["password"]
-- 
1.7.11.7



More information about the anaconda-patches mailing list