fileutil

package module
v0.0.0-...-7f5e33c Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package fileutil implements utility functions related to files and paths.

Package fileutil implements utility functions related to files and paths.

Index

Constants

View Source
const (
	// FsMagicAufs filesystem id for Aufs
	FsMagicAufs = 0x61756673
	// FsMagicBtrfs filesystem id for Btrfs
	FsMagicBtrfs = 0x9123683E
	// FsMagicCramfs filesystem id for Cramfs
	FsMagicCramfs = 0x28cd3d45
	// FsMagicEcryptfs filesystem id for eCryptfs
	FsMagicEcryptfs = 0xf15f
	// FsMagicExtfs filesystem id for Extfs
	FsMagicExtfs = 0x0000EF53
	// FsMagicF2fs filesystem id for F2fs
	FsMagicF2fs = 0xF2F52010
	// FsMagicGPFS filesystem id for GPFS
	FsMagicGPFS = 0x47504653
	// FsMagicJffs2Fs filesystem if for Jffs2Fs
	FsMagicJffs2Fs = 0x000072b6
	// FsMagicJfs filesystem id for Jfs
	FsMagicJfs = 0x3153464a
	// FsMagicNfsFs filesystem id for NfsFs
	FsMagicNfsFs = 0x00006969
	// FsMagicRAMFs filesystem id for RamFs
	FsMagicRAMFs = 0x858458f6
	// FsMagicReiserFs filesystem id for ReiserFs
	FsMagicReiserFs = 0x52654973
	// FsMagicSmbFs filesystem id for SmbFs
	FsMagicSmbFs = 0x0000517B
	// FsMagicSquashFs filesystem id for SquashFs
	FsMagicSquashFs = 0x73717368
	// FsMagicTmpFs filesystem id for TmpFs
	FsMagicTmpFs = 0x01021994
	// FsMagicVxFS filesystem id for VxFs
	FsMagicVxFS = 0xa501fcf5
	// FsMagicXfs filesystem id for Xfs
	FsMagicXfs = 0x58465342
	// FsMagicZfs filesystem id for Zfs
	FsMagicZfs = 0x2fc12fc1
	// FsMagicOverlay filesystem id for overlay
	FsMagicOverlay = 0x794C7630
)
View Source
const (
	// PrivateFileMode grants owner to read/write a file.
	PrivateFileMode = 0600
	// PrivateDirMode grants owner to make/remove files inside the directory.
	PrivateDirMode = 0700
)
View Source
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

Variables

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

Functions

func AtomicWriteFile

func AtomicWriteFile(filename, tmpext string, data []byte, perm os.FileMode) (err error)

AtomicWriteFile **atomically** writes data to a file named by filename. If an error occurs, the target file is guaranteed to be either fully written, or not written at all. If the file does not exist, WriteFile creates it with permissions perm; otherwise WriteFile truncates it before writing.

func CreateDirAll

func CreateDirAll(dir string) error

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

func Exist

func Exist(name string) bool

Exist returns true if a file or directory exists.

func Fdatasync

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 Frangesync

func Frangesync(f *os.File, off, n int64, strict bool) error

Frangesync invokes sync_file_range syscall for the given offset and nbytes.

func Fsync

func Fsync(f *os.File) error

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

func IsDirWriteable

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 IsSyncFileRangeSupported

func IsSyncFileRangeSupported(f *os.File) bool

IsSyncFileRangeSupported returns true when the filesystem which the given file is located in is support sync_file_range syscall.

func OpenDir

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

OpenDir opens a directory for syncing.

func Preallocate

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 ReadDir

func ReadDir(dirpath string) ([]string, error)

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

func SyncDir

func SyncDir(dir string) error

SyncDir call fsync() on a directory. Calling fsync() does not necessarily ensure that the entry in the directory containing the file has also reached disk. For that an explicit fsync() on a file descriptor for the directory is also needed.

func TouchDirAll

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

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.

Types

type LockedFile

type LockedFile struct{ *os.File }

func LockFile

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

func TryLockFile

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

Jump to

Keyboard shortcuts

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