avoid using openFile when withFile can be used
Potentially fixes some FD leak if an action on an opened file handle fails for some reason. There have been some hard to reproduce reports of git-annex leaking FDs, and this may solve them.
This commit is contained in:
parent
fd1382f96f
commit
1572c460e8
6 changed files with 14 additions and 28 deletions
|
@ -52,8 +52,7 @@ associatedFiles key = do
|
||||||
associatedFilesRelative :: Key -> Annex [FilePath]
|
associatedFilesRelative :: Key -> Annex [FilePath]
|
||||||
associatedFilesRelative key = do
|
associatedFilesRelative key = do
|
||||||
mapping <- calcRepo $ gitAnnexMapping key
|
mapping <- calcRepo $ gitAnnexMapping key
|
||||||
liftIO $ catchDefaultIO [] $ do
|
liftIO $ catchDefaultIO [] $ withFile mapping ReadMode $ \h -> do
|
||||||
h <- openFile mapping ReadMode
|
|
||||||
fileEncoding h
|
fileEncoding h
|
||||||
lines <$> hGetContents h
|
lines <$> hGetContents h
|
||||||
|
|
||||||
|
|
|
@ -51,19 +51,15 @@ getAnnexLinkTarget file = ifM (coreSymlinks <$> Annex.getGitConfig)
|
||||||
| otherwise -> return Nothing
|
| otherwise -> return Nothing
|
||||||
Nothing -> fallback
|
Nothing -> fallback
|
||||||
|
|
||||||
probefilecontent f = do
|
probefilecontent f = withFile f ReadMode $ \h -> do
|
||||||
h <- openFile f ReadMode
|
|
||||||
fileEncoding h
|
fileEncoding h
|
||||||
-- The first 8k is more than enough to read; link
|
-- The first 8k is more than enough to read; link
|
||||||
-- files are small.
|
-- files are small.
|
||||||
s <- take 8192 <$> hGetContents h
|
s <- take 8192 <$> hGetContents h
|
||||||
-- If we got the full 8k, the file is too large
|
-- If we got the full 8k, the file is too large
|
||||||
if length s == 8192
|
if length s == 8192
|
||||||
then do
|
then return ""
|
||||||
hClose h
|
else
|
||||||
return ""
|
|
||||||
else do
|
|
||||||
hClose h
|
|
||||||
-- If there are any NUL or newline
|
-- If there are any NUL or newline
|
||||||
-- characters, or whitespace, we
|
-- characters, or whitespace, we
|
||||||
-- certianly don't have a link to a
|
-- certianly don't have a link to a
|
||||||
|
|
|
@ -478,10 +478,9 @@ recordStartTime = do
|
||||||
createAnnexDirectory $ parentDir f
|
createAnnexDirectory $ parentDir f
|
||||||
liftIO $ do
|
liftIO $ do
|
||||||
nukeFile f
|
nukeFile f
|
||||||
h <- openFile f WriteMode
|
withFile f WriteMode $ \h -> do
|
||||||
t <- modificationTime <$> getFileStatus f
|
t <- modificationTime <$> getFileStatus f
|
||||||
hPutStr h $ showTime $ realToFrac t
|
hPutStr h $ showTime $ realToFrac t
|
||||||
hClose h
|
|
||||||
where
|
where
|
||||||
showTime :: POSIXTime -> String
|
showTime :: POSIXTime -> String
|
||||||
showTime = show
|
showTime = show
|
||||||
|
|
|
@ -340,11 +340,8 @@ parseTransferFile file
|
||||||
bits = splitDirectories file
|
bits = splitDirectories file
|
||||||
|
|
||||||
writeTransferInfoFile :: TransferInfo -> FilePath -> IO ()
|
writeTransferInfoFile :: TransferInfo -> FilePath -> IO ()
|
||||||
writeTransferInfoFile info tfile = do
|
writeTransferInfoFile info tfile = writeFileAnyEncoding tfile $
|
||||||
h <- openFile tfile WriteMode
|
writeTransferInfo info
|
||||||
fileEncoding h
|
|
||||||
hPutStr h $ writeTransferInfo info
|
|
||||||
hClose h
|
|
||||||
|
|
||||||
{- File format is a header line containing the startedTime and any
|
{- File format is a header line containing the startedTime and any
|
||||||
- bytesComplete value. Followed by a newline and the associatedFile.
|
- bytesComplete value. Followed by a newline and the associatedFile.
|
||||||
|
@ -365,10 +362,8 @@ writeTransferInfo info = unlines
|
||||||
]
|
]
|
||||||
|
|
||||||
readTransferInfoFile :: Maybe PID -> FilePath -> IO (Maybe TransferInfo)
|
readTransferInfoFile :: Maybe PID -> FilePath -> IO (Maybe TransferInfo)
|
||||||
readTransferInfoFile mpid tfile = catchDefaultIO Nothing $ do
|
readTransferInfoFile mpid tfile = catchDefaultIO Nothing $
|
||||||
h <- openFile tfile ReadMode
|
readTransferInfo mpid <$> readFileStrictAnyEncoding tfile
|
||||||
fileEncoding h
|
|
||||||
hClose h `after` (readTransferInfo mpid <$> hGetContentsStrict h)
|
|
||||||
|
|
||||||
readTransferInfo :: Maybe PID -> String -> Maybe TransferInfo
|
readTransferInfo :: Maybe PID -> String -> Maybe TransferInfo
|
||||||
readTransferInfo mpid s = TransferInfo
|
readTransferInfo mpid s = TransferInfo
|
||||||
|
|
|
@ -145,9 +145,8 @@ storeEncrypted r buprepo (cipher, enck) k _p =
|
||||||
retrieve :: BupRepo -> Key -> AssociatedFile -> FilePath -> MeterUpdate -> Annex Bool
|
retrieve :: BupRepo -> Key -> AssociatedFile -> FilePath -> MeterUpdate -> Annex Bool
|
||||||
retrieve buprepo k _f d _p = do
|
retrieve buprepo k _f d _p = do
|
||||||
let params = bupParams "join" buprepo [Param $ bupRef k]
|
let params = bupParams "join" buprepo [Param $ bupRef k]
|
||||||
liftIO $ catchBoolIO $ do
|
liftIO $ catchBoolIO $ withFIle d WriteMode $
|
||||||
tofile <- openFile d WriteMode
|
pipeBup params Nothing . Just
|
||||||
pipeBup params Nothing (Just tofile)
|
|
||||||
|
|
||||||
retrieveCheap :: BupRepo -> Key -> FilePath -> Annex Bool
|
retrieveCheap :: BupRepo -> Key -> FilePath -> Annex Bool
|
||||||
retrieveCheap _ _ _ = return False
|
retrieveCheap _ _ _ = return False
|
||||||
|
|
|
@ -133,10 +133,8 @@ setSticky f = modifyFileMode f $ addModes [stickyMode]
|
||||||
- as writeFile.
|
- as writeFile.
|
||||||
-}
|
-}
|
||||||
writeFileProtected :: FilePath -> String -> IO ()
|
writeFileProtected :: FilePath -> String -> IO ()
|
||||||
writeFileProtected file content = do
|
writeFileProtected file content = withFile file WriteMode $ \h -> do
|
||||||
h <- openFile file WriteMode
|
|
||||||
void $ tryIO $
|
void $ tryIO $
|
||||||
modifyFileMode file $
|
modifyFileMode file $
|
||||||
removeModes [groupReadMode, otherReadMode]
|
removeModes [groupReadMode, otherReadMode]
|
||||||
hPutStr h content
|
hPutStr h content
|
||||||
hClose h
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue