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
|
{-
- Copyright 2018 Raymond Hogenson
- This file is part of BlockCopy
- BlockCopy is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- BlockCopy is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with BlockCopy. If not, see <https://www.gnu.org/licenses/>.
-}
module Lib (blockCopyMain) where
import qualified Foreign.C.Error as E
import qualified System.Fuse as F
import qualified System.Posix.Types as T
import qualified Data.ByteString as B
import qualified System.FilePath as FP
import qualified Data.IORef as IOR
import qualified Data.Map as M
import qualified Control.Concurrent.ReadWriteLock as RWL
import qualified System.Posix.User as U
import qualified Control.Exception as Ex
import qualified System.Posix.Time as Ti
import qualified Data.ByteString.Char8 as C8
import qualified System.Environment as En
import qualified System.Directory as D
import qualified Control.Concurrent.ParallelIO as P
newtype Handle = Handle ()
data BlockCopy =
BlockCopy (IOR.IORef (M.Map Int RWL.RWLock)) FilePath
statsMessage :: BlockCopy -> IO B.ByteString
statsMessage (BlockCopy _ store) = do
let stats = "store: " ++ store ++ "\n"
return $ C8.pack stats
mkFuseGetFileStat :: BlockCopy -> FilePath -> IO (Either E.Errno F.FileStat)
mkFuseGetFileStat b s = do
ln ("fileStat on " ++ s)
userID <- U.getEffectiveUserID
groupID <- U.getEffectiveGroupID
statsSize <- fmap (fromIntegral . B.length) $ statsMessage b
case s of
"/" -> return $ Right $ F.FileStat F.Directory 0o777 1 userID groupID 0 0 0 0 0 0
"/disk" -> return $ Right $ F.FileStat F.RegularFile 0o666 1 userID groupID 0 maxBound 0 0 0 0
"/stats" -> return $ Right $ F.FileStat F.RegularFile 0o666 1 userID groupID 0 statsSize 0 0 0 0
_ -> return $ Left $ E.eNOENT
fuseReadSymbolicLink :: FilePath -> IO (Either E.Errno FilePath)
fuseReadSymbolicLink _ = return $ Left $ E.eNOENT
fuseCreateDevice :: FilePath -> F.EntryType -> T.FileMode -> T.DeviceID -> IO E.Errno
fuseCreateDevice _ _ _ _ = do
ln "createDevice"
return E.eNOTSUP
fuseCreateDirectory :: FilePath -> T.FileMode -> IO E.Errno
fuseCreateDirectory _ _ = do
ln "createDirectory"
return E.eNOTSUP
fuseRemoveLink :: FilePath -> IO E.Errno
fuseRemoveLink _ = do
ln "removeLink"
return E.eNOTSUP
fuseRemoveDirectory :: FilePath -> IO E.Errno
fuseRemoveDirectory _ = return E.eNOTSUP
fuseCreateSymbolicLink :: FilePath -> FilePath -> IO E.Errno
fuseCreateSymbolicLink _ _ = return E.eNOTSUP
fuseRename :: FilePath -> FilePath -> IO E.Errno
fuseRename _ _ = do
ln "rename"
return E.eNOTSUP
fuseCreateLink :: FilePath -> FilePath -> IO E.Errno
fuseCreateLink _ _ = return E.eNOTSUP
fuseSetFileMode :: FilePath -> T.FileMode -> IO E.Errno
fuseSetFileMode _ _ = return E.eNOTSUP
fuseSetOwnerAndGroup :: FilePath -> T.UserID -> T.GroupID -> IO E.Errno
fuseSetOwnerAndGroup _ _ _ = return E.eNOTSUP
mkFuseSetFileSize :: BlockCopy -> FilePath -> T.FileOffset -> IO E.Errno
mkFuseSetFileSize _ _ _ = return E.eOK
fuseSetFileTimes :: FilePath -> T.EpochTime -> T.EpochTime -> IO E.Errno
fuseSetFileTimes _ _ _ = return E.eNOTSUP
fuseOpen :: FilePath -> F.OpenMode -> F.OpenFileFlags
-> IO (Either E.Errno Handle)
fuseOpen "/disk" _ _ = do
ln "open /disk"
return $ Right $ Handle ()
fuseOpen "/stats" _ _ = return $ Right $ Handle ()
fuseOpen _ _ _ = return $ Left $ E.eNOENT
blockSize :: T.ByteCount
blockSize = 4096
blockPath :: BlockCopy -> Int -> FilePath
blockPath (BlockCopy _ store) block = store FP.</> show block
getLock :: BlockCopy -> Int -> IO RWL.RWLock
getLock (BlockCopy locks _) block = do
newLock <- RWL.new
IOR.atomicModifyIORef'
locks (\m ->
case M.lookup block m of
Nothing -> (M.insert block newLock m, newLock)
Just a -> (m, a))
withReadLocks :: BlockCopy -> [Int] -> IO a -> IO a
withReadLocks b blocks op = do
ls <- mapM (getLock b) blocks
mapM_ RWL.acquireRead ls
x <- op
mapM_ RWL.releaseRead ls
return x
withWriteLocks :: BlockCopy -> [Int] -> IO a -> IO a
withWriteLocks b blocks op = do
ls <- mapM (getLock b) blocks
mapM_ RWL.acquireWrite ls
x <- op
mapM_ RWL.releaseWrite ls
return x
readBlock :: BlockCopy -> Int -> IO B.ByteString
readBlock b block = do
x <- Ex.try
$ B.readFile
$ blockPath b block :: IO (Either Ex.SomeException B.ByteString)
case x of
Left e -> do
ln $ show e ++ ": read of non-existent block " ++ show block
return $ B.replicate (fromIntegral blockSize) 0
Right s -> return $ B.take (fromIntegral blockSize) s
mkFuseRead :: BlockCopy -> FilePath -> Handle -> T.ByteCount -> T.FileOffset
-> IO (Either E.Errno B.ByteString)
mkFuseRead store "/disk" _ count offset = do
ln $ "Read /disk count=" ++ show count ++ " offset=" ++ show offset
let block = fromIntegral offset `div` fromIntegral blockSize
let lastBlock = fromIntegral (offset + fromIntegral count - 1)
`div` fromIntegral blockSize
let blockOffset = offset `mod` fromIntegral blockSize
fmap
(Right . B.take (fromIntegral count) . B.drop (fromIntegral blockOffset))
$ withReadLocks store [block..lastBlock] $ fmap (B.intercalate B.empty)
$ P.parallel $ map (readBlock store) [block..lastBlock]
mkFuseRead b "/stats" _ count offset = do
bs <- statsMessage b
return $ Right $ B.take (fromIntegral count) $ B.drop (fromIntegral offset) bs
mkFuseRead _ _ _ _ _ = return $ Left E.eNOENT
ln :: String -> IO ()
ln s = do
time <- Ti.epochTime
_ <- Ex.try
$ appendFile "/home/ray/log"
$ "[" ++ show time ++ "] " ++ s ++ "\n" :: IO (Either Ex.SomeException ())
return ()
blit :: B.ByteString -> Int -> B.ByteString -> B.ByteString
blit a i x = B.intercalate B.empty [B.take i a, B.take (B.length a - i) x, B.drop (B.length x + i) a]
mkFuseWrite :: BlockCopy -> FilePath -> Handle -> B.ByteString -> T.FileOffset
-> IO (Either E.Errno T.ByteCount)
mkFuseWrite store "/disk" _ string offset = do
ln $ "Write to /disk of size " ++ show (B.length string)
let block = fromIntegral offset `div` fromIntegral blockSize
let lastBlock = fromIntegral (offset + fromIntegral (B.length string) - 1)
`div` fromIntegral blockSize
let blockOffset = offset `mod` fromIntegral blockSize
withWriteLocks store [block..lastBlock] $ P.parallel_
$ map (\bl ->
let mySuffix =
B.drop
((bl - 1) * fromIntegral blockSize + fromIntegral blockOffset)
string
in if bl == block && blockOffset /= 0
then do
currentContents <- readBlock store bl
B.writeFile (blockPath store bl)
(blit currentContents (fromIntegral blockOffset) string)
else if bl == lastBlock && B.length mySuffix < fromIntegral blockSize
then do
currentContents <- readBlock store bl
B.writeFile (blockPath store bl)
(blit currentContents 0 mySuffix)
else do
B.writeFile (blockPath store bl)
(B.take (fromIntegral blockSize) mySuffix))
[block..lastBlock]
return $ Right $ fromIntegral $ B.length string
mkFuseWrite _ _ _ _ _ = return $ Left E.eNOENT
fuseGetFileSystemStats :: String -> IO (Either E.Errno F.FileSystemStats)
fuseGetFileSystemStats _ = return $ Right $ F.FileSystemStats 512 0 0 0 1 0 255
fuseFlush :: FilePath -> Handle -> IO E.Errno
fuseFlush _ _ = return E.eOK
fuseRelease :: FilePath -> Handle -> IO ()
fuseRelease _ _ = return ()
fuseSynchronizeFile :: FilePath -> F.SyncType -> IO E.Errno
fuseSynchronizeFile _ _ = return E.eOK
fuseOpenDirectory :: FilePath -> IO E.Errno
fuseOpenDirectory _ = return E.eOK
mkFuseReadDirectory :: BlockCopy -> FilePath
-> IO (Either E.Errno [(FilePath, F.FileStat)])
mkFuseReadDirectory b _ = do
s <- mkFuseGetFileStat b "/disk"
k <- mkFuseGetFileStat b "/stats"
case (s, k) of
(Right a, Right g) -> return $ Right [("disk", a), ("stats", g)]
(Left e, _) -> return $ Left e
(_, Left e) -> return $ Left e
fuseReleaseDirectory :: FilePath -> IO E.Errno
fuseReleaseDirectory _ = return E.eOK
fuseSynchronizeDirectory :: FilePath -> F.SyncType -> IO E.Errno
fuseSynchronizeDirectory _ _ = return E.eOK
fuseAccess :: FilePath -> Int -> IO E.Errno
fuseAccess _ _ = return E.eOK
fuseInit :: IO ()
fuseInit = ln "init"
fuseDestroy :: IO ()
fuseDestroy = ln "destroy"
handler :: Ex.SomeException -> IO E.Errno
handler e = do
ln $ "ERROR: " ++ show e
return E.eFAULT
blockCopyMain :: IO ()
blockCopyMain = do
locks <- IOR.newIORef M.empty
prog <- En.getProgName
args <- En.getArgs
case args of
[] -> putStrLn "please store"
(store : mArgs) -> do
ln "main"
trueStore <- D.canonicalizePath store
let bs = BlockCopy locks trueStore
F.fuseRun prog mArgs
(F.FuseOperations (mkFuseGetFileStat bs) fuseReadSymbolicLink
fuseCreateDevice
fuseCreateDirectory fuseRemoveLink
fuseRemoveDirectory fuseCreateSymbolicLink fuseRename fuseCreateLink
fuseSetFileMode fuseSetOwnerAndGroup
(mkFuseSetFileSize bs) fuseSetFileTimes fuseOpen (mkFuseRead bs)
(mkFuseWrite bs) fuseGetFileSystemStats
fuseFlush fuseRelease fuseSynchronizeFile fuseOpenDirectory
(mkFuseReadDirectory bs) fuseReleaseDirectory
fuseSynchronizeDirectory fuseAccess fuseInit fuseDestroy)
handler
|