Documentation
¶
Overview ¶
Package fileutil implements utility functions related to files and paths.
Package fileutil implements utility functions related to files and paths.
Index ¶
- Constants
- Variables
- func AtomicWriteFile(filename, tmpext string, data []byte, perm os.FileMode) (err error)
- func CreateDirAll(dir string) error
- func Exist(name string) bool
- func Fdatasync(f *os.File) error
- func Frangesync(f *os.File, off, n int64, strict bool) error
- func Fsync(f *os.File) error
- func IsDirWriteable(dir string) error
- func IsSyncFileRangeSupported(f *os.File) bool
- func OpenDir(path string) (*os.File, error)
- func Preallocate(f *os.File, sizeInBytes int64, extendFile bool) error
- func ReadDir(dirpath string) ([]string, error)
- func SyncDir(dir string) error
- func TouchDirAll(dir string) error
- func ZeroToEnd(f *os.File) error
- type LockedFile
Constants ¶
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 )
const ( // PrivateFileMode grants owner to read/write a file. PrivateFileMode = 0600 // PrivateDirMode grants owner to make/remove files inside the directory. PrivateDirMode = 0700 )
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 ¶
var (
ErrLocked = errors.New("fileutil: file already locked")
)
Functions ¶
func AtomicWriteFile ¶
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 ¶
CreateDirAll is similar to TouchDirAll but returns error if the deepest directory was not empty.
func Fdatasync ¶
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 ¶
Frangesync invokes sync_file_range syscall for the given offset and nbytes.
func IsDirWriteable ¶
IsDirWriteable checks if dir is writable by writing and removing a file to dir. It returns nil if dir is writable.
func IsSyncFileRangeSupported ¶
IsSyncFileRangeSupported returns true when the filesystem which the given file is located in is support sync_file_range syscall.
func Preallocate ¶
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 SyncDir ¶
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 ¶
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.