From: Michele Tartara Date: Fri, 12 Jul 2013 16:10:47 +0000 (+0000) Subject: Add hs function to easily change file ownership X-Git-Tag: v2.8.0rc1~26 X-Git-Url: https://code.grnet.gr/git/ganeti-local/commitdiff_plain/a39cd547d3530d28c5aaeaed43cf452e70dab625 Add hs function to easily change file ownership 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 Reviewed-by: Iustin Pop --- diff --git a/src/Ganeti/Utils.hs b/src/Ganeti/Utils.hs index 89a054d..51aa671 100644 --- a/src/Ganeti/Utils.hs +++ b/src/Ganeti/Utils.hs @@ -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