File: Rules.hs

package info (click to toggle)
haskell-system-filepath 0.4.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 136 kB
  • sloc: haskell: 1,278; makefile: 2
file content (396 lines) | stat: -rw-r--r-- 13,381 bytes parent folder | download
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
-- |
-- Module: Filesystem.Path.Rules
-- Copyright: 2010 John Millikin
-- License: MIT
--
-- Maintainer:  jmillikin@gmail.com
-- Portability:  portable
--
module Filesystem.Path.Rules
  ( Rules
  , posix
  , posix_ghc702
  , posix_ghc704
  , windows
  , darwin
  , darwin_ghc702

  -- * Type conversions
  , toText
  , fromText
  , encode
  , decode
  , encodeString
  , decodeString

  -- * Rule‐specific path properties
  , valid
  , splitSearchPath
  , splitSearchPathString
  ) where

import           Prelude hiding (FilePath, null)
import qualified Prelude as P

import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import           Data.Char (toUpper, chr, ord)
import           Data.List (intersperse, intercalate)
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import           System.IO ()

import           Filesystem.Path hiding (root, filename, basename)
import           Filesystem.Path.Internal

-------------------------------------------------------------------------------
-- POSIX
-------------------------------------------------------------------------------

-- | Linux, BSD, and other UNIX or UNIX-like operating systems.
posix :: Rules B.ByteString
posix = Rules
  { rulesName = T.pack "POSIX"
  , valid = posixValid
  , splitSearchPath = posixSplitSearch
  , splitSearchPathString = posixSplitSearch . B8.pack
  , toText = posixToText
  , fromText = posixFromText
  , encode = posixToBytes
  , decode = posixFromBytes
  , encodeString = B8.unpack . posixToBytes
  , decodeString = posixFromBytes . B8.pack
  }

-- | Linux, BSD, and other UNIX or UNIX-like operating systems.
--
-- This is a variant of 'posix' for use with GHC 7.2, which tries to decode
-- file paths in its IO computations.
--
-- Since: 0.3.3 / 0.4.2
posix_ghc702 :: Rules B.ByteString
posix_ghc702 = posix
  { rulesName = T.pack "POSIX (GHC 7.2)"
  , splitSearchPathString = posixSplitSearchString posixFromGhc702String
  , encodeString = posixToGhc702String
  , decodeString = posixFromGhc702String
  }

-- | Linux, BSD, and other UNIX or UNIX-like operating systems.
--
-- This is a variant of 'posix' for use with GHC 7.4 or later, which tries to
-- decode file paths in its IO computations.
--
-- Since: 0.3.7 / 0.4.6
posix_ghc704 :: Rules B.ByteString
posix_ghc704 = posix
  { rulesName = T.pack "POSIX (GHC 7.4)"
  , splitSearchPathString = posixSplitSearchString posixFromGhc704String
  , encodeString = posixToGhc704String
  , decodeString = posixFromGhc704String
  }

posixToText :: FilePath -> Either T.Text T.Text
posixToText p = if good then Right text else Left text where
  good = and (map snd chunks)
  text = T.concat (root : map fst chunks)

  root = rootText (pathRoot p)
  chunks = intersperse (T.pack "/", True) (map unescape (directoryChunks p))

posixFromChunks :: [Chunk] -> FilePath
posixFromChunks chunks = FilePath root directories basename exts where
  (root, pastRoot) = if P.null (head chunks)
    then (Just RootPosix, tail chunks)
    else (Nothing, chunks)

  (directories, filename)
    | P.null pastRoot = ([], "")
    | otherwise = case last pastRoot of
      fn | fn == dot -> (goodDirs pastRoot, "")
      fn | fn == dots -> (goodDirs pastRoot, "")
      fn -> (goodDirs (init pastRoot), fn)

  goodDirs = filter (not . P.null)

  (basename, exts) = parseFilename filename

