diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index 9ddc16c3..2d4235d6 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -551,3 +551,73 @@ extension FilePermissions { } } #endif + +// (1)MARK:-file Removal Ops + +#if !os(Windows) +@available(System 1.7.0,*) +extension FilePath{ + /// Removes the file at this path. + /// + /// - Parameters: + /// - retryOnInterrupt: Whether to retry the operation + /// if it throws ``Errno/interrupted``. + /// The default is `true`. + /// Pass `false` to try only once and throw an error upon interruption. + /// + /// The corresponding C function is `unlink`. + @_alwaysEmitIntoClient + public func remove(retryOnInterrupt: Bool=true) throws{ + try withCString {try $0.remove(retryOnInterrupt: retryOnInterrupt)} + } + + /// Removes the empty directory at this path. + /// + /// - Parameters: + /// - retryOnInterrupt: Whether to retry the operation + /// if it throws ``Errno/interrupted``. + /// The default is `true`. + /// Pass `false` to try only once and throw an error upon interruption. + /// + /// The corresponding C function is `rmdir`. + @_alwaysEmitIntoClient + public func removeDirectory(retryOnInterrupt: Bool=true) throws{ + try withCString {try $0.removeDirectory(retryOnInterrupt: retryOnInterrupt)} + } +} + +@available(System 1.7.0, *) +extension UnsafePointer where Pointee == CChar { + /// Removes the file at this path. + /// + /// - Parameters: + /// - retryOnInterrupt: Whether to retry the operation + /// if it throws ``Errno/interrupted``. + /// The default is `true`. + /// Pass `false` to try only once and throw an error upon interruption. + /// + /// The corresponding C function is `unlink`. + @_alwaysEmitIntoClient + public func remove(retryOnInterrupt: Bool = true) throws { + try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) { + system_unlink(self) + }.get() + } + + /// Removes the empty directory at this path. + /// + /// - Parameters: + /// - retryOnInterrupt: Whether to retry the operation + /// if it throws ``Errno/interrupted``. + /// The default is `true`. + /// Pass `false` to try only once and throw an error upon interruption. + /// + /// The corresponding C function is `rmdir`. + @_alwaysEmitIntoClient + public func removeDirectory(retryOnInterrupt: Bool=true) throws { + try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) { + system_rmdir(self) + }.get() + } +} +#endif diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index f6eb5339..448c6c36 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -176,6 +176,15 @@ internal func system_rmdir( return rmdir(path) } +internal func system_unlink( + _ path:UnsafePointer +) -> CInt { +#if ENABLE_MOCKING + if mockingEnabled { return _mock(path: path) } +#endif + return unlink(path) +} + #if SYSTEM_PACKAGE_DARWIN internal let SYSTEM_CS_DARWIN_USER_TEMP_DIR = _CS_DARWIN_USER_TEMP_DIR