FilePathTempWindows.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. This source file is part of the Swift System open source project
  3. Copyright (c) 2024 Apple Inc. and the Swift System project authors
  4. Licensed under Apache License v2.0 with Runtime Library Exception
  5. See https://swift.org/LICENSE.txt for license information
  6. */
  7. #if os(Windows)
  8. import WinSDK
  9. /// Get the path to the system temporary directory.
  10. internal func _getTemporaryDirectory() throws -> FilePath {
  11. return try withUnsafeTemporaryAllocation(of: CInterop.PlatformChar.self,
  12. capacity: Int(MAX_PATH) + 1) {
  13. buffer in
  14. guard GetTempPathW(DWORD(buffer.count), buffer.baseAddress) != 0 else {
  15. throw Errno(windowsError: GetLastError())
  16. }
  17. return FilePath(SystemString(platformString: buffer.baseAddress!))
  18. }
  19. }
  20. /// Invoke a closure for each file within a particular directory.
  21. ///
  22. /// - Parameters:
  23. /// - path: The path at which we should enumerate items.
  24. /// - body: The closure that will be invoked.
  25. ///
  26. /// We skip the `.` and `..` pseudo-entries.
  27. fileprivate func forEachFile(
  28. at path: FilePath,
  29. _ body: (WIN32_FIND_DATAW) throws -> ()
  30. ) rethrows {
  31. let searchPath = path.appending("\\*")
  32. try searchPath.withPlatformString { szPath in
  33. var findData = WIN32_FIND_DATAW()
  34. let hFind = try szPath.withCanonicalPathRepresentation({ szPath in FindFirstFileW(szPath, &findData) })
  35. if hFind == INVALID_HANDLE_VALUE {
  36. throw Errno(windowsError: GetLastError())
  37. }
  38. defer {
  39. FindClose(hFind)
  40. }
  41. repeat {
  42. // Skip . and ..
  43. if findData.cFileName.0 == 46
  44. && (findData.cFileName.1 == 0
  45. || (findData.cFileName.1 == 46
  46. && findData.cFileName.2 == 0)) {
  47. continue
  48. }
  49. try body(findData)
  50. } while FindNextFileW(hFind, &findData)
  51. }
  52. }
  53. /// Delete the entire contents of a directory, including its subdirectories.
  54. ///
  55. /// - Parameters:
  56. /// - path: The directory to be deleted.
  57. ///
  58. /// Removes a directory completely, including all of its contents.
  59. internal func _recursiveRemove(
  60. at path: FilePath
  61. ) throws {
  62. // First, deal with subdirectories
  63. try forEachFile(at: path) { findData in
  64. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 {
  65. let name = withUnsafeBytes(of: findData.cFileName) {
  66. return SystemString(platformString: $0.assumingMemoryBound(
  67. to: CInterop.PlatformChar.self).baseAddress!)
  68. }
  69. let component = FilePath.Component(name)!
  70. let subpath = path.appending(component)
  71. try _recursiveRemove(at: subpath)
  72. }
  73. }
  74. // Now delete everything else
  75. try forEachFile(at: path) { findData in
  76. let name = withUnsafeBytes(of: findData.cFileName) {
  77. return SystemString(platformString: $0.assumingMemoryBound(
  78. to: CInterop.PlatformChar.self).baseAddress!)
  79. }
  80. let component = FilePath.Component(name)!
  81. let subpath = path.appending(component)
  82. if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 {
  83. try subpath.withPlatformString { subpath in
  84. if try !subpath.withCanonicalPathRepresentation({ DeleteFileW($0) }) {
  85. throw Errno(windowsError: GetLastError())
  86. }
  87. }
  88. }
  89. }
  90. // Finally, delete the parent
  91. try path.withPlatformString {
  92. if try !$0.withCanonicalPathRepresentation({ RemoveDirectoryW($0) }) {
  93. throw Errno(windowsError: GetLastError())
  94. }
  95. }
  96. }
  97. #endif // os(Windows)