|
| 1 | +/* |
| 2 | + This source file is part of the Swift System open source project |
| 3 | + |
| 4 | + Copyright (c) 2024 Apple Inc. and the Swift System project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + */ |
| 9 | + |
| 10 | +#if !os(Windows) |
| 11 | + |
| 12 | +/// Get the path to the system temporary directory. |
| 13 | +internal func _getTemporaryDirectory() throws -> FilePath { |
| 14 | + guard let tmp = system_getenv("TMPDIR") else { |
| 15 | + return "/tmp" |
| 16 | + } |
| 17 | + |
| 18 | + return FilePath(SystemString(platformString: tmp)) |
| 19 | +} |
| 20 | + |
| 21 | +/// Delete the entire contents of a directory, including its subdirectories. |
| 22 | +/// |
| 23 | +/// - Parameters: |
| 24 | +/// - path: The directory to be deleted. |
| 25 | +/// |
| 26 | +/// Removes a directory completely, including all of its contents. |
| 27 | +internal func _recursiveRemove( |
| 28 | + at path: FilePath |
| 29 | +) throws { |
| 30 | + let dirfd = try FileDescriptor.open(path, .readOnly, options: .directory) |
| 31 | + defer { |
| 32 | + try? dirfd.close() |
| 33 | + } |
| 34 | + |
| 35 | + let dot: (CInterop.PlatformChar, CInterop.PlatformChar) = (46, 0) |
| 36 | + try withUnsafeBytes(of: dot) { |
| 37 | + try recursiveRemove( |
| 38 | + in: dirfd.rawValue, |
| 39 | + name: $0.assumingMemoryBound(to: CInterop.PlatformChar.self).baseAddress! |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + try path.withPlatformString { |
| 44 | + if system_rmdir($0) != 0 { |
| 45 | + throw Errno.current |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// Open a directory by reference to its parent and name. |
| 51 | +/// |
| 52 | +/// - Parameters: |
| 53 | +/// - dirfd: An open file descriptor for the parent directory. |
| 54 | +/// - name: The name of the directory to open. |
| 55 | +/// - Returns: A pointer to a `DIR` structure. |
| 56 | +/// |
| 57 | +/// This is like `opendir()`, but instead of taking a path, it uses a |
| 58 | +/// file descriptor pointing at the parent, thus avoiding path length |
| 59 | +/// limits. |
| 60 | +fileprivate func impl_opendirat( |
| 61 | + _ dirfd: CInt, |
| 62 | + _ name: UnsafePointer<CInterop.PlatformChar> |
| 63 | +) -> system_DIRPtr? { |
| 64 | + let fd = system_openat(dirfd, name, |
| 65 | + FileDescriptor.AccessMode.readOnly.rawValue |
| 66 | + | FileDescriptor.OpenOptions.directory.rawValue) |
| 67 | + if fd < 0 { |
| 68 | + return nil |
| 69 | + } |
| 70 | + return system_fdopendir(fd) |
| 71 | +} |
| 72 | + |
| 73 | +/// Invoke a closure for each file within a particular directory. |
| 74 | +/// |
| 75 | +/// - Parameters: |
| 76 | +/// - dirfd: The parent of the directory to be enumerated. |
| 77 | +/// - subdir: The subdirectory to be enumerated. |
| 78 | +/// - body: The closure that will be invoked. |
| 79 | +/// |
| 80 | +/// We skip the `.` and `..` pseudo-entries. |
| 81 | +fileprivate func forEachFile( |
| 82 | + in dirfd: CInt, |
| 83 | + subdir: UnsafePointer<CInterop.PlatformChar>, |
| 84 | + _ body: (system_dirent) throws -> () |
| 85 | +) throws { |
| 86 | + guard let dir = impl_opendirat(dirfd, subdir) else { |
| 87 | + throw Errno.current |
| 88 | + } |
| 89 | + defer { |
| 90 | + _ = system_closedir(dir) |
| 91 | + } |
| 92 | + |
| 93 | + while let dirent = system_readdir(dir) { |
| 94 | + // Skip . and .. |
| 95 | + if dirent.pointee.d_name.0 == 46 |
| 96 | + && (dirent.pointee.d_name.1 == 0 |
| 97 | + || (dirent.pointee.d_name.1 == 46 |
| 98 | + && dirent.pointee.d_name.2 == 0)) { |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + try body(dirent.pointee) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/// Delete the entire contents of a directory, including its subdirectories. |
| 107 | +/// |
| 108 | +/// - Parameters: |
| 109 | +/// - dirfd: The parent of the directory to be removed. |
| 110 | +/// - name: The name of the directory to be removed. |
| 111 | +/// |
| 112 | +/// Removes a directory completely, including all of its contents. |
| 113 | +fileprivate func recursiveRemove( |
| 114 | + in dirfd: CInt, |
| 115 | + name: UnsafePointer<CInterop.PlatformChar> |
| 116 | +) throws { |
| 117 | + // First, deal with subdirectories |
| 118 | + try forEachFile(in: dirfd, subdir: name) { dirent in |
| 119 | + if dirent.d_type == SYSTEM_DT_DIR { |
| 120 | + try withUnsafeBytes(of: dirent.d_name) { |
| 121 | + try recursiveRemove( |
| 122 | + in: dirfd, |
| 123 | + name: $0.assumingMemoryBound(to: CInterop.PlatformChar.self) |
| 124 | + .baseAddress! |
| 125 | + ) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Now delete the contents of this directory |
| 131 | + try forEachFile(in: dirfd, subdir: name) { dirent in |
| 132 | + let flag: CInt |
| 133 | + |
| 134 | + if dirent.d_type == SYSTEM_DT_DIR { |
| 135 | + flag = SYSTEM_AT_REMOVE_DIR |
| 136 | + } else { |
| 137 | + flag = 0 |
| 138 | + } |
| 139 | + |
| 140 | + let result = withUnsafeBytes(of: dirent.d_name) { |
| 141 | + system_unlinkat(dirfd, |
| 142 | + $0.assumingMemoryBound(to: CInterop.PlatformChar.self) |
| 143 | + .baseAddress!, |
| 144 | + flag) |
| 145 | + } |
| 146 | + |
| 147 | + if result != 0 { |
| 148 | + throw Errno.current |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +#endif // !os(Windows) |
0 commit comments