Add hs function to easily change file ownership
authorMichele Tartara <mtartara@google.com>
Fri, 12 Jul 2013 16:10:47 +0000 (16:10 +0000)
committerMichele Tartara <mtartara@google.com>
Thu, 18 Jul 2013 08:34:55 +0000 (08:34 +0000)
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 <mtartara@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

src/Ganeti/Utils.hs

index 89a054d..51aa671 100644 (file)
@@ -56,19 +56,23 @@ module Ganeti.Utils
   , exitIfEmpty
   , splitEithers
   , recombineEithers
+  , setOwnerAndGroupFromNames
   ) where
 
 import Data.Char (toUpper, isAlphaNum, isDigit, isSpace)
 import Data.Function (on)
 import Data.List
+import qualified Data.Map as M
 import Control.Monad (foldM)
 
 import Debug.Trace
 
 import Ganeti.BasicTypes
 import qualified Ganeti.Constants as C
+import Ganeti.Runtime
 import System.IO
 import System.Exit
+import System.Posix.Files
 import System.Time
 
 -- * Debug functions
@@ -431,3 +435,16 @@ 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 -> GanetiDaemon -> GanetiGroup -> IO ()
+setOwnerAndGroupFromNames filename daemon dGroup = do
+  -- TODO: it would be nice to rework this (or getEnts) so that runtimeEnts
+  -- is read only once per daemon startup, and then cached for further usage.
+  runtimeEnts <- getEnts
+  ents <- exitIfBad "Can't find required user/groups" runtimeEnts
+  -- note: we use directly ! as lookup failures shouldn't happen, due
+  -- to the map construction
+  let uid = fst ents M.! daemon
+  let gid = snd ents M.! dGroup
+  setOwnerAndGroup filename uid gid