-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuseOperations.hs
317 lines (272 loc) · 9.26 KB
/
FuseOperations.hs
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
{-# LANGUAGE TupleSections #-}
module FuseOperations where
import Control.Applicative
import Control.Monad
import Data.Maybe
import System.Fuse hiding (RegularFile)
import System.IO
import Data.IORef
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B
import Data.Map (Map)
import qualified Data.Map as M
import System.Posix.Files
import System.Posix.Types
import System.FilePath
import TagFS
import TagSet hiding (TagSet)
import Stat
data Status = Status
{ getTagSet :: TagSet
, getFileMapping :: Map File FilePath
, getRoute :: Route Entry
, save :: TagSet -> IO ()
}
--getRoute :: Status -> Route Entry
--getRoute = buildBaseRoute . getTagSet
resetRoute :: Status -> Status
resetRoute s = updateStatus s (getTagSet s)
getRealPath :: Status -> File -> FilePath
getRealPath s p = fromMaybe "/dev/null" $ M.lookup p (getFileMapping s)
newStatus :: TagSet -> Map File FilePath -> (TagSet -> IO ()) -> Status
newStatus ts m = Status ts m (buildBaseRoute ts)
updateStatus :: Status -> TagSet -> Status
updateStatus s ts = s { getTagSet = ts, getRoute = buildBaseRoute ts }
updateStatusRef :: IORef Status -> TagSet -> IO ()
updateStatusRef ref ts = do
s <- readIORef ref
let s' = updateStatus s ts
save s' (getTagSet s')
writeIORef ref s'
-- helper functions
returnLeft :: Monad m => a -> m (Either a b)
returnLeft = return . Left
returnRight :: Monad m => b -> m (Either a b)
returnRight = return . Right
getEntryStat :: Status -> FuseContext -> Entry -> IO FileStat
--getEntryStat s _ (RegularFile name) = realFileStat $ getRealPath s name
--getEntryStat _ ctx (RegularFile _) = return $ fileStat ctx (0 :: Integer)
getEntryStat _ ctx (RegularFile _) = return $ linkStat ctx (256 :: Integer)
getEntryStat _ ctx (TagFile ts _) = return $ fileStat ctx (tagFileContentLength ts)
getDirStat :: Status -> FuseContext -> Dir -> IO FileStat
getDirStat _ ctx _ = return $ dirStat ctx
tagFileContent :: [Tag] -> ByteString
tagFileContent = B.pack . unlines . map formatTag
tagFileContentLength :: [Tag] -> Int
tagFileContentLength = B.length . tagFileContent
parseTags :: ByteString -> [Tag]
parseTags = mapMaybe parseTag . lines . B.unpack
forFile :: Route Entry -> String -> IO a -> (Dir -> IO a) -> (Entry -> IO a)
-> IO a
forFile r p noent nofile f = do
let r' = route r p
case r' of
Nothing -> noent
Just (Left dir) -> nofile dir
Just (Right e) -> f e
forFile' :: Route Entry -> [String] -> IO a -> (Dir -> IO a)
-> (Entry -> IO a) -> IO a
forFile' r p noent nofile f = do
let r' = route' r p
case r' of
Nothing -> noent
Just (Left dir) -> nofile dir
Just (Right e) -> f e
forDir :: Route Entry -> String -> IO a -> (Entry -> IO a)
-> (Dir -> IO a) -> IO a
forDir r p noent nodir f = do
let r' = route r p
case r' of
Nothing -> noent
Just (Right e) -> nodir e
Just (Left dir) -> f dir
forDir' :: Route Entry -> [String] -> IO a -> (Entry -> IO a)
-> (Dir -> IO a) -> IO a
forDir' r p noent nodir f = do
let r' = route' r p
case r' of
Nothing -> noent
Just (Right e) -> nodir e
Just (Left dir) -> f dir
-- fuse operations
getFileStat :: IORef Status -> FilePath -> IO (Either Errno FileStat)
getFileStat ref p = do
ctx <- getFuseContext
status <- readIORef ref
let r = getRoute status
forFile r p (returnLeft eNOENT)
(\d -> Right <$> getDirStat status ctx d)
(\d -> Right <$> getEntryStat status ctx d)
tagfsReadSymbolicLink :: IORef Status -> FilePath -> IO (Either Errno FilePath)
tagfsReadSymbolicLink ref p = do
status <- readIORef ref
let r = getRoute status
forFile r p (returnLeft eNOENT) (\_ -> returnLeft eINVAL) $ \e -> case e of
RegularFile n -> do
let m = getFileMapping status
case M.lookup n m of
Nothing -> returnLeft eNOENT
Just n' -> returnRight n'
_ -> returnLeft eINVAL
-- directories
defaultStats :: FuseContext -> [(String, FileStat)]
defaultStats ctx = [(".", dirStat ctx), ("..", dirStat ctx)]
openDirectory :: IORef Status -> FilePath -> IO Errno
openDirectory _ _ = return eOK
readDirectory :: IORef Status -> FilePath
-> IO (Either Errno [(FilePath, FileStat)])
readDirectory ref p = do
ctx <- getFuseContext
status <- resetRoute <$> readIORef ref
-- reset route for further operations to save memory
writeIORef ref status
let r = getRoute status
let buildEntries entries = do
stats <- mapM (makeStat status ctx) entries
returnRight $ defaultStats ctx ++ stats
maybe (returnLeft eNOENT) (maybe (returnLeft eNOTDIR) buildEntries) $ routeDir r p
where
makeStat _ c (n, Left _) = return (n, dirStat c)
makeStat s c (n, Right e) = (n,) <$> getEntryStat s c e
softInit :: [a] -> [a]
softInit [] = []
softInit x = init x
createDirectory :: IORef Status -> FilePath -> FileMode -> IO Errno
createDirectory ref p _ = do
let seg = split p
let tag = parseTag (last seg)
status <- readIORef ref
let r = getRoute status
let r' = route' r seg
if isJust r'
then return eEXIST
else do
let r'' = route' r (softInit seg)
case (,) <$> tag <*> r'' of
Just (Simple v, Left (ExtendedBaseDir name))
-> newTag (Extended name v) status
Just (_, Left (ExtendedBaseDir _)) -> return ePERM
Just (tag', Left _) -> newTag tag' status
_ -> return eINVAL
where
newTag tag status = do
let ts = getTagSet status
let tsNew = createTag tag ts
updateStatusRef ref tsNew
return eOK
removeDirectory :: IORef Status -> FilePath -> IO Errno
removeDirectory ref p = do
let seg = split p
status <- readIORef ref
let r = getRoute status
let ts = getTagSet status
forDir' r seg (return eNOENT) (\_ -> return eNOTDIR) $ \x -> case x of
TagDir t -> do
let tsNew = wipeTag t ts
updateStatusRef ref tsNew
return eOK
ExtendedBaseDir name -> do
let tags' = filter ((== name) . getName) (tags ts)
let tsNew = foldr wipeTag ts tags'
updateStatusRef ref tsNew
return eOK
_ -> return ePERM
-- files
tempFile :: IO Handle
tempFile = do
(path, handle) <- openTempFile "/tmp" ""
removeLink path
return handle
toIOMode :: OpenMode -> OpenFileFlags -> IOMode
toIOMode ReadOnly _ = ReadMode
toIOMode WriteOnly (OpenFileFlags a _ _ _ _) = if a then AppendMode else WriteMode
toIOMode ReadWrite _ = ReadWriteMode
tagfsOpen :: IORef Status -> FilePath -> OpenMode -> OpenFileFlags
-> IO (Either Errno Handle)
tagfsOpen ref p mode flags = do
status <- readIORef ref
let r = getRoute status
let iomode = toIOMode mode flags
forFile r p (returnLeft eNOENT) (\_ -> returnLeft ePERM) $ \x -> case x of
RegularFile name -> do
h <- openFile (getRealPath status name) iomode
returnRight h
TagFile t _ -> do
h <- tempFile
when (iomode /= WriteMode) $ B.hPut h (tagFileContent t)
when (iomode /= AppendMode) $ hSeek h AbsoluteSeek 0
returnRight h
tagfsRead :: IORef Status -> FilePath -> Handle -> ByteCount -> FileOffset
-> IO (Either Errno ByteString)
tagfsRead _ _ h count offset = do
hSeek h AbsoluteSeek (toInteger offset)
Right <$> B.hGet h (fromInteger $ toInteger count)
tagfsWrite :: IORef Status -> FilePath -> Handle -> ByteString -> FileOffset
-> IO (Either Errno ByteCount)
tagfsWrite _ _ h content offset = do
hSeek h AbsoluteSeek (toInteger offset)
B.hPut h content
returnRight . fromInteger . toInteger $ B.length content
tagfsRelease :: IORef Status -> FilePath -> Handle -> IO ()
tagfsRelease ref p h = do
status <- readIORef ref
let r = getRoute status
forFile r p (return ()) (const $ return ()) $ \x -> case x of
TagFile _ name -> do
hSeek h AbsoluteSeek 0
content <- B.hGetContents h
let ts = getTagSet status
let tsNew = setTags (parseTags content) name ts
updateStatusRef ref tsNew
_ -> return ()
hClose h
tagfsSetFileSize :: IORef Status -> FilePath -> FileOffset -> IO Errno
tagfsSetFileSize ref p size = do
status <- readIORef ref
let r = getRoute status
forFile r p (return eNOENT) (const $ return eINVAL) $ \x -> case x of
RegularFile name
-> setFileSize (getRealPath status name) size >> return eOK
TagFile {} -> return eOK
tagfsCreateLink :: IORef Status -> FilePath -> FilePath -> IO Errno
tagfsCreateLink ref src dst = do
status <- readIORef ref
let r = getRoute status
let dstpath = dropFileName dst
forFile r src (return eNOENT) (const $ return eINVAL) $ \f -> case f of
RegularFile name ->
forDir r dstpath (return eNOENT) (const $ return eNOTDIR) $ \d -> case d of
TagDir tag -> do
let ts = getTagSet status
let tsNew = addTag tag name ts
updateStatusRef ref tsNew
return eOK
_ -> return eINVAL
_ -> return eINVAL
-- filesystem
getFileSystemStats :: String -> IO (Either Errno FileSystemStats)
getFileSystemStats _ = returnRight FileSystemStats
{ fsStatBlockSize = 510
, fsStatBlockCount = 1
, fsStatBlocksFree = 1
, fsStatBlocksAvailable = 1
, fsStatFileCount = 5 -- IS THIS CORRECT?
, fsStatFilesFree = 10 -- WHAT IS THIS?
, fsStatMaxNameLength = 255 -- SEEMS SMALL?
}
fsOps :: IORef Status -> FuseOperations Handle
fsOps r = defaultFuseOps
{ fuseGetFileStat = getFileStat r
, fuseReadSymbolicLink = tagfsReadSymbolicLink r
, fuseOpenDirectory = openDirectory r
, fuseReadDirectory = readDirectory r
, fuseCreateDirectory = createDirectory r
, fuseRemoveDirectory = removeDirectory r
, fuseOpen = tagfsOpen r
, fuseRead = tagfsRead r
, fuseWrite = tagfsWrite r
, fuseRelease = tagfsRelease r
, fuseSetFileSize = tagfsSetFileSize r
, fuseCreateLink = tagfsCreateLink r
, fuseGetFileSystemStats = getFileSystemStats
}