From 23ac1906318799fd925f5efe321fb0d8f34b87fa Mon Sep 17 00:00:00 2001 From: Si Beaumont Date: Wed, 4 May 2022 12:33:03 +0100 Subject: [PATCH] FileHelpers: Add FileDescriptor.fileSize Signed-off-by: Si Beaumont --- Sources/System/FileHelpers.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Sources/System/FileHelpers.swift b/Sources/System/FileHelpers.swift index 0825f83..6f7885f 100644 --- a/Sources/System/FileHelpers.swift +++ b/Sources/System/FileHelpers.swift @@ -122,4 +122,18 @@ extension FileDescriptor { return .success(buffer.count) } } + + /// Return the current size of the file. + /// + /// - Returns: The current size of the file, in bytes. + @_alwaysEmitIntoClient + @discardableResult + public func fileSize( + retryOnInterrupt: Bool = true + ) throws -> Int64 { + let current = try seek(offset: 0, from: .current) + let size = try seek(offset: 0, from: .end) + try seek(offset: current, from: .start) + return size + } }