Skip to content

Commit 13bb308

Browse files
committed
Add async mkdir/rmdir/unlink, add rename
All have been missing.
1 parent f138a6b commit 13bb308

File tree

2 files changed

+113
-12
lines changed

2 files changed

+113
-12
lines changed

Sources/fs/File.swift

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Macro
44
//
55
// Created by Helge Hess.
6-
// Copyright © 2020-2023 ZeeZide GmbH. All rights reserved.
6+
// Copyright © 2020-2026 ZeeZide GmbH. All rights reserved.
77
//
88

99
import protocol NIO.EventLoop
@@ -24,6 +24,7 @@ import struct xsys.mode_t
2424
import let xsys.mkdir
2525
import let xsys.rmdir
2626
import var xsys.errno
27+
import let xsys.rename
2728

2829
// Most, not all, funcs currently require Foundation and should be reimplemented
2930
// using Posix as an alternative.
@@ -406,6 +407,67 @@ public struct MakeDirOptions: Equatable {
406407
}
407408
}
408409

410+
// MARK: - Async Callback Variants
411+
412+
/**
413+
* Create a directory asynchronously on the thread pool (blocks).
414+
*
415+
* - Parameters:
416+
* - path: The path to the directory to create.
417+
* - options: The ``MakeDirOptions`` specifying the creation behaviour.
418+
* - yield: Called on completion with an optional error.
419+
*/
420+
@inlinable
421+
public func mkdir(_ path: String,
422+
_ options: MakeDirOptions = .init(),
423+
yield: @escaping ( Error? ) -> Void)
424+
{
425+
FileSystemModule._evalAsync(
426+
mkdirSync, (path, options), yield)
427+
}
428+
429+
/**
430+
* Delete a directory asynchronously on the thread pool (blocks).
431+
*
432+
* - Parameters:
433+
* - path: The path to the directory to remove.
434+
* - yield: Called on completion with an optional error.
435+
*/
436+
@inlinable
437+
public func rmdir(_ path: String, yield: @escaping ( Error? ) -> Void) {
438+
FileSystemModule._evalAsync(rmdirSync, path, yield)
439+
}
440+
441+
/**
442+
* Delete a file asynchronously on the thread pool (blocks).
443+
*
444+
* - Parameters:
445+
* - path: The path to the file to remove.
446+
* - yield: Called on completion with an optional error.
447+
*/
448+
@inlinable
449+
public func unlink(_ path: String, yield: @escaping ( Error? ) -> Void) {
450+
FileSystemModule._evalAsync(unlinkSync, path, yield)
451+
}
452+
453+
/**
454+
* Rename a file or directory asynchronously on the thread pool (blocks).
455+
*
456+
* - Parameters:
457+
* - oldPath: The current path.
458+
* - newPath: The new path.
459+
* - yield: Called on completion with an optional error.
460+
*/
461+
@inlinable
462+
public func rename(_ oldPath: String, _ newPath: String,
463+
yield: @escaping ( Error? ) -> Void)
464+
{
465+
FileSystemModule._evalAsync(renameSync, (oldPath, newPath), yield)
466+
}
467+
468+
469+
// MARK: - Sync Variants
470+
409471
/**
410472
* Create a directory synchronously, on the active thread.
411473
*
@@ -443,7 +505,7 @@ public func mkdirSync(_ path: String, _ umask: String) throws {
443505
}
444506

445507
/**
446-
* Delete a directory synchronously, on the active thread.
508+
* Delete a directory synchronously, on the active thread (blocks).
447509
*
448510
* - Parameters:
449511
* - path: The path to the directory to create.
@@ -459,7 +521,7 @@ public func rmdirSync(_ path: String) throws {
459521
}
460522

461523
/**
462-
* Delete a directory or file synchronously, on the active thread.
524+
* Delete a directory or file synchronously, on the active thread (blocks).
463525
*
464526
* - Parameters:
465527
* - path: The path to the directory to create.
@@ -474,6 +536,18 @@ public func unlinkSync(_ path: String) throws {
474536
#endif
475537
}
476538

539+
/**
540+
* Rename a file or directory synchronously, on the active thread (blocks).
541+
*
542+
* - Parameters:
543+
* - path: The path to rename
544+
* - newPath: The new name.
545+
*/
546+
public func renameSync(_ path: String, _ newPath: String) throws {
547+
let rc = rename(path, newPath)
548+
if rc != 0 { try throwErrno() }
549+
}
550+
477551
/**
478552
* Throw a custom error for the currently active Posix `errno` value.
479553
*/

