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
|
module CPM.FileUtil
( joinSearchPath
, copyDirectory
, createSymlink
, removeSymlink
, isSymlink, linkTarget, getRealPath
, copyDirectoryFollowingSymlinks
, quote
, tempDir
, cleanTempDir
, inTempDir
, inDirectory
, recreateDirectory
, removeDirectoryComplete
, safeReadFile, checkAndGetVisibleDirectoryContents
, whenFileExists, ifFileExists, writeFileIfNotExists
) where
import System.Directory ( doesFileExist, doesDirectoryExist
, setCurrentDirectory, getDirectoryContents
, getTemporaryDirectory, doesDirectoryExist
, createDirectory, createDirectoryIfMissing
, getAbsolutePath, getCurrentDirectory )
import System.Process ( system, exitWith, getPID )
import System.Environment ( getEnv )
import System.FilePath ( FilePath, replaceFileName, (</>)
, searchPathSeparator )
import Data.List ( intercalate, isPrefixOf, splitOn )
import Control.Monad ( when )
import System.IOExts ( evalCmd, readCompleteFile )
import CPM.Helpers ( stripSpaces )
joinSearchPath :: [FilePath] -> String
joinSearchPath = intercalate [searchPathSeparator] . map emptyPath2Dot
where
emptyPath2Dot p = if null p then "." else p
copyDirectory :: String -> String -> IO ()
copyDirectory src dst = do
retCode <- system $ "cp -pR \"" ++ src ++ "\" \"" ++ dst ++ "\""
if retCode /= 0
then error $ "Copy failed with " ++ (show retCode)
else return ()
copyDirectoryFollowingSymlinks :: String -> String -> IO ()
copyDirectoryFollowingSymlinks src dst = do
retCode <- system $ "cp -pLR \"" ++ src ++ "\" \"" ++ dst ++ "\""
if retCode /= 0
then error $ "Copy failed with " ++ (show retCode)
else return ()
createSymlink :: String -> String -> IO Int
createSymlink from to = system $ "ln -s " ++ (quote from) ++ " " ++ (quote to)
removeSymlink :: String -> IO Int
removeSymlink link = system $ "rm " ++ quote link
isSymlink :: String -> IO Bool
isSymlink link = do
(code, _, _) <- evalCmd "readlink" ["-n", link] ""
return $ code == 0
linkTarget :: String -> IO String
linkTarget link = do
(rc, out, _) <- evalCmd "readlink" ["-n", link] ""
return $ if rc == 0 then replaceFileName link out
else ""
getRealPath :: String -> IO String
getRealPath path = do
(rc, out, _) <- evalCmd "realpath" [path] ""
if rc == 0 then return (stripSpaces out)
else getAbsolutePath path
quote :: String -> String
quote s = "\"" ++ s ++ "\""
tempDir :: IO String
tempDir = do
t <- getTemporaryDirectory
pid <- getPID
return (t </> "cpm" ++ show pid)
cleanTempDir :: IO ()
cleanTempDir = tempDir >>= removeDirectoryComplete
inTempDir :: IO b -> IO b
inTempDir b = do
t <- tempDir
exists <- doesDirectoryExist t
if exists
then return ()
else createDirectory t
inDirectory t b
inDirectory :: String -> IO b -> IO b
inDirectory dir b = do
previous <- getCurrentDirectory
setCurrentDirectory dir
b' <- b
setCurrentDirectory previous
return b'
recreateDirectory :: String -> IO ()
recreateDirectory dir = do
removeDirectoryComplete dir
createDirectoryIfMissing True dir
removeDirectoryComplete :: String -> IO ()
removeDirectoryComplete dir = do
exists <- doesDirectoryExist dir
when exists $ system ("rm -Rf " ++ quote dir) >> return ()
safeReadFile :: String -> IO (Either IOError String)
safeReadFile fname = do
catch (readCompleteFile fname >>= return . Right)
(return . Left)
checkAndGetDirectoryContents :: FilePath -> IO [FilePath]
checkAndGetDirectoryContents dir = do
exdir <- doesDirectoryExist dir
if exdir then getDirectoryContents dir
else do putStrLn $ "ERROR: Directory '" ++ dir ++ "' does not exist!"
exitWith 1
checkAndGetVisibleDirectoryContents :: FilePath -> IO [FilePath]
checkAndGetVisibleDirectoryContents dir =
checkAndGetDirectoryContents dir >>= return . filter (not . isPrefixOf ".")
ifFileExists :: FilePath -> IO a -> IO a -> IO a
ifFileExists fname thenact elseact = do
exfile <- doesFileExist fname
if exfile then thenact else elseact
whenFileExists :: FilePath -> IO () -> IO ()
whenFileExists fname act =
ifFileExists fname act (return ())
writeFileIfNotExists :: FilePath -> String -> IO ()
writeFileIfNotExists fname cnt =
ifFileExists fname (return ()) (writeFile fname cnt)
|