fileops

package
v0.1.32 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyThere = errors.New("already there")

ErrAlreadyThere marks a move to where the item already is.

View Source
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.

View Source
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

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

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

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

func CreateFile(path string) error

CreateFile makes the empty file path, whose parent exists.

func Mkdir

func Mkdir(path string) error

Mkdir makes the folder path, whose parent exists.

func Move

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

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

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

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

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

func ZipName(dir string) string

ZipName is the name a folder downloads as; the root, which has none, is "root.zip".

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL