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,
stm,
time >= 1.4.2,
unix
unix,
unix-bytestring
hs-source-dirs: src
default-language: Haskell2010
Default-Extensions: RecordWildCards
@ -89,7 +90,8 @@ executable hsfm-gtk
stm,
time >= 1.4.2,
transformers,
unix
unix,
unix-bytestring
hs-source-dirs: src
default-language: Haskell2010
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 #-}
{-# LANGUAGE PackageImports #-}
-- |This module provides all the atomic IO related file operations like
-- copy, delete, move and so on. It operates primarily on `AnchoredFile`, which
@ -78,10 +79,10 @@ import System.Posix.Files
, unionFileModes
, removeLink
)
import System.Posix.IO
import qualified "unix" System.Posix.IO as SPI
import "unix-bytestring" System.Posix.IO.ByteString
(
closeFd
, createFile
fdWrite
)
import System.Posix.Types
(
@ -245,10 +246,10 @@ copyFile' from to = do
throwCantOpenDirectory $ P.dirname to
fromFstatus <- getSymbolicLinkStatus from'
fromContent <- BS.readFile from'
fd <- System.Posix.IO.createFile to'
fd <- SPI.createFile to'
(System.Posix.Files.fileMode fromFstatus)
closeFd fd
BS.writeFile to' fromContent
_ <- fdWrite fd fromContent
SPI.closeFd fd
-- |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
let fullp = fullPath td P.</> fn
throwFileDoesExist fullp
fd <- System.Posix.IO.createFile (P.fromAbs fullp) newFilePerms
closeFd fd
fd <- SPI.createFile (P.fromAbs fullp) newFilePerms
SPI.closeFd fd
createFile _ _ = throw $ InvalidOperation "wrong input type"