File: SwiftFileManager.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (359 lines) | stat: -rw-r--r-- 13,889 bytes parent folder | download
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#if !FOUNDATION_FRAMEWORK

public struct FileAttributeType : Hashable, RawRepresentable, Sendable {
    public let rawValue: String
    public init(rawValue: String) {
        self.rawValue = rawValue
    }
    
    public init(_ rawValue: String) {
        self.rawValue = rawValue
    }
    
    public static let typeBlockSpecial: Self = Self("NSFileTypeBlockSpecial")
    public static let typeCharacterSpecial: Self = Self("NSFileTypeCharacterSpecial")
    public static let typeDirectory: Self = Self("NSFileTypeDirectory")
    public static let typeRegular: Self = Self("NSFileTypeRegular")
    public static let typeSocket: Self = Self("NSFileTypeSocket")
    public static let typeSymbolicLink: Self = Self("NSFileTypeSymbolicLink")
    public static let typeUnknown: Self = Self("NSFileTypeUnknown")
}

public struct FileAttributeKey: Hashable, RawRepresentable, Sendable {
    public typealias RawValue = String
    public let rawValue: String
    
    public init(rawValue: String) {
        self.rawValue = rawValue
    }
    
    public init(_ rawValue: String) {
        self.rawValue = rawValue
    }
    
    public static let type = Self(rawValue: "NSFileType")
    public static let size = Self(rawValue: "NSFileSize")
    public static let modificationDate = Self(rawValue: "NSFileModificationDate")
    public static let referenceCount = Self(rawValue: "NSFileCount")
    public static let deviceIdentifier = Self(rawValue: "NSFileDeviceIdentifier")
    public static let ownerAccountName = Self(rawValue: "NSFileOwnerAccountName")
    public static let groupOwnerAccountName = Self(rawValue: "NSFileGroupOwnerAccountName")
    public static let posixPermissions = Self(rawValue: "NSFilePosixPermissions")
    public static let systemNumber = Self(rawValue: "NSFileSystemNumber")
    public static let systemFileNumber = Self(rawValue: "NSFileSystemFileNumber")
    public static let extensionHidden = Self(rawValue: "NSFileExtensionHidden")
    public static let hfsCreatorCode = Self(rawValue: "NSFileHFSCreatorCode")
    public static let hfsTypeCode = Self(rawValue: "NSFileHFSTypeCode")
    public static let immutable = Self(rawValue: "NSFileImmutable")
    public static let appendOnly = Self(rawValue: "NSFileAppendOnly")
    public static let creationDate = Self(rawValue: "NSFileCreationDate")
    public static let ownerAccountID = Self(rawValue: "NSFileOwnerAccountID")
    public static let groupOwnerAccountID = Self(rawValue: "NSFileGroupOwnerAccountID")
    public static let busy = Self(rawValue: "NSFileBusy")
    public static let protectionKey = Self(rawValue: "NSFileProtectionKey")
    public static let systemSize = Self(rawValue: "NSFileSystemSize")
    public static let systemFreeSize = Self(rawValue: "NSFileSystemFreeSize")
    public static let systemNodes = Self(rawValue: "NSFileSystemNodes")
    public static let systemFreeNodes = Self(rawValue: "NSFileSystemFreeNodes")
}

public struct FileProtectionType : RawRepresentable, Sendable {
    public let rawValue: String
    
    public init(rawValue: String) {
        self.rawValue = rawValue
    }
    
    public static let none = Self(rawValue: "NSFileProtectionNone")
    public static let complete = Self(rawValue: "NSFileProtectionComplete")
    public static let completeUnlessOpen = Self(rawValue: "NSFileProtectionCompleteUnlessOpen")
    public static let completeUntilFirstUserAuthentication = Self(rawValue: "NSFileProtectionCompleteUntilFirstUserAuthentication")
    public static let inactive = Self(rawValue: "NSFileProtectionCompleteWhenUserInactive")
}

extension FileManager {
    public struct UnmountOptions : OptionSet, Sendable {
        public let rawValue: UInt
        
        public init(rawValue: UInt) {
            self.rawValue = rawValue
        }
        
        public static let allPartitionsAndEjectDisk = Self(rawValue: 1 << 0)
        public static let withoutUI = Self(rawValue: 1 << 1)
    }
    
    public struct DirectoryEnumerationOptions : OptionSet, Sendable {
        public let rawValue: UInt
        
        public init(rawValue: UInt) {
            self.rawValue = rawValue
        }
        
