-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
545 lines (466 loc) · 20.1 KB
/
Main.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
module Main where
import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as BS
import qualified Data.Map as M
import qualified Config as C
import Data.List
import Data.List.Split(chunk)
memory = fmap readMemory $ B.readFile "test.bin"
dirFiles = fmap (getDirFiles 0) memory
memory2 = fmap (truncateF "MyFile1.txt" 96) memory
dirFiles2 = fmap (getDirFiles 0) memory2
memory3 = fmap (truncateF "MyFile1.txt" 0) memory
dirFiles3 = fmap (getDirFiles 0) memory3
memory' = fmap (putFileInDir (4, "New.bin") 0) memory
dirFiles' = fmap (getDirFiles 0) memory'
memory'' = fmap (createFile "creadte" 0) memory'
dirFiles'' = fmap (getDirFiles 0) memory''
main = do
memory <- memory
undefined
type Filename = String
type Reference = Int
type DescIndex = Int
newtype Memory = Memory (M.Map Reference MemoryBlock) deriving (Show)
data MemoryBlock = MemoryBlock {
getContent :: B.ByteString,
isFree :: Bool
} deriving (Show)
data DriverInfo = DriverInfo {
getBlockSize :: Int,
getFileRefCount :: Int,
getMaxFilenameLength :: Int,
getMaxDescriptorCount :: Int
}
data MemorySession = MemorySession {
getFDs :: M.Map ReadFileDescriptor DescIndex,
getMax :: ReadFileDescriptor
}
emptyMemSes = MemorySession M.empty (-1)
type ReadFileDescriptor = Int
type DescriptorIndex = Int
data FileType = File | Folder | None deriving (Eq, Show)
data Descriptor = Descriptor {
getType :: FileType,
getSize :: Int,
getLinkCount :: Int,
getRefs :: [Reference]
}
instance Show Descriptor where
show (Descriptor fileType size linkCount refs) =
(show fileType) ++ ":" ++
(show size) ++ ":" ++
(intercalate ":" $ map show refs)
class Noneable a where
isNone :: a -> Bool
notNone :: a -> Bool
isFile :: a -> Bool
notNone = not . isNone
isNone = not . notNone
instance Noneable FileType where
isNone None = True
isNone _ = False
isFile File = True
isFile _ = False
instance Noneable Descriptor where
isNone = isNone . getType
isFile = isFile . getType
class HasDescriptors a where
readDescriptors :: a -> [Descriptor]
writeDescriptors :: [Descriptor] -> a -> a
setDescriptor :: DescriptorIndex -> Descriptor -> a -> a
takeDescriptor :: Int -> a -> Descriptor
takeDescriptor i = (!! i) . readDescriptors
instance HasDescriptors B.ByteString where
readDescriptors = map readDescriptor
. splitAtChunks C.descriptorLength
writeDescriptors descs = mappend descsStr . B.drop (B.length descsStr)
where descsStr = mconcat
. map showDescriptor
$ descs
setDescriptor i desc mem = mconcat [before, showDescriptor desc, after]
where (before, after) = B.splitAt (i*C.descriptorLength) mem
(_, after') = B.splitAt C.descriptorLength after
instance HasDescriptors MemoryBlock where
readDescriptors = readDescriptors . getContent
writeDescriptors descs = modifyContent $ writeDescriptors descs
setDescriptor i desc = modifyContent $ setDescriptor i desc
instance HasDescriptors Memory where
readDescriptors (Memory mem) = take C.maxDescriptorCount
. concat . M.map readDescriptors
$ mem
writeDescriptors descs (Memory mem) = Memory
. snd
. (\xs -> M.mapAccum saveMemory xs isFreeMap)
. splitAtChunks C.blockSize
. writeDescriptors descs
. M.foldr mappend mempty
. M.map getContent
$ mem
where isFreeMap = M.map isFree $ mem
saveMemory (x:xs) fr = (xs, MemoryBlock x fr)
setDescriptor i desc mem = flip writeDescriptors mem
. insertAt i desc
. readDescriptors
$ mem
splitAtChunks :: Int -> B.ByteString -> [B.ByteString]
splitAtChunks n = map (B.take n)
. takeWhile ((>0).(B.length))
. iterate (B.drop n)
modifyContent :: (B.ByteString -> B.ByteString) -> MemoryBlock -> MemoryBlock
modifyContent f (MemoryBlock cont free) = MemoryBlock (f cont) free
modifyMemory f (Memory mem) = Memory (f mem)
readFileType 0 = None
readFileType 1 = File
readFileType 2 = Folder
writeFileType None = B.singleton 0
writeFileType File = B.singleton 1
writeFileType Folder = B.singleton 2
setFileType fileType desc = desc { getType = fileType}
insertBetween :: ([a] -> ([a], [a])) -> a -> [a] -> [a]
insertBetween f el xs = before ++ (el:after)
where (before, _:after) = f xs
insertAt :: Int -> a -> [a] -> [a]
insertAt i = insertBetween (splitAt i)
insertWhen :: (Descriptor -> Bool) -> Descriptor -> [Descriptor] -> [Descriptor]
insertWhen f = insertBetween (break f)
insertWhenNone :: Descriptor -> [Descriptor] -> [Descriptor]
insertWhenNone = insertWhen isNone
readDescriptor :: B.ByteString -> Descriptor
readDescriptor str = Descriptor {
getType = readFileType fileType,
getSize = size,
getLinkCount = linkCount,
getRefs = refs
} where mapFst f (a, b) = (f a, b)
(fileType, str') = mapFst readNumber $ B.splitAt C.fileTypeLength str
(size, str'') = mapFst readNumber $ B.splitAt C.sizeLength str'
(linkCount, str''') = mapFst readNumber $ B.splitAt C.linkCountLength str''
refs = take C.maxRefCount . map (readNumber.fst) . tail
. iterate (B.splitAt C.refLength . snd)
$ (B.empty, str''')
showDescriptor :: Descriptor -> B.ByteString
showDescriptor (Descriptor fileType size linkCount refs) = mconcat $
[writeFileType fileType
,writeNumberN C.sizeLength size
,writeNumberN C.linkCountLength linkCount] ++
map (writeNumberN C.refLength) refs
showDescriptors :: [Descriptor] -> B.ByteString
showDescriptors = mconcat . map showDescriptor
writeNumberN :: Int -> Int -> B.ByteString
writeNumberN n = B.pack
. reverse
. map (fromIntegral . (`mod` 256))
. take n
. iterate (`div` 256)
writeNumbersN :: Int -> [Int] -> B.ByteString
writeNumbersN n = mconcat . map (writeNumberN n)
readNumberN :: Int -> B.ByteString -> Int
readNumberN n = readNumber . B.take n
readNumbersN :: Int -> B.ByteString -> [Int]
readNumbersN n = map (readNumberN n)
. takeWhile ((>=n) . B.length)
. iterate (B.drop n)
readNumber :: B.ByteString -> Int
readNumber = foldl1 (\acc b -> acc*256+b)
. map fromIntegral
. B.unpack
toByteString :: String -> B.ByteString
toByteString = B.pack . map BS.c2w
fromByteString :: B.ByteString -> String
fromByteString = map BS.w2c . B.unpack
readFiles :: B.ByteString -> [(DescIndex, Filename)]
readFiles =
let readPair = transformPair . B.splitAt C.refLength
transformPair (s1, s2) = (readNumberN C.refLength s1, fromByteString s2)
in map readPair
. splitAtChunks C.fileFieldLength
readDriverInfo :: String -> DriverInfo
readDriverInfo str =
case map read . lines $ str of
bs:frc:mfl:mdc:_ -> DriverInfo bs frc mfl mdc
_ -> error $ "Not DriverInfo format "
takeBlock :: Int -> Memory -> MemoryBlock
takeBlock i (Memory mem) = mem M.! i
readMemory :: B.ByteString -> Memory
readMemory str = calcMemoryFreedom
. map (B.take C.blockSize)
. takeWhile ((>0) . B.length)
. iterate (B.drop C.blockSize)
. B.append (B.pack (replicate addByteCount 0))
$ str
where len = B.length str
addByteCount = mod (-(mod len C.blockSize)) C.blockSize
calcMemoryFreedom :: [B.ByteString] -> Memory
calcMemoryFreedom blocks = Memory . snd
. M.mapAccum (\(x:xs) el -> (xs, (MemoryBlock x el))) blocks
. foldr (`M.insert` False) initMap
. concat
. map descriptorAllRefs
. filter notNone
$ descriptors
where descriptors = take C.maxDescriptorCount
. readDescriptors . mconcat $ blocks
blocksLen = length blocks
initMap = M.fromList initList
initList = zip [0..blocksLen-1] $ (replicate C.blocksForDescriptors False)
++ (repeat True)
descriptorAllRefs d = neededRefs ++ (if isFile d then take size moreRefs else [])
where size = getSize d
neededRefsCount = calcRefsCount size
neededRefs = take neededRefsCount refs
refs = getRefs d
moreRefs = concat
. map (readNumbersN C.refLength
. (blocks!!))
$ refs
getDirFiles :: DescriptorIndex -> Memory -> [(DescIndex, Filename)]
getDirFiles folderId mem = files
where folder = (!! folderId) . readDescriptors $ mem
size = getSize folder
refs = getRefs folder
files = take size
. concat
. map (readFiles
. getContent
. (`takeBlock` mem))
$ refs
showFileField :: (Reference, String) -> B.ByteString
showFileField (descindex, filename) = mappend
(writeNumberN 2 descindex) (toByteString filename)
putFileInDir :: (DescIndex, Filename) -> DescIndex -> Memory -> Memory
putFileInDir (descindex, filename) folderIndex mem@(Memory blocks) = memory'
where fileField = showFileField (descindex, filename)
folderDesc = takeDescriptor folderIndex mem
size = getSize folderDesc
refs = getRefs folderDesc
size' = size + 1
refsCount = calcFileFieldsCount size
refsCount' = calcFileFieldsCount size'
isNewBlock = refsCount' > refsCount
(freeBlockIndex,_) = head
. M.toList
. M.filter isFree
$ blocks
folderDesc' = folderDesc {
getSize = size',
getRefs = if isNewBlock
then insertAt refsCount freeBlockIndex refs
else refs
}
fieldIndexToWrite = mod size C.fileFieldsInBlock
addFileField = modifyContent $
mconcat
. insertAt fieldIndexToWrite fileField
. splitAtChunks C.fileFieldLength
changedBlock = (getRefs folderDesc') !! (refsCount'-1)
memory' = setDescriptor folderIndex folderDesc'
. Memory
. ( if isNewBlock
then M.adjust (setFreedom False) freeBlockIndex
else id
)
. M.adjust addFileField changedBlock
$ blocks
setFreedom :: Bool -> MemoryBlock -> MemoryBlock
setFreedom isFree block = block {isFree = isFree}
createFile :: Filename -> DescriptorIndex -> Memory -> Memory
createFile filename folderId mem = putFileInDir (desc'Index, filename)
folderId
$ writeDescriptors descriptors' mem
where descriptors = readDescriptors mem
descriptors' = insertWhenNone desc' descriptors
desc'Index = fst . head . dropWhile (notNone . snd)
$ zip [0,1..] descriptors
desc' = Descriptor {
getType = File,
getSize = 0,
getLinkCount = 1,
getRefs = replicate C.maxRefCount 0
}
filenameEquals :: Filename -> Filename -> Bool
filenameEquals f1 f2 = foldr strEq True $ zip (f1++(repeat '\NUL')) f2
where strEq ('0', '0') _ = True
strEq (x, '0') _ = False
strEq ('0', x) _ = False
strEq (x, y) b = x == y && b
findFileIndex :: Filename -> Memory -> DescIndex
findFileIndex filename mem = fst . head
. filter (filenameEquals filename . snd)
. getDirFiles 0
$ mem
openFile :: Filename -> Memory -> MemorySession
-> (ReadFileDescriptor, MemorySession)
openFile filename mem memSes =
(curId, memSes {
getFDs = M.insert curId fileDescIndex $ getFDs memSes,
getMax = curId
})
where fileDescIndex = findFileIndex filename mem
curId = 1 + (getMax memSes)
closeFile :: ReadFileDescriptor -> MemorySession -> MemorySession
closeFile fd memSes = memSes {
getFDs = M.delete fd $ getFDs memSes
}
clearBlock :: Reference -> Memory -> Memory
clearBlock index = modifyMemory
$ M.adjust (modifyContent $ const zeroString)
index
where zeroString = B.pack $ replicate C.blockSize 0
truncateF :: Filename -> Int -> Memory -> Memory
truncateF filename newSizeB mem = mem'
where fileDescIndex = findFileIndex filename mem
descriptor = takeDescriptor fileDescIndex mem
descriptor' = descriptor { getSize = newSize }
curSize = getSize descriptor
curRefCount = calcRefsCount curSize
newSize = div newSizeB C.blockSize
newRefCount = calcRefsCount newSize
(Memory blocks) = mem
refs = take curSize . concat
. map (readNumbersN C.refLength
. getContent
. (`takeBlock` mem))
$ getRefs descriptor
freeBlockRefs = map fst . M.toList $ M.filter isFree blocks
(freeBlockRefs1, rest) = splitAt (newSize - curSize) freeBlockRefs
freeBlockRefs2 = take (newRefCount-curRefCount) rest
refs' = chunk C.refsInBlock (refs ++ freeBlockRefs1)
refToRefs' = zip ((getRefs descriptor) ++ freeBlockRefs2) refs'
changedBlocks = drop (div curSize C.refsInBlock) refToRefs'
insertChangedBlock (i, refs) =
let str = writeNumbersN C.refLength refs
in M.adjust ( setFreedom False
. modifyContent (mappend str
. B.drop (B.length str))) i
freeMemBlock = MemoryBlock {
getContent = B.pack $ replicate C.blockSize 0,
isFree = False
}
mem' = if newSize <= curSize
then setDescriptor fileDescIndex descriptor'
. Memory . foldr (M.adjust $ setFreedom True) blocks
$ ((drop newSize refs)
++ (drop newRefCount $ getRefs descriptor))
else Memory
. flip (foldr insertChangedBlock) changedBlocks
. foldr (`M.insert` freeMemBlock) blocks
$ freeBlockRefs1
makeLink :: Filename -> Filename -> Memory -> Memory
makeLink source target mem = setDescriptor fileDescIndex descriptor'
. putFileInDir (fileDescIndex, target) 0
$ mem
where fileDescIndex = findFileIndex source mem
descriptor = takeDescriptor fileDescIndex mem
descriptor' = descriptor {
getLinkCount = 1 + (getLinkCount descriptor)
}
deleteLink :: Filename -> Memory -> Memory
deleteLink filename mem = freeBlocks
. changeFileDesriptor
. changeFolderDescriptor
. swapLinks
$ mem
where folder = takeDescriptor 0 mem
folder' = folder {
getSize = size - 1
}
changeFolderDescriptor = setDescriptor 0 folder'
size = getSize folder
refs = getRefs folder
refsCount = calcFileFieldsCount size
fields = take size . concat
. map (readFiles
. getContent
. (`takeBlock` mem))
$ refs
(before, (ind, field):after) = break (filenameEquals filename . snd . snd) $ zip [0..] fields
refIndex = div ind C.fileFieldsInBlock
inBlockIndex = mod ind C.fileFieldsInBlock
field' = snd $ last after
swapLinks = if null after then id
else modifyMemory $
M.adjust (modifyContent $
mconcat
. insertAt inBlockIndex (showFileField field')
. splitAtChunks C.fileFieldLength
) (refs !! refIndex)
freeBlocks = if mod size C.fileFieldsInBlock /= 1
then id
else modifyMemory $ M.adjust (setFreedom True) (refs !! (refsCount-1))
fileDescInd = fst field
fileDesc = takeDescriptor fileDescInd mem
newLinkCount' = getLinkCount fileDesc - 1
fileDesc' = fileDesc {
getLinkCount = newLinkCount'
}
fileSize = getSize fileDesc
fileRefsCount = calcRefsCount fileSize
fileRefs = take fileRefsCount (getRefs fileDesc)
fileMoreRefs = take fileSize . concat
. map (readNumbersN C.refLength
. getContent
. (`takeBlock` mem))
$ fileRefs
changeFileDesriptor =
if newLinkCount' > 0
then setDescriptor fileDescInd fileDesc'
else setDescriptor fileDescInd (setFileType None fileDesc')
. (modifyMemory $
flip (foldr (M.adjust $ setFreedom True))
(fileRefs ++ fileMoreRefs)
)
type Offset = Int
type Size = Int
write :: ReadFileDescriptor -> Offset -> String
-> MemorySession -> Memory -> Memory
write fd offset str memSes mem = (modifyMemory $
flip (foldr insertBlock) blockData)
$ mem
where insertBlock (i, block) =
M.adjust (modifyContent $ const block) i
fileDescIndex = (getFDs memSes) M.! fd
fileDesc = takeDescriptor fileDescIndex mem
fileSize = getSize fileDesc
refsCount = calcRefsCount fileSize
refs = take refsCount (getRefs fileDesc)
moreRefs = take fileSize . concat
. map ( readNumbersN C.refLength
. getContent
. (`takeBlock` mem))
$ refs
fileData = mconcat
. map ( getContent
. (`takeBlock` mem))
$ moreRefs
(before, rest) = B.splitAt offset fileData
rest' = snd $ B.splitAt (length str) rest
fileData' = B.take (fileSize*C.blockSize)
$ mconcat [before, toByteString str, rest']
blockData = drop (div offset C.blockSize)
. take (calcRefsCount (offset + (length str)))
. zip moreRefs
$ splitAtChunks C.blockSize fileData'
readFromFile :: ReadFileDescriptor -> Offset -> Size
-> MemorySession -> Memory -> String
readFromFile fd offset size memSes mem = fromByteString
. B.drop offset
. B.take (offset+size)
$ fileData
where fileDescIndex = (getFDs memSes) M.! fd
fileDesc = takeDescriptor fileDescIndex mem
fileSize = getSize fileDesc
refsCount = calcRefsCount fileSize
refs = take refsCount (getRefs fileDesc)
moreRefs = take fileSize . concat
. map ( readNumbersN C.refLength
. getContent
. (`takeBlock` mem))
$ refs
fileData = mconcat
. map ( getContent
. (`takeBlock` mem))
$ moreRefs
calcCount objInBlock size = 1 + (div (size-1) objInBlock)
calcRefsCount = calcCount C.refsInBlock
calcFileFieldsCount = calcCount C.fileFieldsInBlock