Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Sources/System/FileOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions Sources/System/Internals/Syscalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ internal func system_rmdir(
return rmdir(path)
}

internal func system_unlink(
_ path:UnsafePointer<CInterop.PlatformChar>
) -> 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

Expand Down