posixFromText :: T.Text -> FilePath
posixFromText text = if T.null text
  then empty
  else posixFromChunks (map escape (textSplitBy (== '/') text))

posixToBytes :: FilePath -> B.ByteString
posixToBytes p = B.concat (root : chunks) where
  root = B8.pack (rootChunk (pathRoot p))
  chunks = intersperse (B8.pack "/") (map chunkBytes (directoryChunks p))
  chunkBytes c = unescapeBytes' c

posixFromBytes :: B.ByteString -> FilePath
posixFromBytes bytes = if B.null bytes
  then empty
  else posixFromChunks $ flip map (B.split 0x2F bytes) $ \b -> case maybeDecodeUtf8 b of
    Just text -> escape text
    Nothing -> processInvalidUtf8 b

processInvalidUtf8 :: B.ByteString -> Chunk
processInvalidUtf8 bytes = intercalate "." textChunks where
  byteChunks = B.split 0x2E bytes
  textChunks = map unicodeDammit byteChunks
  unicodeDammit b = case maybeDecodeUtf8 b of
    Just t -> escape t
    Nothing -> map (\c -> if ord c >= 0x80
      then chr (ord c + 0xDC00)
      else c) (B8.unpack b)

posixToGhc702String :: FilePath -> String
posixToGhc702String p = P.concat (root : chunks) where
  root = rootChunk (pathRoot p)
  chunks = intersperse "/" (map escapeToGhc702 (directoryChunks p))

escapeToGhc702 :: Chunk -> String
escapeToGhc702 = map (\c -> if ord c >= 0xDC80 && ord c <= 0xDCFF
  then chr (ord c - 0xDC00 + 0xEF00)
  else c)

posixFromGhc702String :: String -> FilePath
posixFromGhc702String cs = if P.null cs
  then empty
  else posixFromChunks (map escapeFromGhc702 (splitBy (== '/') cs))

escapeFromGhc702 :: String -> String
escapeFromGhc702 = map (\c -> if ord c >= 0xEF80 && ord c <= 0xEFFF
  -- hopefully this isn't a valid UTF8 filename decoding to these
  -- codepoints, but there's no way to tell here.
  then chr (ord c - 0xEF00 + 0xDC00)
  else c)

posixToGhc704String :: FilePath -> String
posixToGhc704String p = P.concat (root : chunks) where
  root = rootChunk (pathRoot p)
  chunks = intersperse "/" (directoryChunks p)

posixFromGhc704String :: String -> FilePath
posixFromGhc704String cs = if P.null cs
  then empty
  else posixFromChunks (splitBy (== '/') cs)

posixValid :: FilePath -> Bool
posixValid p = validRoot && validDirectories where
  validDirectories = all validChunk (directoryChunks p)
  validChunk ch = not (any (\c -> c == '\0' || c == '/') ch)
  validRoot = case pathRoot p of
    Nothing -> True
    Just RootPosix -> True
    _ -> False

posixSplitSearch :: B.ByteString -> [FilePath]
posixSplitSearch = map (posixFromBytes . normSearch) . B.split 0x3A where
  normSearch bytes = if B.null bytes then B8.pack "." else bytes

posixSplitSearchString :: (String -> FilePath) -> String -> [FilePath]
posixSplitSearchString toPath = map (toPath . normSearch) . splitBy (== ':') where
  normSearch s = if P.null s then "." else s

-------------------------------------------------------------------------------
-- Darwin
-------------------------------------------------------------------------------

-- | Darwin and Mac OS X.
--
-- This is almost identical to 'posix', but with a native path type of 'T.Text'
-- rather than 'B.ByteString'.
--
-- Since: 0.3.4 / 0.4.3
darwin :: Rules T.Text
darwin = Rules
  { rulesName = T.pack "Darwin"
  , valid = posixValid
  , splitSearchPath = darwinSplitSearch
  , splitSearchPathString = darwinSplitSearch . TE.decodeUtf8 . B8.pack
  , toText = Right . darwinToText
  , fromText = posixFromText
  , encode = darwinToText
  , decode = posixFromText
  , encodeString = darwinToString
  , decodeString = darwinFromString
  }

