cautious-file 0.1.5 → 1.0.2
raw patch · 5 files changed
Files
- Test.hs +1/−1
- cautious-file.cabal +7/−7
- src/System/IO/Cautious.hs +8/−5
- src/System/Posix/ByteLevel.hsc +1/−1
- src/System/Posix/Fsync.hsc +1/−1
Test.hs view
@@ -29,7 +29,7 @@ stdTestCase :: Int -> FilePath -> IO () stdTestCase dataLen outputFile = do- testData <- replicateM dataLen $ randomRIO ('\0', '\255')+ testData <- replicateM dataLen $ randomRIO ('\0', '\127') writeFile outputFile testData testData' <- readFile outputFile unless (testData == testData') . fail $ "stdTestCase: failed with " ++ show dataLen ++ " " ++ show outputFile
cautious-file.cabal view
@@ -1,23 +1,23 @@ name: cautious-file-version: 0.1.5+version: 1.0.2 Cabal-Version: >= 1.6 synopsis: Ways to write a file cautiously, to reduce the chances of problems such as data loss due to crashes or power failures-description: If the posix flag is enabled, posix-specific functions are used to reduce the chance of data loss further+description: On non-Windows systems, posix-specific functions are used to reduce the chance of data loss further category: System license: BSD3 license-file: LICENSE-copyright: Copyright (C) Robin Green 2009+copyright: Copyright (C) Robin Green 2009, 2011 author: Robin Green maintainer: Robin Green <greenrd@greenrd.org> build-type: Custom stability: experimental bug-reports: mailto:greenrd@greenrd.org-tested-with: GHC == 6.10.3+tested-with: GHC == 7.0.2 extra-source-files: Test.hs source-repository head type: darcs- location: http://patch-tag.com/publicrepos/cautious-file+ location: http://patch-tag.com/r/greenrd/cautious-file Flag posix description: Use POSIX-specific features@@ -25,8 +25,8 @@ Library hs-source-dirs: src- build-Depends: base >= 4, base < 5, directory, filepath, bytestring- if flag(posix)+ build-Depends: base >= 4, base < 5, directory >= 1.1, directory < 1.3, filepath >= 1.2, filepath < 1.4, bytestring >= 0.9, bytestring < 0.11+ if flag(posix) && !os(Windows) cpp-options: -D_POSIX build-Depends: unix exposed-modules: System.Posix.ByteLevel, System.Posix.Fsync
src/System/IO/Cautious.hs view
@@ -13,14 +13,14 @@ import Prelude hiding (writeFile) +import Control.Exception (tryJust)+import Control.Monad (guard) import Data.ByteString.Lazy.Char8 (ByteString, pack) import System.Directory (canonicalizePath, renameFile) import System.FilePath (splitFileName) import System.IO (openTempFile)-#ifdef _POSIX-import Control.Exception (tryJust)-import Control.Monad (guard) import System.IO.Error (isDoesNotExistError)+#ifdef _POSIX import System.Posix.ByteLevel (writeAllL) import System.Posix.Files (fileMode, getFileStatus, setFdMode) import System.Posix.Fsync (fsync)@@ -41,16 +41,19 @@ writeFileWithBackup :: IO () -> FilePath -> String -> IO () writeFileWithBackup backup fp = writeFileWithBackupL backup fp . pack +ignoreNotFound :: IO a -> IO (Either () a)+ignoreNotFound = tryJust (guard . isDoesNotExistError)+ -- | Backs up the old version of the file with "backup". "backup" must not fail if there is no -- old version of the file. writeFileWithBackupL :: IO () -> FilePath -> ByteString -> IO () writeFileWithBackupL backup fp bs = do- cfp <- canonicalizePath fp+ cfp <- either (const fp) id `fmap` ignoreNotFound (canonicalizePath fp) (tempFP, handle) <- uncurry openTempFile $ splitFileName cfp #ifdef _POSIX fd <- handleToFd handle writeAllL fd bs- tryJust (\e -> guard (isDoesNotExistError e) >> return ()) $ setFdMode fd . fileMode =<< getFileStatus cfp+ _ <- ignoreNotFound $ setFdMode fd . fileMode =<< getFileStatus cfp fsync fd closeFd fd #else
src/System/Posix/ByteLevel.hsc view
@@ -9,7 +9,7 @@ import Data.Function (fix) import Foreign.C.Error (throwErrnoIfMinus1Retry) import Foreign.C.String (CString, CStringLen)-import Foreign.C.Types (CInt, CSize)+import Foreign.C.Types (CInt(..), CSize(..)) import System.Posix.Types (ByteCount, Fd(..)) #include <unistd.h>
src/System/Posix/Fsync.hsc view
@@ -2,7 +2,7 @@ module System.Posix.Fsync (fsync) where import Foreign.C.Error (throwErrnoIfMinus1_)-import Foreign.C.Types (CInt)+import Foreign.C.Types (CInt(..)) import System.Posix.Types (Fd(..)) #include <unistd.h>