-
Notifications
You must be signed in to change notification settings - Fork 60
/
File.hsc
238 lines (202 loc) · 8.09 KB
/
File.hsc
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
-----------------------------------------------------------------------------
-- |
-- Module : System.Win32.File
-- Copyright : (c) Alastair Reid, 1997-2003
-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : Esa Ilari Vuokko <ei@vuokko.info>
-- Stability : provisional
-- Portability : portable
--
-- A collection of FFI declarations for interfacing with Win32.
--
-----------------------------------------------------------------------------
module System.Win32.WindowsString.File
( deleteFile
, copyFile
, moveFile
, moveFileEx
, setCurrentDirectory
, createDirectory
, createDirectoryEx
, removeDirectory
, getBinaryType
, createFile
, setFileAttributes
, getFileAttributes
, getFileAttributesExStandard
, findFirstChangeNotification
, getFindDataFileName
, findFirstFile
, defineDosDevice
, getDiskFreeSpace
, setVolumeLabel
, getFileExInfoStandard
, getFileExMaxInfoLevel
, module System.Win32.File
) where
import System.Win32.File.Internal
import System.Win32.File hiding (
deleteFile
, copyFile
, moveFile
, moveFileEx
, setCurrentDirectory
, createDirectory
, createDirectoryEx
, removeDirectory
, getBinaryType
, createFile
, setFileAttributes
, getFileAttributes
, getFileAttributesExStandard
, findFirstChangeNotification
, getFindDataFileName
, findFirstFile
, defineDosDevice
, getDiskFreeSpace
, setVolumeLabel
, getFileExInfoStandard
, getFileExMaxInfoLevel
)
import System.Win32.WindowsString.Types
import System.OsString.Windows
import Unsafe.Coerce (unsafeCoerce)
import Foreign hiding (void)
##include "windows_cconv.h"
#include <windows.h>
#include "alignment.h"
deleteFile :: WindowsString -> IO ()
deleteFile name =
withFilePath name $ \ c_name ->
failIfFalseWithRetry_ (unwords ["DeleteFile",show name]) $
c_DeleteFile c_name
copyFile :: WindowsString -> WindowsString -> Bool -> IO ()
copyFile src dest over =
withFilePath src $ \ c_src ->
withFilePath dest $ \ c_dest ->
failIfFalseWithRetry_ (unwords ["CopyFile",show src,show dest]) $
c_CopyFile c_src c_dest over
moveFile :: WindowsString -> WindowsString -> IO ()
moveFile src dest =
withFilePath src $ \ c_src ->
withFilePath dest $ \ c_dest ->
failIfFalseWithRetry_ (unwords ["MoveFile",show src,show dest]) $
c_MoveFile c_src c_dest
moveFileEx :: WindowsString -> Maybe WindowsString -> MoveFileFlag -> IO ()
moveFileEx src dest flags =
withFilePath src $ \ c_src ->
maybeWith withFilePath dest $ \ c_dest ->
failIfFalseWithRetry_ (unwords ["MoveFileEx",show src,show dest]) $
c_MoveFileEx c_src c_dest flags
setCurrentDirectory :: WindowsString -> IO ()
setCurrentDirectory name =
withFilePath name $ \ c_name ->
failIfFalse_ (unwords ["SetCurrentDirectory",show name]) $
c_SetCurrentDirectory c_name
createDirectory :: WindowsString -> Maybe LPSECURITY_ATTRIBUTES -> IO ()
createDirectory name mb_attr =
withFilePath name $ \ c_name ->
failIfFalseWithRetry_ (unwords ["CreateDirectory",show name]) $
c_CreateDirectory c_name (maybePtr mb_attr)
createDirectoryEx :: WindowsString -> WindowsString -> Maybe LPSECURITY_ATTRIBUTES -> IO ()
createDirectoryEx template name mb_attr =
withFilePath template $ \ c_template ->
withFilePath name $ \ c_name ->
failIfFalseWithRetry_ (unwords ["CreateDirectoryEx",show template,show name]) $
c_CreateDirectoryEx c_template c_name (maybePtr mb_attr)
removeDirectory :: WindowsString -> IO ()
removeDirectory name =
withFilePath name $ \ c_name ->
failIfFalseWithRetry_ (unwords ["RemoveDirectory",show name]) $
c_RemoveDirectory c_name
getBinaryType :: WindowsString -> IO BinaryType
getBinaryType name =
withFilePath name $ \ c_name ->
alloca $ \ p_btype -> do
failIfFalse_ (unwords ["GetBinaryType",show name]) $
c_GetBinaryType c_name p_btype
peek p_btype
----------------------------------------------------------------
-- HANDLE operations
----------------------------------------------------------------
createFile :: WindowsString -> AccessMode -> ShareMode -> Maybe LPSECURITY_ATTRIBUTES -> CreateMode -> FileAttributeOrFlag -> Maybe HANDLE -> IO HANDLE
createFile name access share mb_attr mode flag mb_h =
withFilePath name $ \ c_name ->
failIfWithRetry (==iNVALID_HANDLE_VALUE) (unwords ["CreateFile",show name]) $
c_CreateFile c_name access share (maybePtr mb_attr) mode flag (maybePtr mb_h)
setFileAttributes :: WindowsString -> FileAttributeOrFlag -> IO ()
setFileAttributes name attr =
withFilePath name $ \ c_name ->
failIfFalseWithRetry_ (unwords ["SetFileAttributes",show name])
$ c_SetFileAttributes c_name attr
getFileAttributes :: WindowsString -> IO FileAttributeOrFlag
getFileAttributes name =
withFilePath name $ \ c_name ->
failIfWithRetry (== 0xFFFFFFFF) (unwords ["GetFileAttributes",show name]) $
c_GetFileAttributes c_name
getFileAttributesExStandard :: WindowsString -> IO WIN32_FILE_ATTRIBUTE_DATA
getFileAttributesExStandard name = alloca $ \res -> do
withFilePath name $ \ c_name ->
failIfFalseWithRetry_ "getFileAttributesExStandard" $
c_GetFileAttributesEx c_name (unsafeCoerce getFileExInfoStandard) res
peek res
----------------------------------------------------------------
-- File Notifications
--
-- Use these to initialise, "increment" and close a HANDLE you can wait
-- on.
----------------------------------------------------------------
findFirstChangeNotification :: WindowsString -> Bool -> FileNotificationFlag -> IO HANDLE
findFirstChangeNotification path watch flag =
withFilePath path $ \ c_path ->
failIfNull (unwords ["FindFirstChangeNotification",show path]) $
c_FindFirstChangeNotification c_path watch flag
----------------------------------------------------------------
-- Directories
----------------------------------------------------------------
getFindDataFileName :: FindData -> IO WindowsString
getFindDataFileName fd = case unsafeCoerce fd of
(FindData fp) ->
withForeignPtr fp $ \p ->
peekTString ((# ptr WIN32_FIND_DATAW, cFileName ) p)
findFirstFile :: WindowsString -> IO (HANDLE, FindData)
findFirstFile str = do
fp_finddata <- mallocForeignPtrBytes (# const sizeof(WIN32_FIND_DATAW) )
withForeignPtr fp_finddata $ \p_finddata -> do
handle <- withFilePath str $ \tstr -> do
failIf (== iNVALID_HANDLE_VALUE) "findFirstFile" $
c_FindFirstFile tstr p_finddata
return (handle, unsafeCoerce (FindData fp_finddata))
----------------------------------------------------------------
-- DOS Device flags
----------------------------------------------------------------
defineDosDevice :: DefineDosDeviceFlags -> WindowsString -> Maybe WindowsString -> IO ()
defineDosDevice flags name path =
maybeWith withFilePath path $ \ c_path ->
withFilePath name $ \ c_name ->
failIfFalse_ "DefineDosDevice" $ c_DefineDosDevice flags c_name c_path
----------------------------------------------------------------
-- %fun GetDriveType :: Maybe String -> IO DriveType
getDiskFreeSpace :: Maybe WindowsString -> IO (DWORD,DWORD,DWORD,DWORD)
getDiskFreeSpace path =
maybeWith withFilePath path $ \ c_path ->
alloca $ \ p_sectors ->
alloca $ \ p_bytes ->
alloca $ \ p_nfree ->
alloca $ \ p_nclusters -> do
failIfFalse_ "GetDiskFreeSpace" $
c_GetDiskFreeSpace c_path p_sectors p_bytes p_nfree p_nclusters
sectors <- peek p_sectors
bytes <- peek p_bytes
nfree <- peek p_nfree
nclusters <- peek p_nclusters
return (sectors, bytes, nfree, nclusters)
setVolumeLabel :: Maybe WindowsString -> Maybe WindowsString -> IO ()
setVolumeLabel path name =
maybeWith withFilePath path $ \ c_path ->
maybeWith withFilePath name $ \ c_name ->
failIfFalse_ "SetVolumeLabel" $ c_SetVolumeLabel c_path c_name
----------------------------------------------------------------
-- End
----------------------------------------------------------------