-- | Darwin and Mac OS X.
--
-- This is a variant of 'darwin' for use with GHC 7.2 or later, which tries to
-- decode file paths in its IO computations.
--
-- Since: 0.3.4 / 0.4.3
darwin_ghc702 :: Rules T.Text
darwin_ghc702 = darwin
  { rulesName = T.pack "Darwin (GHC 7.2)"
  , splitSearchPathString = darwinSplitSearch . T.pack
  , encodeString = T.unpack . darwinToText
  , decodeString = posixFromText . T.pack
  }

darwinToText :: FilePath -> T.Text
darwinToText p = T.concat (root : chunks) where
  root = rootText (pathRoot p)
  chunks = intersperse (T.pack "/") (map unescape' (directoryChunks p))

darwinToString :: FilePath -> String
darwinToString = B8.unpack . TE.encodeUtf8 . darwinToText

darwinFromString :: String -> FilePath
darwinFromString = posixFromText . TE.decodeUtf8 . B8.pack

darwinSplitSearch :: T.Text -> [FilePath]
darwinSplitSearch = map (posixFromText . normSearch) . textSplitBy (== ':') where
  normSearch text = if T.null text then T.pack "." else text

-------------------------------------------------------------------------------
-- Windows
-------------------------------------------------------------------------------

-- | Windows and DOS
windows :: Rules T.Text
windows = Rules
  { rulesName = T.pack "Windows"
  , valid = winValid
  , splitSearchPath = winSplit
  , splitSearchPathString = winSplit . T.pack
  , toText = Right . winToText
  , fromText = winFromText
  , encode = winToText
  , decode = winFromText
  , encodeString = T.unpack . winToText
  , decodeString = winFromText . T.pack
  }

winToText :: FilePath -> T.Text
winToText p = case pathRoot p of
  Just RootWindowsUnc{} -> uncToText p
  _ -> dosToText p

dosToText :: FilePath -> T.Text
dosToText p = T.concat (root : chunks) where
  root = rootText (pathRoot p)
  chunks = intersperse (T.pack "\\") (map unescape' (directoryChunks p))

uncToText :: FilePath -> T.Text
uncToText p = T.concat (root : chunks) where
  root = if all T.null chunks
    then rootText (pathRoot p)
    else rootText (pathRoot p) `T.append` T.pack "\\"
  chunks = intersperse (T.pack "\\") (filter (not . T.null) (map unescape' (directoryChunks p)))

winFromText :: T.Text -> FilePath
winFromText text = if T.null text then empty else path where
  path = FilePath root directories basename exts

  -- Windows has various types of absolute paths:
  --
  -- * C:\foo\bar -> DOS-style absolute path
  -- * \\?\C:\foo\bar -> extended-length absolute path
  -- * \\host\share\foo\bar -> UNC path
  -- * \\?\UNC\host\share\foo\bar -> extended-length UNC path
  --
  -- \foo\bar looks like an absolute path, but is actually a path
  -- relative to the current DOS drive.
  --
  -- http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
  (root, pastRoot) = if T.isPrefixOf (T.pack "\\\\") text
    then case stripUncasedPrefix (T.pack "\\\\?\\UNC\\") text of
      Just stripped -> parseUncRoot stripped True
      Nothing -> case T.stripPrefix (T.pack "\\\\?\\") text of
        Just stripped -> parseDosRoot stripped True
        Nothing -> case T.stripPrefix (T.pack "\\\\") text of
          Just stripped -> parseUncRoot stripped False
          Nothing -> parseDosRoot text False
    else case T.stripPrefix (T.pack "\\??\\") text of
      Just stripped -> parseDoubleQmark stripped
      Nothing -> parseDosRoot text False

  (directories, filename)
    | P.null pastRoot = ([], Nothing)
    | otherwise = case last pastRoot of
      fn | fn == T.pack "." -> (goodDirs pastRoot, Just "")
      fn | fn == T.pack ".." -> (goodDirs pastRoot, Just "")
      fn -> (goodDirs (init pastRoot), Just (escape fn))

  goodDirs :: [T.Text] -> [Chunk]
  goodDirs = map escape . filter (not . T.null)

  (basename, exts) = case filename of
    Just fn -> parseFilename fn
    Nothing -> (Nothing, [])

stripUncasedPrefix :: T.Text -> T.Text -> Maybe T.Text
stripUncasedPrefix prefix text = if T.toCaseFold prefix == T.toCaseFold (T.take (T.length prefix) text)
  then Just (T.drop (T.length prefix) text)
  else Nothing

parseDosRoot :: T.Text -> Bool -> (Maybe Root, [T.Text])
parseDosRoot text extended = parsed where
  split = textSplitBy (\c -> c == '/' || c == '\\') text

  head' = head split
  tail' = tail split
  parsed = if T.null head'
    then (Just RootWindowsCurrentVolume, tail')
    else if T.any (== ':') head'
      then (Just (parseDrive head'), tail')
        else (Nothing, split)

  parseDrive c = RootWindowsVolume (toUpper (T.head c)) extended

parseDoubleQmark :: T.Text -> (Maybe Root, [T.Text])
parseDoubleQmark text = (Just RootWindowsDoubleQMark, components) where
  components = textSplitBy (\c -> c == '/' || c == '\\') text

parseUncRoot :: T.Text -> Bool -> (Maybe Root, [T.Text])
parseUncRoot text extended = parsed where
  (host, pastHost) = T.break (== '\\') text
  (share, pastShare) = T.break (== '\\') (T.drop 1 pastHost)
  split = if T.null pastShare
    then []
    else textSplitBy (== '\\') pastShare
  parsed = (Just (RootWindowsUnc (T.unpack host) (T.unpack share) extended), split)

winValid :: FilePath -> Bool
winValid p = case pathRoot p of
  Nothing -> dosValid p
  Just RootWindowsCurrentVolume -> dosValid p
  Just (RootWindowsVolume v _) -> elem v ['A'..'Z'] && dosValid p
  Just (RootWindowsUnc host share _) -> uncValid p host share
  -- don't even try to validate \??\ paths
  Just RootWindowsDoubleQMark -> True
  Just RootPosix -> False

dosValid :: FilePath -> Bool
dosValid p = noReserved && validCharacters where
  reservedChars = map chr [0..0x1F] ++ "/\\?*:|\"<>"
  reservedNames =
    [ "AUX", "CLOCK$", "COM1", "COM2", "COM3", "COM4"
    , "COM5", "COM6", "COM7", "COM8", "COM9", "CON"
    , "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6"
    , "LPT7", "LPT8", "LPT9", "NUL", "PRN"
    ]

  noExt = p { pathExtensions = [] }
  noReserved = flip all (directoryChunks noExt)
    $ \fn -> notElem (map toUpper fn) reservedNames

  validCharacters = flip all (directoryChunks p)
    $ not . any (`elem` reservedChars)

uncValid :: FilePath -> String -> String -> Bool
uncValid _ "" _ = False
uncValid _ _ "" = False
uncValid p host share = ok host && ok share && all ok (dropWhileEnd P.null (directoryChunks p)) where
  ok ""  = False
  ok c = not (any invalidChar c)
  invalidChar c = c == '\x00' || c == '\\'

dropWhileEnd :: (a -> Bool) -> [a] -> [a]
dropWhileEnd p = foldr (\x xs -> if p x && P.null xs then [] else x : xs) []

winSplit :: T.Text -> [FilePath]
winSplit = map winFromText . filter (not . T.null) . textSplitBy (== ';')