-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipfs_ls_recursive.hs
113 lines (70 loc) · 2.34 KB
/
ipfs_ls_recursive.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
import Data.List
import System.Process
import System.Environment
import qualified Data.ByteString.Lazy.Char8 as B
main = do
a<- getArgs
r<-run (head a) $ B.pack ""
B.putStrLn $ B.unlines r
--mapM_ B.putStrLn r
ipfs_ls hash = "ipfs ls " ++ hash
ipfs_ls' hash = do
--(runCommand $ "echo \"\" > " ++ output) >>= waitForProcess
(runCommand $ ipfs_ls hash ++ " > " ++ output) >>= waitForProcess
s <- B.readFile output
return $ B.lines s
where
output = "ipfs_ls_hashes"
getFolders :: [B.ByteString] -> [B.ByteString]
getFolders s = filter isFolder s
run hash prep
|isHash hash = do
x<- ipfs_ls' hash
case (all lineIsSane x) of
True -> do
y<- mapM (\x-> run (B.unpack $ strip_to_hash x)
(B.append prep $ strip_to_name x)
) $ getFolders x
return $
(map (output.split_to_hash_and_name) x)
--x
-- ++ [B.pack $ show $ all lineIsSane x]
-- ++ [B.pack "--------------"]
++ (concat y )
False -> return $
[B.pack "!!!!!!!!!!!!!!!!"]
++ x
++ [B.pack "!!!!!!!!!!!!!!!!"]
|otherwise = return [B.pack (hash ++ " !!!!!!!!" )]
where
output (h,n) = B.append h $ B.append (B.pack " ") $ B.append prep n
--prepend p s = B.append p $ strip_to_name s
strip_to_hash s = head $ B.words s
strip_to_name s = B.unwords $ drop 2 $ B.words s
split_to_hash_and_name s = (strip_to_hash s, strip_to_name s)
isFolder :: B.ByteString -> Bool
isFolder s
|lastChar == '/' = True
|otherwise = False
where
lastChar = B.last $ last $ B.words s
lineIsSane :: B.ByteString -> Bool
lineIsSane s = step1 w
where
step1 :: [B.ByteString] -> Bool
step1 (hash:size:name) = (isHash $ B.unpack hash)&&(isNumber size)&&((length name)>0)
step1 _ = False
isNumber s =
((length $ intersect "0123456789" $ B.unpack s)>0)
&&
((length $ intersect "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM{}:\"<>?,./[];'" $ B.unpack s)==0)
w = B.words s
isHash h
|(l)&&(qm h) = True
|otherwise = False
where
l
|(length h) == 46 = True
|otherwise = False
qm ('Q':'m':_) = True
qm _ = False