        public static let skipsSubdirectoryDescendants = Self(rawValue: 1 << 0)
        public static let skipsPackageDescendants = Self(rawValue: 1 << 1)
        public static let skipsHiddenFiles = Self(rawValue: 1 << 2)
        public static let includesDirectoriesPostOrder = Self(rawValue: 1 << 3)
        public static let producesRelativePathURLs = Self(rawValue: 1 << 4)
    }
    
    public enum SearchPathDirectory : UInt, Sendable {
        // The following are Darwin-only and will not produce directories on non-Darwin
        case applicationDirectory = 1
        case demoApplicationDirectory = 2
        case developerApplicationDirectory = 3
        case adminApplicationDirectory = 4
        case libraryDirectory = 5
        case developerDirectory = 6
        case documentationDirectory = 8
        case coreServiceDirectory = 10
        case inputMethodsDirectory = 16
        case preferencePanesDirectory = 22
        case allApplicationsDirectory = 100
        case allLibrariesDirectory = 101
        case itemReplacementDirectory = 99
        case printerDescriptionDirectory = 20
        
        // The following will not produce paths in swift-foundation because it requires the code signing identifier
        case applicationScriptsDirectory = 23
        
        // The following are cross-platform and may produce valid paths on non-Darwin
        case userDirectory = 7
        case documentDirectory = 9
        case autosavedInformationDirectory = 11
        case desktopDirectory = 12
        case cachesDirectory = 13
        case applicationSupportDirectory = 14
        case downloadsDirectory = 15
        case moviesDirectory = 17
        case musicDirectory = 18
        case picturesDirectory = 19
        case sharedPublicDirectory = 21
        case trashDirectory = 102
    }
    
    public struct SearchPathDomainMask : OptionSet, Sendable {
        public let rawValue: UInt
        
        public init(rawValue: UInt) {
            self.rawValue = rawValue
        }

        public static let userDomainMask = Self(rawValue: 1 << 0)
        public static let localDomainMask = Self(rawValue: 1 << 1)
        public static let networkDomainMask = Self(rawValue: 1 << 2)
        public static let systemDomainMask = Self(rawValue: 1 << 3)
        public static let allDomainsMask = Self(rawValue: 0xFFFF)
    }
    
    public enum URLRelationship : Int, Sendable {
        case contains = 0
        case same = 1
        case other = 2
    }
    
    public struct ItemReplacementOptions : OptionSet, Sendable {
        public let rawValue: UInt
        
        public init(rawValue: UInt) {
            self.rawValue = rawValue
        }
        
        public static let usingNewMetadataOnly = Self(rawValue: 1 << 0)
        public static let withoutDeletingBackupItem = Self(rawValue: 1 << 1)
    }
}

@_nonSendable
open class FileManager : @unchecked Sendable {
    // Sendable note: _impl may only be mutated in `init`
    private var _impl: _FileManagerImpl
    private let _lock = LockedState<State>(initialState: .init(delegate: nil))
    
    private static let _default = FileManager()
    open class var `default`: FileManager {
        _default
    }
    
    private struct State {
        weak var delegate: (any FileManagerDelegate)?
    }

    open weak var delegate: (any FileManagerDelegate)? {
        get {
            _lock.withLock { $0.delegate }
        }
        set {
            _lock.withLock { $0.delegate = newValue }
        }
    }
    
    public init() {
        _impl = _FileManagerImpl()
        _impl._manager = self
    }

    open func setAttributes(_ attributes: [FileAttributeKey : Any], ofItemAtPath path: String) throws {
        try _impl.setAttributes(attributes, ofItemAtPath: path)
    }

    open func createDirectory(at url: URL, withIntermediateDirectories createIntermediates: Bool, attributes: [FileAttributeKey : Any]? = nil) throws {
        try _impl.createDirectory(at: url, withIntermediateDirectories: createIntermediates, attributes: attributes)
    }

    open func createDirectory(atPath path: String, withIntermediateDirectories createIntermediates: Bool, attributes: [FileAttributeKey : Any]? = nil) throws {
        try _impl.createDirectory(atPath: path, withIntermediateDirectories: createIntermediates, attributes: attributes)
    }

    open func contentsOfDirectory(atPath path: String) throws -> [String] {
        try _impl.contentsOfDirectory(atPath: path)
    }

    open func subpathsOfDirectory(atPath path: String) throws -> [String] {
        try _impl.subpathsOfDirectory(atPath: path)
    }
    
    open func urls(for directory: FileManager.SearchPathDirectory, in domainMask: FileManager.SearchPathDomainMask) -> [URL] {
        _impl.urls(for: directory, in: domainMask)
    }
    
