hrfsize (empty) → 0.0.1
raw patch · 8 files changed
+220/−0 lines, 8 filesdep +basedep +hrfsizedep +processsetup-changed
Dependencies added: base, hrfsize, process, tasty, tasty-hunit
Files
- ChangeLog.md +3/−0
- LICENSE +30/−0
- README.md +15/−0
- Setup.hs +2/−0
- hrfsize.cabal +48/−0
- src/System/HrfSize.hs +39/−0
- stack.yaml +64/−0
- test/Spec.hs +19/−0
+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for hrfsize++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Dias Kozhabay (c) 2019++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Dias Kozhabay nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,15 @@+# hrfsize++## Simple Haskell library for determining size of file in human readable format+++Example:++```haskell++import System.HrfSize (determineFileSize)++main :: IO ()+main = determineFileSize "/path/to/file" >>= print++```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hrfsize.cabal view
@@ -0,0 +1,48 @@+cabal-version: 1.12++name: hrfsize+version: 0.0.1+description: Simple Haskell library for determining size of file in human readable format+synopsis: File size in human readable format+homepage: https://github.com/librerush/hrfsize#readme+bug-reports: https://github.com/librerush/hrfsize/issues+author: Dias Kozhabay <librerush@tutanota.com>+maintainer: Dias Kozhabay <librerush@tutanota.com>+license: BSD3+license-file: LICENSE+category: System+build-type: Simple+extra-source-files:+ README.md+ ChangeLog.md++source-repository head+ type: git+ location: https://github.com/librerush/hrfsize++library+ exposed-modules:+ System.HrfSize+ other-modules:+ Paths_hrfsize+ hs-source-dirs:+ src+ build-depends:+ base >=4.7 && <5+ default-language: Haskell2010++test-suite hrfsize-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ other-modules:+ Paths_hrfsize+ hs-source-dirs:+ test+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ build-depends:+ base >=4.7 && <5+ , tasty+ , tasty-hunit+ , process+ , hrfsize+ default-language: Haskell2010
+ src/System/HrfSize.hs view
@@ -0,0 +1,39 @@+module System.HrfSize+ ( determineFileSize,+ FileSize(..),+ convertSize+ ) where++import System.IO (FilePath, IOMode (ReadMode), hFileSize,+ withBinaryFile)++-- | IEC standard+data FileSize = Bytes Double | KiB Double | MiB Double+ | GiB Double | TiB Double+ deriving (Show, Eq, Ord)++convertSize :: Double -> FileSize+convertSize size | size < 1024.0 = Bytes $ trunc size+ | size < 1024.0 ^ (2 :: Int) =+ KiB $ trunc $ size / 1024.0+ | size < 1024.0 ^ (3 :: Int) =+ MiB $ trunc $ size / 1024.0 ^ (2 :: Int)+ | size < 1024.0 ^ (4 :: Int) =+ GiB $ trunc $ size / 1024.0 ^ (3 :: Int)+ | otherwise =+ TiB $ trunc $ size / 1024.0 ^ (4 :: Int)+ where+ -- it's crazy method of truncating+ trunc n | length (dropWhile (/= '.') $ show n) > 3 =+ read parsePrecision :: Double+ | otherwise = n+ where+ parsePrecision = let (a, b) = span (/= '.') $ show n in+ a ++ take 3 b++-- | Determines file's size+determineFileSize :: FilePath -> IO FileSize+determineFileSize pathToFile = do+ fileInt <- withBinaryFile pathToFile ReadMode hFileSize+ let file = fromInteger fileInt :: Double+ return $ convertSize file
+ stack.yaml view
@@ -0,0 +1,64 @@+# This file was automatically generated by 'stack init'+#+# Some commonly used options have been documented as comments in this file.+# For advanced use and comprehensive documentation of the format, please see:+# https://docs.haskellstack.org/en/stable/yaml_configuration/++# Resolver to choose a 'specific' stackage snapshot or a compiler version.+# A snapshot resolver dictates the compiler version and the set of packages+# to be used for project dependencies. For example:+#+# resolver: lts-3.5+# resolver: nightly-2015-09-21+# resolver: ghc-7.10.2+#+# The location of a snapshot can be provided as a file or url. Stack assumes+# a snapshot provided as a file might change, whereas a url resource does not.+#+# resolver: ./custom-snapshot.yaml+# resolver: https://example.com/snapshots/2018-01-01.yaml+resolver: lts-13.21++# User packages to be built.+# Various formats can be used as shown in the example below.+#+# packages:+# - some-directory+# - https://example.com/foo/bar/baz-0.0.2.tar.gz+# - location:+# git: https://github.com/commercialhaskell/stack.git+# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a+# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a+# subdirs:+# - auto-update+# - wai+packages:+- .+# Dependency packages to be pulled from upstream that are not in the resolver+# using the same syntax as the packages field.+# (e.g., acme-missiles-0.3)+# extra-deps: []++# Override default flag values for local packages and extra-deps+# flags: {}++# Extra package databases containing global packages+# extra-package-dbs: []++# Control whether we use the GHC we find on the path+# system-ghc: true+#+# Require a specific version of stack, using version ranges+# require-stack-version: -any # Default+# require-stack-version: ">=1.9"+#+# Override the architecture used by stack, especially useful on Windows+# arch: i386+# arch: x86_64+#+# Extra directories used by stack for building+# extra-include-dirs: [/path/to/dir]+# extra-lib-dirs: [/path/to/dir]+#+# Allow a newer minor version of GHC than the snapshot specifies+# compiler-check: newer-minor
+ test/Spec.hs view
@@ -0,0 +1,19 @@+import System.HrfSize+import Test.Tasty+import Test.Tasty.HUnit++main :: IO ()+main = do+ f1 <- determineFileSize "/tmp/file1"+ f2 <- determineFileSize "/tmp/file2"+ defaultMain (tests [f1, f2])++-- | Creating files using+-- `head -c size /dev/urandom > /tmp/fileN`+tests :: [FileSize] -> TestTree+tests files = testGroup "determineFileSize"+ [ testCase "file1" $+ MiB 1.95 @?= head files+ , testCase "file2" $+ Bytes 99.0 @?= head (tail files)+ ]