etcd: Index | Files

package fileutil

import ""

Package fileutil implements utility functions related to files and paths.


Package Files

dir_unix.go doc.go fileutil.go lock.go lock_flock.go lock_linux.go preallocate.go preallocate_unix.go purge.go read_dir.go sync_linux.go


const (
    F_OFD_GETLK  = 37
    F_OFD_SETLK  = 37
    F_OFD_SETLKW = 38

This used to call syscall.Flock() but that call fails with EBADF on NFS. An alternative is lockf() which works on NFS but that call lets a process lock the same file twice. Instead, use Linux's non-standard open file descriptor locks which will block if the process already holds the file lock.

constants from /usr/include/bits/fcntl-linux.h

const (
    // PrivateDirMode grants owner to make/remove files inside the directory.
    PrivateDirMode = 0700
const (
    // PrivateFileMode grants owner to read/write a file.
    PrivateFileMode = 0600


var (
    ErrLocked = errors.New("fileutil: file already locked")

func CheckDirPermission Uses

func CheckDirPermission(dir string, perm os.FileMode) error

CheckDirPermission checks permission on an existing dir. Returns error if dir is empty or exist with a different permission than specified.

func CreateDirAll Uses

func CreateDirAll(dir string) error

CreateDirAll is similar to TouchDirAll but returns error if the deepest directory was not empty.

func DirEmpty Uses

func DirEmpty(name string) bool

DirEmpty returns true if a directory empty and can access.

func Exist Uses

func Exist(name string) bool

Exist returns true if a file or directory exists.

func Fdatasync Uses

func Fdatasync(f *os.File) error

Fdatasync is similar to fsync(), but does not flush modified metadata unless that metadata is needed in order to allow a subsequent data retrieval to be correctly handled.

func Fsync Uses

func Fsync(f *os.File) error

Fsync is a wrapper around file.Sync(). Special handling is needed on darwin platform.

func IsDirWriteable Uses

func IsDirWriteable(dir string) error

IsDirWriteable checks if dir is writable by writing and removing a file to dir. It returns nil if dir is writable.

func OpenDir Uses

func OpenDir(path string) (*os.File, error)

OpenDir opens a directory for syncing.

func Preallocate Uses

func Preallocate(f *os.File, sizeInBytes int64, extendFile bool) error

Preallocate tries to allocate the space for given file. This operation is only supported on linux by a few filesystems (btrfs, ext4, etc.). If the operation is unsupported, no error will be returned. Otherwise, the error encountered will be returned.

func PurgeFile Uses

func PurgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) <-chan error

func PurgeFileWithDoneNotify Uses

func PurgeFileWithDoneNotify(lg *zap.Logger, dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) (<-chan struct{}, <-chan error)

func ReadDir Uses

func ReadDir(d string, opts ...ReadDirOption) ([]string, error)

ReadDir returns the filenames in the given directory in sorted order.

func TouchDirAll Uses

func TouchDirAll(dir string) error

TouchDirAll is similar to os.MkdirAll. It creates directories with 0700 permission if any directory does not exists. TouchDirAll also ensures the given directory is writable.

func ZeroToEnd Uses

func ZeroToEnd(f *os.File) error

ZeroToEnd zeros a file starting from SEEK_CUR to its SEEK_END. May temporarily shorten the length of the file.

type LockedFile Uses

type LockedFile struct{ *os.File }

func LockFile Uses

func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error)

func TryLockFile Uses

func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error)

type ReadDirOp Uses

type ReadDirOp struct {
    // contains filtered or unexported fields

ReadDirOp represents an read-directory operation.

type ReadDirOption Uses

type ReadDirOption func(*ReadDirOp)

ReadDirOption configures archiver operations.

func WithExt Uses

func WithExt(ext string) ReadDirOption

WithExt filters file names by their extensions. (e.g. WithExt(".wal") to list only WAL files)

Package fileutil imports 11 packages (graph) and is imported by 180 packages. Updated 2020-07-11. Refresh now. Tools for package owners.