Commit 0c09ecc2 authored by Klaus Aehlig's avatar Klaus Aehlig
Browse files

Make safeRenameFile create dirs with defined permissions



If, and only if, safeRenameFile creates a new directory, make sure
it has well defined permissions. While there, also optimize for the
common case. The main use of safeRenameFile is archiving jobs. As
we store them in directories of 10000 jobs each, in 99.99% of all
calls, the directory will already exist. So first try the rename
directly, and only if that fails, try creating the directory.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarPetr Pudlak <pudlak@google.com>
parent f23daea8
......@@ -545,7 +545,8 @@ archiveSomeJobsUntil replicateFn qDir endt cutt arch torepl (jid:jids) = do
then do
let live = liveJobFile qDir jid
archive = archivedJobFile qDir jid
renameResult <- safeRenameFile live archive
renameResult <- safeRenameFile queueDirPermissions
live archive
case renameResult of
Bad s -> do
logWarning $ "Renaming " ++ live ++ " to " ++ archive
......
......@@ -345,7 +345,8 @@ handleCall qlock _ cfg (ArchiveJob jid) = do
let mcs = Config.getMasterCandidates cfg
live = liveJobFile qDir jid
archive = archivedJobFile qDir jid
renameResult <- safeRenameFile live archive
renameResult <- safeRenameFile queueDirPermissions
live archive
putMVar qlock ()
case renameResult of
Bad s -> return . Bad . JobQueueError
......
......@@ -687,9 +687,15 @@ ensurePermissions fpath perms = do
else return . Bad $ show errors
-- | Safely rename a file, creating the target directory, if needed.
safeRenameFile :: FilePath -> FilePath -> IO (Result ())
safeRenameFile from to = do
result <- try $ do
createDirectoryIfMissing True $ takeDirectory to
renameFile from to
return $ either (Bad . show) Ok (result :: Either IOError ())
safeRenameFile :: FilePermissions -> FilePath -> FilePath -> IO (Result ())
safeRenameFile perms from to = do
directtry <- try $ renameFile from to
case (directtry :: Either IOError ()) of
Right () -> return $ Ok ()
Left _ -> do
result <- try $ do
let dir = takeDirectory to
createDirectoryIfMissing True dir
_ <- ensurePermissions dir perms
renameFile from to
return $ either (Bad . show) Ok (result :: Either IOError ())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment