File: FileInfo.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (253 lines) | stat: -rw-r--r-- 9,263 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(Linux) || os(Android)
import SystemPackage

#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#endif

/// Information about a file system object.
///
/// The information available for a file depends on the platform, ``FileInfo`` provides
/// convenient access to a common subset of properties. Using these properties ensures that
/// code is portable. If available, the platform specific information is made available via
/// ``FileInfo/platformSpecificStatus``. However users should take care to ensure their
/// code uses the correct platform checks when using it to ensure their code is portable.
public struct FileInfo: Hashable, Sendable {
    /// Wraps `CInterop.Stat` providing `Hashable` and `Equatable` conformance.
    private var _platformSpecificStatus: Stat?

    /// The information about the file returned from the filesystem, if available.
    ///
    /// This value is platform specific: you should be careful when using
    /// it as some of its fields vary across platforms. In most cases prefer
    /// using other properties on this type instead.
    ///
    /// See also: the manual pages for 'stat' (`man 2 stat`)
    public var platformSpecificStatus: CInterop.Stat? {
        get { self._platformSpecificStatus?.stat }
        set { self._platformSpecificStatus = newValue.map { Stat($0) } }
    }

    /// The type of the file.
    public var type: FileType

    /// Permissions currently set on the file.
    public var permissions: FilePermissions

    /// The size of the file in bytes.
    public var size: Int64

    /// User ID of the file.
    public var userID: UserID

    /// Group ID of the file.
    public var groupID: GroupID

    /// The last time the file was accessed.
    public var lastAccessTime: Timespec

    /// The last time the files data was last changed.
    public var lastDataModificationTime: Timespec

    /// The last time the status of the file was changed.
    public var lastStatusChangeTime: Timespec

    /// Creates a ``FileInfo`` by deriving values from a platform-specific value.
    public init(platformSpecificStatus: CInterop.Stat) {
        self._platformSpecificStatus = Stat(platformSpecificStatus)
        self.type = FileType(platformSpecificMode: CInterop.Mode(platformSpecificStatus.st_mode))
        self.permissions = FilePermissions(masking: CInterop.Mode(platformSpecificStatus.st_mode))
        self.size = Int64(platformSpecificStatus.st_size)
        self.userID = UserID(rawValue: platformSpecificStatus.st_uid)
        self.groupID = GroupID(rawValue: platformSpecificStatus.st_gid)

        #if canImport(Darwin)
        self.lastAccessTime = Timespec(platformSpecificStatus.st_atimespec)
        self.lastDataModificationTime = Timespec(platformSpecificStatus.st_mtimespec)
        self.lastStatusChangeTime = Timespec(platformSpecificStatus.st_ctimespec)
        #elseif canImport(Glibc) || canImport(Musl)
        self.lastAccessTime = Timespec(platformSpecificStatus.st_atim)
        self.lastDataModificationTime = Timespec(platformSpecificStatus.st_mtim)
        self.lastStatusChangeTime = Timespec(platformSpecificStatus.st_ctim)
        #endif
    }

    /// Creates a ``FileInfo`` from the provided values.
    ///
    /// If you have a platform specific status value prefer calling
    /// ``init(platformSpecificStatus:)``.
    public init(
        type: FileType,
        permissions: FilePermissions,
        size: Int64,
        userID: UserID,
        groupID: GroupID,
        lastAccessTime: Timespec,
        lastDataModificationTime: Timespec,
        lastStatusChangeTime: Timespec
    ) {
        self._platformSpecificStatus = nil
        self.type = type
        self.permissions = permissions
        self.size = size
        self.userID = userID
        self.groupID = groupID
        self.lastAccessTime = lastAccessTime
        self.lastDataModificationTime = lastDataModificationTime
        self.lastStatusChangeTime = lastStatusChangeTime
    }
}

extension FileInfo {
    /// The numeric ID of a user.
    public struct UserID: Hashable, Sendable, CustomStringConvertible {
        public var rawValue: UInt32

        public init(rawValue: UInt32) {
            self.rawValue = rawValue
        }

        public var description: String {
            String(describing: self.rawValue)
        }
    }

    /// The numeric ID of a group.
    public struct GroupID: Hashable, Sendable, CustomStringConvertible {
        public var rawValue: UInt32

        public init(rawValue: UInt32) {
            self.rawValue = rawValue
        }

        public var description: String {
            String(describing: self.rawValue)
        }
    }

