github.com/rotari-dev/rotari/internal/fileops

Overview

package fileops // import "github.com/rotari-dev/rotari/internal/fileops"

Package fileops changes files the way a terminal application's file pane does:
making a folder or an empty file, moving, copying a file, and removing. Every
operation refuses to replace what exists, names are checked as one path element,
and nothing is copied between file systems behind a move. It knows nothing of
HTTP; paths are absolute and cleaned by the caller's checks or here.

Variables

var ErrAlreadyThere = errors.New("already there")
    ErrAlreadyThere marks a move to where the item already is.

var ErrBadName = errors.New("bad name")
    ErrBadName marks a name or path the caller should not have sent: a refused
    name, a move into itself, or the root.

var ErrNotRegular = errors.New("not a regular file")
    ErrNotRegular is returned by Replace for a name that is not a regular file,
    such as a folder or a symbolic link, which it does not replace.

Functions

func CheckName(name string) error
    CheckName accepts a new name for one entry of a directory: not empty, not .
    or .., and without /, \, or a control character. \ is allowed by Linux
    but refused, as everywhere rotari takes one path element, and a control
    character could not be typed into a shell.

func CopyFile(from, to string, unique bool) (string, error)
    CopyFile copies the file from to to and returns the path it made.
    A folder is refused, a symbolic link is copied as a link, and a file keeps
    its permission bits; what a failed copy wrote is removed. With unique,
    a name taken at to is not an error: the copy is named "name copy.ext",
    then "name copy 2.ext", and so on.

func CopyName(base string, n int) string
    CopyName is the name of the nth copy (from 1) of base in its own folder:
    "name copy.ext", "name copy 2.ext". A leading dot is not an extension.

func CreateFile(path string) error
    CreateFile makes the empty file path, whose parent exists.

func Mkdir(path string) error
    Mkdir makes the folder path, whose parent exists.

func Move(from, to string) error
    Move renames from to to, within one file system, never replacing what is
    at to. A move into itself or below itself is ErrBadName, and to where it is
    already ErrAlreadyThere. A symbolic link moves as the link.

func Remove(path string) error
    Remove removes path, and everything in it when it is a folder. A symbolic
    link is removed, not what it points to. The root is refused, and a path that
    is not there is an error rather than nothing to do.

func Replace(dir, name string, content io.Reader) (string, error)
    Replace writes content as the file name in dir, replacing a regular file of
    that name, whose permissions it keeps, or writing a new one, and returns
    its path. As WriteNew, content goes into a temporary file in dir first,
    renamed to name only when written whole, so a failure partway leaves what
    was there. Anything else under name, a folder or a symbolic link among them,
    is refused with ErrNotRegular.

func WriteNew(dir, name string, content io.Reader) (string, error)
    WriteNew writes content as the new file name in dir and returns its path.
    It never replaces anything: content goes into a temporary file in dir first,
    which is renamed to name without replacing, so a failure partway leaves no
    file under name, and the temporary file is removed.

func WriteZip(ctx context.Context, writer io.Writer, dir string) error
    WriteZip writes dir as a zip archive to writer while reading it, with every
    entry under dir's own name and folders as entries of their own, so that
    an empty one survives. Symbolic links are neither followed nor stored,
    and other special files are left out. Any failure, such as a file that
    cannot be read, ends it with the error, and so does ctx ending; nothing is
    skipped silently.

func ZipName(dir string) string
    ZipName is the name a folder downloads as; the root, which has none,
    is "root.zip".