-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathFilePathTempWindows.swift
More file actions
137 lines (118 loc) · 4.39 KB
/
Copy pathFilePathTempWindows.swift
File metadata and controls
137 lines (118 loc) · 4.39 KB
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
/*
This source file is part of the Swift System open source project
Copyright (c) 2024 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
*/
#if os(Windows)
import WinSDK
/// Get the path to the system temporary directory.
internal func _getTemporaryDirectory() throws -> FilePath {
return try withUnsafeTemporaryAllocation(of: CInterop.PlatformChar.self,
capacity: Int(MAX_PATH) + 1) {
buffer in
guard GetTempPathW(DWORD(buffer.count), buffer.baseAddress) != 0 else {
throw Errno(windowsError: GetLastError())
}
return FilePath(SystemString(platformString: buffer.baseAddress!))
}
}
/// Invoke a closure for each file within a particular directory.
///
/// - Parameters:
/// - path: The path at which we should enumerate items.
/// - body: The closure that will be invoked.
///
/// We skip the `.` and `..` pseudo-entries.
fileprivate func forEachFile(
at path: FilePath,
_ body: (WIN32_FIND_DATAW) throws -> ()
) throws {
let searchPath = path.appending("\\*")
try searchPath.withPlatformString { szPath in
var findData = WIN32_FIND_DATAW()
let hFind = try szPath.withCanonicalPathRepresentation({ szPath in FindFirstFileW(szPath, &findData) })
if hFind == INVALID_HANDLE_VALUE {
throw Errno(windowsError: GetLastError())
}
defer {
FindClose(hFind)
}
repeat {
// Skip . and ..
if findData.cFileName.0 == 46
&& (findData.cFileName.1 == 0
|| (findData.cFileName.1 == 46
&& findData.cFileName.2 == 0)) {
continue
}
try body(findData)
} while FindNextFileW(hFind, &findData)
// FindNextFileW returns false both at the end of the enumeration and on
// error; only ERROR_NO_MORE_FILES is the normal terminator. Treating a
// transient error as end-of-directory would silently skip the remaining
// entries and leave the tree partially deleted.
let error = GetLastError()
if error != ERROR_NO_MORE_FILES {
throw Errno(windowsError: error)
}
}
}
/// Delete the entire contents of a directory, including its subdirectories.
///
/// - Parameters:
/// - path: The directory to be deleted.
///
/// Removes a directory completely, including all of its contents.
internal func _recursiveRemove(
at path: FilePath
) throws {
// First, deal with subdirectories
try forEachFile(at: path) { findData in
guard (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 else {
return
}
let name = withUnsafeBytes(of: findData.cFileName) {
return SystemString(platformString: $0.assumingMemoryBound(
to: CInterop.PlatformChar.self).baseAddress!)
}
let component = FilePath.Component(name)!
let subpath = path.appending(component)
// A directory that is also a reparse point (a junction or directory
// symlink) must not be recursed into: enumerating it would traverse into
// the *target* and delete its contents. Remove the link itself instead;
// RemoveDirectoryW deletes the reparse point without touching the target.
if (findData.dwFileAttributes & DWORD(FILE_ATTRIBUTE_REPARSE_POINT)) != 0 {
try subpath.withPlatformString { subpath in
if try !subpath.withCanonicalPathRepresentation({ RemoveDirectoryW($0) }) {
throw Errno(windowsError: GetLastError())
}
}
} else {
try _recursiveRemove(at: subpath)
}
}
// Now delete everything else
try forEachFile(at: path) { findData in
let name = withUnsafeBytes(of: findData.cFileName) {
return SystemString(platformString: $0.assumingMemoryBound(
to: CInterop.PlatformChar.self).baseAddress!)
}
let component = FilePath.Component(name)!
let subpath = path.appending(component)
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 {
try subpath.withPlatformString { subpath in
if try !subpath.withCanonicalPathRepresentation({ DeleteFileW($0) }) {
throw Errno(windowsError: GetLastError())
}
}
}
}
// Finally, delete the parent
try path.withPlatformString {
if try !$0.withCanonicalPathRepresentation({ RemoveDirectoryW($0) }) {
throw Errno(windowsError: GetLastError())
}
}
}
#endif // os(Windows)