diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-06-10 11:12:52 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-06-10 11:12:52 +0200 |
commit | d5189cba07f63c3d2f8c575a31c1734f7c9aeed6 (patch) | |
tree | b8048e0ac75b8a48040ed9f77cf0710bdfabbde3 /src | |
parent | 36913cee57aa06357734cd1bfe3362a720b005a5 (diff) | |
download | trivmix-d5189cba07f63c3d2f8c575a31c1734f7c9aeed6.tar trivmix-d5189cba07f63c3d2f8c575a31c1734f7c9aeed6.tar.gz trivmix-d5189cba07f63c3d2f8c575a31c1734f7c9aeed6.tar.bz2 trivmix-d5189cba07f63c3d2f8c575a31c1734f7c9aeed6.tar.xz trivmix-d5189cba07f63c3d2f8c575a31c1734f7c9aeed6.zip |
Now pruning entire tree of directories created on startup.
Setting more sensible mode on directories created.
Diffstat (limited to 'src')
-rw-r--r-- | src/Trivmix.hs | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/src/Trivmix.hs b/src/Trivmix.hs index 3542e0d..0c1a1a4 100644 --- a/src/Trivmix.hs +++ b/src/Trivmix.hs | |||
@@ -12,6 +12,7 @@ import System.Directory | |||
12 | import System.FilePath | 12 | import System.FilePath |
13 | import System.Posix.Files | 13 | import System.Posix.Files |
14 | import System.Posix.IO | 14 | import System.Posix.IO |
15 | import System.Posix.Types | ||
15 | import System.Environment | 16 | import System.Environment |
16 | import System.Process | 17 | import System.Process |
17 | 18 | ||
@@ -95,9 +96,6 @@ optionParser = Options <$> | |||
95 | ) | 96 | ) |
96 | ) | 97 | ) |
97 | 98 | ||
98 | initialLevel :: Level | ||
99 | initialLevel = Lin 0 | ||
100 | |||
101 | watchedAttrs :: [EventVariety] | 99 | watchedAttrs :: [EventVariety] |
102 | watchedAttrs = [ Modify | 100 | watchedAttrs = [ Modify |
103 | , Move | 101 | , Move |
@@ -109,6 +107,23 @@ watchedAttrs = [ Modify | |||
109 | , DeleteSelf | 107 | , DeleteSelf |
110 | ] | 108 | ] |
111 | 109 | ||
110 | initialLevel :: Level | ||
111 | initialLevel = Lin 0 | ||
112 | |||
113 | defFileMode :: FileMode | ||
114 | defFileMode = foldl unionFileModes nullFileMode [ ownerReadMode | ||
115 | , ownerWriteMode | ||
116 | , groupReadMode | ||
117 | , groupWriteMode | ||
118 | , otherReadMode | ||
119 | ] | ||
120 | |||
121 | defDirectoryMode :: FileMode | ||
122 | defDirectoryMode = foldl unionFileModes nullFileMode [ ownerModes | ||
123 | , groupModes | ||
124 | , otherReadMode | ||
125 | , otherExecuteMode | ||
126 | ] | ||
112 | main :: IO () | 127 | main :: IO () |
113 | main = execParser opts >>= trivmix | 128 | main = execParser opts >>= trivmix |
114 | where | 129 | where |
@@ -164,37 +179,41 @@ handleFiles inotify level files = do | |||
164 | onStateFile :: FilePath -> String -> IO a -> IO a | 179 | onStateFile :: FilePath -> String -> IO a -> IO a |
165 | onStateFile file initial action = do | 180 | onStateFile file initial action = do |
166 | let directory = takeDirectory file | 181 | let directory = takeDirectory file |
167 | dirExists <- doesDirectoryExist directory | 182 | directories = iterate takeDirectory directory |
183 | createDirs <- takeWhileM (\d -> not <$> doesDirectoryExist d) directories | ||
168 | exists <- doesFileExist file | 184 | exists <- doesFileExist file |
169 | let acquireFile = case exists of | 185 | let acquireFile = case exists of |
170 | True -> return () | 186 | True -> return () |
171 | False -> do | 187 | False -> do |
172 | hPutStrLn stderr $ "Creating ‘" ++ file ++ "’ (file)" | 188 | hPutStrLn stderr $ "Creating ‘" ++ file ++ "’ (file)" |
173 | createFile file fileMode >>= closeFd >> writeFile file initial | 189 | createFile file defFileMode >>= closeFd >> writeFile file initial |
174 | fileMode = foldl unionFileModes nullFileMode [ ownerReadMode | ||
175 | , ownerWriteMode | ||
176 | , groupReadMode | ||
177 | , groupWriteMode | ||
178 | ] | ||
179 | releaseFile = case exists of | 190 | releaseFile = case exists of |
180 | True -> return () | 191 | True -> return () |
181 | False -> do | 192 | False -> do |
182 | hPutStrLn stderr $ "Removing ‘" ++ file ++ "’ (file)" | 193 | hPutStrLn stderr $ "Removing ‘" ++ file ++ "’ (file)" |
183 | removeFile file | 194 | removeFile file |
184 | acquireDir = case dirExists of | 195 | acquireDir = (flip mapM) (reverse createDirs) $ \directory -> do |
185 | True -> return () | ||
186 | False -> do | ||
187 | hPutStrLn stderr $ "Creating ‘" ++ directory ++ "’ (dir" | 196 | hPutStrLn stderr $ "Creating ‘" ++ directory ++ "’ (dir" |
188 | createDirectoryIfMissing True directory | 197 | createDirectory directory |
189 | releaseDir = case dirExists of | 198 | setFileMode directory defDirectoryMode |
190 | True -> return () | 199 | releaseDir = (flip mapM) createDirs $ \directory -> do |
191 | False -> do | ||
192 | hPutStrLn stderr $ "Removing ‘" ++ directory ++ "’ (dir)" | 200 | hPutStrLn stderr $ "Removing ‘" ++ directory ++ "’ (dir)" |
193 | removeDirectory directory | 201 | removeDirectory directory |
194 | acquire = acquireDir >> acquireFile | 202 | acquire = acquireDir >> acquireFile |
195 | release = releaseFile >> releaseDir | 203 | release = releaseFile >> releaseDir |
196 | bracket_ acquire release action | 204 | bracket_ acquire release action |
197 | 205 | ||
206 | takeWhileM :: Monad m => (a -> m Bool) -> [a] -> m [a] | ||
207 | takeWhileM _ [] = return [] | ||
208 | takeWhileM pred (x:xs) = do | ||
209 | take <- pred x | ||
210 | case take of | ||
211 | True -> do | ||
212 | rest <- takeWhileM pred xs | ||
213 | return $ x:rest | ||
214 | False -> do | ||
215 | return [] | ||
216 | |||
198 | readLevel :: Chan Level -> MVar Level -> FilePath -> IO () | 217 | readLevel :: Chan Level -> MVar Level -> FilePath -> IO () |
199 | readLevel levelChan current file = catch action handler | 218 | readLevel levelChan current file = catch action handler |
200 | where | 219 | where |