Files
xtool-mirror/Sources/XUtils/Foundation+Utils.swift
Kabir Oberai 836fca8daf Improve tmpdir management (#147)
- We now have a single tmpdir "root" that can be recreated at launch to
clean up old stragglers
- The location of this tmpdir root can be controlled by `XTL_TMPDIR` or
`TMPDIR`. In general the path is `$TMPDIR/sh.xtool`.

With this change it should be possible to `export XTL_TMPDIR=/var/tmp`
if `/tmp` doesn't have enough space, which fixes #23.
2025-08-10 01:43:09 -04:00

26 lines
743 B
Swift

import Foundation
extension Data {
// AsyncBytes is Darwin-only :/
package init(reading fileHandle: FileHandle) async throws {
#if canImport(Darwin)
self = try await fileHandle.bytes.reduce(into: Data()) { $0.append($1) }
#else
self = try fileHandle.readToEnd() ?? Data()
#endif
}
package init(reading file: URL) async throws {
#if canImport(Darwin)
self = try await file.resourceBytes.reduce(into: Data()) { $0.append($1) }
#else
try self.init(contentsOf: file)
#endif
}
}
package func stderrPrint(_ message: String, terminator: String = "\n") {
try? FileHandle.standardError.write(contentsOf: Data("\(message)\(terminator)".utf8))
}