Files
helder/Sources/HelderServices/Files/FileServicing.swift
T

83 lines
3.9 KiB
Swift

import Foundation
import HelderCore
public enum FileServiceError: Error, Equatable {
case outsideProject
case alreadyExists(String)
case invalidName(String)
}
/// What a file holds, as far as the editor cares.
public enum FileContents: Equatable, Sendable {
case text(String)
/// A NUL byte in the first 8000 bytes.
case binary
}
public protocol FileServicing: Sendable {
var root: URL { get }
/// The project tree: ripgrep's file list (honouring the config) plus empty folders; a plain walk without ripgrep.
func tree() async -> FileNode
/// Fresh children of one folder ("" is the root), re-read on every expand and collapse.
func children(of directory: String) async -> [FileNode]
/// A text file's contents; "" for a binary file.
func read(_ path: String) async throws -> String
/// A file's text, or `.binary`, so an empty file and a binary one can be told apart.
func readText(_ path: String) async throws -> FileContents
/// An image's bytes; nil for a non-image, a file over 25 MB, or a path outside the project.
func readImage(_ path: String) async -> Data?
/// Replaces the file in one step; a symlinked file is written at its target and keeps its permissions.
func write(_ path: String, contents: String) async throws
/// Creates an empty file and its parent folders; throws `alreadyExists` rather than overwrite.
func createFile(_ path: String) async throws
func createDirectory(_ path: String) async throws
/// Renames within the same folder and returns the new relative path.
func rename(_ path: String, to name: String) async throws -> String
/// Deletes a file or a whole folder.
func delete(_ path: String) async throws
}
extension FileServicing {
/// Built on `read`, which cannot see binary files; only conformers that never meet them should rely on it.
public func readText(_ path: String) async throws -> FileContents {
.text(try await read(path))
}
/// The absolute URL for a relative path, or nil when it escapes the project root.
///
/// The check is on the spelling only, so a symlinked folder can still lead outside; anything
/// that creates, renames or deletes goes through `containedURL(for:)` instead.
public func absoluteURL(for path: String) -> URL? {
let base = root.standardizedFileURL
let target = (path.isEmpty ? base : base.appendingPathComponent(path)).standardizedFileURL
return Self.isInside(target.path, base.path) ? target : nil
}
/// `absoluteURL(for:)`, but nil unless the entry's folder, with every symlink resolved, lies inside the resolved root.
///
/// The entry itself is not resolved: deleting or renaming a symlink acts on the link, not on its target.
public func containedURL(for path: String) -> URL? {
guard let url = absoluteURL(for: path) else { return nil }
let realRoot = Self.realPath(root.standardizedFileURL.path)
if url.path == root.standardizedFileURL.path { return url }
let parent = Self.realPath(url.deletingLastPathComponent().path)
return Self.isInside(parent, realRoot) || parent == realRoot ? url : nil
}
private static func isInside(_ path: String, _ base: String) -> Bool {
let prefix = base.hasSuffix("/") ? base : base + "/"
return path == base || path.hasPrefix(prefix)
}
/// Resolves every symlink; a path that does not exist yet keeps its resolved existing ancestor.
static func realPath(_ path: String) -> String {
if let resolved = realpath(path, nil) {
defer { free(resolved) }
return String(cString: resolved)
}
let parent = (path as NSString).deletingLastPathComponent
guard !parent.isEmpty, parent != path else { return path }
return (realPath(parent) as NSString).appendingPathComponent((path as NSString).lastPathComponent)
}
}