Skip to content

Commit 833062d

Browse files
committed
feat: Add one-shot attribute methods to FileSystemProtocol (#3648)
1 parent 15b77e6 commit 833062d

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

Sources/NIOFS/FileSystemProtocol.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,3 +683,48 @@ extension FileSystemProtocol {
683683
}
684684
}
685685
}
686+
687+
// MARK: - File attributes
688+
689+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
690+
extension FileSystemProtocol {
691+
/// Sets the file's last access and last data modification times to the given times.
692+
///
693+
/// - Parameters:
694+
/// - path: The path of the file to modify.
695+
/// - lastAccess: The new value of the file's last access time, as time elapsed since the Epoch.
696+
/// - lastDataModification: The new value of the file's last data modification time, as time elapsed since the Epoch.
697+
public func setTimes(
698+
forFileAt path: FilePath,
699+
lastAccess: FileInfo.Timespec?,
700+
lastDataModification: FileInfo.Timespec?
701+
) async throws {
702+
try await self.withFileHandle(forReadingAt: path) { handle in
703+
try await handle.setTimes(lastAccess: lastAccess, lastDataModification: lastDataModification)
704+
}
705+
}
706+
707+
/// Sets the file's last access time to the given time.
708+
///
709+
/// - Parameters:
710+
/// - path: The path of the file to modify.
711+
/// - time: The time to which the file's last access time should be set.
712+
public func setLastAccessTime(
713+
forFileAt path: FilePath,
714+
to time: FileInfo.Timespec
715+
) async throws {
716+
try await self.setTimes(forFileAt: path, lastAccess: time, lastDataModification: nil)
717+
}
718+
719+
/// Sets the file's last data modification time to the given time.
720+
///
721+
/// - Parameters:
722+
/// - path: The path of the file to modify.
723+
/// - time: The time to which the file's last data modification time should be set.
724+
public func setLastDataModificationTime(
725+
forFileAt path: FilePath,
726+
to time: FileInfo.Timespec
727+
) async throws {
728+
try await self.setTimes(forFileAt: path, lastAccess: nil, lastDataModification: time)
729+
}
730+
}

Sources/_NIOFileSystem/FileSystemProtocol.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,3 +772,48 @@ extension FileSystemProtocol {
772772
}
773773
}
774774
}
775+
776+
// MARK: - File attributes
777+
778+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
779+
extension FileSystemProtocol {
780+
/// Sets the file's last access and last data modification times to the given times.
781+
///
782+
/// - Parameters:
783+
/// - path: The path of the file to modify.
784+
/// - lastAccess: The new value of the file's last access time, as time elapsed since the Epoch.
785+
/// - lastDataModification: The new value of the file's last data modification time, as time elapsed since the Epoch.
786+
public func setTimes(
787+
forFileAt path: FilePath,
788+
lastAccess: FileInfo.Timespec?,
789+
lastDataModification: FileInfo.Timespec?
790+
) async throws {
791+
try await self.withFileHandle(forReadingAt: path) { handle in
792+
try await handle.setTimes(lastAccess: lastAccess, lastDataModification: lastDataModification)
793+
}
794+
}
795+
796+
/// Sets the file's last access time to the given time.
797+
///
798+
/// - Parameters:
799+
/// - path: The path of the file to modify.
800+
/// - time: The time to which the file's last access time should be set.
801+
public func setLastAccessTime(
802+
forFileAt path: FilePath,
803+
to time: FileInfo.Timespec
804+
) async throws {
805+
try await self.setTimes(forFileAt: path, lastAccess: time, lastDataModification: nil)
806+
}
807+
808+
/// Sets the file's last data modification time to the given time.
809+
///
810+
/// - Parameters:
811+
/// - path: The path of the file to modify.
812+
/// - time: The time to which the file's last data modification time should be set.
813+
public func setLastDataModificationTime(
814+
forFileAt path: FilePath,
815+
to time: FileInfo.Timespec
816+
) async throws {
817+
try await self.setTimes(forFileAt: path, lastAccess: nil, lastDataModification: time)
818+
}
819+
}

0 commit comments

Comments
 (0)