18import qualified Codec.Archive.Zip as Zip
19import Control.Exception (catch)
20import qualified Data.Binary as Binary
21import qualified Data.ByteString as BS
22import qualified Data.ByteString.Internal as BS
23import qualified Data.ByteString.Builder as B
24import qualified Data.ByteString.Lazy as LBS
25import qualified Data.Fixed as Fixed
26import qualified Data.List as List
27import qualified Data.Time.Clock.POSIX as Time
28import qualified Foreign.ForeignPtr as FPtr
29import GHC.IO.Exception (IOException, IOErrorType(InvalidArgument))
30import qualified System.Directory as Dir
31import qualified System.FilePath as FP
32import System.FilePath ((</>))
33import qualified System.IO as IO
34import System.IO.Error (ioeGetErrorType, annotateIOError, modifyIOError)
41newtype Time = Time Fixed.Pico
45getTime :: FilePath -> IO Time
48 (Time . realToFrac . Time.utcTimeToPOSIXSeconds)
49-- TODO (Time . Time.nominalDiffTimeToSeconds . Time.utcTimeToPOSIXSeconds)
50 (Dir.getModificationTime path)
58instance Binary.Binary Time where
59 put (Time time) = Binary.put time
60 get = Time <$> Binary.get
67writeBinary :: (Binary.Binary a) => FilePath -> a -> IO ()
68writeBinary path value =
69 do let dir = FP.dropFileName path
70 Dir.createDirectoryIfMissing True dir
71 Binary.encodeFile path value
74readBinary :: (Binary.Binary a) => FilePath -> IO (Maybe a)
76 do pathExists <- Dir.doesFileExist path
79 do result <- Binary.decodeFileOrFail path
81 Right a -> return (Just a)
82 Left _ -> return Nothing
91writeUtf8 :: FilePath -> BS.ByteString -> IO ()
92writeUtf8 path content =
93 withUtf8 path IO.WriteMode $ \handle ->
94 BS.hPut handle content
97withUtf8 :: FilePath -> IO.IOMode -> (IO.Handle -> IO a) -> IO a
98withUtf8 path mode callback =
99 IO.withFile path mode $ \handle ->
100 do IO.hSetEncoding handle IO.utf8
108readUtf8 :: FilePath -> IO BS.ByteString
110 withUtf8 path IO.ReadMode $ \handle ->
111 modifyIOError (encodingError path) $
112 do fileSize <- catch (IO.hFileSize handle) useZeroIfNotRegularFile
113 let readSize = max 0 (fromIntegral fileSize) + 1
114 hGetContentsSizeHint handle readSize (max 255 readSize)
117useZeroIfNotRegularFile :: IOException -> IO Integer
118useZeroIfNotRegularFile _ =
122hGetContentsSizeHint :: IO.Handle -> Int -> Int -> IO BS.ByteString
123hGetContentsSizeHint handle =
126 readChunks chunks readSize incrementSize =
127 do fp <- BS.mallocByteString readSize
128 readCount <- FPtr.withForeignPtr fp $ \buf -> IO.hGetBuf handle buf readSize
129 let chunk = BS.PS fp 0 readCount
130 if readCount < readSize && readSize > 0
131 then return $! BS.concat (reverse (chunk:chunks))
132 else readChunks (chunk:chunks) incrementSize (min 32752 (readSize + incrementSize))
135encodingError :: FilePath -> IOError -> IOError
136encodingError path ioErr =
137 case ioeGetErrorType ioErr of
140 (userError "Bad encoding; the file must be valid UTF-8")
153writeBuilder :: FilePath -> B.Builder -> IO ()
154writeBuilder path builder =
155 IO.withBinaryFile path IO.WriteMode $ \handle ->
156 do IO.hSetBuffering handle (IO.BlockBuffering Nothing)
157 B.hPutBuilder handle builder
164writePackage :: FilePath -> Zip.Archive -> IO ()
165writePackage destination archive =
166 case Zip.zEntries archive of
171 do let root = length (Zip.eRelativePath entry)
172 mapM_ (writeEntry destination root) entries
175writeEntry :: FilePath -> Int -> Zip.Entry -> IO ()
176writeEntry destination root entry =
178 path = drop root (Zip.eRelativePath entry)
180 if path == "LICENSE" || List.isPrefixOf "src/" path then
181 if not (null path) && last path == '/'
182 then Dir.createDirectoryIfMissing True (destination </> path)
183 else LBS.writeFile (destination </> path) (Zip.fromEntry entry)
192exists :: FilePath -> IO Bool
194 Dir.doesFileExist path
201remove :: FilePath -> IO ()
203 do exists_ <- Dir.doesFileExist path
205 then Dir.removeFile path
209removeDir :: FilePath -> IO ()
211 do exists_ <- Dir.doesDirectoryExist path
213 then Dir.removeDirectoryRecursive path
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list