fileutil

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: BSD-3-Clause Imports: 13 Imported by: 7

README

This is a goinstall-able mirror of modified code already published at:
http://git.nic.cz/redmine/projects/gofileutil/repository

Packages in this repository:

Install: $go get modernc.org/fileutil
Godocs: http://godoc.org/modernc.org/fileutil

Install: $go get modernc.org/fileutil/storage
Godocs: http://godoc.org/modernc.org/fileutil/storage

Install: $go get modernc.org/fileutil/falloc
Godocs: http://godoc.org/modernc.org/fileutil/falloc

Install: $go get modernc.org/fileutil/hdb
Godocs: http://godoc.org/modernc.org/fileutil/hdb

Documentation

Overview

Package fileutil collects some file utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyDir added in v1.1.0

func CopyDir(fsys fs.FS, dst, src string, canOverwrite func(fn string, fi os.FileInfo) bool) (files int, bytes int64, rerr error)

CopyDir recursively copies src in fsys to dest in the OS file system, preserving permissions and times where/when possible. If canOverwrite is not nil, it is consulted whether a destination file can be overwritten. If canOverwrite is nil then destination is overwritten if permissions allow that, otherwise the function fails.

func CopyFile added in v1.1.0

func CopyFile(fsys fs.FS, dst, src string, canOverwrite func(fn string, fi os.FileInfo) bool) (n int64, rerr error)

CopyFile copies src in fsys, to dest in the OS file system, preserving permissions and times where/when possible. If canOverwrite is not nil, it is consulted whether a destination file can be overwritten. If canOverwrite is nil then destination is overwritten if permissions allow that, otherwise the function fails.

func Fadvise

func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error

Fadvise predeclares an access pattern for file data. See also 'man 2 posix_fadvise'.

func IsEOF

func IsEOF(err error) bool

IsEOF reports whether err is an EOF condition.

func PunchHole

func PunchHole(f *os.File, off, len int64) error

PunchHole deallocates space inside a file in the byte range starting at offset and continuing for len bytes. No-op for kernels < 2.6.38 (or < 2.7).

func Read

func Read(r io.Reader, buf []byte) (err error)

Read reads buf from r. It will either fill the full buf or fail. It wraps the functionality of an io.Reader which may return less bytes than requested, but may block if not all data are ready for the io.Reader.

func TempFile

func TempFile(dir, prefix, suffix string) (f *os.File, err error)

TempFile creates a new temporary file in the directory dir with a name ending with suffix, basename starting with prefix, opens the file for reading and writing, and returns the resulting *os.File. If dir is the empty string, TempFile uses the default directory for temporary files (see os.TempDir). Multiple programs calling TempFile simultaneously will not choose the same file. The caller can use f.Name() to find the pathname of the file. It is the caller's responsibility to remove the file when no longer needed.

NOTE: This function differs from ioutil.TempFile.

Types

type FadviseAdvice

type FadviseAdvice int

FadviseAdvice is used by Fadvise.

const (
	// $ grep FADV /usr/include/bits/fcntl.h
	POSIX_FADV_NORMAL     FadviseAdvice = iota // No further special treatment.
	POSIX_FADV_RANDOM                          // Expect random page references.
	POSIX_FADV_SEQUENTIAL                      // Expect sequential page references.
	POSIX_FADV_WILLNEED                        // Will need these pages.
	POSIX_FADV_DONTNEED                        // Don't need these pages.
	POSIX_FADV_NOREUSE                         // Data will be accessed once.
)

FAdviseAdvice values.

type GoMFile

type GoMFile struct {
	// contains filtered or unexported fields
}

GoMFile is a concurrent access safe version of MFile.

func NewGoMFile

func NewGoMFile(fname string, flag int, perm os.FileMode, delta_ns int64) (m *GoMFile, err error)

NewGoMFile return a newly created GoMFile.

func (*GoMFile) File

func (m *GoMFile) File() (file *os.File, err error)

func (*GoMFile) SetChanged

func (m *GoMFile) SetChanged()

func (*GoMFile) SetHandler

func (m *GoMFile) SetHandler(h MFileHandler)

type MFile

type MFile struct {
	// contains filtered or unexported fields
}

MFile represents an os.File with a guard/handler on change/modification. Example use case is an app with a configuration file which can be modified at any time and have to be reloaded in such event prior to performing something configurable by that file. The checks are made only on access to the MFile file by File() and a time threshold/hysteresis value can be chosen on creating a new MFile.

func NewMFile

func NewMFile(fname string, flag int, perm os.FileMode, delta_ns int64) (m *MFile, err error)

NewMFile returns a newly created MFile or Error if any. The fname, flag and perm parameters have the same meaning as in os.Open. For meaning of the delta_ns parameter please see the (m *MFile) File() docs.

func (*MFile) File

func (m *MFile) File() (file *os.File, err error)

File returns an os.File from MFile. If time elapsed between the last invocation of this function and now is at least delta_ns ns (a parameter of NewMFile) then the file is checked for change/modification. For delta_ns == 0 the modification is checked w/o getting os.Time(). If a change is detected a handler is invoked on the MFile file. Any of these steps can produce an Error. If that happens the function returns nil, Error.

func (*MFile) SetChanged

func (m *MFile) SetChanged()

SetChanged forces next File() to unconditionally handle modification of the wrapped os.File.

func (*MFile) SetHandler

func (m *MFile) SetHandler(h MFileHandler)

SetHandler sets a function to be invoked when modification of MFile is to be processed.

type MFileHandler

type MFileHandler func(*os.File) error

MFileHandler resolves modifications of File. Possible File context is expected to be a part of the handler's closure.

Directories

Path Synopsis
Package ccgo collects utilities often used while generating code with ccgo.
Package ccgo collects utilities often used while generating code with ccgo.
WIP: Package falloc provides allocation/deallocation of space within a file/store (WIP, unstable API).
WIP: Package falloc provides allocation/deallocation of space within a file/store (WIP, unstable API).
WIP: Package hdb provides a "handle"/value DB like store, but actually it's closer to the model of a process's virtual memory and its alloc, free and move methods.
WIP: Package hdb provides a "handle"/value DB like store, but actually it's closer to the model of a process's virtual memory and its alloc, free and move methods.
WIP: Package storage defines and implements storage providers and store accessors.
WIP: Package storage defines and implements storage providers and store accessors.

Jump to

Keyboard shortcuts

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