Sources/fs/fs.swift

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Macro
44
//
55
// Created by Helge Hess.
6-
// Copyright © 2020-2021 ZeeZide GmbH. All rights reserved.
6+
// Copyright © 2020-2026 ZeeZide GmbH. All rights reserved.
77
//
88

99
import class NIO.NIOThreadPool
@@ -80,10 +80,10 @@ public extension FileSystemModule {
8080
return fs.createReadStream(path)
8181
}
8282
@inlinable
83-
static func createWriteStream(on eventLoop: EventLoop? = nil,
84-
_ path: String,
85-
flags: NIOFileHandle.Flags
86-
= .allowFileCreation())
83+
static func createWriteStream(on eventLoop : EventLoop? = nil,
84+
_ path : String,
85+
flags : NIOFileHandle.Flags
86+
= .allowFileCreation())
8787
-> FileWriteStream
8888
{
8989
return fs.createWriteStream(on: eventLoop, path, flags: flags)
@@ -106,7 +106,8 @@ public extension FileSystemModule {
106106
/// Check whether we have access to the given path in the given mode.
107107
@inlinable
108108
static func access(_ path: String, _ mode: Int = F_OK,
109-
yield: @escaping ( Error? ) -> Void) {
109+
yield: @escaping ( Error? ) -> Void)
110+
{
110111
fs.access(path, mode, yield: yield)
111112
}
112113

@@ -126,9 +127,10 @@ public extension FileSystemModule {
126127

127128
// MARK: - Synchronous wrappers
128129

129-
// If you do a lot of FS operations in sequence, you might want to use a single
130-
// (async) GCD call, instead of using the convenience async functions.
130+
// If you do a lot of FS operations in sequence, you might want to use a
131+
// single (async) GCD call, instead of using the convenience async functions.
131132
//
133+
// TODO(2026-04-04): Update note for Macro, this was for Noze.io :-)
132134
// Example:
133135
// FileSystemModule.workerQueue.async {
134136
// statSync(...)
@@ -249,6 +251,31 @@ public extension FileSystemModule {
249251
try fs.writeFileSync(path, string, encoding)
250252
}
251253

254+
// MARK: - Async Callback Variants
255+
256+
@inlinable
257+
static func mkdir(_ path: String, _ options: MakeDirOptions = .init(),
258+
yield: @escaping ( Error? ) -> Void)
259+
{
260+
fs.mkdir(path, options, yield: yield)
261+
}
262+
@inlinable
263+
static func rmdir(_ path: String, yield: @escaping ( Error? ) -> Void) {
264+
fs.rmdir(path, yield: yield)
265+
}
266+
@inlinable
267+
static func unlink(_ path: String, yield: @escaping ( Error? ) -> Void) {
268+
fs.unlink(path, yield: yield)
269+
}
270+
@inlinable
271+
static func rename(_ oldPath: String, _ newPath: String,
272+
yield: @escaping ( Error? ) -> Void)
273+
{
274+
fs.rename(oldPath, newPath, yield: yield)
275+
}
276+
277+
// MARK: - Sync Variants
278+
252279
@inlinable
253280
static func mkdirSync(_ path: String, _ options: MakeDirOptions = .init())
254281
throws
@@ -303,7 +330,7 @@ public extension FileSystemModule {
303330
/**
304331
* Check whether the path exists.
305332
*
306-
* Use `fs.access()` instead.
333+
* Use ``fs/access(_:_:yield:)`` instead.
307334
*/
308335
@available(*, deprecated, message: "Using `access` is recommended.")
309336
@inlinable

0 commit comments

Comments
 (0)