aboutsummaryrefslogtreecommitdiff
path: root/Annex
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2014-02-03 10:16:05 -0400
committerGravatar Joey Hess <joey@kitenet.net>2014-02-03 10:19:06 -0400
commit19fee049f6dfba4d4294e16d6d530bc7d0e78c54 (patch)
treec7888ba624904092b280223a34a044f0c2bdbd9a /Annex
parent6295ec998fb65607fb8c1d88035b41fa0cb3b0d6 (diff)
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.
Diffstat (limited to 'Annex')
-rw-r--r--Annex/Content/Direct.hs3
-rw-r--r--Annex/Link.hs10
2 files changed, 4 insertions, 9 deletions
diff --git a/Annex/Content/Direct.hs b/Annex/Content/Direct.hs
index 35d2c9b8d..f897cca6a 100644
--- a/Annex/Content/Direct.hs
+++ b/Annex/Content/Direct.hs
@@ -52,8 +52,7 @@ associatedFiles key = do
associatedFilesRelative :: Key -> Annex [FilePath]
associatedFilesRelative key = do
mapping <- calcRepo $ gitAnnexMapping key
- liftIO $ catchDefaultIO [] $ do
- h <- openFile mapping ReadMode
+ liftIO $ catchDefaultIO [] $ withFile mapping ReadMode $ \h -> do
fileEncoding h
lines <$> hGetContents h
diff --git a/Annex/Link.hs b/Annex/Link.hs
index 30d8c2ae8..234e4cb2a 100644
--- a/Annex/Link.hs
+++ b/Annex/Link.hs
@@ -51,19 +51,15 @@ getAnnexLinkTarget file = ifM (coreSymlinks <$> Annex.getGitConfig)
| otherwise -> return Nothing
Nothing -> fallback
- probefilecontent f = do
- h <- openFile f ReadMode
+ probefilecontent f = withFile f ReadMode $ \h -> do
fileEncoding h
-- The first 8k is more than enough to read; link
-- files are small.
s <- take 8192 <$> hGetContents h
-- If we got the full 8k, the file is too large
if length s == 8192
- then do
- hClose h
- return ""
- else do
- hClose h
+ then return ""
+ else
-- If there are any NUL or newline
-- characters, or whitespace, we
-- certianly don't have a link to a