← std

std/fs.kur

//! The file system by path: whole files read and written, what a path names,
//! directories listed, made and removed, and files renamed. A path is any
//! byte view, read through the capability that permits it (a `Str` under
//! its domain, a static literal under `StaticRead`, a `Buffer!`'s bytes
//! once opened), and copied with a NUL into a buffer freed when the call
//! returns (`std::io::c_string`); a path holding a NUL is EINVAL. Every
//! failure is a `std::io::Error` holding the errno. Nothing here prevents
//! another process from changing the file system between two calls.

use package as std;

use alloc::arena::Allocate;
use alloc::buffer::Buffer!;
use core::fmt::Writer;
use core::cstr::cstr_len;
use core::mem::alloca;
use core::option::{Option, Result};
use core::str::Str;
use core::raw::raw_byte_at;
use core::slice::ReadBytes;
use libc::fs::{kur_libc_fs_error, kur_libc_fs_release, kur_libc_fs_canonicalize, kur_libc_fs_stat, kur_libc_fs_remove,
    kur_libc_fs_rename, kur_libc_fs_mkdir, kur_libc_fs_open_directory, kur_libc_fs_next_entry, kur_libc_fs_entry_name,
    kur_libc_fs_entry_is_directory, kur_libc_fs_close_directory};
use std::io::{Error, ErrorKind, File!, c_string};

/// The error of a C function (`libc::fs`) that returns 0 or an errno.
fn status(code: i32) -> Result[(), Error] {
    if code == 0i32 {
        Result::Ok.(())
    } else {
        Result::Err.(Error.{ errno: code })
    }
}

/// The whole of the file at `path`, read to its end through an `Input!`
/// (`Input!::read_to_end`): a regular file, or a pipe or device (such as
/// `/dev/stdin` or a shell's `<(command)`) read until it ends; a directory
/// is EISDIR. The file is closed whatever the read gave. A failure to read,
/// or else to close, is the error: a read error takes precedence, and a
/// failed close discards what was read.
forall C, S where ReadBytes[C, S], Copy[S].
fn read(&cx: C, path: S) -> Result[Buffer!, Error] {
    match File!::open_read(&cx, path) {
        Err(e) => Result::Err.(e),
        Ok(file) => file.input().read_to_end(),
    }
}