    /// A time interval consisting of whole seconds and nanoseconds.
    public struct Timespec: Hashable, Sendable {
        /// The number of seconds.
        public var seconds: Int

        /// The number of nanoseconds.
        public var nanoseconds: Int

        init(_ timespec: timespec) {
            self.seconds = timespec.tv_sec
            self.nanoseconds = timespec.tv_nsec
        }

        public init(seconds: Int, nanoseconds: Int) {
            self.seconds = seconds
            self.nanoseconds = nanoseconds
        }
    }
}

/// A wrapper providing `Hashable` and `Equatable` conformance for `CInterop.Stat`.
private struct Stat: Hashable {
    var stat: CInterop.Stat

    init(_ stat: CInterop.Stat) {
        self.stat = stat
    }

    func hash(into hasher: inout Hasher) {
        let stat = self.stat
        // Different platforms have different underlying values; these are
        // common between Darwin and Glibc.
        hasher.combine(stat.st_dev)
        hasher.combine(stat.st_mode)
        hasher.combine(stat.st_nlink)
        hasher.combine(stat.st_ino)
        hasher.combine(stat.st_uid)
        hasher.combine(stat.st_gid)
        hasher.combine(stat.st_rdev)
        hasher.combine(stat.st_size)
        hasher.combine(stat.st_blocks)
        hasher.combine(stat.st_blksize)

        #if canImport(Darwin)
        hasher.combine(FileInfo.Timespec(stat.st_atimespec))
        hasher.combine(FileInfo.Timespec(stat.st_mtimespec))
        hasher.combine(FileInfo.Timespec(stat.st_ctimespec))
        hasher.combine(FileInfo.Timespec(stat.st_birthtimespec))
        hasher.combine(stat.st_flags)
        hasher.combine(stat.st_gen)
        #elseif canImport(Glibc) || canImport(Musl)
        hasher.combine(FileInfo.Timespec(stat.st_atim))
        hasher.combine(FileInfo.Timespec(stat.st_mtim))
        hasher.combine(FileInfo.Timespec(stat.st_ctim))
        #endif

    }

    static func == (lhs: Stat, rhs: Stat) -> Bool {
        let lStat = lhs.stat
        let rStat = rhs.stat

        // Different platforms have different underlying values; these are
        // common between Darwin and Glibc.
        var isEqual = lStat.st_dev == rStat.st_dev
        isEqual = isEqual && lStat.st_mode == rStat.st_mode
        isEqual = isEqual && lStat.st_nlink == rStat.st_nlink
        isEqual = isEqual && lStat.st_ino == rStat.st_ino
        isEqual = isEqual && lStat.st_uid == rStat.st_uid
        isEqual = isEqual && lStat.st_gid == rStat.st_gid
        isEqual = isEqual && lStat.st_rdev == rStat.st_rdev
        isEqual = isEqual && lStat.st_size == rStat.st_size
        isEqual = isEqual && lStat.st_blocks == rStat.st_blocks
        isEqual = isEqual && lStat.st_blksize == rStat.st_blksize

        #if canImport(Darwin)
        isEqual =
            isEqual
            && FileInfo.Timespec(lStat.st_atimespec) == FileInfo.Timespec(rStat.st_atimespec)
        isEqual =
            isEqual
            && FileInfo.Timespec(lStat.st_mtimespec) == FileInfo.Timespec(rStat.st_mtimespec)
        isEqual =
            isEqual
            && FileInfo.Timespec(lStat.st_ctimespec) == FileInfo.Timespec(rStat.st_ctimespec)
        isEqual =
            isEqual
            && FileInfo.Timespec(lStat.st_birthtimespec)
                == FileInfo.Timespec(rStat.st_birthtimespec)
        isEqual = isEqual && lStat.st_flags == rStat.st_flags
        isEqual = isEqual && lStat.st_gen == rStat.st_gen
        #elseif canImport(Glibc) || canImport(Musl)
        isEqual = isEqual && FileInfo.Timespec(lStat.st_atim) == FileInfo.Timespec(rStat.st_atim)
        isEqual = isEqual && FileInfo.Timespec(lStat.st_mtim) == FileInfo.Timespec(rStat.st_mtim)
        isEqual = isEqual && FileInfo.Timespec(lStat.st_ctim) == FileInfo.Timespec(rStat.st_ctim)
        #endif

        return isEqual
    }
}

extension FilePermissions {
    internal init(masking rawValue: CInterop.Mode) {
        self = .init(rawValue: rawValue & ~S_IFMT)
    }
}

#endif