Skip to content

Commit 40e1deb

Browse files
committed
Consolidate internal Stat functions
1 parent eee0145 commit 40e1deb

File tree

1 file changed

+17
-71
lines changed

1 file changed

+17
-71
lines changed

Sources/System/FileSystem/Stat.swift

Lines changed: 17 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public struct Stat: RawRepresentable, Sendable {
103103
followTargetSymlink: Bool = true,
104104
retryOnInterrupt: Bool = true
105105
) throws(Errno) {
106-
self.rawValue = try path.withPlatformString {
106+
self = try path.withPlatformString {
107107
Self._stat(
108108
$0,
109109
followTargetSymlink: followTargetSymlink,
@@ -126,7 +126,7 @@ public struct Stat: RawRepresentable, Sendable {
126126
followTargetSymlink: Bool = true,
127127
retryOnInterrupt: Bool = true
128128
) throws(Errno) {
129-
self.rawValue = try Self._stat(
129+
self = try Self._stat(
130130
path,
131131
followTargetSymlink: followTargetSymlink,
132132
retryOnInterrupt: retryOnInterrupt
@@ -138,15 +138,15 @@ public struct Stat: RawRepresentable, Sendable {
138138
_ ptr: UnsafePointer<CChar>,
139139
followTargetSymlink: Bool,
140140
retryOnInterrupt: Bool
141-
) -> Result<CInterop.Stat, Errno> {
141+
) -> Result<Stat, Errno> {
142142
var result = CInterop.Stat()
143143
return nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
144144
if followTargetSymlink {
145145
system_stat(ptr, &result)
146146
} else {
147147
system_lstat(ptr, &result)
148148
}
149-
}.map { result }
149+
}.map { Stat(rawValue: result) }
150150
}
151151

152152
/// Creates a `Stat` struct from a `FileDescriptor`.
@@ -157,7 +157,7 @@ public struct Stat: RawRepresentable, Sendable {
157157
_ fd: FileDescriptor,
158158
retryOnInterrupt: Bool = true
159159
) throws(Errno) {
160-
self.rawValue = try Self._fstat(
160+
self = try Self._fstat(
161161
fd,
162162
retryOnInterrupt: retryOnInterrupt
163163
).get()
@@ -167,11 +167,11 @@ public struct Stat: RawRepresentable, Sendable {
167167
internal static func _fstat(
168168
_ fd: FileDescriptor,
169169
retryOnInterrupt: Bool
170-
) -> Result<CInterop.Stat, Errno> {
170+
) -> Result<Stat, Errno> {
171171
var result = CInterop.Stat()
172172
return nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
173173
system_fstat(fd.rawValue, &result)
174-
}.map { result }
174+
}.map { Stat(rawValue: result) }
175175
}
176176

177177
/// Creates a `Stat` struct from a `FilePath` and `Flags`.
@@ -185,7 +185,7 @@ public struct Stat: RawRepresentable, Sendable {
185185
flags: Stat.Flags,
186186
retryOnInterrupt: Bool = true
187187
) throws(Errno) {
188-
self.rawValue = try path.withPlatformString {
188+
self = try path.withPlatformString {
189189
Self._fstatat(
190190
$0,
191191
relativeTo: _AT_FDCWD,
@@ -209,7 +209,7 @@ public struct Stat: RawRepresentable, Sendable {
209209
flags: Stat.Flags,
210210
retryOnInterrupt: Bool = true
211211
) throws(Errno) {
212-
self.rawValue = try path.withPlatformString {
212+
self = try path.withPlatformString {
213213
Self._fstatat(
214214
$0,
215215
relativeTo: fd.rawValue,
@@ -230,7 +230,7 @@ public struct Stat: RawRepresentable, Sendable {
230230
flags: Stat.Flags,
231231
retryOnInterrupt: Bool = true
232232
) throws(Errno) {
233-
self.rawValue = try Self._fstatat(
233+
self = try Self._fstatat(
234234
path,
235235
relativeTo: _AT_FDCWD,
236236
flags: flags,
@@ -252,7 +252,7 @@ public struct Stat: RawRepresentable, Sendable {
252252
flags: Stat.Flags,
253253
retryOnInterrupt: Bool = true
254254
) throws(Errno) {
255-
self.rawValue = try Self._fstatat(
255+
self = try Self._fstatat(
256256
path,
257257
relativeTo: fd.rawValue,
258258
flags: flags,
@@ -266,11 +266,11 @@ public struct Stat: RawRepresentable, Sendable {
266266
relativeTo fd: FileDescriptor.RawValue,
267267
flags: Stat.Flags,
268268
retryOnInterrupt: Bool
269-
) -> Result<CInterop.Stat, Errno> {
269+
) -> Result<Stat, Errno> {
270270
var result = CInterop.Stat()
271271
return nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
272272
system_fstatat(fd, path, &result, flags.rawValue)
273-
}.map { result }
273+
}.map { Stat(rawValue: result) }
274274
}
275275

276276

@@ -573,19 +573,7 @@ extension FileDescriptor {
573573
public func stat(
574574
retryOnInterrupt: Bool = true
575575
) throws(Errno) -> Stat {
576-
try _fstat(
577-
retryOnInterrupt: retryOnInterrupt
578-
).get()
579-
}
580-
581-
@usableFromInline
582-
internal func _fstat(
583-
retryOnInterrupt: Bool
584-
) -> Result<Stat, Errno> {
585-
var result = CInterop.Stat()
586-
return nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
587-
system_fstat(self.rawValue, &result)
588-
}.map { Stat(rawValue: result) }
576+
try Stat(self, retryOnInterrupt: retryOnInterrupt)
589577
}
590578
}
591579

@@ -607,27 +595,7 @@ extension FilePath {
607595
followTargetSymlink: Bool = true,
608596
retryOnInterrupt: Bool = true
609597
) throws(Errno) -> Stat {
610-
try _stat(
611-
followTargetSymlink: followTargetSymlink,
612-
retryOnInterrupt: retryOnInterrupt
613-
).get()
614-
}
615-
616-
@usableFromInline
617-
internal func _stat(
618-
followTargetSymlink: Bool,
619-
retryOnInterrupt: Bool
620-
) -> Result<Stat, Errno> {
621-
var result = CInterop.Stat()
622-
return withPlatformString { ptr in
623-
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
624-
if followTargetSymlink {
625-
system_stat(ptr, &result)
626-
} else {
627-
system_lstat(ptr, &result)
628-
}
629-
}.map { Stat(rawValue: result) }
630-
}
598+
try Stat(self, followTargetSymlink: followTargetSymlink, retryOnInterrupt: retryOnInterrupt)
631599
}
632600

633601
/// Creates a `Stat` struct for the file referenced by this `FilePath` using the given `Flags`.
@@ -640,11 +608,7 @@ extension FilePath {
640608
flags: Stat.Flags,
641609
retryOnInterrupt: Bool = true
642610
) throws(Errno) -> Stat {
643-
try _fstatat(
644-
relativeTo: _AT_FDCWD,
645-
flags: flags,
646-
retryOnInterrupt: retryOnInterrupt
647-
).get()
611+
try Stat(self, flags: flags, retryOnInterrupt: retryOnInterrupt)
648612
}
649613

650614
/// Creates a `Stat` struct for the file referenced by this `FilePath` using the given `Flags`,
@@ -660,25 +624,7 @@ extension FilePath {
660624
flags: Stat.Flags,
661625
retryOnInterrupt: Bool = true
662626
) throws(Errno) -> Stat {
663-
try _fstatat(
664-
relativeTo: fd.rawValue,
665-
flags: flags,
666-
retryOnInterrupt: retryOnInterrupt
667-
).get()
668-
}
669-
670-
@usableFromInline
671-
internal func _fstatat(
672-
relativeTo fd: FileDescriptor.RawValue,
673-
flags: Stat.Flags,
674-
retryOnInterrupt: Bool
675-
) -> Result<Stat, Errno> {
676-
var result = CInterop.Stat()
677-
return withPlatformString { ptr in
678-
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
679-
system_fstatat(fd, ptr, &result, flags.rawValue)
680-
}.map { Stat(rawValue: result) }
681-
}
627+
try Stat(self, relativeTo: fd, flags: flags, retryOnInterrupt: retryOnInterrupt)
682628
}
683629
}
684630

0 commit comments

Comments
 (0)