/// `read`'s bytes in the arena of `cx`, as text of it.
forall 'k, C, S, R where ReadBytes[C, S], Copy[S], Allocate[C -> 'k, R].
fn read_to_arena(&cx: C, path: S) -> Result[Str['k], Error] {
    match read(&cx, path) {
        Err(e) => Result::Err.(e),
        Ok(contents) => Result::Ok.(contents.save(&cx)),
    }
}

/// Writes `contents` to the file at `path`, created or emptied first, and
/// frees it. The file is closed whatever the write gave. A failure to
/// write, or else to close, is the error: a write error takes precedence
/// (`Output!::finish`).
forall C, S where ReadBytes[C, S], Copy[S].
fn write(&cx: C, path: S, contents: Buffer!) -> Result[(), Error] {
    match File!::create(&cx, path) {
        Err(e) => {
            contents.destroy();
            Result::Err.(e)
        },
        Ok(file) => file.output().put(contents).finish(),
    }
}

/// What a path names: a regular file, a directory, or something else.
exposed enum FileKind {
    File,
    Directory,
    // a device, a pipe, a socket
    Other,
}

/// What `stat(2)` says of a path: the kind of what it names, after
/// following symbolic links, and its size in bytes.
exposed struct Metadata {
    kind: FileKind,
    size: u64,
}

/// What `stat(2)` says of `path`, following symbolic links.
forall C, S where ReadBytes[C, S], Copy[S].
fn metadata(&cx: C, path: S) -> Result[Metadata, Error] {
    var text = c_string(&cx, path);
    let p = unsafe &text.as_ptr();
    let size = unsafe alloca[u64]();
    unsafe *size = 0u64;
    // `p` holds the path and a NUL; `size` is a live `u64`.
    let kind = unsafe kur_libc_fs_stat(p, &text.len() - 1usize, size);
    text.destroy();
    if kind < 0i32 {
        return Result::Err.(Error.{ errno: 0i32 - kind });
    }
    let k = if kind == 1i32 { FileKind::File } else if kind == 2i32 { FileKind::Directory } else { FileKind::Other };
    Result::Ok.(Metadata.{ kind: k, size: unsafe *size })
}

/// Whether `path` names anything, following symbolic links. It does not
/// when `metadata` fails with ENOENT, or with ENOTDIR for a path through
/// something other than a directory; any other failure (EACCES, ELOOP,
/// EIO, ...) says nothing about whether it does, and is the error.
forall C, S where ReadBytes[C, S], Copy[S].
fn path_exists(&cx: C, path: S) -> Result[bool, Error] {
    match metadata(&cx, path) {
        Ok(found) => Result::Ok.(true),
        Err(e) => match e.kind() {
            NotFound => Result::Ok.(false),
            NotADirectory => Result::Ok.(false),
            _ => Result::Err.(e),
        },
    }
}

/// The absolute path with no `.`, `..` or symbolic link that `path` names,
/// which must exist (`realpath(3)`).
forall C, S where ReadBytes[C, S], Copy[S].
fn canonicalize(&cx: C, path: S) -> Result[Buffer!, Error] {
    var text = c_string(&cx, path);
    let p = unsafe &text.as_ptr();
    let result = unsafe kur_libc_fs_canonicalize(p, &text.len() - 1usize);
    let error = unsafe kur_libc_fs_error();
    text.destroy();
    if unsafe (result as usize) == 0usize {
        Result::Err.(Error.{ errno: error })
    } else {
        // `result` is a NUL-terminated string `realpath` allocated; it is
        // copied, then released to C's allocator.
        let len = unsafe cstr_len(result);
        let output = unsafe Buffer!::new().write_raw(result, len);
        let unit = unsafe kur_libc_fs_release(result);
        Result::Ok.(output)
    }
}

/// Removes a file, or an empty directory (`remove(3)`).
forall C, S where ReadBytes[C, S], Copy[S].
fn remove(&cx: C, path: S) -> Result[(), Error] {
    var text = c_string(&cx, path);
    let p = unsafe &text.as_ptr();
    let code = unsafe kur_libc_fs_remove(p, &text.len() - 1usize);
    text.destroy();
    status(code)
}

/// Renames `from` to `to`, replacing what `to` named (`rename(2)`). The two
/// paths may be different kinds of view, read under the one capability `cx`.
forall C, F, T where ReadBytes[C, F], Copy[F], ReadBytes[C, T], Copy[T].
fn rename(&cx: C, from: F, to: T) -> Result[(), Error] {
    var a = c_string(&cx, from);
    var b = c_string(&cx, to);
    let pa = unsafe &a.as_ptr();
    let pb = unsafe &b.as_ptr();
    let code = unsafe kur_libc_fs_rename(pa, &a.len() - 1usize, pb, &b.len() - 1usize);
    a.destroy();
    b.destroy();
    status(code)
}

/// Makes the directory `path`, with mode 0777 less the umask; its parent
/// must exist.
forall C, S where ReadBytes[C, S], Copy[S].
fn mkdir(&cx: C, path: S) -> Result[(), Error] {
    var text = c_string(&cx, path);
    let p = unsafe &text.as_ptr();
    let code = unsafe kur_libc_fs_mkdir(p, &text.len() - 1usize);
    text.destroy();
    status(code)
}

// ----------------------------------------------------------- directories --

/// An entry of a directory: its name, which owns its bytes, and whether it
/// is a directory. Classification follows symbolic links, uses the entry
/// type the system records where it has one, and counts a failed
/// observation as not a directory, as directory walks do; it is an
/// observation, not access authority.
exposed struct DirEntry! {
    name: Buffer!,
    is_directory: bool,
}

/// An open directory: owns one `DIR *`, which `close` releases. No raw
/// handle is extracted, and no safe code outside makes one.
struct Directory! {
    handle: *u8,
}

/// Opens the directory at `path` to list its entries.
forall C, S where ReadBytes[C, S], Copy[S].
fn read_dir(&cx: C, path: S) -> Result[Directory!, Error] {
    var text = c_string(&cx, path);
    let p = unsafe &text.as_ptr();
    let handle = unsafe kur_libc_fs_open_directory(p, &text.len() - 1usize);
    let error = unsafe kur_libc_fs_error();
    text.destroy();
    if unsafe (handle as usize) == 0usize {
        Result::Err.(Error.{ errno: error })
    } else {
        Result::Ok.(Directory!.{ handle })
    }
}

/// Whether the NUL-terminated `name` is `.` or `..`. Caller: `name` is a
/// live NUL-terminated string.
unsafe fn is_dot(name: *u8) -> bool {
    let n = unsafe cstr_len(name);
    n >= 1usize && n <= 2usize && unsafe raw_byte_at(name, n, 0usize) == b'.' && unsafe raw_byte_at(name, n, n - 1usize) == b'.'
}

impl Directory! {
    /// The next entry other than `.` and `..`, `Ok.(None)` at the end, in
    /// the order the system lists them.
    fn next(&self) -> Result[Option[DirEntry!], Error] {
        let { handle } = self;
        while true {
            let raw = unsafe kur_libc_fs_next_entry(handle);
            if unsafe (raw as usize) == 0usize {
                let errno = unsafe kur_libc_fs_error();
                self = Directory!.{ handle };
                return if errno == 0i32 { Result::Ok.(Option::None) } else { Result::Err.(Error.{ errno }) };
            }
            // `raw` is the entry just read, valid until the next read or
            // close of `handle`, and its name a NUL-terminated string
            // within it.
            let name = unsafe kur_libc_fs_entry_name(raw);
            if !unsafe is_dot(name) {
                let is_directory = unsafe kur_libc_fs_entry_is_directory(handle, raw) != 0i32;
                let len = unsafe cstr_len(name);
                let entry = unsafe Buffer!::new().write_raw(name, len);
                self = Directory!.{ handle };
                return Result::Ok.(Option::Some.(DirEntry!.{ name: entry, is_directory }));
            }
        }
        self = Directory!.{ handle };
        Result::Ok.(Option::None)
    }

    /// Releases the handle, whatever `closedir` reports.
    fn close(self) -> Result[(), Error] {
        let { handle } = self;
        status(unsafe kur_libc_fs_close_directory(handle))
    }
}