make removeAuthorizedKeys robust if the file DNE
authorJoey Hess <joeyh@joeyh.name>
Wed, 2 Sep 2020 18:36:18 +0000 (14:36 -0400)
committerJoey Hess <joeyh@joeyh.name>
Wed, 2 Sep 2020 18:37:42 +0000 (14:37 -0400)
Noticed this could potentially crash, although the only thing using it
would normally create the file first, if something then deleted it..

Assistant/Ssh.hs
Utility/Tmp.hs

index e12c6bbfae4d642be6e610c45d3c84564394671b..5acab87ba9c9e606a73ebba374d857f4c681bf9c 100644 (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.
index 905bd8c301a2f76e27b09d2b08247b871bca7cf1..c4c16f429a0d4ed1472f725617cd88e9b4a2054d 100644 (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