make removeAuthorizedKeys robust if the file DNE

Noticed this could potentially crash, although the only thing using it
would normally create the file first, if something then deleted it..
This commit is contained in:
Joey Hess 2020-09-02 14:36:18 -04:00
parent eed20fe3b7
commit 6361f7c310
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
2 changed files with 4 additions and 6 deletions

View file

@ -159,8 +159,10 @@ removeAuthorizedKeys gitannexshellonly dir pubkey = do
let keyline = authorizedKeysLine gitannexshellonly dir pubkey
sshdir <- sshDir
let keyfile = sshdir </> "authorized_keys"
ls <- lines <$> readFileStrict keyfile
viaTmp writeSshConfig keyfile $ unlines $ filter (/= keyline) ls
tryWhenExists (lines <$> readFileStrict keyfile) >>= \case
Just ls -> viaTmp writeSshConfig keyfile $
unlines $ filter (/= keyline) ls
Nothing -> noop
{- Implemented as a shell command, so it can be run on remote servers over
- ssh.

View file

@ -30,10 +30,6 @@ type Template = String
{- Runs an action like writeFile, writing to a temp file first and
- then moving it into place. The temp file is stored in the same
- directory as the final file to avoid cross-device renames.
-
- Note that the tmp file will have a file mode that only allows the
- current user to access it. The write action can change the mode
- to whatever is desired.
-}
viaTmp :: (MonadMask m, MonadIO m) => (FilePath -> v -> m ()) -> FilePath -> v -> m ()
viaTmp a file content = bracketIO setup cleanup use