    open func url(for directory: FileManager.SearchPathDirectory, in domain: FileManager.SearchPathDomainMask, appropriateFor url: URL?, create shouldCreate: Bool) throws -> URL {
        try _impl.url(for: directory, in: domain, appropriateFor: url, create: shouldCreate)
    }

    open func attributesOfItem(atPath path: String) throws -> [FileAttributeKey : Any] {
        try _impl.attributesOfItem(atPath: path)
    }

    open func attributesOfFileSystem(forPath path: String) throws -> [FileAttributeKey : Any] {
        try _impl.attributesOfFileSystem(forPath: path)
    }

    open func createSymbolicLink(atPath path: String, withDestinationPath destPath: String) throws {
        try _impl.createSymbolicLink(atPath: path, withDestinationPath: destPath)
    }
    
    open func createSymbolicLink(at url: URL, withDestinationURL destURL: URL) throws {
        try _impl.createSymbolicLink(at: url, withDestinationURL: destURL)
    }

    open func destinationOfSymbolicLink(atPath path: String) throws -> String {
        try _impl.destinationOfSymbolicLink(atPath: path)
    }

    open func copyItem(atPath srcPath: String, toPath dstPath: String) throws {
        try _impl.copyItem(atPath: srcPath, toPath: dstPath, options: [])
    }

    open func moveItem(atPath srcPath: String, toPath dstPath: String) throws {
        try _impl.moveItem(atPath: srcPath, toPath: dstPath, options: [])
    }

    open func linkItem(atPath srcPath: String, toPath dstPath: String) throws {
        try _impl.linkItem(atPath: srcPath, toPath: dstPath)
    }

    open func removeItem(atPath path: String) throws {
        try _impl.removeItem(atPath: path)
    }

    open func copyItem(at srcURL: URL, to dstURL: URL) throws {
        try _impl.copyItem(at: srcURL, to: dstURL, options: [])
    }

    open func moveItem(at srcURL: URL, to dstURL: URL) throws {
        try _impl.moveItem(at: srcURL, to: dstURL, options: [])
    }

    open func linkItem(at srcURL: URL, to dstURL: URL) throws {
        try _impl.linkItem(at: srcURL, to: dstURL)
    }

    open func removeItem(at URL: URL) throws {
        try _impl.removeItem(at: URL)
    }

    open var currentDirectoryPath: String {
        _impl.currentDirectoryPath!
    }

    open func changeCurrentDirectoryPath(_ path: String) -> Bool {
        _impl.changeCurrentDirectoryPath(path)
    }

    open func fileExists(atPath path: String) -> Bool {
        _impl.fileExists(atPath: path)
    }

    open func fileExists(atPath path: String, isDirectory: inout Bool) -> Bool {
        _impl.fileExists(atPath: path, isDirectory: &isDirectory)
    }

    open func isReadableFile(atPath path: String) -> Bool {
        _impl.isReadableFile(atPath: path)
    }

    open func isWritableFile(atPath path: String) -> Bool {
        _impl.isWritableFile(atPath: path)
    }

    open func isExecutableFile(atPath path: String) -> Bool {
        _impl.isExecutableFile(atPath: path)
    }

    open func isDeletableFile(atPath path: String) -> Bool {
        _impl.isDeletableFile(atPath: path)
    }

    open func contentsEqual(atPath path1: String, andPath path2: String) -> Bool {
        _impl.contentsEqual(atPath: path1, andPath: path2)
    }
    
    open func contents(atPath path: String) -> Data? {
        _impl.contents(atPath: path)
    }

    open func createFile(atPath path: String, contents data: Data?, attributes attr: [FileAttributeKey : Any]? = nil) -> Bool {
        _impl.createFile(atPath: path, contents: data, attributes: attr)
    }

    open func string(withFileSystemRepresentation str: UnsafePointer<CChar>, length len: Int) -> String {
        _impl.string(withFileSystemRepresentation: str, length: len)
    }
    
    open func withFileSystemRepresentation<R>(for path: String, _ body: (UnsafePointer<CChar>?) throws -> R) rethrows -> R {
        try path.withFileSystemRepresentation(body)
    }

    open var temporaryDirectory: URL {
        _impl.temporaryDirectory
    }
    
    @available(iOS, unavailable)
    @available(watchOS, unavailable)
    @available(tvOS, unavailable)
    open var homeDirectoryForCurrentUser: URL {
        _impl.homeDirectoryForCurrentUser
    }

    @available(iOS, unavailable)
    @available(watchOS, unavailable)
    @available(tvOS, unavailable)
    open func homeDirectory(forUser userName: String) -> URL? {
        _impl.homeDirectory(forUser: userName)
    }
}

#endif