The haskell library functions only allow to change file ownership using
uid/gid. A function for doing that with explicit names is added by this
commit.

Signed-off-by: Michele Tartara <[email protected]>
---
 src/Ganeti/Utils.hs |   12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/Ganeti/Utils.hs b/src/Ganeti/Utils.hs
index 89a054d..92b5ed8 100644
--- a/src/Ganeti/Utils.hs
+++ b/src/Ganeti/Utils.hs
@@ -56,6 +56,7 @@ module Ganeti.Utils
   , exitIfEmpty
   , splitEithers
   , recombineEithers
+  , setOwnerAndGroupFromNames
   ) where
 
 import Data.Char (toUpper, isAlphaNum, isDigit, isSpace)
@@ -69,6 +70,8 @@ import Ganeti.BasicTypes
 import qualified Ganeti.Constants as C
 import System.IO
 import System.Exit
+import System.Posix.Files
+import System.Posix.User
 import System.Time
 
 -- * Debug functions
@@ -431,3 +434,12 @@ recombineEithers lefts rights trail =
           recombiner (_,  ls, rs) t = Bad $ "Inconsistent trail log: l=" ++
                                       show ls ++ ", r=" ++ show rs ++ ",t=" ++
                                       show t
+
+-- | Set the owner and the group of a file (given as names, not numeric id).
+setOwnerAndGroupFromNames :: FilePath -> String -> String -> IO ()
+setOwnerAndGroupFromNames filename username groupname = do
+  let nameUid = fmap userID (getUserEntryForName username)
+      nameGid = fmap groupID (getGroupEntryForName groupname)
+  uid <- nameUid
+  gid <- nameGid
+  setOwnerAndGroup filename uid gid
-- 
1.7.10.4

Reply via email to