File: RawFilePath.hs

package info (click to toggle)
git-annex 10.20251029-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 75,300 kB
  • sloc: haskell: 91,492; javascript: 9,103; sh: 1,593; makefile: 216; perl: 137; ansic: 44
file content (95 lines) | stat: -rw-r--r-- 3,139 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
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
{- Portability shim for basic operations on RawFilePaths.
 -
 - On unix, this makes syscalls using RawFilesPaths as efficiently as
 - possible.
 -
 - On Windows, filenames are in unicode, so RawFilePaths have to be
 - decoded. So this library will work, but less efficiently than using
 - FilePath would. However, this library also takes care to support long
 - filenames on Windows, by either using other libraries that do, or by
 - doing UNC-style conversion itself.
 -
 - Copyright 2019-2023 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

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

module Utility.RawFilePath (
	RawFilePath,
	readSymbolicLink,
	createSymbolicLink,
	createLink,
	getFileStatus,
	getSymbolicLinkStatus,
	setFileMode,
	setOwnerAndGroup,
	rename,
	createNamedPipe,
	fileAccess,
) where

#ifndef mingw32_HOST_OS
import Utility.FileSystemEncoding (RawFilePath)
import System.Posix.Files.ByteString

#else
import System.PosixCompat (FileStatus, FileMode)
-- System.PosixCompat does not handle UNC-style conversion itself,
-- so all uses of it library have to be pre-converted below. See
-- https://github.com/jacobstanley/unix-compat/issues/56
import qualified System.PosixCompat as P
import qualified System.Directory as D
import Utility.FileSystemEncoding
import Utility.Path.Windows

readSymbolicLink :: RawFilePath -> IO RawFilePath
readSymbolicLink f = toRawFilePath <$> P.readSymbolicLink (fromRawFilePath f)

createSymbolicLink :: RawFilePath -> RawFilePath -> IO ()
createSymbolicLink a b = do
	a' <- fromRawFilePath <$> convertToWindowsNativeNamespace a
	b' <- fromRawFilePath <$> convertToWindowsNativeNamespace b
	P.createSymbolicLink a' b'

createLink :: RawFilePath -> RawFilePath -> IO ()
createLink a b = do
	a' <- fromRawFilePath <$> convertToWindowsNativeNamespace a
	b' <- fromRawFilePath <$> convertToWindowsNativeNamespace b
	P.createLink a' b'

getFileStatus :: RawFilePath -> IO FileStatus
getFileStatus p = P.getFileStatus . fromRawFilePath
	=<< convertToWindowsNativeNamespace p

getSymbolicLinkStatus :: RawFilePath -> IO FileStatus
getSymbolicLinkStatus p = P.getSymbolicLinkStatus . fromRawFilePath
	=<< convertToWindowsNativeNamespace p

setFileMode :: RawFilePath -> FileMode -> IO () 
setFileMode p m = do
	p' <- fromRawFilePath <$> convertToWindowsNativeNamespace p
	P.setFileMode p' m

{- Using renamePath rather than the rename provided in unix-compat
 - because of this bug https://github.com/jacobstanley/unix-compat/issues/56 -}
rename :: RawFilePath -> RawFilePath -> IO ()
rename a b = D.renamePath (fromRawFilePath a) (fromRawFilePath b)

setOwnerAndGroup :: RawFilePath -> P.UserID -> P.GroupID -> IO ()
setOwnerAndGroup p u g = do
	p' <- fromRawFilePath <$> convertToWindowsNativeNamespace p
	P.setOwnerAndGroup p' u g

createNamedPipe :: RawFilePath -> FileMode -> IO ()
createNamedPipe p m = do
	p' <- fromRawFilePath <$> convertToWindowsNativeNamespace p
	P.createNamedPipe p' m

fileAccess :: RawFilePath -> Bool -> Bool -> Bool -> IO Bool
fileAccess p a b c = do
	p' <- fromRawFilePath <$> convertToWindowsNativeNamespace p
	P.fileAccess p' a b c
#endif