Debian Codegraph @

bin/hs/Index.hs

raw symbols SCIP
1 module Index (makeIndex) where
2
3 import Control.Monad
4 import Data.IORef
5 import Data.List
6 import Data.Time
7 import Data.Time.Clock.POSIX
8 import Database.HDBC.Sqlite3
9 import Mail
10 import Msg
11 import Param
12 import Sql
13 import System.EasyFile
14 import Text.Regex.Posix
15
16 ----------------------------------------------------------------
17
18 data Control = Control { toFolder :: FilePath -> String
19 , fromFolder :: String -> FilePath
20 , ignoreRegex :: String
21 , msgRegex :: String
22 , dirModFile :: String
23 , dbModTime :: Maybe Integer
24 , refDel :: IORef Int
25 , refReg :: IORef Int
26 , getmsg :: Msg -> IO [Msg]
27 , addmsg :: Msg -> IO ()
28 , delmsg :: Msg -> IO ()
29 , processDir :: String -> IO ()
30 , ignoreDir :: String -> IO ()
31 , skipDir :: String -> IO ()
32 }
33
34 defaultCtl :: Control
35 defaultCtl = Control { toFolder = undefined
36 , fromFolder = undefined
37 , ignoreRegex = defaultIgnoreRegex
38 , msgRegex = defaultMessageRegex
39 , dirModFile = defaultDirModFile
40 , dbModTime = Nothing
41 , refDel = undefined
42 , refReg = undefined
43 , getmsg = undefined
44 , addmsg = toBeAdded
45 , delmsg = toBeDeled
46 , processDir = process
47 , ignoreDir = ignore
48 , skipDir = skip
49 }
50
51 ----------------------------------------------------------------
52
53 process :: String -> IO ()
54 process = putStrLn
55
56 ignore :: String -> IO ()
57 ignore dir = putStrLn $ dir ++ " (ignored)"
58
59 skip :: String -> IO ()
60 skip dir = putStrLn $ dir ++ " (skipped)"
61
62 toBeDeled :: Msg -> IO ()
63 toBeDeled msg = putStrLn $ " " ++ path msg ++ " (to be deleted)"
64
65 toBeAdded :: Msg -> IO ()
66 toBeAdded msg = putStrLn $ " " ++ path msg ++ " (to be added)"
67
68 ----------------------------------------------------------------
69
70 makeIndex :: Bool -> Bool -> FilePath -> FilePath -> Regexp -> Maybe FilePath
71 -> IO (Int, Int)
72 makeIndex dryRun fullUpdate db dir re msubdir =
73 withNewDB db (not fullUpdate) $ \conn -> do
74 createDB conn
75 stime <- utctimeToInteger <$> getCurrentTime
76 ctl <- makeControl conn
77 walkDirectory (getTargetDir msubdir) ctl
78 indexDB conn
79 unless dryRun $ setDBModtime conn stime
80 results ctl
81 where
82 getTargetDir Nothing = dir
83 getTargetDir (Just subdir) = dir </> subdir
84 makeControl conn = do
85 let ctl0 = defaultCtl
86 ctl1 <- makeControl1 ctl0 dir re
87 ctl2 <- makeControl2 ctl1 fullUpdate conn
88 makeControl3 ctl2 dryRun conn
89 results ctl = do
90 registered <- readIORef (refReg ctl)
91 deleted <- readIORef (refDel ctl)
92 return (registered,deleted)
93
94 withNewDB :: FilePath -> Bool -> (Connection -> IO a) -> IO a
95 withNewDB db repl action = do
96 when repl $ do
97 exist <- doesFileExist db
98 when exist $ copyFile db newdb
99 ret <- withDB newdb action
100 renameFile newdb db
101 return ret
102 where
103 newdb = db ++ ".new"
104
105 makeControl1 :: Control -> FilePath -> String -> IO Control
106 makeControl1 ctl dir re = do
107 let dlen = length dir + 1
108 refd <- newIORef 0
109 refr <- newIORef 0
110 return ctl { toFolder = drop dlen
111 , fromFolder = (dir </>)
112 , ignoreRegex = re
113 , refDel = refd
114 , refReg = refr
115 }
116 -- Fullupdate
117 makeControl2 :: Control -> Bool -> Connection -> IO Control
118 makeControl2 ctl True _ = return ctl
119 makeControl2 ctl False conn = do
120 dbMod <- dbModtime conn
121 return ctl { dbModTime = dbMod }
122
123 -- Dryrun
124 makeControl3 :: Control -> Bool -> Connection -> IO Control
125 makeControl3 ctl True conn = do
126 gstmt <- prepareGet conn
127 return ctl { getmsg = getMsg gstmt }
128 makeControl3 ctl False conn = do
129 gstmt <- prepareGet conn
130 astmt <- prepareAdd conn
131 dstmt <- prepareDel conn
132 return ctl { getmsg = getMsg gstmt
133 , addmsg = addMsg astmt
134 , delmsg = delMsg dstmt
135 }
136
137 ----------------------------------------------------------------
138
139 walkDirectory :: FilePath -> Control -> IO ()
140 walkDirectory dir ctl = do
141 files <- filter (`notElem` [".", ".."]) <$> getDirectoryContents dir
142 forM_ files $ \file -> do
143 let file' = dir </> file
144 isDir <- doesDirectoryExist file'
145 if isDir
146 then do
147 isSym <- isSymlink file'
148 if isSym
149 then ignoreDir ctl (toFolder ctl file')
150 else handleDirectory file' ctl
151 else handleFile file' ctl
152
153 handleDirectory :: FilePath -> Control -> IO ()
154 handleDirectory dir ctl
155 | takeFileName dir =~ ignoreRegex ctl = ignoreDir ctl (toFolder ctl dir)
156 | otherwise = do
157 modified <- isModified
158 if modified
159 then do
160 processDir ctl (toFolder ctl dir)
161 walkDirectory dir ctl
162 else do
163 skipDir ctl (toFolder ctl dir)
164 mn <- getLinkCount dir
165 case mn of
166 Nothing -> walkDirectory dir ctl
167 Just n -> when (n > 2) $ walkDirectory dir ctl
168 where
169 isModified = case dbModTime ctl of
170 Nothing -> return True
171 Just dbmt -> do
172 let mtimeFile = dir </> dirModFile ctl
173 exist <- doesFileExist mtimeFile
174 if exist
175 then (dbmt <) <$> getModTime mtimeFile
176 else return True
177
178 handleFile :: FilePath -> Control -> IO ()
179 handleFile file ctl
180 | not (takeFileName file =~ msgRegex ctl) = return ()
181 | otherwise = do
182 modified <- isModified
183 when modified $ do
184 mmsg <- fileMsg file (toFolder ctl file)
185 case mmsg of
186 Nothing -> return ()
187 Just msg -> do
188 register <- deleteMsgIfMoved msg
189 when register (registerMsg msg)
190 where
191 isModified = case dbModTime ctl of
192 Nothing -> doesFileExist file
193 Just dbmt -> do
194 exist <- doesFileExist file
195 if exist
196 then do
197 tm <- getChangeTime file
198 case tm of
199 Just x -> return . (dbmt <) . utctimeToInteger $ x
200 Nothing -> (dbmt <) . utctimeToInteger <$> getModificationTime file
201 else return False
202 deleteMsgIfMoved msg = case dbModTime ctl of
203 Nothing -> return True
204 Just _ -> do
205 msgs <- getmsg ctl msg
206 msgs' <- filterM doesExist msgs
207 let msgsToDel = msgs \\ msgs'
208 mapM_ (delmsg ctl) msgsToDel
209 modifyIORef (refDel ctl) (+ length msgsToDel)
210 let exist = any (\m -> path m == path msg) msgs'
211 return (not exist)
212 doesExist m = doesFileExist $ fromFolder ctl $ path m
213 registerMsg msg = do
214 addmsg ctl msg
215 modifyIORef (refReg ctl) (+1)
216
217 ----------------------------------------------------------------
218
219 getModTime :: FilePath -> IO Integer
220 getModTime file = utctimeToInteger <$> getModificationTime file
221
222 utctimeToInteger :: UTCTime -> Integer
223 utctimeToInteger = truncate . toRational . utcTimeToPOSIXSeconds