File: Windows.hs

package info (click to toggle)
git-annex 10.20250416-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 73,572 kB
  • sloc: haskell: 90,656; javascript: 9,103; sh: 1,469; makefile: 211; perl: 137; ansic: 44
file content (54 lines) | stat: -rw-r--r-- 1,771 bytes parent folder | download | duplicates (3)
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
{- Windows paths
 -
 - Copyright 2022-2023 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-tabs #-}

module Utility.Path.Windows (
	convertToWindowsNativeNamespace
) where

import Utility.Path
import Utility.OsPath
import qualified Utility.OsString as OS
import Utility.SystemDirectory
import Utility.FileSystemEncoding

import qualified Data.ByteString as B
import qualified System.FilePath.Windows as WinPath

{- Convert a filepath to use Windows's native namespace.
 - This avoids filesystem length limits.
 -
 - This is similar to the way base converts filenames on windows,
 - but as that is implemented in C (create_device_name) and not
 - exported, it cannot be used here. Several edge cases are not handled,
 - including network shares and dos short paths.
 -}
convertToWindowsNativeNamespace :: RawFilePath -> IO RawFilePath
convertToWindowsNativeNamespace f
	| win32_dev_namespace `B.isPrefixOf` f = return f
	| win32_file_namespace `B.isPrefixOf` f = return f
	| nt_device_namespace `B.isPrefixOf` f = return f
	| otherwise = do
		-- Make absolute because any '.' and '..' in the path
		-- will not be resolved once it's converted.
		cwd <- getCurrentDirectory
		let p = simplifyPath (combine cwd (toOsPath f))
		-- If the input path is absolute but does not include a drive,
		-- add the drive from the cwd, because a path in the native
		-- namespace must include a drive.
		let p' = if OS.null (takeDrive p)
			then joinDrive (takeDrive cwd) p
			else p
		-- Normalize slashes.
		let p'' = encodeBS $ WinPath.normalise $ fromOsPath p'
		return (win32_file_namespace <> p'')
  where
	win32_dev_namespace = "\\\\.\\"
	win32_file_namespace = "\\\\?\\"
	nt_device_namespace = "\\Device\\"