Documentation
¶
Index ¶
- func IsNotSupported(err error) bool
- func Lock(f File) error
- func RLock(f File) error
- func Read(name string) ([]byte, error)
- func Transform(name string, t func([]byte) ([]byte, error)) (err error)
- func TryLock(f File) (bool, error)
- func TryRLock(f File) (bool, error)
- func Unlock(f File) error
- func Write(name string, content io.Reader, perm fs.FileMode) (err error)
- type File
- type LockedFile
- func Create(name string) (*LockedFile[*os.File], error)
- func Edit(name string) (*LockedFile[*os.File], error)
- func NewLockedFile(file *os.File) (*LockedFile[*os.File], error)
- func Open(name string) (*LockedFile[*os.File], error)
- func OpenFile(name string, flag int, perm fs.FileMode) (*LockedFile[*os.File], error)
- type Mutex
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsNotSupported ¶
IsNotSupported returns a boolean indicating whether the error is known to report that a function is not supported (possibly for a specific input). It is satisfied by errors.ErrUnsupported as well as some syscall errors.
func Lock ¶
Lock places an advisory write lock on the file, blocking until it can be locked.
If Lock returns nil, no other process will be able to place a read or write lock on the file until this process exits, closes f, or calls Unlock on it.
If f's descriptor is already read- or write-locked, the behavior of Lock is unspecified.
Closing the file may or may not release the lock promptly. Callers should ensure that Unlock is always called when Lock succeeds.
func RLock ¶
RLock places an advisory read lock on the file, blocking until it can be locked.
If RLock returns nil, no other process will be able to place a write lock on the file until this process exits, closes f, or calls Unlock on it.
If f is already read- or write-locked, the behavior of RLock is unspecified.
Closing the file may or may not release the lock promptly. Callers should ensure that Unlock is always called if RLock succeeds.
func Transform ¶
Transform invokes t with the result of reading the named file, with its lock still held.
If t returns a nil error, Transform then writes the returned contents back to the file, making a best effort to preserve existing contents on error.
t must not modify the slice passed to it.
func TryLock ¶
TryLock tries to lock rw for writing and reports whether it succeeded.
Note that while correct uses of TryLock do exist, they are rare, and use of TryLock is often a sign of a deeper problem in a particular use of mutexes.
func TryRLock ¶
TryRLock tries to lock rw for reading and reports whether it succeeded.
Note that while correct uses of TryRLock do exist, they are rare, and use of TryRLock is often a sign of a deeper problem in a particular use of mutexes.
Types ¶
type File ¶
type File interface { // Name returns the name of the file. Name() string // Fd returns a valid file descriptor. // (If the File is an *os.File, it must not be closed.) Fd() uintptr // Stat returns the FileInfo structure describing file. Stat() (fs.FileInfo, error) }
A File provides the minimal set of methods required to lock an open file. File implementations must be usable as map keys. The usual implementation is *os.File.
type LockedFile ¶
type LockedFile[T File] struct { File T // contains filtered or unexported fields }
func Create ¶
func Create(name string) (*LockedFile[*os.File], error)
Create is like os.Create, but returns a write-locked file.
func Edit ¶
func Edit(name string) (*LockedFile[*os.File], error)
Edit creates the named file with mode 0666 (before umask), but does not truncate existing contents.
If Edit succeeds, methods on the returned File can be used for I/O. The associated file descriptor has mode O_RDWR and the file is write-locked.
func NewLockedFile ¶
NewLockedFile returns a locked file. If flag includes os.O_WRONLY or os.O_RDWR, the file is write-locked; otherwise, it is read-locked.
func Open ¶
func Open(name string) (*LockedFile[*os.File], error)
Open is like os.Open, but returns a read-locked file.
func OpenFile ¶
OpenFile is like os.OpenFile, but returns a locked file. If flag includes os.O_WRONLY or os.O_RDWR, the file is write-locked; otherwise, it is read-locked.
func (*LockedFile[T]) Close ¶
func (f *LockedFile[T]) Close() error
Close unlocks and closes the underlying file.
Close may be called multiple times; all calls after the first will return a non-nil error.
func (*LockedFile[T]) Lock ¶
func (f *LockedFile[T]) Lock() error
func (*LockedFile[T]) RLock ¶
func (f *LockedFile[T]) RLock() error
func (*LockedFile[T]) RUnlock ¶
func (f *LockedFile[T]) RUnlock() error
func (*LockedFile[T]) TryLock ¶
func (f *LockedFile[T]) TryLock() (bool, error)
func (*LockedFile[T]) Unlock ¶
func (f *LockedFile[T]) Unlock() error
type Mutex ¶
type Mutex struct { Path string // The path to the well-known lock file. Must be non-empty. // contains filtered or unexported fields }
A Mutex provides mutual exclusion within and across processes by locking a well-known file. Such a file generally guards some other part of the filesystem: for example, a Mutex file in a directory might guard access to the entire tree rooted in that directory.
Mutex does not implement sync.Locker: unlike a sync.Mutex, a lockedfile.Mutex can fail to lock (e.g. if there is a permission error in the filesystem).
Like a sync.Mutex, a Mutex may be included as a field of a larger struct but must not be copied after first use. The Path field must be set before first use and must not be change thereafter.
func (*Mutex) Lock ¶
Lock attempts to lock the Mutex.
If successful, Lock returns a non-nil unlock function: it is provided as a return-value instead of a separate method to remind the caller to check the accompanying error. (See https://golang.org/issue/20803.)