LIB: fix copyFile'

Previously, BS.writeFile would fail if the created file
was not writable, obviously. Now we use the file-descriptor
to write the content.
This commit is contained in:
Julian Ospald 2016-04-03 18:19:02 +02:00
parent bfcc2f39e5
commit fa7cab69c6
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
2 changed files with 13 additions and 10 deletions

View File

@ -44,7 +44,8 @@ library
safe, safe,
stm, stm,
time >= 1.4.2, time >= 1.4.2,
unix unix,
unix-bytestring
hs-source-dirs: src hs-source-dirs: src
default-language: Haskell2010 default-language: Haskell2010
Default-Extensions: RecordWildCards Default-Extensions: RecordWildCards
@ -89,7 +90,8 @@ executable hsfm-gtk
stm, stm,
time >= 1.4.2, time >= 1.4.2,
transformers, transformers,
unix unix,
unix-bytestring
hs-source-dirs: src hs-source-dirs: src
default-language: Haskell2010 default-language: Haskell2010
Default-Extensions: RecordWildCards Default-Extensions: RecordWildCards

View File

@ -17,6 +17,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
--} --}
{-# OPTIONS_HADDOCK ignore-exports #-} {-# OPTIONS_HADDOCK ignore-exports #-}
{-# LANGUAGE PackageImports #-}
-- |This module provides all the atomic IO related file operations like -- |This module provides all the atomic IO related file operations like
-- copy, delete, move and so on. It operates primarily on `AnchoredFile`, which -- copy, delete, move and so on. It operates primarily on `AnchoredFile`, which
@ -78,10 +79,10 @@ import System.Posix.Files
, unionFileModes , unionFileModes
, removeLink , removeLink
) )
import System.Posix.IO import qualified "unix" System.Posix.IO as SPI
import "unix-bytestring" System.Posix.IO.ByteString
( (
closeFd fdWrite
, createFile
) )
import System.Posix.Types import System.Posix.Types
( (
@ -245,10 +246,10 @@ copyFile' from to = do
throwCantOpenDirectory $ P.dirname to throwCantOpenDirectory $ P.dirname to
fromFstatus <- getSymbolicLinkStatus from' fromFstatus <- getSymbolicLinkStatus from'
fromContent <- BS.readFile from' fromContent <- BS.readFile from'
fd <- System.Posix.IO.createFile to' fd <- SPI.createFile to'
(System.Posix.Files.fileMode fromFstatus) (System.Posix.Files.fileMode fromFstatus)
closeFd fd _ <- fdWrite fd fromContent
BS.writeFile to' fromContent SPI.closeFd fd
-- |Copies the given file to the given file destination, overwriting it. -- |Copies the given file to the given file destination, overwriting it.
@ -407,8 +408,8 @@ createFile _ InvFN = throw InvalidFileName
createFile (ADirOrSym td) (ValFN fn) = do createFile (ADirOrSym td) (ValFN fn) = do
let fullp = fullPath td P.</> fn let fullp = fullPath td P.</> fn
throwFileDoesExist fullp throwFileDoesExist fullp
fd <- System.Posix.IO.createFile (P.fromAbs fullp) newFilePerms fd <- SPI.createFile (P.fromAbs fullp) newFilePerms
closeFd fd SPI.closeFd fd
createFile _ _ = throw $ InvalidOperation "wrong input type" createFile _ _ = throw $ InvalidOperation "wrong input type"