-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07break.hs
More file actions
177 lines (155 loc) · 6.35 KB
/
07break.hs
File metadata and controls
177 lines (155 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TupleSections #-}
module Main (main) where
import Data.Bits
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8
import Data.ByteString (ByteString)
import System.Environment
import qualified Data.Map.Strict as M
import Control.Applicative
import Data.AffineSpace ((.-^))
import Data.Char (isUpper, isDigit)
import Data.Maybe (fromMaybe)
import Control.Lens
import Data.Thyme
newtype Parser a = Parser { getParser :: BS.ByteString -> Maybe (a, BS.ByteString) }
deriving (Functor)
instance Applicative Parser where
pure x = Parser (Just . (x,))
{-# INLINE pure #-}
Parser f <*> Parser x = Parser $! \s -> f s >>= \(f', s') -> x s' >>= \(x', s'') -> return $! (f' x', s'')
{-# INLINE (<*>) #-}
instance Alternative Parser where
empty = Parser (const Nothing)
Parser a <|> Parser b = Parser $! \s -> a s <|> b s
instance Monad Parser where
Parser a >>= f = Parser $ \s -> a s >>= \(!a', !s') -> getParser (f a') s'
{-# INLINE (>>=) #-}
getInt :: BS.ByteString -> Int
getInt = BS.foldl' (\acc n -> acc * 10 + fromIntegral (n - 0x30)) 0
{-# INLINE getInt #-}
getOctal :: BS.ByteString -> Int
getOctal = BS.foldl' (\acc n -> acc * 8 + fromIntegral (n - 0x30)) 0
{-# INLINE getOctal #-}
decimal :: Parser Int
decimal = getInt <$> takeWhile1 isDigit
{-# INLINE decimal #-}
char :: Char -> Parser ()
char c = Parser $ \s -> if BS.null s then Nothing else if BS8.head s == c then Just ((), BS.tail s) else Nothing
{-# INLINE char #-}
scientific :: Parser Double
scientific = do
d <- decimal
f <- fmap (fromMaybe 0) $ optional $ do
char '.'
s <- takeWhile1 isDigit
let ln = BS.length s
v = fromIntegral $ getInt s
return (v / (10 ^ ln))
return $! (fromIntegral d + f)
takeWhile1 :: (Char -> Bool) -> Parser BS.ByteString
takeWhile1 prd = Parser $ \s -> case BS8.span prd s of
("", _) -> Nothing
(a,b) -> Just (a,b)
{-# INLINE takeWhile1 #-}
parseOnly :: Parser a -> BS.ByteString -> Maybe a
parseOnly (Parser p) s = case p s of
Just (output, "") -> Just output
_ -> Nothing
data UnixFile = UnixFileGen { _fileInode :: !Int
, _fileHardLinks :: !Int
, _fileAtime :: !UTCTime
, _fileMtime :: !UTCTime
, _fileCtime :: !UTCTime
, _fileUser :: !ByteString
, _fileGroup :: !ByteString
, _fileBlocks :: !Int
, _fileType :: !FileType
, _filePerms :: !FPerms
, _fileSize :: !Int
, _filePath :: !ByteString
, _fileTarget :: !(Maybe ByteString)
} deriving (Show, Eq)
data FileType = TFile
| TDirectory
| TLink
| TPipe
| TSocket
| TBlock
| TChar
| TDoor
deriving (Eq, Show)
findline :: ByteString -> Maybe UnixFile
findline s = case BS8.split ' ' s of
(inode : hardlinks : atime : mtime : ctime : user : group : blocks : ftype : perms : size : rst) -> do
let (path, target) = case break (== "->") rst of
(a, []) -> (BS8.unwords a, Nothing)
(a, ["->"]) -> (BS8.unwords a, Nothing)
(a, b) -> (BS8.unwords a, Just (BS8.unwords b))
atime' <- parseOnly timestamp atime
mtime' <- parseOnly timestamp mtime
!ctime' <- parseOnly timestamp ctime
ft <- if BS8.length ftype == 1
then char2ft (BS8.head ftype)
else Nothing
return $! UnixFileGen (getInt inode)
(getInt hardlinks)
atime'
mtime'
ctime'
user
group
(getInt blocks)
ft
(FPerms $! getOctal perms)
(getInt size)
path
target
_ -> Nothing
char2ft :: Char -> Maybe FileType
char2ft x = case x of
'-' -> Just TFile
'f' -> Just TFile
'd' -> Just TDirectory
'l' -> Just TLink
'p' -> Just TPipe
's' -> Just TSocket
'b' -> Just TBlock
'c' -> Just TChar
'D' -> Just TDoor
_ -> Nothing
newtype FPerms = FPerms Int
deriving (Show, Ord, Eq, Num, Bits)
parseYMD :: Parser Day
parseYMD = do
!y <- decimal <* char '-'
!m <- decimal <* char '-'
!d <- decimal
return $! YearMonthDay y m d ^. from gregorian
parseDTime :: Parser DiffTime
parseDTime = do
!h <- decimal <* char ':'
!mi <- decimal <* char ':'
!s <- scientific
return $! fromSeconds $ fromIntegral (h * 3600 + mi * 60 :: Int) + s
timestamp :: Parser UTCTime
timestamp = do
!day <- parseYMD <* char '+'
!difftime <- parseDTime <* char '+'
let !tm = UTCTime day difftime ^. from utcTime
!tz <- takeWhile1 isUpper
return $! case tz of
"CEST" -> tm .-^ fromSeconds (7200 :: Int)
"CET" -> tm .-^ fromSeconds (3600 :: Int)
_ -> tm
parseFile :: FilePath -> IO [UnixFile]
parseFile fp = maybe (error "fail") id . mapM findline . BS8.lines <$> BS.readFile fp
main :: IO ()
main = do
parsed <- getArgs >>= mapM parseFile
let resultmap = M.fromList $ map (\f -> (_filePath f, f)) $ concat parsed
print $ M.size resultmap