Rename File to OSFileHandle. Move OSFileHandle to OSFileHandle.swift

This commit is contained in:
Wojciech Nagrodzki 2019-10-30 20:24:02 +01:00
parent 1eb496f343
commit 3f90ca9fa6
Signed by: wnagrodzki
GPG key ID: E9D0EB0302264569
5 changed files with 33 additions and 20 deletions

View file

@ -23,6 +23,7 @@
2ED103E12135C61100EB3683 /* LogrotateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E02135C61100EB3683 /* LogrotateTests.swift */; };
2ED103E32135D3FB00EB3683 /* SizeLimitedFileTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E22135D3FB00EB3683 /* SizeLimitedFileTests.swift */; };
2ED103E52138553B00EB3683 /* DiskLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED103E42138553B00EB3683 /* DiskLoggerTests.swift */; };
2ED83781236A19A60008C01F /* OSFileHandle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2ED83780236A19A60008C01F /* OSFileHandle.swift */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -66,6 +67,7 @@
2ED103E02135C61100EB3683 /* LogrotateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogrotateTests.swift; sourceTree = "<group>"; };
2ED103E22135D3FB00EB3683 /* SizeLimitedFileTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SizeLimitedFileTests.swift; sourceTree = "<group>"; };
2ED103E42138553B00EB3683 /* DiskLoggerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiskLoggerTests.swift; sourceTree = "<group>"; };
2ED83780236A19A60008C01F /* OSFileHandle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSFileHandle.swift; sourceTree = "<group>"; };
2EDA8AE8213ACCFF00FE5840 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -149,6 +151,7 @@
2EBF4B552122B598008E4117 /* SizeLimitedFile.swift */,
2EBF4B562122B598008E4117 /* Logrotate.swift */,
2ED077DA2132B0320058EEFC /* OSFileManager.swift */,
2ED83780236A19A60008C01F /* OSFileHandle.swift */,
);
path = DiskLogger;
sourceTree = "<group>";
@ -259,6 +262,7 @@
buildActionMask = 2147483647;
files = (
2EBF4B582122B598008E4117 /* SizeLimitedFile.swift in Sources */,
2ED83781236A19A60008C01F /* OSFileHandle.swift in Sources */,
2EBF4B572122B598008E4117 /* DiskLogger.swift in Sources */,
2EBF4B4C2122AF53008E4117 /* NullLogger.swift in Sources */,
2EBF4B3E2122AA34008E4117 /* Logger.swift in Sources */,

View file

@ -148,14 +148,7 @@ private class FileWriterFactory: SizeLimitedFileFactory {
}
private class FileHandleFactory: FileFactory {
func makeInstance(forWritingTo: URL) throws -> File {
func makeInstance(forWritingTo: URL) throws -> OSFileHandle {
return try FileHandle(forWritingTo: forWritingTo)
}
}
extension FileHandle: File {
func swift_write(_ data: Data) throws {
try __write(data, error: ())
}
}

View file

@ -0,0 +1,23 @@
//
// OSFileHandle.swift
// Logger
//
// Created by Wojciech Nagrodzki on 30/10/2019.
// Copyright © 2019 Wojciech Nagrodzki. All rights reserved.
//
import Foundation
protocol OSFileHandle {
func seekToEndOfFile() -> UInt64
func swift_write(_ data: Data) throws
func synchronizeFile()
func closeFile()
}
extension FileHandle: OSFileHandle {
func swift_write(_ data: Data) throws {
try __write(data, error: ())
}
}

View file

@ -24,15 +24,8 @@
import Foundation
protocol File {
func seekToEndOfFile() -> UInt64
func swift_write(_ data: Data) throws
func synchronizeFile()
func closeFile()
}
protocol FileFactory {
func makeInstance(forWritingTo: URL) throws -> File
func makeInstance(forWritingTo: URL) throws -> OSFileHandle
}
/// Write failed as allowed size limit would be exceeded.
@ -55,7 +48,7 @@ protocol SizeLimitedFile {
/// Allows writing to a file while respecting allowed size limit.
final class SizeLimitedFileImpl {
private let file: File
private let file: OSFileHandle
private let sizeLimit: UInt64
private var currentSize: UInt64

View file

@ -69,7 +69,7 @@ private class UnopenableFileFactory: FileFactory {
struct OpenFileError: Error {}
func makeInstance(forWritingTo: URL) throws -> File {
func makeInstance(forWritingTo: URL) throws -> OSFileHandle {
throw OpenFileError()
}
}
@ -78,12 +78,12 @@ private class FileMockFactory: FileFactory {
let mock = FileMock()
func makeInstance(forWritingTo: URL) throws -> File {
func makeInstance(forWritingTo: URL) throws -> OSFileHandle {
return mock
}
}
private class FileMock: File {
private class FileMock: OSFileHandle {
private(set) var writtenData = Data()
private(set) var synchronizeFileCallCount = 0